From owner-freebsd-threads@FreeBSD.ORG Sun Feb 1 07:21:19 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 7946816A4CF for ; Sun, 1 Feb 2004 07:21:19 -0800 (PST) Received: from rwcrmhc11.comcast.net (rwcrmhc11.comcast.net [204.127.198.35]) by mx1.FreeBSD.org (Postfix) with ESMTP id 7AD1C43D45 for ; Sun, 1 Feb 2004 07:21:12 -0800 (PST) (envelope-from rodrigc@crodrigues.org) Received: from h00609772adf0.ne.client2.attbi.com ([66.31.45.197]) by comcast.net (rwcrmhc11) with ESMTP id <20040201152111013009oot9e>; Sun, 1 Feb 2004 15:21:11 +0000 Received: from h00609772adf0.ne.client2.attbi.com (localhost.crodrigues.org [127.0.0.1])i11FL6G9002532; Sun, 1 Feb 2004 10:21:06 -0500 (EST) (envelope-from rodrigc@h00609772adf0.ne.client2.attbi.com) Received: (from rodrigc@localhost)i11FL5na002531; Sun, 1 Feb 2004 10:21:05 -0500 (EST) (envelope-from rodrigc) Date: Sun, 1 Feb 2004 10:21:05 -0500 From: Craig Rodrigues To: Julian Elischer Message-ID: <20040201152105.GA2370@crodrigues.org> References: <20040201061212.GA6349@crodrigues.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4.1i 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 15:21:19 -0000 On Sat, Jan 31, 2004 at 11:27:39PM -0800, Julian Elischer wrote: > 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 -lthr, I get: Max threads reached at: 109 If I link with -lc_r, I get: Max threads reached at: 14118 If I link with -lpthread, I get about 161 threads created, and then pthread_create() blocks and I never get to the "Max threads" message. > > > > > > 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? No that is not what I expected. I expected that pthread_create() should either: - if it creates the thread successfully, return 0 - if it cannot create the thread, return EAGAIN if a resource limit is it, or EINVAL if bad parameters were passed down into the pthread_create() call I never expected that pthread_create() would block. > 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 Understood. Keep in mind, I am trying to "kick the tires" on libpthread, to see how it reacts on crazy corner cases. I can certainly increase the values of these sysctls, but I think that pthread_create() should not block if it hits a limit. > > 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. Well, this may be so, but I don't think pthread_create() should block. For kicks, I ran this code on Solaris 2.7, and pthread_create() returned EAGAIN after spawning 3399 threads. -- Craig Rodrigues http://crodrigues.org rodrigc@crodrigues.org