delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2003/12/18/04:34:38

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Message-ID: <E087F7025D7E494B8015119695CEDB4C0209B0C0@zpl02exm01.mpsc.mot.com>
From: Nowakowski Maciej-AMN011 <maciej DOT nowakowski AT motorola DOT com>
To: "'cygwin AT cygwin DOT com'" <cygwin AT cygwin DOT com>
Subject: RE: kill(pid, 0) issue
Date: Thu, 18 Dec 2003 10:34:12 +0100
MIME-Version: 1.0

> -----Original Message-----
> From: Christopher Faylor 
> Sent: Wednesday, December 17, 2003 6:27 PM
> To: cygwin AT cygwin DOT com
> Subject: Re: kill(pid, 0) issue
> 
> 
> On Wed, Dec 17, 2003 at 05:23:20PM +0100, Nowakowski 
> Maciej-AMN011 wrote:
> >My application creates additional process using fork() function. 
> >Created child process listens on a socket and exits when it receives 
> >anything.  The main process checks the child PID using kill(pid, 0) 
> >with child PID as a parameter.  Even when the child has exited this 
> >function call returns 0.  When I have supplied any PID which hasn't 
> >ever existed it's fine and kill(non-existentPID, 0) returns -1.
> >
> >Has anyone experienced something like this?
> 
> This seems like a perfect place for a simple test case.  For instance:
> 
> #include <stdio.h>
> #include <stdlib.h>
> #include <signal.h>
> #include <unistd.h>
> #include <sys/wait.h>
> 
> int
> main (int argc, char **argv)
> {
>   int pid;
>   if (argv[1])
>     pid = atoi(argv[1]);
>   else if ((pid = fork ()) == 0)
>     {
>       puts ("forking a process and then exiting");
>       exit (0);
>     }
>   else
>     {
>       int dummy;
>       wait (&dummy);
>     }
>   printf ("%d = kill (%d, 0)\n", kill (pid, 0), pid);
>   exit (0);
> }
> 
> I tried the above with no argument and with an argument of a 
> previously forked-and-exited process.  Both cases produced 
> the expected result, as did trying this on a running process.
> 
> I suspect that you are not 'wait()'ing for the process to 
> exit before checking if it exists.  kill(pid, 0) will succeed 
> on both linux and cygwin if the process is not reaped by 
> calling wait (or waitpid, etc.) first.
> 
> cgf
> 

Christopher,

Thanks for a reply. I have run your test case and it really works.
So then I have modified it slightly to make it similar to what my 
application is doing. Now it reproduces the behaviour of my app. 
I'm running this test below in a Windows console and using Cygwin 
console to display processes issuing 'ps' command. I have expected
the child process to be finished after some time(when main process
is blocked on 'getc()') but kill(pid, 0) returns 0.

Probably there is an issue lying beneath I'm not aware of.

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>

static void ChildExitSigHandle(int sig)
{
  printf ("Child termination signal received...\n");
}

int
main (int argc, char **argv)
{
  int pid;
  
  signal(SIGCHLD, ChildExitSigHandle);
  
  if (argv[1])
    pid = atoi(argv[1]);
  else if ((pid = fork ()) == 0)
    {
      puts ("forking a process and then exiting");
      exit (0);
    }
  else
    {
      //int dummy;
      //wait (&dummy);
      getc(stdin);
    }
  printf ("%d = kill (%d, 0)\n", kill (pid, 0), pid);
  exit (0);
}

Regards,

Maciek

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019