Operating System - Tru64 Unix
1753863 Members
7371 Online
108809 Solutions
New Discussion юеВ

Re: kill does not return error when pid does not exist

 
Dubost_2
Advisor

kill does not return error when pid does not exist

I have a process that fork a son. I try to use kill( pid_of_son ) to detect when the son is dead. kill should return -1 when the pid killed does not exist.

Unfortunately, kill always return 0, even when the pid killed is dead.

Here is the code sample that demonstrate that :

#include
#include
#include

int pid_fils ;

int Son( void ) ;
int Father( void ) ;

const char * getDateTime( void )
{
struct timeval tp ;
struct tm * time_tm ;
char microsec[20] ;
static char time_msg[100] ;

/* get the current date and time */
gettimeofday( & tp, NULL ) ;
time_tm = localtime( & tp.tv_sec ) ;
strftime( time_msg, 25, "%H:%M:%S", time_tm ) ;
sprintf( microsec, "%06ld", tp.tv_usec ) ;
strcat( time_msg, ":" ) ;
strcat( time_msg, microsec ) ;

return time_msg ;
}

int main( int argc, char * argv[] )
{
printf("Begin : %s\n", getDateTime() ) ;

pid_fils = fork() ;
if( pid_fils == 0 )
{
printf("Son : pid = %d\n", getpid() ) ;
Son() ;
}
else
{
printf("Pere : pid of the son = %d\n", pid_fils ) ;
Father() ;
}
return 0 ;
}

int Son( void )
{
sleep( 5 ) ;
printf("Son : end at %s\n", getDateTime() ) ;
return 23 ;
}

int Father( void )
{
while ( kill( pid_fils, 0 ) != -1 )
{
printf("Father : wait for end of %d at %s\n", pid_fils, getDateTime() ) ;
sleep( 1 ) ;
}
printf("Father : %d terminated\n" ) ;
printf("Father : end at %s\n", getDateTime() ) ;

return 46 ;
}


The output of this program is :

Begin : 10:39:16:466291
Pere : pid of the son = 8277
Son : pid = 8277
Father : wait for end of 8277 at 10:39:16:469851
Father : wait for end of 8277 at 10:39:17:476504
Father : wait for end of 8277 at 10:39:18:486445
Father : wait for end of 8277 at 10:39:19:496379
Father : wait for end of 8277 at 10:39:20:506327
Son : end at 10:39:21:476340
Father : wait for end of 8277 at 10:39:21:516323
Father : wait for end of 8277 at 10:39:22:526253
Father : wait for end of 8277 at 10:39:23:536220
Father : wait for end of 8277 at 10:39:24:546165

which means that the father process, using kill, does not detect the end of the son process.

Can anyone help me and explain why this program does not work ?
6 REPLIES 6
Orjan Petersson
Frequent Advisor

Re: kill does not return error when pid does not exist

I am not near a Tru64 system so I have not tested your program but a couple of points to verify:

*) the first argument to kill(2) should be of type pid_t, not int.

*) what does kill(1) return for the child process? I.e. from the shell, what is the output from
$ kill -0 child_pid
(both before and after the child process finish)

*) is the child process visible from ps(1) after it has returned from main()?

Also what version of Tru64 do you use, and how do you compile the program?
Dubost_2
Advisor

Re: kill does not return error when pid does not exist

if I kill the child with kill -0 child_pid, it returns 0

The system is HP-UX 11.00 as shown with the result of uname -a :
HP-UX tax124 B.11.00 U 9000/800 1127991527 unlimited-user license

I compile with the ordinary cc compiler. what cc returns the result :
/usr/bin/cc:
LINT B.11.11.06 CXREF B.11.11.06
HP92453-01 B.11.11.06 HP C Compiler
$ Sep 8 2000 23:13:51 $
Dubost_2
Advisor

Re: kill does not return error when pid does not exist

if I kill -0 child_pid before the child is finished, it returns 0
After the child is finished, it returns 0
Dubost_2
Advisor

Re: kill does not return error when pid does not exist

This is the output of ps after the child has finished :

PID TTY TIME COMMAND
16459 pts/1 0:00 a.out
16460 pts/1 0:00

So the child is visible with the command =
Dubost_2
Advisor

Re: kill does not return error when pid does not exist

Your question drives me to ask what is a zombie process.

I can find in "man exit" the following explication :

If the parent process of the calling process is not executing a wait(), wait3(), or waitpid(), and does not have SIGCLD set to SIG_IGN, the calling process is transformed into a zombie process.

So I have just to put in the father process the line :
signal ( SIGCLD, SIG_IGN ) ;
and now my process is working and the father process detects the end of the child process.

Thank you for your help.
Venkatesh BL
Honored Contributor

Re: kill does not return error when pid does not exist

I think you would be better off trying this post on the HP-UX forum.

This one is a Tru64 forum and the program works as expected on my Tru64 box!