Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Userspace process preempts kernel thread?

Tags:

c

linux

Currently I am reading "Understanding the Linux kernel, 3rd edition" and on p.22 I can read:

In the simplest case, the CPU executes a kernel control path sequentially from the first instruction to the last. When one of the following events occurs, however, the CPU interleaves the kernel control paths:

  • A process executing in User Mode invokes a system call, and the corresponding kernel control path verifies that the request cannot be satisfied immediately; it then invokes the scheduler to select a new process to run. As a result, a process switch occurs. The first kernel control path is left unfinished, and the CPU resumes the execution of some other kernel control path. In this case, the two control paths are executed on behalf of two different processes.

The kernel control path can be interrupted from a user space process doing a system call? I thought the priority was pretty much:

  • interrupts
  • kernel threads
  • user space processes

I have checked the errata and could not find anything about this.

like image 694
ola1olsson Avatar asked Dec 10 '11 00:12

ola1olsson


People also ask

What is a userspace process?

Each user space process normally runs in its own virtual memory space, and, unless explicitly allowed, cannot access the memory of other processes. This is the basis for memory protection in today's mainstream operating systems, and a building block for privilege separation.

What is difference between userspace and kernel space?

Kernel space is that area of virtual memory where kernel processes will run and user space is that area of virtual memory where user processes will be running.

Is the only way to transit from userspace to kernel space?

System Call Interfaces (SCI) are the only way to transit from User space to kernel space. Kernel space switching is achieved by Software Interrupt, which changes the processor mode and jump the CPU execution into interrupt handler, which executes corresponding System Call routine.

Can a kernel thread be preempted?

Any kernel thread or generally speaking any portion of code can be preempted : By a thread of higher priority. While not in a section protected against interrupts and preemption.


1 Answers

You are right about the priority list, but what (I think) the book is trying to say is:

  • When a (user) process makes a system call, the kernel starts executing on its behalf.
  • If the system call can be completed (the kernel control path does not run into a roadblock), then it will usually return direct to the calling process - think getpid() function call.
  • On the other hand, if the system call cannot be completed (for example, because the disk system must read a block into the kernel buffer pool before its data can be returned to the calling process), then the scheduler is used to select a new process to run - preempting the (kernel thread of control that was running on behalf of the) user process.
  • In due course, the original system call will be able to continue, and the original (kernel thread of control that was running on behalf of the) user process will be able to continue and eventually complete, returning control to the user space process running in user space and not in the kernel.

So "No": it is not the case that the 'kernel path can be interrupted from a user space process doing a system call'.

The kernel path can be interrupted while it is executing a system call on behalf of a user space process because: an interrupt occurs, or the kernel path must wait for a resource to become available, or ...

like image 76
Jonathan Leffler Avatar answered Oct 03 '22 00:10

Jonathan Leffler