今天在此记录一下几种情况。
情况一:杀掉父进程,子进程依然运行。
情况二:先暂停子进程,然后杀掉父进程,则子进程退出
这两种情况下的代码如下所示。
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <stdlib.h>
#include <errno.h>
typedef void (*SIGFUNCTYPE)(int);
SIGFUNCTYPE Signal_(int sig, void (*handler)(int))
{
struct sigaction act, oact;
act.sa_handler = handler;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, sig);
if(sigaction(sig, &act, &oact) == -1){
oact.sa_handler = (void (*)())SIG_ERR;
}
return(oact.sa_handler);
}
void
term_handler(int signum)
{
exit(0);
}
void hup_handler(int signum)
{
int pid = getpid();
printf("receive hup sig in pid %d\n", pid);
}
void cont_handler(int signum)
{
int pid = getpid();
printf("receive cont sig in pid %d\n", pid);
}
int main()
{
//Signal_(SIGTERM, (SIGFUNCTYPE) term_handler);
//Signal_(SIGHUP, hup_handler);
//Signal_(SIGCONT, cont_handler);
int pid = fork();
if(pid == 0)
{
/*
int pid2=getpid();
int sid2 = getsid(pid2);
printf("in child process, session id is %d\n", sid2);
setsid();
sid2 = getsid(pid2);
printf("in child process, after setsid, session id is %d\n", sid2);
*/
while(1);
}
int sid1 = getsid(getpid());
printf("in parent process, session id is %d\n", sid1);
sleep(60);
//while(1);
return 0;
}
针对第二种情况,为何子进程会退出,这里做简单说明,杀掉父进程后,子进程会接收到SIGHUP信号,而此信号,子进程没有处理,则默认结束子进程;但是这里有一点本人没明白,杀掉父进程,子进程为何会收到SIGHUP信号,这一点没弄明白,因为本人发现子进程若是活动状态,则其不会收到SIGHUP信号,只有kill -SIGSTOP暂停子进程后,杀掉父进程,子进程才会收到SIGHUP信号,哪位大神若是明白,请评论区中给予指导,谢谢。
第三种情况:针对SIGHUP,子进程进行处理。此时暂停子进程,然后杀掉父进程,则子进程被激活,并且不会退出。
代码如下:
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <stdlib.h>
#include <errno.h>
typedef void (*SIGFUNCTYPE)(int);
SIGFUNCTYPE Signal_(int sig, void (*handler)(int))
{
struct sigaction act, oact;
act.sa_handler = handler;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, sig);
if(sigaction(sig, &act, &oact) == -1){
oact.sa_handler = (void (*)())SIG_ERR;
}
return(oact.sa_handler);
}
void
term_handler(int signum)
{
exit(0);
}
void hup_handler(int signum)
{
int pid = getpid();
printf("receive hup sig in pid %d\n", pid);
}
void cont_handler(int signum)
{
int pid = getpid();
printf("receive cont sig in pid %d\n", pid);
}
int main()
{
//Signal_(SIGTERM, (SIGFUNCTYPE) term_handler);
Signal_(SIGHUP, hup_handler);
//Signal_(SIGCONT, cont_handler);
int pid = fork();
if(pid == 0)
{
/*
int pid2=getpid();
int sid2 = getsid(pid2);
printf("in child process, session id is %d\n", sid2);
setsid();
sid2 = getsid(pid2);
printf("in child process, after setsid, session id is %d\n", sid2);
*/
while(1);
}
int sid1 = getsid(getpid());
printf("in parent process, session id is %d\n", sid1);
sleep(60);
//while(1);
return 0;
}
这里针对第三种情况解释下,为何杀掉父进程后,子进程还被激活了,这里是由于操作系统的设计,当操作系统需要给进程发送SIGHUP信号时,若该进程被暂停了,则首先发送SIGCONT给该进程,然后发送SIGHUP给该进程,所以该进程必须对SIGHUP进行处理,否则其会退出。
第四种情况:暂停子进程后,如果杀掉父进程,依然想让子进程呈暂停状态怎么办(不死掉,也不激活),这个可以给子进程设置独立的session id来解决,代码如下:
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <stdlib.h>
#include <errno.h>
typedef void (*SIGFUNCTYPE)(int);
SIGFUNCTYPE Signal_(int sig, void (*handler)(int))
{
struct sigaction act, oact;
act.sa_handler = handler;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, sig);
if(sigaction(sig, &act, &oact) == -1){
oact.sa_handler = (void (*)())SIG_ERR;
}
return(oact.sa_handler);
}
void
term_handler(int signum)
{
exit(0);
}
void hup_handler(int signum)
{
int pid = getpid();
printf("receive hup sig in pid %d\n", pid);
}
void cont_handler(int signum)
{
int pid = getpid();
printf("receive cont sig in pid %d\n", pid);
}
int main()
{
//Signal_(SIGTERM, (SIGFUNCTYPE) term_handler);
Signal_(SIGHUP, hup_handler);
//Signal_(SIGCONT, cont_handler);
int pid = fork();
if(pid == 0)
{
int pid2=getpid();
int sid2 = getsid(pid2);
printf("in child process, session id is %d\n", sid2);
setsid();
sid2 = getsid(pid2);
printf("in child process, after setsid, session id is %d\n", sid2);
while(1);
}
int sid1 = getsid(getpid());
printf("in parent process, session id is %d\n", sid1);
sleep(60);
//while(1);
return 0;
}