我想在这里做一些有点奇怪的事情。我需要从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