我的程序创建了 n 个子进程,每个子进程 count(+5) 如果超过 100,它会向父进程发送一个信号,父进程应该杀死这个子进程。 我做了这个程序,但它不起作用,它一直在计算第一个 child ,这意味着 SIGKILL 不起作用。
#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
pid_t pids[10];
int pidval[10];
int l;
void handler1(int mysignal)
{
int i;
for (i=0; i<l; i++) {
if (pidval[i]>100) {
kill(pids[i], SIGKILL);
printf("\n killed ");
}
}
}
int main(int argc, char ** argv)
{
int i, s;
l = atoi(argv[1]);
pid_t pid;
for(i=0; i<l; i++)
{
pid=fork();
if(pid<0)
printf("\n error \n");
if (pid==0) {
pids[i] = getpid();
while(1) {
s+=5;
if(s>100)
{
pidval[i]=s;
printf("\noverflow,%d,%d,%d",s,pids[i],getpid());
kill(getppid(), SIGALRM);
};
}
}
if(pid>0) {
signa(SIGALRM,handler1);
waitpid(-1,NULL,0);
}
}
}
最佳答案
使用SIGTERM,看看这个article .
关于c - 在子进程发出完成信号后,SIGKILL 将无法停止子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21234067/