gpt4 book ai didi

c - 使用函数 kill() 杀死具有相同父进程的进程

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:06:22 26 4
gpt4 key购买 nike

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

我的代码: (./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):  5162pregunta1(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: 5155The process 5162 killed: 5156The process 5162 killed: 5157The process 5162 killed: 5158The process 5162 killed: 5159The process 5162 killed: 5160The process 5162 killed: 5161The process 5162 killed: 5163The process 5162 killed: 5164pregunta1(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):  5162pregunta1(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: 5155The process 5162 killed: 5156The process 5162 killed: 5157The process 5162 killed: 5158The process 5162 killed: 5159The process 5162 killed: 5160The process 5162 killed: 5161The process 5162 killed: 5163The process 5162 killed: 5164pregunta1(5154)─┬─pregunta1(5162)                └─sh(5167)───pstree(5168)

最佳答案

看起来你被杀死的子进程处于僵尸状态。您必须为它们全部调用 wait()(不仅仅是一个)。并在调用 pstree 之前执行此操作。看这里:Make parent wait for all child processes

在你的代码中而不是

system(cadena);
wait(&status);

你应该有

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

关于c - 使用函数 kill() 杀死具有相同父进程的进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45909449/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com