APUE Chapter 9.Process Relationships 后台任务设为前台任务的后果
来源:互联网 发布:json串转json对象 编辑:程序博客网 时间:2024/05/16 14:19
结论: 对于支持job control的环境, 后台任务设置为前台任务的后果就是,该后台任务变为前台任务,执行结束后,Terminal自行退出。
测试代码:
#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include <sys/wait.h>#include "apue.h"#include <errno.h>#include <signal.h>static voidsig_tou(int signo){ printf("SIGTTOU received, pid = %ld\n", (long)getpid());}static voidsig_tin(int signo){ printf("SIGTTIN received, pid = %ld\n", (long)getpid());}static voidsig_int(int signo){ printf("SIGTINT received, pid = %ld\n", (long)getpid());}void pr_exit(int status);int main(){ int pid; int state; int fd; if((fd = open("/dev/tty", O_RDWR))<0) err_sys("no terminal"); signal(SIGTTOU, SIG_IGN); signal(SIGTTIN, sig_tin); signal(SIGINT, sig_int); if((pid = fork()) == 0) { setpgid(0,getpid()); while(tcsetpgrp(fd,getpid())<0){ printf("child:%s\n", strerror(errno)); if(errno == EINTR ) continue; else err_sys("child set fail"); } printf("After tcsetpgrp,the fore process group is %d\n",(int)tcgetpgrp(0)); printf("I am child,process id is %d,process group id is %d\n",getpid(),getpgrp()); exit(6); } else { setpgid(pid,pid); while(tcsetpgrp(fd,pid)<0){ if(errno == EINTR ) continue; else exit(1); } //printf("I am father,process id is %d,process group id is %d\n",getpid(),getpgrp()); //printf("in father perspective,the fore process group is %d\n",(int)tcgetpgrp(0)); while(waitpid(pid, &state, WNOHANG) != pid){} pr_exit(state); } return 0;}voidpr_exit(int status){ if (WIFEXITED(status)) printf("normal termination, exit status = %d\n", WEXITSTATUS(status)); else if (WIFSIGNALED(status)) printf("abnormal termination, signal number = %d%s\n", WTERMSIG(status),#ifdef WCOREDUMP WCOREDUMP(status) ? " (core file generated)" : "");#else "");#endif else if (WIFSTOPPED(status)) printf("child stopped, signal number = %d\n", WSTOPSIG(status));}
MacOSX 13.4.0 本地运行结果:
$ gcc prog.c libapue.a;
$ ./a.out&
[1] 29190
$ After tcsetpgrp,the fore process group is 29191
I am child,process id is 29191,process group id is 29191
normal termination, exit status = 6
logout[Process completed]
0 0
- APUE Chapter 9.Process Relationships 后台任务设为前台任务的后果
- 《APUE》Chapter 9 Process relationships (学习笔记加上自己的代码)
- Linux前台和后台任务的切换
- Win7的任务计划怎么是后台运行的,怎么设置能让任务前台运行?
- linux下关于后台任务和前台任务之间的且切换命令小结
- sheel任务由前台切换到后台
- linux中的前台任务、后台任务和守护进程
- IOS的后台任务
- IOS的后台任务
- 后台任务的抉择
- IOS的后台任务
- IOS的后台任务
- iOS的后台任务
- 项目中使用到的封装---后台子线程执行任务,前台显示加载条
- 【RHCE学习笔记1】linux任务的前台和后台执行
- 后台任务
- !后台任务
- 后台任务
- OERR: ORA-3137 "TTC protocol int…
- DBA的考核
- arp命令--arptables
- 执行scp命令时不需要输入用户密码
- CONFIGURE ARCHIVELOG DELETION PO…
- APUE Chapter 9.Process Relationships 后台任务设为前台任务的后果
- rman的一些配置的解释
- RMAN-20004: target database name…
- AutoPatch warning:zfaprod.txt do…
- Java实现动态代理
- linux中用shell获取昨天、明天或多…
- 查看操作系统上系统块大小
- 利用原生JS将下载链接转换为二维码
- What are the meaning of the code…