Understanding the difference between pid_max, ulimit -u and thread_max
Sorry, the accepted answer is bad information on several fronts.
/proc/sys/kernel/pid_max has nothing to do with the maximum number of processes that can be run at any given time. It is, in fact, the maximum numerical PROCESS IDENTIFIER than can be assigned by the kernel.
In the Linux kernel, a process and a thread are one an the same. They're handled the same way by the kernel. They both occupy a slot in the task_struct data structure. A thread, by common terminology, is in Linux a process that shares resources with another process (they will also share a thread group ID). A thread in the Linux kernel is largely a conceptual construct as far as the scheduler is concerned.
Now that you understand that the kernel largely does not differentiate between a thread and a process, it should make more sense that /proc/sys/kernel/threads-max is actually the maximum number of elements contained in the data structure task_struct. Which is the data structure that contains the list of processes, or as they can be called, tasks.
ulimit is, as the name implies, a per-user limit. The -u flag is defined as "The maximum number of processes available to a single user". An element of task_struct contains the uid of the user that created the task. A per-uid count is maintained and incremented/decremented every time a task is added/removed from task_struct. So, ulimit -u indicates the maximum number of elements (processes) that one particular user is allowed to have within task_struct at any given time.
I hope that clears things up.
Let us understand the difference between a process and a thread. As per this link,
The typical difference is that threads (of the same process) run in a shared memory space, while processes run in separate memory spaces.
Now, we have the pid_max
parameter which can be determined as below.
cat /proc/sys/kernel/pid_max
So the above command returns 32,768 which means I can execute 32,768 processes simultaneously in my system that can run in separate memory spaces.
Now, we have the threads-max
parameter which can be determined as below.
cat /proc/sys/kernel/threads-max
The above command returns me the output as 126406 which means I can have 126406 threads in a shared memory space.
Now, let us take the 3rd parameter ulimit -u
which says the total processes a user can have at a particular time. The above command returns me the output as 63203. This means for all the processes that a user has created at a point of time the user can have 63203 processes running.
Hypothetical case
So assuming there are 2 processes simultaneously being run by 2 users and each process is consuming memory heavily, both the processes will effectively use the 63203 user limit on the processes. So, if that is the case, the 2 users will have effectively used up the entire 126406 threads-max
size.
Now, I need to determine how many processes an user can run at any point of time. This can be determined from the file, /etc/security/limits.conf
. So, there are basically 2 settings in this file as explained over here.
A soft limit is like a warning and hard limit is a real max limit. For example, following will prevent anyone in the student group from having more than 50 processes, and a warning will be given at 30 processes.
@student hard nproc 50
@student soft nproc 30
Hard limits are maintained by the kernel while the soft limits are enforced by the shell.