9

I am trying to understand the Linux processes. I have confusion on the terms pid_max, ulimit -u and thread_max. What exactly is the difference between these terms? Can someone clarify me the differences?

16

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.

  • I guess you have mentioned wrongly that ulimit -u denotes maximum threads a user can have, but actually it means the maximum processes a user can have. – Kannan Mohan Nov 5 '14 at 7:40
  • @KannanMohan, I think you are right. When I checked the man page, it showed up as process only. I will try and change the answer in sometime. Thanks for letting me know. I appreciate it. – Ramesh Nov 6 '14 at 0:39
  • I've edited the answer to fix the references of ulimit -u being associated with threads to being associated with processes. – Aaron Jun 29 '16 at 15:32
  • There is a small connection between "max user processes" and threads. It limits the amount of "native" threads that user is allowed to have. For example use 3 java processes to open 500 threads each assuming that "max user processes" is 1000. Then try to run java -version and see the JVM crush miserably – Roman M Sep 25 '16 at 15:32
11

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.