Quote:
Originally Posted by
nicos
hello everybody!!
i want ur help! it is urgent!!
...
pid=fork();
if(pid==0)
{
execl(a program);
exit(1);}
else if (pid>0)
{
timer(5); //(command 1)timer is a function that count up to 5sec
if(kill(pid,0)==0)kill(pid,9);//(command 2)
wait(&status);
....
}
else
perror("error");
....
The purpose of command1 and command2 is, if the execution of child process
does not finish in 5sec(timer(5)), i want to kill it (kill(pid,9)).
Can anybody pleasee help why this thing does not work properly!!!
THANX in advance!:-)
It's because before calling wait() to wait child exits, the child won't actually disappear in the system, i.e. if a child exits, but the parent does not immediately call wait() or waitpid() to handle the SIGCHLD from its child process, the exited child process would keep as a zombie in the system, until the parent exits or the parent call wait() or waitpid(). And kill -SIGNAL to a zombie will always succeed, so kill() returns 0.
You can try the following code zombie.c:
Quote:
include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
pid_t pid;
pid=fork();
if(pid==0) {
printf("child process, sleep 5s and exit!");
sleep(5);
exit(0);
}
else if(pid>0) {
printf("parent process, sleep 10s, then kill and wait!");
sleep(10);
printf("parent process, kill!");
if(kill(pid, 0)==0) {
printf("child still alive!");
kill(pid, 9);
}
waitpid(pid, NULL);
exit(0);
}
}
you will see after 5s, child exits, and before parent kill it and wait it, by "pf -ef | grep zombie", a <defunct> process could be found.