c - 如何通过执行 exec 终止正在运行另一个程序的子进程

标签 c

我正在我的主程序中执行 fork,并在将运行另一个程序的子进程中执行 exec。现在我想终止子程序(即 exec 调用的程序)并返回主程序(或父程序)。我怎么能做到这一点..我尝试使用 ctrl+c 但它也杀死了父进程和子进程。请帮助我。

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

现在我想杀掉“test application”(由exec调用),回到父进程或“else block”继续程序。

最佳答案

您需要执行以下操作:

  1. 首先执行 kill(pid, SIGTERM) - 这让子进程有机会优雅地终止
  2. 等待一段时间(使用sleep)。时间段取决于子进程正常关闭所需的时间。
  3. 使用 waitpid(pid, &status, WNOHANG) 检查返回值。如果进程没有中止,请执行第 4 步
  4. 执行 kill(pid, SIGKILL) 然后通过执行 waitpid(pid, &status, 0) 收割僵尸。

这些步骤确保您为子进程提供了一个关闭信号处理程序,并确保您没有僵尸进程。

关于c - 如何通过执行 exec 终止正在运行另一个程序的子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14110738/

相关文章:

c - C中反转字符串的函数

c - R XML - 无法从内存中删除内部 C 节点

转换为连续 if 语句的无分支

c - 弹性炼金术 : Returning a ByteArray from C function

c - linux:stdin 导致 select 在我不希望它返回时返回

c - 如何确定时间戳计数器 (TSC) 重置了多少次

字符内存分配

c - 解释为什么这两个多维数组看起来是有效的语法,但只有一个给出了预期的结果?

c - 使用 argp.h 解析为整数

c - 从结构体指针添加 int