Differences between pthread_create in the Linux kernel 2.4.20 and 2.4.36

I have code on two systems running kernel 2.4.20 and kernel 2.4.38 . Both have gcc 3.2.2 and glibc 2.3.2

In kernel 2.4.38 pthread_t handles are not reused. In a heavy load test, the application crashes after the handles reach 0xFFFFFFFF .

(I suspected this in the first place, because the application crashes in deployments where IT uses a network port scanner - threads are created to handle socket connections)

This simple example recreates the problem:

 void* ThreadProc(void* param) { usleep(10000); printf(" Thread 0x%x\n", (unsigned int)pthread_self()); usleep(10000); return NULL; } int main(int argc, char* argv[]) { pthread_t sThread; while(1) { pthread_create(&sThread, NULL, ThreadProc, NULL); printf("Created 0x%x\n", (unsigned int)sThread); pthread_join(sThread, NULL); }; return 0; } 

In section 2.4.20:

  Created 0x40838cc0 Thread 0x40838cc0 Created 0x40838cc0 Thread 0x40838cc0 Created 0x40838cc0 Thread 0x40838cc0 ...and on and on... 

In section 2.4.36:

  Created 0x4002 Thread 0x4002 Created 0x8002 Thread 0x8002 Created 0xc002 Thread 0xc002 ...keeps growing... 

How can I get kernel 2.4.36 to handle descriptors? Unfortunately, I cannot easily change the kernel. Thanks!

+6
source share
2 answers

If your observations are correct, there are only two possible solutions.

Or

  • Update the kernel. This may or may not be feasible for you.
  • Recycle threads in your application.

Option 2 is what you can do, even if the kernel behaves badly. You can keep a pool of threads that remain in a sleep state when not in use. Thread pools are a well-known software development pattern (see http://en.wikipedia.org/wiki/Thread_pool_pattern ). This is probably the best solution for you.

+4
source

Turns out I didn't go into my threads properly in the load test.

When I restarted the load test again, the flow handlers reached 0xFFFFF002 and then flipped to 0x1002 and continued successfully.

Moral of the story: make sure your threads are connected or disconnected!

0
source

Source: https://habr.com/ru/post/919753/


All Articles