我的程序创建 n 个子进程,每个子进程计数(+5)如果超过 100,它会向父进程发送信号,父进程应该杀死这个孩子。我做了这个程序,但它不起作用,它一直在计算第一个孩子,这意味着 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);
}
}
}