- 论坛徽章:
- 0
|
- #include <sys/types.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <signal.h>
- #include <stdio.h>
- #include <unistd.h>
- //////////////////////////////////////
- static void sig_hup(int);
- static void pr_ids(char *);
- //////////////////////////////////////
- int main(void)
- {
- char c;
- pid_t pid;
- ////////////////////////////////
- pr_ids("parent");
- ////////////////////////////////
- if ( (pid = fork()) < 0 )
- perror("fork error");
- else if ( pid > 0 ) {
- sleep(5);
- exit(0);
- } else {
- pr_ids("child");
- signal(SIGHUP, sig_hup);
- kill(getpid(), SIGTSTP);
- pr_ids("child");
- if ( read(0, &c, 1) != 1 )
- printf("read error from control terminal, errno = %d\n", errno);
- exit(0);
- }
- }
- ////////////////////////////////////////////////////////////////////////
- static void sig_hup(int signo)
- {
- printf("SIGHUP recevid, pid = %d\n", getpid());
- return;
- }
- ////////////////////////////////////////////////////////////////////////
- static void pr_ids(char *name)
- {
- printf("%s: pid = %d, ppid = %d, pgrp = %d\n", name, getpid(), getppid(), getpgrp());
- fflush(stdout);
- }
复制代码
上述程序是APUE第9章中的一个例子,该例子创建了一个孤儿进程组。编译后的输出为:
- [k3rn3l@localhost test]$ ./a.out
- parent: pid = 3235, ppid = 3069, pgrp = 3235
- child: pid = 3236, ppid = 3235, pgrp = 3235
- [k3rn3l@localhost test]$ SIGHUP recevid, pid = 3236
- child: pid = 3236, ppid = 1, pgrp = 3235
- read error from control terminal, errno = 5
复制代码
对于此,在APUE中的描述是:“注意,因为两个进程,登陆shell和子进程都写向终端,所以shell提示符和子进程的输出一起出现。”这里我不太明白,哪位DX能解释一下,小弟先谢过了。
另外,不知道下面这个if语句在这个程序中起到一个什么样的作用???
- if ( read(0, &c, 1) != 1 )
- printf("read error from control terminal, errno = %d\n", errno);
复制代码 |
|