commit 8ee6ad4d96bdb1f56cece8c11e75e5491ccc4020 from: rsc date: Mon Feb 14 18:58:56 2005 UTC different fix for main proc bug commit - 8da6bca8074977c0fd1105ca4da2d5fb225b3ec4 commit + 8ee6ad4d96bdb1f56cece8c11e75e5491ccc4020 blob - 286df9c9f6ffdfc2439592e095773090cad04d55 blob + c86f941f6946e28f8e3f92a29d6ae0eead3677f6 --- src/libthread/daemonize.c +++ src/libthread/daemonize.c @@ -121,7 +121,6 @@ _threadsetupdaemonize(void) close(p[1]); break; case 0: - for(i=0; i<100; i++) sched_yield(); notedisable("sys: child"); signal(SIGCHLD, SIG_DFL); /* rfork(RFNOTEG); */ blob - c07a84f3ae02802831e9f972d9d849214c2533ab blob + 57ffa1cb049651a5879911a7ab2c95793275b897 --- src/libthread/thread.c +++ src/libthread/thread.c @@ -281,6 +281,24 @@ procscheduler(Proc *p) Out: _threaddebug("scheduler exit"); + if(p->mainproc){ + /* + * Stupid bug - on Linux 2.6 and maybe elsewhere, + * if the main thread exits then the others keep running + * but the process shows up as a zombie in ps and is not + * attachable with ptrace. We'll just sit around pretending + * to be a system proc instead of exiting. + */ + _threaddaemonize(); + lock(&threadnproclock); + if(++threadnsysproc == threadnproc) + threadexitsall(p->msg); + p->sysproc = 1; + unlock(&threadnproclock); + for(;;) + sleep(1000); + } + delproc(p); lock(&threadnproclock); if(p->sysproc) @@ -298,7 +316,7 @@ _threadsetsysproc(void) { lock(&threadnproclock); if(++threadnsysproc == threadnproc) - exit(0); + threadexitsall(nil); unlock(&threadnproclock); proc()->sysproc = 1; } @@ -576,21 +594,13 @@ main(int argc, char **argv) _pthreadinit(); p = procalloc(); + p->mainproc = 1; _threadsetproc(p); if(mainstacksize == 0) mainstacksize = 256*1024; _threadcreate(p, threadmainstart, nil, mainstacksize); procscheduler(p); - _threaddaemonize(); - /* - * On Linux 2.6, if the main thread exits then the others - * keep running but the process shows up as a zombie in ps - * and is not attachable with ptrace. We'll just sit around - * instead of exiting. - */ - for(;;) - sleep(1000); - _threadpexit(); + /* does not return */ return 0; } blob - f0b55d971a3c1016329be22b33377a996a22d60a blob + d41b918ba8117e029fa194a6d9fae848be61b206 --- src/libthread/threadimpl.h +++ src/libthread/threadimpl.h @@ -108,6 +108,7 @@ struct Proc Context schedcontext; void *udata; Jmp sigjmp; + int mainproc; }; #define proc() _threadproc()