如何终止该做的exec运行的其他程序一个子进程 [英] How to terminate a child process which is running another program by doing exec

查看:130
本文介绍了如何终止该做的exec运行的其他程序一个子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做我的主程序叉,并在子进程,这将运行另一个程序做的exec。现在我想终止子(即,由EXEC调用的程序)并返回到主程序(或父程序)。我怎么能做到这一点。我用Ctrl + C,但其杀死父进程和子试图also.please帮助我。

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);
}

现在我想要杀测试应用程序(由EXEC调用),并返回到父进程或else块,以继续执行程序。

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

推荐答案

您需要做到以下几点:


  1. 请在杀(PID,SIGTERM )第一 - 这使子进程一个机会,优雅地终止

  2. 等待一段时间(使用睡眠)。的时间周期取决于子过程需要从容关闭的时间。

  3. 使用 waitpid函数(PID,和放大器;状态,WNOHANG)检查返回值。如果进程不会中止执行步骤4

  4. 请在杀(PID,SIGKILL ),然后通过执行收获僵尸 waitpid函数(PID,和放大器;状态,0)

  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.

这篇关于如何终止该做的exec运行的其他程序一个子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆