Threads per process linux

max thread per process in linux

I wrote a simple program to calculate the maximum number of threads that a process can have in linux (Centos 5). here is the code:

int main() < pthread_t thrd[400]; for(int i=0;i<400;i++) < int err=pthread_create(&thrd[i],NULL,thread,(void*)i); if(err!=0) cout << "thread creation failed: " << i <<" error code: " << err << endl; >return 0; > void * thread(void* i) < sleep(100);//make the thread still alive return 0; >

I figured out that max number for threads is only 300!? What if i need more than that? I have to mention that pthread_create returns 12 as error code. Thanks before

You shouldn’t hit that limit whatever it is. You should create a pool of threads (possibly with a user-configured size).

Stacksize of 1mb is still to big. errcode 12 = out of mem. strerror() will print the error code by the way. Try 16k stacksize.

5 Answers 5

There is a thread limit for linux and it can be modified runtime by writing desired limit to /proc/sys/kernel/threads-max . The default value is computed from the available system memory. In addition to that limit, there’s also another limit: /proc/sys/vm/max_map_count which limits the maximum mmapped segments and at least recent kernels will mmap memory per thread. It should be safe to increase that limit a lot if you hit it.

However, the limit you’re hitting is lack of virtual memory in 32bit operating system. Install a 64 bit linux if your hardware supports it and you’ll be fine. I can easily start 30000 threads with a stack size of 8MB. The system has a single Core 2 Duo + 8 GB of system memory (I’m using 5 GB for other stuff in the same time) and it’s running 64 bit Ubuntu with kernel 2.6.32. Note that memory overcommit (/proc/sys/vm/overcommit_memory) must be allowed because otherwise system would need at least 240 GB of committable memory (sum of real memory and swap space).

If you need lots of threads and cannot use 64 bit system your only choice is to minimize the memory usage per thread to conserve virtual memory. Start with requesting as little stack as you can live with.

Linux uses physical ram to calculate the maximum number of thread therefore it is different for every system (github.com/torvalds/linux/blob/master/kernel/fork.c) static void set_max_threads(unsigned int max_threads_suggested) < u64 threads; if (fls64(totalram_pages) + fls64(PAGE_SIZE) >64) threads = MAX_THREADS; else threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE, (u64) THREAD_SIZE * 8UL); if (threads > max_threads_suggested) threads = max_threads_suggested; max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS); >

Your system limits may not be allowing you to map the stacks of all the threads you require. Look at /proc/sys/vm/max_map_count , and see this answer. I’m not 100% sure this is your problem, because most people run into problems at much larger thread counts.

Oh well, that isn’t the answer either. Another thing to try is to find your stack size with pthread_attr_getstacksize and check that your limits allow you to malloc that many stacks.

Читайте также:  Linux системное программирование 2016

I had also encountered the same problem when my number of threads crosses some threshold. It was because of the user level limit (number of process a user can run at a time) set to 1024 in /etc/security/limits.conf .

so check your /etc/security/limits.conf and look for entry:-

username -/soft/hard -nproc 1024

change it to some larger values to something 100k(requires sudo privileges/root) and it should work for you.

check the stack size per thread with ulimit, in my case Redhat Linux 2.6:

 ulimit -a . stack size (kbytes, -s) 10240 

Each of your threads will get this amount of memory (10MB) assigned for it’s stack. With a 32bit program and a maximum address space of 4GB, that is a maximum of only 4096MB / 10MB = 409 threads . Minus program code, minus heap-space will probably lead to your observed max. of 300 threads.

You should be able to raise this by compiling a 64bit application or setting ulimit -s 8192 or even ulimit -s 4096. But if this is advisable is another discussion.

You will run out of memory too unless u shrink the default thread stack size. Its 10MB on our version of linux.

EDIT: Error code 12 = out of memory, so I think the 1mb stack is still too big for you. Compiled for 32 bit, I can get a 100k stack to give me 30k threads. Beyond 30k threads I get Error code 11 which means no more threads allowed. A 1MB stack gives me about 4k threads before error code 12. 10MB gives me 427 threads. 100MB gives me 42 threads. 1 GB gives me 4. We have 64 bit OS with 64 GB ram. Is your OS 32 bit? When I compile for 64bit, I can use any stack size I want and get the limit of threads.

Also I noticed if i turn the profiling stuff (Tools|Profiling) on for netbeans and run from the ide. I only can get 400 threads. Weird. Netbeans also dies if you use up all the threads.

Here is a test app you can run:

#include #include #include #include // this prevents the compiler from reordering code over this COMPILER_BARRIER // this doesnt do anything #define COMPILER_BARRIER() __asm__ __volatile__ ("" . "memory") sigset_t _fSigSet; volatile int _cActive = 0; pthread_t thrd[1000000]; void * thread(void *i) < int nSig, cActive; cActive = __sync_fetch_and_add(&_cActive, 1); COMPILER_BARRIER(); // make sure the active count is incremented before sigwait // sigwait is a handy way to sleep a thread and wake it on command sigwait(&_fSigSet, &nSig); //make the thread still alive COMPILER_BARRIER(); // make sure the active count is decrimented after sigwait cActive = __sync_fetch_and_add(&_cActive, -1); //printf("%d(%d) ", i, cActive); return 0; >int main(int argc, char** argv) < pthread_attr_t attr; int cThreadRequest, cThreads, i, err, cActive, cbStack; cbStack = (argc >1) ? atoi(argv[1]) : 0x100000; cThreadRequest = (argc > 2) ? atoi(argv[2]) : 30000; sigemptyset(&_fSigSet); sigaddset(&_fSigSet, SIGUSR1); sigaddset(&_fSigSet, SIGSEGV); printf("Start\n"); pthread_attr_init(&attr); if ((err = pthread_attr_setstacksize(&attr, cbStack)) != 0) printf("pthread_attr_setstacksize failed: err: %d %s\n", err, strerror(err)); for (i = 0; i < cThreadRequest; i++) < if ((err = pthread_create(&thrd[i], &attr, thread, (void*)i)) != 0) < printf("pthread_create failed on thread %d, error code: %d %s\n", i, err, strerror(err)); break; >> cThreads = i; printf("\n"); // wait for threads to all be created, although we might not wait for // all threads to make it through sigwait while (1) < cActive = _cActive; if (cActive == cThreads) break; printf("Waiting A %d/%d,", cActive, cThreads); sched_yield(); >// wake em all up so they exit for (i = 0; i < cThreads; i++) pthread_kill(thrd[i], SIGUSR1); // wait for them all to exit, although we might be able to exit before // the last thread returns while (1) < cActive = _cActive; if (!cActive) break; printf("Waiting B %d/%d,", cActive, cThreads); sched_yield(); >printf("\nDone. Threads requested: %d. Threads created: %d. StackSize=%lfmb\n", cThreadRequest, cThreads, (double)cbStack/0x100000); return 0; > 

Источник

Читайте также:  Linux посмотреть права группы

Threads per process linux

  • The number one in Linux and UNIX Health Checks
  • Run hundreds of checks on your system in minutes
  • Available for AIX and Red Hat Enterprise Linux

«It’s all about the ways clients can deploy Linux and UNIX systems to improve business performance.»

How to view threads of a process on Linux

Threads are a popular programming abstraction for parallel execution on modern operating systems. When threads are forked inside a program for multiple flows of execution, these threads share certain resources (e.g., memory address space, open files) among themselves to minimize forking overhead and avoid expensive IPC (inter-process communication) channel. These properties make threads an efficient mechanism for concurrent execution.

In Linux, threads (also called Lightweight Processes (LWP)) created within a program will have the same «thread group ID» as the program’s PID. Each thread will then have its own thread ID (TID). To the Linux kernel’s scheduler, threads are nothing more than standard processes which happen to share certain resources. Classic command-line tools such as ps or top, which display process-level information by default, can be instructed to display thread-level information.

Here are several ways to show threads for a process on Linux:

The «-T» option for the ps command enables thread views. The following command list all threads created by a process with

# ps -ef | grep 97947 deploy 97947 97942 1 00:51 ? 00:13:51 java
# ps -T -p 97947 PID SPID TTY TIME CMD 97947 97947 ? 00:00:00 java 97947 97948 ? 00:00:00 java 97947 97949 ? 00:00:00 java

The «SID» column represents thread IDs, and «CMD» column shows thread names.

Using the top command

The top command can show a real-time view of individual threads. To enable thread views in the top output, invoke top with «-H» option. This will list all Linux threads. You can also toggle on or off thread view mode while top is running, by pressing ‘H’ key.

top - 14:43:25 up 6 days, 5:40, 2 users, load average: 0.87, 0.33, 0.22 Threads: 684 total, 1 running, 683 sleeping, 0 stopped, 0 zombie %Cpu(s): 6.3 us, 4.0 sy, 0.0 ni, 89.6 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 7910136 total, 384812 free, 1603096 used, 5922228 buff/cache KiB Swap: 8388604 total, 8239100 free, 149504 used. 5514264 avail Mem

Note how in the example above the number of threads on the system is listed.

Читайте также:  Delete from path linux

To restrict the top output to a particular process and check all threads running inside the process:

A more user-friendly way to view threads per process is via htop, an ncurses-based interactive process viewer. This program allows you to monitor individual threads in tree views.

To enable thread views in htop, launch htop, and press F2 to enter htop setup menu. Choose «Display option» under «Setup» column, and toggle on «Tree view» and «Show custom thread names» options. Presss F10 to exit the setup.

UNIX Health Check delivers software to scan Linux and AIX systems for potential issues. Run our software on your system, and receive a report in just a few minutes. UNIX Health Check is an automated check list. It will report on perfomance, capacity, stability and security issues. It will alert on configurations that can be improved per best practices, or items that should be improved per audit guidelines. A report will be generated in the format you wish, and the report includes the issues discovered and information on how to solve the issues as well.

Interested in learning more?

  • Try a demo version
  • Order UNIX Health Check
  • Contact us

Order

No time to lose? Need to know what’s wrong with
your UNIX system now? Then get started TODAY!

Topics

  • AIX (231)
  • Backup & restore (42)
  • DB2 (6)
  • Docker (1)
  • EMC (13)
  • EMC Networker (3)
  • Fun (3)
  • GPFS (10)
  • Hardware (12)
  • HMC (15)
  • HP Output Server (17)
  • IBM Content Manager (20)
  • Installation (30)
  • Kubernetes (1)
  • Logical Partitioning (6)
  • LVM (16)
  • Monitoring (15)
  • Networking (32)
  • NIM (12)
  • ODM (4)
  • Oracle (3)
  • Performance (13)
  • PowerHA / HACMP (31)
  • Red Hat / Linux (103)
  • SAN (20)
  • Scripting (3)
  • SDD (7)
  • Security (45)
  • Spectrum Protect (18)
  • SSA (3)
  • Storage (51)
  • Sun Solaris (3)
  • System Admin (249)
  • Veritas NetBackup (1)
  • Virtual I/O Server (5)
  • Virtualization (11)
  • VMWare (1)
  • WebSphere (6)
  • X11 (1)

This website is owned and operated by UNIX Health Check and protected by copyright. The material and information on this website may not be sold, duplicated on other websites or in any other forms, incorporated in commercial documents or products, or used for promotional purposes, without the prior written approval of UNIX Health Check.

Источник

Оцените статью
Adblock
detector