Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux - threads and process scheduling priorities

if we create pthreads (pthread_create) or processes (fork) with default scheduling policies on linux, will the scheduler treats the processes and threads with same priority while scheduling them?

let us say there is process P1 with one thread and process P2 with 2 threads T1 T2

lets say there is only one core..will the scheduling be P1 T1 P1 T2 P1 T1 P1 T2

or

P1 T1 T2 P1 T1 T2

like image 723
Medicine Avatar asked Aug 17 '12 02:08

Medicine


1 Answers

Linux no longer schedules processes at all.

Within the kernel, threads are scheduled. The concept of a process is now an artificial construct seen mostly by things outside the kernel. Obviously, the kernel has to know how threads are tied together, but not for scheduling purposes.

Basically, the kernel maintains a whole lot of threads and each thread has a thread group leader, which is what's seen on the outside as the process. A thread has a thread ID and a thread group ID - it's a lot like the relationship between a PID and a PPID (process ID and parent process ID).

When you create a regular thread, the kernel gives it a brand new thread ID but its thread group ID is set identical to the group ID of the thread that created it. That way, it looks like a thread within a process to the outside world.

When you fork, the kernel gives it a brand new thread ID and sets its thread group ID to the same value as its thread ID. That way, it looks like a process to the outside world.

Most non-kernel utilities that report on processes are really just reporting on threads where the thread ID is the same as the thread group ID.

There are subtleties with other methods which are probably too complicated to go into here. What I've written above is (hopefully) a good medium level treatise.

Now, for your specific question, it would be neither case since P1 only has one thread (there is no P1T2).

Withing the kernel, the threads are P1T1, P2T1 and P2T2 and, assuming they have the same scheduling properties and behave the same (a), that's how they'll be scheduled.


See also:

  • Linux - Threads and Process;
  • If I have a process, and I clone it, is the PID the same?; and
  • Will a CPU process have at least one thread?

for more information.


(a): Obviously that changes if the threads start blocking on I/O (kernel won't schedule them until I/O is available) or releasing their time quanta early (kernel will probably up their priority as a reward for playing nicely) but then they're not behaving the same.

like image 121
paxdiablo Avatar answered Oct 01 '22 12:10

paxdiablo