From owner-freebsd-threads@FreeBSD.ORG Sat Jan 31 23:27:44 2004 Return-Path: Delivered-To: freebsd-threads@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 4789A16A4CE for ; Sat, 31 Jan 2004 23:27:44 -0800 (PST) Received: from rwcrmhc11.comcast.net (rwcrmhc11.comcast.net [204.127.198.35]) by mx1.FreeBSD.org (Postfix) with ESMTP id 35F9C43D31 for ; Sat, 31 Jan 2004 23:27:42 -0800 (PST) (envelope-from julian@elischer.org) Received: from interjet.elischer.org ([24.7.73.28]) by comcast.net (rwcrmhc11) with ESMTP id <20040201072741013009n1g3e>; Sun, 1 Feb 2004 07:27:41 +0000 Received: from localhost (localhost.elischer.org [127.0.0.1]) by InterJet.elischer.org (8.9.1a/8.9.1) with ESMTP id XAA51542; Sat, 31 Jan 2004 23:27:40 -0800 (PST) Date: Sat, 31 Jan 2004 23:27:39 -0800 (PST) From: Julian Elischer To: Craig Rodrigues In-Reply-To: <20040201061212.GA6349@crodrigues.org> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII cc: freebsd-threads@freebsd.org Subject: Re: pthread_create() blocks if maxthreads reached? X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 01 Feb 2004 07:27:44 -0000 On Sun, 1 Feb 2004, Craig Rodrigues wrote: > Hi, > > I wrote the following small test program to spawn > a large number of threads. > > ================================================================== > > #include > #include > #include > #include > > #define NUM 100000 > > void *thr(void *p) > { > select(0, NULL, NULL, NULL, NULL); > return p; > } > > > int > main() > { > pthread_t t[NUM]; > > int ret=0; > int i; > > for(i=0; ; ++i) > { > printf("Entering pthread_create...\n"); > ret = pthread_create(&t[i], NULL, thr, NULL); > printf("Left pthread_create...\n"); > > if( ret != 0 ) { > printf("%d %s\n", ret, strerror(ret)); > break; > } > } > printf("Max threads reached at: %d\n", i); > > return 0; > } > ============================================================== > > > If I link this program with -lthr or -lc_r, the > program will terminate. Successfully? that's interesting.. libthr should theoretically b elimited to about 8000 threads.. (on x86). > > If I link with -lpthread, the program seems to > block in the pthread_create() call, instead of > returning EAGAIN (to indicate that a new thread cannot > be created due to hitting some system limit). is that what you expected? > > Any ideas on this? yes to stop processes running away and making the system unusable while we are developing the therad package we made a limit on how many threads we allow to ente the kernel at one time per process.. If you trust your program, you could over-ride the limit. see: kern.threads.max_threads_per_proc: 150 kern.threads.max_groups_per_proc: 50 Your threads are all waiting in the kernel. If you used a blocked semaphore or some other non-kernel method of blocking then you would discover that you could do more or you could allow more threads to enter the kernel at a time but that would be a poor use of resources.. it is much more efficient to keep blocked threads in userland than to block them in the kernel where they use precious kernel resources. I'm not sure but I think 'sleep()' is intecepted for example to keep the sleeping threads in userland. (I may be wrong. I don't know if Dan did that yet). BTW the limit is not on making more threads, its that the last select() REALLY blocked and did not return to the userland program to allow you to make a new thread. > > Thanks. > -- > Craig Rodrigues > http://crodrigues.org > rodrigc@crodrigues.org > _______________________________________________ > freebsd-threads@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-threads > To unsubscribe, send any mail to "freebsd-threads-unsubscribe@freebsd.org" >