Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do system call in Linux involve thread switching

Tags:

linux

I was reading on Wikipedia section on "system call" that when a "user level" thread makes a system call, then eventually a "kernel" thread actually carries out the request. So does that mean the user thread is descheduled and context switched with a kernel thread ? If yes how is the result returned back to user thread?

I am confused by this line on wikipedia: "Many-to-one model: All system calls from any user thread in a process are handled by a single kernel-level thread."

like image 429
Saurav Prakash Avatar asked May 24 '19 11:05

Saurav Prakash


1 Answers

See system call and context switch for a detailed explanation of system call context switches.

I still don't understand that is the system call executed in same thread but in kernel mode or is there a separate kernel thread for the system calls

Same thread, same process. There are no "user threads" and "kernel threads", there are just threads. A thread switches back and forth between user and kernel mode every time a system call is made.

The context switches described in the linked question are not scheduling context switches. User/kernel mode switches and thread scheduling switches are independent, orthogonal concepts. Switching from user mode to kernel mode or back does not involve scheduling at all. Similarly, a thread scheduling switch can happen any time, whether the process is in user mode or kernel mode.

I was confused by this line on wikipedia: "Many-to-one model: All system calls from any user thread in a process are handled by a single kernel-level thread."

That's a way for OSes to handle system calls, but it's not the Linux way. Linux uses the model from the second bullet: "One-to-one model: Every user thread gets attached to a distinct kernel-level thread during a system call. This model solves the above problem of blocking system calls. It is found in all major Linux distributions, macOS, iOS, recent Windows and Solaris versions."

But don't let that wording confuse you. Linux doesn't distinguish between user threads and kernel threads. There are just threads, unqualified. The "one-to-one" mapping the article describes is conceptual. There's no actual separation or mapping in Linux's source code.

like image 163
John Kugelman Avatar answered Oct 02 '22 21:10

John Kugelman