控制进程结束的时候,后台进程信号处理
来源:互联网 发布:销售网络建设与管理 编辑:程序博客网 时间:2024/05/22 17:14
Kernel sends SIGHUP
to 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 SIGHUP
to 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阅读全文
0 0
- 控制进程结束的时候,后台进程信号处理
- 当子进程结束的时候,其设置的信号捕捉函数不会再父进程生效
- SIGPIPE信号结束进程.
- 进程对信号的处理
- linux信号处理--通过发送信号控制进程
- 使用信号控制进程
- linux 批量结束后台进程
- Linux系统编程——进程的控制:结束进程、等待进程结束
- Linux系统编程——进程的控制:结束进程、等待进程结束
- Linux系统编程——进程的控制:结束进程、等待进程结束
- 【Linux系统编程】进程的控制:结束进程、等待进程结束
- Linux系统编程——进程的控制:结束进程、等待进程结束
- Linux系统编程——进程的控制:结束进程、等待进程结束
- Linux——进程的控制 信号
- 进程信号掩码处理
- PHP扩展pcntl(进程控制以及信号处理)中文文档
- PHP扩展pcntl(进程控制以及信号处理)中文文档
- PHP扩展pcntl(进程控制以及信号处理)中文文档
- c++利用STL编写简易通讯录
- RIP1 与RIP2 对比
- LoopViewPager+LoopIndicator
- 我的学习路径
- Squares POJ2002 哈希的应用
- 控制进程结束的时候,后台进程信号处理
- fill_parent、wrap_content和match_parent的区别
- 阿里云新一代关系型数据库 PolarDB 剖析
- 华为机试题
- handler以及handleThread相关的资识
- SQL数据库中的范式
- php环境搭建
- 剑指offer之二进制中1的个数
- [简单逻辑学]学习逻辑学的思想准备——将观念付诸语言