본문 바로가기

Programming/Linux_Platform

kill parent and child process sametime



#include <stdio.h>

//#include <sys/mount.h>

#include <signal.h>

#include <stdlib.h>

#include <unistd.h>

#include <errno.h>

#include <sys/wait.h>


int main(int argc, char *argv[])

{

//    signal(SIGALRM, timer_handler); // add handler about sigalrm

    pid_t pid, child_pid;

    int rc=0;


    pid = fork();

    if (pid == 0){ // child

printf("[child] I'm child(%d)\n", getpid());

        //char *argvs[] = {~~~~~~~, (char *)0};

        //return execv(~~~~~~~~~); // run other program 

pid_t ppid = getppid();

pid_t pgid = getpgrp();

pid = getpid();

printf("[child] I will kill parent(%d)\n", ppid);

sleep(1);

printf("[child] kill parent(%d) now\n", ppid);

rc = kill(ppid, 9); // kill parent

printf("[child] rc = %d\n", rc);

sleep(1);

kill(0, SIGTERM); // kill self

printf("[child] rc = %d\n", rc);

#if 1

while(1) {

sleep(1);

printf("[child] rc = %d\n", rc);

}

#endif

return 0;

    }

    else if (pid>0){ // parent

printf("[Parent] I'm Parent(%d)\n", getpid());

        child_pid = pid;

//        alarm(30);

        waitpid(pid, NULL, 0);

printf("[Parent] wait done\n");

    }

    else{

        perror("fork");

        return 1;

    }

/*

    if (mount("/dev/sdg1", "/media/flashCorsaire/", "fuseblk",

               MS_MGC_VAL,"rw,nosuid,nodev,allow_other,blksize=4096") != 0)

    {

        fprintf(stderr, "Error: The program doesn't seem to be able ");

        fprintf(stderr, "to control the USB device\n");

        fprintf(stderr, "%s\n", strerror(errno));

        return 1;

    }

*/

printf("Hello, World! rc = %d\n", rc);

    return 0;


'Programming > Linux_Platform' 카테고리의 다른 글

logging "linux platform log" without usb connection  (0) 2016.09.28
file open 시 fd 번호 부여 방식  (0) 2016.09.01
TEMP_FAILURE_RETRY  (0) 2015.12.01
[linux] openat / open 의 차이점  (0) 2015.10.29
Android init.rc 분석  (0) 2015.10.26