different fix for main proc bug
diff --git a/src/libthread/daemonize.c b/src/libthread/daemonize.c
index 286df9c..c86f941 100644
--- a/src/libthread/daemonize.c
+++ b/src/libthread/daemonize.c
@@ -121,7 +121,6 @@
 		close(p[1]);
 		break;
 	case 0:
-		for(i=0; i<100; i++) sched_yield();
 		notedisable("sys: child");
 		signal(SIGCHLD, SIG_DFL);
 	/*	rfork(RFNOTEG); */
diff --git a/src/libthread/thread.c b/src/libthread/thread.c
index c07a84f..57ffa1c 100644
--- a/src/libthread/thread.c
+++ b/src/libthread/thread.c
@@ -281,6 +281,24 @@
 
 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 @@
 {
 	lock(&threadnproclock);
 	if(++threadnsysproc == threadnproc)
-		exit(0);
+		threadexitsall(nil);
 	unlock(&threadnproclock);
 	proc()->sysproc = 1;
 }
@@ -576,21 +594,13 @@
 
 	_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;
 }
 
diff --git a/src/libthread/threadimpl.h b/src/libthread/threadimpl.h
index f0b55d9..d41b918 100644
--- a/src/libthread/threadimpl.h
+++ b/src/libthread/threadimpl.h
@@ -108,6 +108,7 @@
 	Context	schedcontext;
 	void		*udata;
 	Jmp		sigjmp;
+	int		mainproc;
 };
 
 #define proc() _threadproc()