我有一个父进程可能产生许多子进程的情况.我想要实现的是,如果父进程被终止或者它退出,那么它的所有子进程应该与父进程终止.
在帖子(下面的链接)中,我找到了通过让父进程成为组长来存档的建议.如果我理解正确,这也是过程组的主要目的.我对吗?
Post也提到了prctl(PR_SET_PDEATHSIG,SIGHUP); 和其他一些方法,但它们是以太网操作系统特定的,否则不会如此优雅.
我已经写了一个小的演示来试图更好地理解事物,但它并不像我期望的那样工作.我究竟做错了什么?
//https://www.andrew.cmu.edu/course/15-310/applications/homework/homework4/terminalgroups1.html
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <stddef.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/termios.h>
int main()
{
int status;
int cpid;
int ppid;
ppid = getpid();
printf("parent: %d\n", ppid);
if (!(cpid=fork()))
{
printf("child: %d\n", getpid());
if(setpgid(0,ppid) == -1)
printf("child setpgid errno %s\n", strerror(errno));
else
printf("child gid %d\n", getpgid(0));
pause();
printf("child exited\n");
exit (-1);
}
if (cpid < 0)
exit(-1);
setpgid(0, ppid);
if(setpgid(0,0) == -1)
printf("parent setpgid erno %s\n", strerror(errno));
else …Run Code Online (Sandbox Code Playgroud)