My program creates n child processes, every child count(+5) if it surpasses 100 it sends a signal to the parent, the parent should kill this child. I did the program but it won't work, it keeps counting in the first child, which means the SIGKILL didn't work.
#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);
}
}
}