UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2006-12-27 07:25 PM

wilkesboy
Member
Registered: 2006-12-27
Posts: 3

Re: Thread Prg Hang

Hi,

I got AIX 5.2 and trying to run the following program on this.  The pgsp field from topas command is keep growing and hangs at certain point. The same program works on linux box with 2.6.* kernel.

- ulimit is unlimited.
- I tried by increasing AIXTHREAD_STK

I appreciate any help on this.

Thanks.

#include <pthread.h>
#include <stdio.h>

void *processMessage();

int main()
{
      int retval;
      pthread_attr_t newthrattr;

      while (1) {
            pthread_t newthread;
            pthread_create(&newthread, NULL, processMessage, (void *)NULL);
      }
}

void *processMessage()
{
     printf("Empty\n");
     pthread_exit((void *)NULL);
}

cc -O daemon.c -o daemon -lpthread (or)
cc_r -O daemon.c -o daemon

Offline

#2 2006-12-27 08:58 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Thread Prg Hang

Probably because you don't detach the threads after creating them. Try adding a call to pthread_detach() in processMessage().

Offline

#3 2006-12-28 08:34 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Thread Prg Hang

I'm not sure, just some presumption
As I remember in AIX you can have maximum 512 threads (65k with some define).And your code is doing that creates threads,so if the number of working(some threads,that have not finished printing "Empty") threads grows above the limit,the program hangs.Maybe in linux creating threads (beside i read somewhere that linux threads are processes ,that have the same process memory) is slower than in AIX,so in linux threads have time to finish.
Correct me somebody,if i'm not right,please

Offline

#4 2006-12-28 03:31 PM

wilkesboy
Member
Registered: 2006-12-27
Posts: 3

Re: Thread Prg Hang

Yes. I tried with ...detach() call as follows. It gives the same problem.


main()
{
     pthread_t   newthread;
     pthread_attr_t newthrattr;
     .......

   
     retval = pthread_attr_init(&newthrattr);
     retval = pthread_attr_setdetachstate(&newthrattr, PTHREAD_CREATE_DETACHED);
     while (1) {
          pthread_create) &newthread, &newthrattr, processMessage, (void *)NULL);
          count++;
     }
}

Offline

#5 2006-12-28 03:58 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Thread Prg Hang

What exactly is the problem?

After some point creating new threads will fail because some limit is reached (e.g. out of memory, or number of threads limit). When the program hangs, does it use 100% cpu or is it stuck somehow?

Offline

#6 2006-12-28 06:54 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: Thread Prg Hang

A couple of thoughts...

The pthread_exit( ) call in your code is not required - at least in the code shown... but a detach is required...

The reason for removing the pthread_exit is that a return from the initial thread function performs the same action(s) i.e. takes the function return val and allows it to be read via a pthread_join( ) if the thread wasn't previously detached...  and the pthread_detach is to free up the thread resources automatically on destruction ( so no join then required )...

Now I am going to take a guess as to the cause of your problem...

Your main thread is CPU bound... it does nothing except create threads... no IO or other slow functions... in fact, pthread_create is not interruptable... so a lot of ( most ) thread schedulers will give it a very HIGH priority...

All of the created threads are printing messages to the console... now they are ( each ) only printing a single message before exiting so I wouldn't exactly call them IO bound but close enough... which means a lot of ( most ) thread schedulers will give them a low execution priority and allow them to be ( temporarily ) suspended...

So think about it...

The main thread create a new thread... the new thread hits the print statement... the scheduler says "its IO, its SLOW, main( ) is still waiting, let main( ) execute"... so the main creates a new thread... and around and around we go... until memory or other resources ( e.g. thread ids ) are exhausted...

As for why Linux doesn't explode like AIX...

I am guessing its a difference in the schedulers... and while I am no longer familiar with the internals of the Linux pthread implementation... I would guess that when they see too many threads queued on ( output ) IO and approaching exhaustion that they adjust the priority / execution periods of the IO threads to allow them to execute to completion and free up resources...

So now that I typed more than I initially intended... check if your system implements pthread_yield( ) - which was a function in the draft POSIX API but was later removed - but your version of AIX may implement it... if you do not see pthread_yield, check the header sched.h for sched_yield( ) - which is part of the official POSIX API... one of those two functions should be present... and if by some chance neither is then use select with a 1 milli timeout and 0 file handles...

Whatever function you have available of the three mentioned... after each thread creation call in main, call the function... that should let the scheduler know that main can be switched out and another thread e.g. one of the children doing prints - should be allowed time on the CPU...

With any luck - that should stop your code from exploding... but I must add one final comment... while what I have stated is true and what I have described ( and what your code was doing ) is actually expected behaviour... and the suggestions should fix the problem... I do NOT have an AIX box for testing...

Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#7 2007-02-01 12:12 AM

wilkesboy
Member
Registered: 2006-12-27
Posts: 3

Re: Thread Prg Hang

Thank you very much for detailed explanation.

Offline

Board footer

Powered by FluxBB