Pthread scheduling (System and Process)

multithreadingoperating systemsschedulingscope

I've read the operating system concepts 8th edition written by abraham silberschatz. However, I don't understand Pthread Scheduling.


  • Thread-library schedules user-level-threads on LWP(Lightweight Process).

    This is Process-Contention Scope(PCS) because contention occurs between threads belonging to the same process.


  • OS schedules kernel-threads on CPU.

    This is System-Contention Scope(SCS) because contention occurs between all threads belonging to the system.


enter image description here


I thought that PCS is corresponding to user-level-thread and SCS is corresponding to kernel-level-thread.

But the following code is setting thread mode as user mode.

pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

I don't understand why it is user mode, not kernel mode.
Please Someone aware of this let me know, thank you for reading.

Best Answer

I think you are confusing inter-process (user) space, intraprocess threads, and system space. So let's start with some definitions.

Threads belong to a process. A process can run within user space and system space. Which space a process belongs to is a function of how it is invoked.

System space tends to be the lowest level and is where protected or kernel processes run. Typically, they are owned by the system itself.

User space tends to be higher level and has less privileged processes.

Inter-process (user) space threads can be thought of as threads belonging to separate processes.

Intraprocess threads can be thought of as threads belonging to a single process. And that process may be running in user or system space.


So when you call:

pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

What you're really doing is moving the thread from being part of the rest of that calling process and moving it out into user space. Effectively it makes the thread a new process and is equivalent to fork, but I'm not positive on that aspect so don't quote me on it.

I think your confusion comes from the _SYSTEM portion of that definition. However, an operating system wouldn't necessarily want a process to be able to move from user space into system space as that could lead to an undesirable privilege escalation for that process. The kernel rightfully doesn't trust user space processes as they have been historically proven to be rubbish (from the kernel's perspective).

Said another way, that pthread_attr_setscope call doesn't allow you to move from PCS into SCS.


The man page is semi-helpful in understanding this.

PTHREAD_SCOPE_SYSTEM
The thread competes for resources with all other threads in all processes on the system that are in the same scheduling allocation domain (a group of one or more processors). PTHREAD_SCOPE_SYSTEM threads are scheduled relative to one another according to their scheduling policy and priority.

emphasis added

Note the final clause in the part I emphasized: "that are in the same scheduling allocation domain". And also note that it makes no mention of being able to switch to a different scheduling allocation domain.