multithreadingoperating-systemschedulerpreemption

When does the operating system gain control over the CPU when a user process is executing


I am currently studying about operating systems and have something that I do not completely understand. In a time shared system with preemption, processes are always swapped. However, if a user process is executing on one of the CPUs there can be no other process that is also executing on the same CPU. If all CPUs are taken by user processes, the operating system processes cannot run at the same time, so what is it that causes causes timeout preemption (CPU timer?)?

Moreover, what confuses me is the notion of kernel and user threads. I have read that kernel threads are the ones that get scheduled to execute on the CPU. Moreover, user threads are mapped to kernel threads via some ordering (one-to-many, many-to-one, many-to-many). I believe this has something to do with the question above but I cannot figure it out from the documentation that I have.

Thank you. Cheers!


Solution

  • The "operating system" runs in the context of a process (although some retrograde OS/s use different terminology). The "operating system" takes control when there is either an exception or an interrupt.

    If all CPUs are taken by user processes, the operating system processes cannot run at the same time, so what is it that causes causes timeout preemption (CPU timer?)?

    The operating system sets the CPU timer that triggers an interrupt. The timer interrupt handler resets the timer before it exits.

    Moreover, what confuses me is the notion of kernel and user threads. I have read that kernel threads are the ones that get scheduled to execute on the CPU.

    The kernel thread/user thread distinction is total manure concocted by worthless books on operating system.

    A kernel thread is a thread.

    "user threads" a poor man's way of simulating threads on systems that do not support them. They are library functions and not even part of the operating system. Thus, they do not even belong in a course on operating systems other than for historical information.