This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

1.3.6: bug in fork(): kill(pid,0) does not always find child


Hello!

There is a problem in fork(), in combination with kill(pid, 0)
to check if the child is alive:

When a fork() returned the pid to the parent, and the parent
does a kill(pid, 0) to check if the child is alive, the kill
sometimes return -1 and errno=ESRCH (No such process).

First i thought that fork() just returns before the child
has finished the fork. In the sample this seems to be true
for child 4, 1, and possibly 10.

But then i recognized that the child was already working,
but the parent's kill() still returns ESRCH. When the parent
does the kill a bit later, then it's ok. In the sample
this is the case for child 8, 5, 2 and 0.

I tested this with currently 1.3.6, 1.3.5, 1.1.8 and also
the snapshot-dll from 2002/01/08, but it's always the same.

Test program:
>8>8
#include <stdio.h>
#include <unistd.h>
#include <errno.h>

int main(void)
{
    int nChildPid = 0;
    int nKillRv, nKillCnt;
    int nCnt = 10;
    int nRv = 0;

    do {
        nChildPid = fork();
        switch(nChildPid) {
        case -1:
            perror("can't fork");
            break;
        case 0:
            fprintf(stderr, "child: %d is up\n", nCnt);
            sleep(2);
            break;
        default:
            usleep(nCnt * 10);
            nKillCnt = 2;
            do {
                fprintf(stderr, "parent: child %d", nCnt);
                nKillRv = kill(nChildPid, 0);
                if (nKillRv < 0) {
                    fprintf(stderr, " not found, %s\n",
strerror(errno));
                    ++ nRv;
                    usleep(500000);
                } else {
                    fprintf(stderr, " found\n");
                }
            } while(nKillRv < 0 && --nKillCnt >= 0);
            sleep(1);
            break;
        }
    } while(--nCnt >= 0 && nChildPid > 0);

    return nRv;
}
8<8<

Output sample (not always the same):

>8>8
parent: child 10child: 10 is up
 not found, No such process
parent: child 10 found
child: 9 is up
parent: child 9 found
child: 8 is up
parent: child 8 not found, No such process
parent: child 8 found
child: 7 is up
parent: child 7 found
child: 6 is up
parent: child 6 found
child: 5 is up
parent: child 5 not found, No such process
parent: child 5 found
parent: child 4 not found, No such process
child: 4 is up
parent: child 4 found
child: 3 is up
parent: child 3 found
child: 2 is up
parent: child 2 not found, No such process
parent: child 2 found
parent: child 1 not found, No such process
child: 1 is up
parent: child 1 found
child: 0 is up
parent: child 0 not found, No such process
parent: child 0 found
8<8<

Regards
  haubi
-- 
Michael Haubenwallner                       SALOMON Automation GmbH
Forschung & Entwicklung                     A-8114 Friesach bei Graz
mailto:michael.haubenwallner@salomon.at     http://www.salomon.at

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]