Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Message-ID: <3C480F5A.7EDF164A@salomon.at> Date: Fri, 18 Jan 2002 13:04:42 +0100 From: Michael Haubenwallner X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.2.19 i686) X-Accept-Language: de-AT, en MIME-Version: 1.0 To: cygwin AT cygwin DOT com Subject: 1.3.6: bug in fork(): kill(pid,0) does not always find child Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS perl-11 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 #include #include 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 DOT haubenwallner AT salomon DOT 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/