Raju Kunde
Raju Kunde

Reputation: 982

How to terminate a child process which is running another program by doing exec

I'm doing fork in my main program,and doing exec in the child process which will run another program. Now i want to terminate the child(i.e., the program invoked by exec) and return back to the main program(or parent program). how could i achieve this.. I tried with ctrl+c but its killing parent process and child also.please help me.

/*This is main.c*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>

void sig_int(void);
void sig_term(void);

pid_t pid,ppid;

int main(char argc,char **argv){

int n;
char ch;

printf("***********Application to start or stop services**********\n");

do
{
    printf("Enter 1 to start service no.1\n");
    printf("Enter 2 to start service no.2\n");
    printf("Enter 3 to start service no.3\n");

    scanf("%d",&n);
    if(fork() == 0)
    {
        switch(n)
        {
            case 1: printf("starting service no. 1..\n");
                printf("checking whether the given service is     already running...\n");
            //  system("./det.sh ./test")   
                pid = getpid();
                printf("child process pid = %d\n",pid);
//                  signal(SIGINT,(void *)sig_int);
//                  signal(SIGTERM,(void *)sig_term);
                  //execl("/var/vR_main","vR_main",argv[1],argv[2],argv[3],argv[4],NULL);
                execl("./test","test",0,0);//will run test.c
                break;

            case 2: printf("starting service no. 2..\n");
                break;

            case 3: printf("starting service no. 3..\n");
                break; 

        }

    }
    else
    {   
        int status;
        wait(&status);  
            if (WIFEXITED(status))
                printf("CHILD exited with %d\n", WEXITSTATUS(status));

            if (WIFSIGNALED(status))
                printf("signaled by %d\n", WTERMSIG(status));

            if (WIFSTOPPED(status))      
                printf("stopped by %d\n", WSTOPSIG(status));
//          sleep(2);
        ppid = getpid();
        printf("%d\n",ppid);
//          wait();
        printf("\nDo you want to continue...y/n:");
        scanf(" %c",&ch);
    }
}while(ch == 'y');

return 0;   

}

void sig_int(void)
{
printf("caught signal\n");
kill(pid,SIGKILL);
//  signal(SIGINT,SIG_DFL);
//  exit(0);
}

void sig_term(void)
{
printf("killing the process\n");
signal(SIGINT,SIG_DFL);
//  exit(0);
}

/*This is test.c*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>

void sig_int(void);
void sig_term(void);

pid_t pid;
int main()
{
//  int a=10,b=40,c=50,max;

    pid = getpid();
printf("exec pid = %d\n",pid);

while (1)
{

    signal(SIGINT,(void *)sig_int);
    signal(SIGTERM,(void *)sig_term);
}
//  max=a>b?a>c?a:c:b>c?b:c;
//  printf("%d\n",max);
}
void sig_int(void)
{
printf("caught signal\n");
//  signal(SIGINT,SIG_DFL);
kill(pid,SIGKILL);
//  exit(0);

}

void sig_term(void)
{
printf("killing the process\n");
signal(SIGINT,SIG_DFL);
//  exit(0);
}

Now I want to kill "test application" (invoked by exec),and return to the parent process or the "else block" to continue the program.

Upvotes: 5

Views: 21720

Answers (3)

Ed Heal
Ed Heal

Reputation: 60017

You need to do the following:

  1. Do a kill(pid, SIGTERM) first - this gives the child process an opportunity to terminate gracefully
  2. Wait a period of time (use sleep). The period of time depends on the time the child process takes to close down gracefully.
  3. Use waitpid(pid, &status, WNOHANG) checking the return value. If the process has not aborted do step 4
  4. Do a kill(pid, SIGKILL) then harvest the zombie by doing waitpid(pid, &status, 0).

These steps ensure that you give the child process to have a signal handler to close down and also ensures that you have no zombie processes.

Upvotes: 11

kmkaplan
kmkaplan

Reputation: 18960

POSIX defines the kill(2) system call for this:

kill(pid, SIGKILL);

Upvotes: 2

md5
md5

Reputation: 23717

Either in or outside your program, it is possible to use kill. By including <signal.h>, you can kill a process with a given PID (use the fork return value to do this).

#include <signal.h>

int pid;

switch (pid = fork())
{
case -1:
  /* some stuff */
  break;
case 0: 
  /* some stuff */
  break;
default:
  /* some stuff */
  kill(pid, SIGTERM);
}

It is also possible to use kill command in the shell. To find the PID of your child process, you can run ps command.

man kill
The kill() function shall send a signal to a process or a group of processes specified by pid. The signal to be sent is specified by sig and is either one from the list given in <signal.h> or 0. If sig is 0 (the null signal), error checking is performed but no signal is actually sent. The null signal can be used to check the validity of pid.

Upvotes: 1

Related Questions