2 wjzwjs wjzwjs 于 2014.02.16 11:49 提问

孤儿进程的问题,请教高手

#include stdio.h>
#include stdlib.h>
#include unistd.h>
#include signal.h>
#include errno.h>
#define errexit(msg) do{ perror(msg); exit(EXIT_FAILURE); } while(0)
static void sig_hup(int signo)
{
printf("SIGHUP received, pid = %d\n", getpid());
}
static void sig_cont(int signo)
{
printf("SIGCONT received, pid = %d\n", getpid());
}
static void sig_ttin(int signo)
{
printf("SIGTTIN received, pid = %d\n", getpid());
}
static void pr_ids(char name)
{
printf("%s: pid = %d, ppid = %d, pgrp = %d, tpgrp = %d\n",
name, getpid(), getppid(), getpgrp(), tcgetpgrp(STDIN_FILENO));
}
int main(int argc, char *argv[])
{
char c;
pid_t pid;
setbuf(stdout, NULL);
pr_ids("parent");
if ((pid = fork()) 0) {
errexit("fork error");
} else if (pid > 0) { /
parent /
sleep(5);
printf("parent exit\n");
exit(0);
} else { /
child */
pr_ids("child...1");
signal(SIGCONT, sig_cont);
signal(SIGHUP, sig_hup);
signal(SIGTTIN, sig_ttin);
kill(getpid(), SIGTSTP);
//sleep(10);
pr_ids("child...2");
if (read(STDIN_FILENO, &c, 1) != 1) {
printf("read error from controlling TTY, errno = %d\n", errno);
}
printf("child exit\n");
}
exit(0);
}
将改程序编译成: m
程序执行结果:

[liumin@localhost orphan]$ ./m
parent: pid = 15028, ppid = 8611, pgrp = 15028, tpgrp = 15028
child...1: pid = 15029, ppid = 15028, pgrp = 15028, tpgrp = 15028
parent exit
SIGCONT received, pid = 15029
[liumin@localhost orphan]$ SIGHUP received, pid = 15029
child...2: pid = 15029, ppid = 1, pgrp = 15028, tpgrp = 8611
child exit

但是 ./m > result.txt
parent: pid = 15028, ppid = 8611, pgrp = 15028, tpgrp = 15028
child...1: pid = 15029, ppid = 15028, pgrp = 15028, tpgrp = 15028
parent exit
SIGCONT received, pid = 15029
[liumin@localhost orphan]$ SIGHUP received, pid = 15029
read error from controlling TTY, errno = 5
child...2: pid = 15029, ppid = 1, pgrp = 15028, tpgrp = 8611

read error from controlling TTY, errno = 5 不导入文件就不出现 问什么?
请各位指教。我用ubuntu10.04

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!