6

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.

3

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.

| improve this answer | |
  • 1
    Thank you for kind answer, but I don't understand why inter-process-space-thread is user-space-thread, and why intra-process-space-thread is system-space-thread. – Danny_Kim Oct 30 '15 at 6:00
  • pthread_attr_setscope(PTHREAD_SCOPE_SYSTEM); is allowed in linux. Is it not allowed? But my textbook wrote this code, and linux could be only pthread_scope_system – Danny_Kim Oct 30 '15 at 6:06
  • I'm using the same textbook and still confused 4 years later. – mLstudent33 Dec 30 '19 at 21:23
0

According to the man page:

Linux supports PTHREAD_SCOPE_SYSTEM, but not PTHREAD_SCOPE_PROCESS

And if you take a look at the glibc's implementation:

0034   /* Catch invalid values.  */
0035   switch (scope)
0036     {
0037     case PTHREAD_SCOPE_SYSTEM:
0038       iattr->flags &= ~ATTR_FLAG_SCOPEPROCESS;
0039       break;
0040 
0041     case PTHREAD_SCOPE_PROCESS:
0042       return ENOTSUP;
0043 
0044     default:
0045       return EINVAL;
0046     }

This answer was copied from https://unix.stackexchange.com/questions/287568/pthread-scheduler-scope-variables as I found it right after making my comment.

| improve this answer | |

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.