LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-07-2012, 04:35 PM   #1
wollowizard
LQ Newbie
 
Registered: May 2012
Posts: 13

Rep: Reputation: Disabled
program with threads, pthread_create fails


Ok I have this problem, a server creates 2 threads for each client. I launch 200 clients and the server creates less than 400 threads, because of course I reach the maximum number. Those that are created work fine. Now I kill the clients and the server notices this and make the corresponding thread terminate.
I checked with pstree that when I kill the clients, the server also "kills" the threads. Anyway if I run another client, the server side function "pthread_create" fails. Why? It looks like once this function fails, it will always fail.

Of course if I terminate and run again the entire server, it works.

Edit: I think I answered myself calling pthread_detach. Is it possible?

Last edited by wollowizard; 06-07-2012 at 04:47 PM.
 
Old 06-08-2012, 03:17 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
would be better if you provide some source code. How is this 'server also "kills" the threads' implemented? How the server counts the number of threads?
 
Old 06-08-2012, 03:24 AM   #3
wollowizard
LQ Newbie
 
Registered: May 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
Sorry, my fault, I was not clear. The threads simply call pthread_exit. And the server does not count the number of threads.
http://pastebin.com/ssTCVEph

Could you please have a look and check if I'm using threads properly? I'm new to this world, I'm afraid of making mistakes.

Can I be sure that the thread termination is safe and always done? And that resources free after that?
Thanks

Ps. Once somebody told me to improve my code style. Does it really look like a mess? Thanks

Last edited by wollowizard; 06-08-2012 at 03:26 AM.
 
Old 06-08-2012, 03:42 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I would suggest you to check if malloc was successful (c1 and/or c2 are not null), also in case of error you should print the error code of pthread_create
 
Old 06-08-2012, 08:40 AM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by wollowizard View Post
of course I reach the maximum number.
Note that you can get a lot more threads if you reduce the default thread stack size.

Only the original stack given to a process grows automatically. Threads are limited to a fixed-size stack. The default size is ulimit -s (in kilobytes), default 8192 or 8 megabytes. On a 32-bit system that limits the number of threads a process can have to about 350 or so (assuming 3G/1G split; 240 or so if using 2G/2G memory split).

It is simple to use a different stack size, by supplying suitable pthread attribute to pthread_create():
Code:
pthread_attr_t  attrs;

pthread_attr_init(&attrs);
pthread_attr_setstacksize(&attrs, 65536);

/* After no longer needed, */
pthread_attr_destroy(&attrs);
The attribute is not "consumed" by a pthread_create() call; it is just a set of configuration defaults pthread_create() looks at when it creates a new thread. You can create any number of threads using the same attribute. You can destroy the attribute immediately after creating the threads (while the threads are still running).

The stack size is specified in bytes, and is typically 16384 bytes minimum (PTHREAD_STACK_MIN). I recommend using a power of two, or a multiple of sysconf(_SC_PAGE_SIZE) (otherwise the call is allowed to fail).

The amount of memory your threads need depend on how large the local variables are in its call chain.

In practice, I just look at the deepest case, sum the sizes of all the local variables in the function and its callers, add the 16k (minimum), plus a few k for overhead (like signal handlers and so on). There are tools you can use to find out the stack usage in real world usage, but I've found this to yield better results. If your call chains are complex, then don't allocate arrays or large structures on stack, and avoid recursive functions, and you shouldn't need much larger stack than the minimum.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] program with threads wollowizard Programming 6 05-14-2012 01:03 PM
suspend fails: can't freeze threads Kropotkin Linux - Laptop and Netbook 2 04-01-2011 06:16 AM
[SOLVED] C Program with Threads Carlos2313 Programming 6 10-26-2010 11:39 AM
pthread_create() spawns 2 threads upon first call (what is the first thread?) spotmax777 Programming 2 01-15-2009 06:37 AM
pthread_create creates a number of threads with same tid!!!! dravya Programming 5 08-11-2004 03:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration