控制进程结束的时候,后台进程信号处理
2017-08-16 22:21
288 查看
Kernel sends
process:
for real (hardware) terminal: when disconnect is detected in a terminal driver, e.g. on hang-up on modem line;
for pseudoterminal (pty):
when last descriptor referencing master side of pty is closed, e.g. when you close terminal window.
Kernel sends
other process groups:
to foreground process group, when controlling
process terminates;
to orphaned
process group, when it becomes orphaned and it has stopped members.
参考:https://stackoverflow.com/questions/32780706/does-linux-kill-background-processes-if-we-close-the-terminal-from-which-it-has
代码:
执行结果果然是先接收到SIGCONT然后才是SIGHUP信号,虽然按照APUE书上说是先发送的SIGHUP信号然后是SIGCONT信号,先发送的SIGHUP反而后面收到的
,后面发送SIGCONT先收到了
解释:
The SIGHUP cannot be delivered until the child's execution is resumed. When a process is stopped, all signal delivery is suspended except for SIGCONT and SIGKILL.
So, the SIGHUP does arrive first, but it cannot be processed until the SIGCONT awakens the process execution.
参考:https://stackoverflow.com/questions/17768459/order-of-sigcont-and-sighup-sent-to-orphaned-linux-process-group
SIGHUPto controlling
process:
for real (hardware) terminal: when disconnect is detected in a terminal driver, e.g. on hang-up on modem line;
for pseudoterminal (pty):
when last descriptor referencing master side of pty is closed, e.g. when you close terminal window.
Kernel sends
SIGHUPto
other process groups:
to foreground process group, when controlling
process terminates;
to orphaned
process group, when it becomes orphaned and it has stopped members.
参考:https://stackoverflow.com/questions/32780706/does-linux-kill-background-processes-if-we-close-the-terminal-from-which-it-has
代码:
#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); }
执行结果果然是先接收到SIGCONT然后才是SIGHUP信号,虽然按照APUE书上说是先发送的SIGHUP信号然后是SIGCONT信号,先发送的SIGHUP反而后面收到的
,后面发送SIGCONT先收到了
解释:
The SIGHUP cannot be delivered until the child's execution is resumed. When a process is stopped, all signal delivery is suspended except for SIGCONT and SIGKILL.
So, the SIGHUP does arrive first, but it cannot be processed until the SIGCONT awakens the process execution.
参考:https://stackoverflow.com/questions/17768459/order-of-sigcont-and-sighup-sent-to-orphaned-linux-process-group
相关文章推荐
- PHP扩展pcntl(进程控制以及信号处理)中文文档
- 在linux或者unix操作系统中在系统的引导的时候会开启很多服务,这些服务就叫做守护进程。守护进程是在后台运行不与任何控制终端关联,是Linux中的后台服务进程。它是一个生存期较长的进程,通常独立于
- 进程,ps,优先级,前后台调用,进程信号,用户登录审计,top,系统服务的控制
- 后台封装的easyui框架,处理texbox的时候报错:未结束的字符串常量。
- PHP扩展pcntl(进程控制以及信号处理)中文文档
- LINUX编程学习笔记(十五) 进程控制 文件锁 信号处理与屏蔽
- 当子进程结束的时候,其设置的信号捕捉函数不会再父进程生效
- PHP扩展pcntl(进程控制以及信号处理)中文文档
- linux信号处理--通过发送信号控制进程
- PHP扩展pcntl(进程控制以及信号处理)中文文档
- 进程线程中睡眠函数(sleep)被信号中断后失效,处理方式总结
- jquery自制分页,控制easyui datagrid分页,集中处理了一下前后台
- 进程对信号的处理
- 使用信号控制进程
- 进程间关系:进程、僵尸进程、孤儿进程、进程组、前台进程组、后台进程组、孤儿进程组、会话、控制终端
- Linux运行与控制后台进程的方法:nohup, setsid, &, disown, screen
- linux下的僵尸进程处理SIGCHLD信号
- linux ps、top命令 进程、进程信号、优先级、前后台调用,以及用户审登陆计(unit7)
- linux进程管理之信号处理(1)
- 通过命令行控制窗口置顶和结束指定进程工具c代码