如果因为终端断开引起的向控制进程发送的SIGHUP信号会导致控制进程终止,那么SIGHUP信号会被发送给终端的前台进程组中的成员。控制进程处于任何原因终止,那么前台进程就会收到SIGHUP信号。这个是控制进程终止的结果,不是SIGHUP信号相关联的行为。
-
gwwu@hz-dev2.wgw.com:~/test/signal>more trap_sighup2.c
-
#include <unistd.h>
-
#include <stdio.h>
-
#include <string.h>
-
#include <signal.h>
-
-
static void handler(int sig)
-
{
-
printf("PID %ld caught signal %2d (%s)\n",getpid(),sig,strsignal(sig));
-
}
-
-
int main(int argc, char *argv[])
-
{
-
pid_t parentPid, childPid;
-
int j;
-
struct sigaction sa;
-
-
-
if(argc < 2 || strcmp(argv[1], "--help") == 0) {
-
printf("Usage: %s {d|s} ... [ > sig.log 2>&1 ]\n",argv[0]);
-
return 0;
-
}
-
setbuf(stdout,NULL);
-
-
parentPid = getpid();
-
printf("PID of parent process is %ld,shell process id = %ld\n",(long)parentPid,getppid());
-
printf("Foreground process group ID is: %ld,session id = %ld\n",(long)tcgetpgrp(STDIN_FILENO),getsid(0));
-
-
for(j = 1; j < argc; j++) {
-
childPid = fork();
-
if(childPid < 0) {
-
printf("fork error\n");
-
return -1;
-
}
-
-
if(childPid == 0) {
-
if(argv[j][0] == 'd') {
-
if(setpgid(0,0) == -1) {
-
printf("setpgid error\n");
-
return -1;
-
}
-
}
-
sigemptyset(&sa.sa_mask);
-
sa.sa_flags = 0;
-
sa.sa_handler = handler;
-
if(sigaction(SIGHUP,&sa,NULL) == -1) {
-
printf("sigaction error\n");
-
return -1;
-
}
-
break;
-
}
-
}
-
alarm(60);
-
printf("PID = %ld,PGID = %ld\n",getpid(),getpgrp());
-
for(;;)
-
pause;
-
}
-
gwwu@hz-dev2.wgw.com:~/test/signal>gcc -g trap_sighup2.c -o trap_sighup2
-
gwwu@hz-dev2.wgw.com:~/test/signal>echo $$
-
23078
-
gwwu@hz-dev2.wgw.com:~/test/signal>./trap_sighup2 d s s > trap_sighup2.log 2>&1
-
/*退出后重新登入*/
-
Last login: Thu Apr 23 17:13:20 2015 from 10.155.60.28
-
gwwu@hz-dev2.wgw.com:~/codes/istanbul_4_22>
-
gwwu@hz-dev2.wgw.com:~/codes/istanbul_4_22>cd ~/test/
-
Display all 168 possibilities? (y or n)
-
gwwu@hz-dev2.wgw.com:~/codes/istanbul_4_22>cd ~/test/signal/
-
gwwu@hz-dev2.wgw.com:~/test/signal>more trap_sighup2.log
-
PID of parent process is 23169,shell process id = 23078
-
Foreground process group ID is: 23169,session id = 23078
-
PID = 23169,PGID = 23169
-
PID = 23172,PGID = 23169
-
PID = 23171,PGID = 23169
-
PID = 23170,PGID = 23170
-
PID 23171 caught signal 1 (Hangup)
-
PID 23172 caught signal 1 (Hangup)
阅读(938) | 评论(0) | 转发(0) |