使用prctl API, 在父进程退出后,让子进程也退出

例子程序:

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

#include <stdlib.h>

#include <sys/prctl.h>

#include <signal.h>



 



void my_system(const char*cmd)

{

        pid_t pid;

        pid = fork();

        if (pid == 0)

        {

                prctl(PR_SET_PDEATHSIG, SIGHUP);

                execl("/bin/bash", "/bin/bash", "-c", cmd, NULL);

                exit(0);

        } else if (pid < 0)

        {

                printf("create failed\n");

        }

}



int main()

{

        int i = 0;

        my_system("./tstprogram");

        while (5)

        {

                printf("a\n");

                sleep(1);

                i++;

        }

        return 0;

}

你可能感兴趣的:(api)