Maximum number of threads that can be created within a process in C


Thread of execution is the smallest sequence of programmed instructions that can be managed independently by scheduler. Thread is a component of process and so multiple threads can be associated in a process. Linux doesn’t have a separate threads per process limit, but has a limit on the total number of processes on the system (as threads just processes with a shared address space on Linux).

Our task is to find out maximum number of thread that can be created within a single process (maximum number of thread that pthread_create can create). Maximum number of threads can be seen is ubuntu by using command:

cat /proc/sys/kernel/threads-max

This thread limit for linux can be modified at runtime by writing desired limit to /proc/sys/kernel/threads-max.



Compile the following program on ubuntu operating system, to check maximum number of threads that can be created within a process in C.

cc filename.c -pthread where filename.c 
is the name with which file is saved.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to find maximum number of thread within
// a process
#include<stdio.h>
#include<pthread.h>
  
// This function demonstrates the work of thread
// which is of no use here, So left blank
void *thread ( void *vargp){     }
  
int main()
{
    int err = 0, count = 0;
    pthread_t tid;
      
    // on success, pthread_create returns 0 and 
    // on Error, it returns error number
    // So, while loop is iterated until return value is 0
    while (err == 0)
    {
        err = pthread_create (&tid, NULL, thread, NULL);
        count++;
    }
    printf("Maximum number of thread within a Process"
                                   " is : %d\n", count);
}

chevron_right


Output:

Maximum number of thread within a Process is : 32754

This article is contributed by Aditya Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.