我想在這裏做一些有點奇怪的事情。我需要從deamon開始一個進程logcat,它將在後臺運行並打印到終端,而不用控制stdin。 Logcat可以非常理想地記錄日誌信息,同時還允許用戶輸入標準命令並從shell初始化程序。這是我迄今爲止的守護進程的代碼。程序logcat啓動並顯示日誌消息,但我無法向stdin輸入任何命令,因爲程序看起來已經控制了stdin。在Linux的後臺啓動一個進程C :)
int main (int argc, char** argv, char** env)
{
int fd;
if ((fd = open("/dev/console", O_RDWR)) < 0) {
fd = open("/dev/null", O_RDWR);
}
printf("THIS IS A TEST\n");
dup2(1, fd);
dup2(2, fd);
pid_t childpid = fork();
if(childpid == -1) {
perror("Failed to fork, logcat not starting");
return 1;
}
if(childpid == 0) {
//this is the child, exec logcat
setsid();
int execReturn = execl("/system/bin/logcat", "logcat", (char *) 0);
} else {
//this is the parent do nothing
close(fd);
return 0;
}
close(fd);
return 0;
}
感謝
謝謝sooooo爲您的深入解答。問題在於你提到的stdin。我不得不從logcat重定向到stdin,以及一些其他的東西,比如守護進程。這爲我節省了很多壓力和擔憂。再次感謝,我真的很感激。 – Mike 2010-08-04 16:54:20