使用函数kill()杀死同一父级的进程

时间:2017-08-27 21:16:00

标签: c linux pipe system-calls file-descriptor

我在中使用 fork()构建了一个包含十个进程的树。我的任务是我必须选择一个随机的儿童进程来杀死他的兄弟。所以我的想法是,父母将通过向杀手发送他兄弟的PID,然后使用函数 kill()。但我不知道为什么功能无法正常工作。杀手进程是否需要任何类型的权限来杀死其他进程或我做错了什么?

我的代码:./pregunta1 10

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

int main(int nargv,char *argv[]){
    int n,i,numRand,status,stdout_copy;
    pid_t child,padre,*child_pids,selecto, auxPid;
    char cadena[100];
    int fd[2];

    n = atoi(argv[1]);
    child_pids = (pid_t*)malloc(n*sizeof(pid_t));
    pipe(fd);

    srand(time(NULL));
    numRand = rand()%n;

    padre = getpid();

    for (i = 0; i < n; ++i){
        child = fork();
        child_pids[i] = child;

        if((i == numRand)&&(child==0)){ //killer process
            selecto = getpid();
            printf("The killer process will be (PID):  %d\n\n",selecto);
            dup2(fd[0],STDIN_FILENO);
            close(fd[0]);
            close(fd[1]);
            sleep(1);
            //Kill his brothers
            for(i=0;i<(n-1);i++){
                read(STDIN_FILENO, &auxPid, sizeof(auxPid));
                kill(auxPid, SIGKILL);
                printf("The process %d killed: %d\n",getpid(),auxPid);

            }
            break;
            sleep(3);
        }


        if(child==0){  //childs
            close(fd[0]);
            close(fd[1]);
            sleep(10);
            break;  
        }
    }

    if(getpid() == padre){   //parent process
        //using pstree first time
        sprintf(cadena,"pstree -p %d\n",getpid());
        system(cadena);
        //making a conexion with the parent process and the killer
        stdout_copy = dup(1);
        dup2(fd[1],STDOUT_FILENO);
        close(fd[0]);
        close(fd[1]);
        //Sending PIDs to the killer process
        for(i = 0;i<n;i++){
            if(i != numRand){
                auxPid = child_pids[i];
                write(STDOUT_FILENO,&auxPid,sizeof(auxPid));
            }
        }

        sleep(2);
        //using pstree first time
        dup2(stdout_copy, 1);
        system(cadena);
        wait(&status); 

    }
    return 0;
}

我的输出(错误):

The killer process will be (PID):  5162

pregunta1(5154)─┬─pregunta1(5155)
                ├─pregunta1(5156)
                ├─pregunta1(5157)
                ├─pregunta1(5158)
                ├─pregunta1(5159)
                ├─pregunta1(5160)
                ├─pregunta1(5161)
                ├─pregunta1(5162)
                ├─pregunta1(5163)
                ├─pregunta1(5164)
                └─sh(5165)───pstree(5166)
The process 5162 killed: 5155
The process 5162 killed: 5156
The process 5162 killed: 5157
The process 5162 killed: 5158
The process 5162 killed: 5159
The process 5162 killed: 5160
The process 5162 killed: 5161
The process 5162 killed: 5163
The process 5162 killed: 5164
pregunta1(5154)─┬─pregunta1(5155)
                ├─pregunta1(5156)
                ├─pregunta1(5157)
                ├─pregunta1(5158)
                ├─pregunta1(5159)
                ├─pregunta1(5160)
                ├─pregunta1(5161)
                ├─pregunta1(5162)
                ├─pregunta1(5163)
                ├─pregunta1(5164)
                └─sh(5167)───pstree(5168)

预期输出:

The killer process will be (PID):  5162

pregunta1(5154)─┬─pregunta1(5155)
                ├─pregunta1(5156)
                ├─pregunta1(5157)
                ├─pregunta1(5158)
                ├─pregunta1(5159)
                ├─pregunta1(5160)
                ├─pregunta1(5161)
                ├─pregunta1(5162)
                ├─pregunta1(5163)
                ├─pregunta1(5164)
                └─sh(5165)───pstree(5166)
The process 5162 killed: 5155
The process 5162 killed: 5156
The process 5162 killed: 5157
The process 5162 killed: 5158
The process 5162 killed: 5159
The process 5162 killed: 5160
The process 5162 killed: 5161
The process 5162 killed: 5163
The process 5162 killed: 5164
pregunta1(5154)─┬─pregunta1(5162)
                └─sh(5167)───pstree(5168)

1 个答案:

答案 0 :(得分:2)

看起来您的被杀儿童进程处于僵尸状态。你必须为他们所有人致电wait()(不只是一个)。并在致电pstree之前执行此操作。看看这里:Make parent wait for all child processes

在您的代码中而不是

system(cadena);
wait(&status);
你应该

for (int i = 0; i < n; i++)
    wait(&status);
system(cadena);