6

I have a question going in my mind. I just want to know what is the maximum limit on the number of child process when it is created by a process by using fork() system call? I am using UBUNTU OS (12.04) with kernel 3.2.0-45-generic.

Prak
  • 899
  • 2
  • 11
  • 16

5 Answers5

3

Programmatically,

#include <stdio.h>
#include <sys/resource.h>

int main()
{
    struct rlimit rl;
    getrlimit(RLIMIT_NPROC, &rl);
    printf("%d\n", rl.rlim_cur);
}

where struct rlimit is:

struct rlimit {
    rlim_t rlim_cur;  /* Soft limit */
    rlim_t rlim_max;  /* Hard limit (ceiling for rlim_cur) */
};

From man:

RLIMIT_NPROC

The maximum number of processes (or, more precisely on Linux, threads) that can be created for the real user ID of the calling process. Upon encountering this limit, fork(2) fails with the error EAGAIN.

Community
  • 1
  • 1
mohit
  • 4,428
  • 3
  • 19
  • 36
2

if you need the max process number of user limit , this code also work:

#include "stdio.h"
#include "unistd.h"
void main()
{
        printf("MAX CHILD ID IS :%ld\n",sysconf(_SC_CHILD_MAX));
}
Lidong Guo
  • 2,627
  • 2
  • 14
  • 27
  • 2
    Please don't use `void main()` unless you're on Windows, where you probably can't use `sysconf()`. See [What should `main()` return in C and C++?](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c/18721336#18721336). Also, the standards use `#include ` and `#include ` and you should too. – Jonathan Leffler Jul 22 '14 at 02:21
0

The number of process is not a by process limit, but a by user limit.

hivert
  • 10,116
  • 3
  • 27
  • 51
0

On Linux you can use:

ulimit -u

to tell you the max processes a user can run and using -a argument will tell you all the user limits.

Shafik Yaghmour
  • 143,425
  • 33
  • 399
  • 682
0

Answers are already there to get current maximum value.I would like to add that you can set this limit by making change in /etc/security/limits.conf

sudo vi /etc/security/limits.conf

Then add this line to the bottom of that file

hard nproc 1000

You can raise this to whatever number you want -

nproc is maximum number of processes that can exist simultaneously on the machine.

Dayal rai
  • 6,115
  • 19
  • 28