UNIX环境高级编程——创建孤儿进程
2013-08-03 02:44
381 查看
/* 创建孤儿进程 父进程终止后,向子进程发送挂断信号,又接着发送继续信号。 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <signal.h> #include <sys/types.h> #include <unistd.h> /* 处理接受到的挂断信号 */ static void sig_hup(int signo) { printf("SIGHUP recvived, pid = %d \n", getpid()); } /* 打印进程ID、父进程ID、进程组ID、前台进程组ID */ 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( int agrc, char* argv[]) { char c; pid_t pid; pr_ids("parent"); // 打印主进程(父进程)信息 if ((pid = fork()) < 0) { printf("fork error\n"); exit(-1); } else if( pid > 0) /* parent */ {// 在父进程中 sleep(5); exit(0); } else /* child */ {// 在子进程中 pr_ids("child"); signal(SIGHUP, sig_hup); // 绑定挂断信号 kill(getpid(), SIGTSTP); // 向自己发送停止信号(Ctrl+Z),暂停进程 pr_ids("child"); if (read(STDIN_FILENO, &c, 1) != 1) { printf("read error from controlling TTY, errno = %d\n", errno); } exit(0); } return 0; }运行结果:
huangcheng@ubuntu:~$ ./a.out parent: pid = 3569, ppid = 2135, pgrp = 3569, tpgrp = 3569 child: pid = 3570, ppid = 3569, pgrp = 3569, tpgrp = 3569 SIGHUP recvived, pid = 3570 child: pid = 3570, ppid = 1, pgrp = 3569, tpgrp = 3569
相关文章推荐
- UNIX环境高级编程——创建孤儿进程
- UNIX环境高级编程——创建孤儿进程
- UNIX环境高级编程学习之第九章进程关系-创建孤儿进程
- UNIX环境高级编程学习笔记:9_12 孤儿进程 孤儿进程组
- UNIX环境高级编程之创建进程与线程
- UNIX环境高级编程 第十五章:进程间通信:是指在不同进程之间传播或交换信息
- UNIX环境高级编程有一个例子说fork两次可以避免产生僵死进程,我不知道,为什么fork两次就能避免僵死进程?
- unix环境高级编程_signal及前台后台进程
- UNIX环境高级编程学习之第十三章守护进程 - 初始化一个守护进程
- unix 环境高级编程-1.6-程序和进程
- UNIX环境高级编程之第8章:进程控制
- UNIX环境高级编程——进程关系
- 进程编程3 - UNIX高级环境编程第9章读书笔记
- 进程编程3 - UNIX高级环境编程第9章读书笔记
- UNIX环境高级编程笔记之进程环境
- 进程编程2 – Unix环境高级编程8章读书笔记
- UNIX环境高级编程-打印进程ID
- Unix环境高级编程--进程控制
- UNIX环境高级编程 第7章 进程环境
- UNIX环境高级编程——进程关系