kill a process which run out of time

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!:slight_smile:

please check whether kill (pid, 9) was really successfully called.

the problem i found is that the command if(kill(pid,0)==0) is always true either if the child process finishs on time (before timer ends up) or if the process does not finish on time. ANybody help?

thanx

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:

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.