日期:2014-05-16  浏览次数:20746 次

APUE中关于controlling terminal的一点疑问
static void
sig_hup(int signo)
{
        printf("SIGHUP 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));
        fflush(stdout);
}
int
main(void)
{
        char    c;
        pid_t   pid;
        pr_ids("parent");
        if ((pid = fork()) < 0) {
                printf("fork error");
        }
        else if (pid > 0) {    
                sleep(5);              
                exit(0);                
        }
        else {                  
                pr_ids("child");
                signal(SIGHUP, sig_hup);        
              kill(getpid(), SIGTSTP);        
                pr_ids("child");        //这里调用pr_ids时,父进程已经关闭,但是调用printf却可以向原来的controlling terminal写
                if (read(STDIN_FILENO, &c, 1) != 1)        //但是这里向原来的terminal读却有问题
                        printf("read error from controlling TTY, errno = %d\n",
                            errno);          //这里也可以向原来的controlling terminal写
                exit(0);
        }
}


本人linux菜鸟,各位大神,求指点。

------解决方案--------------------
引用:
C/C++ code?12345678910111213141516171819202122232425262728293031323334353637static