Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System call without context switching?

I was just reading up on how linux works in my OS-book when I came across this..

[...] the kernel is created as a single, monolitic binary. The main reason is to improve performance. Because all kernel code and data structures are kept in a single address space, no context switches are necessary when a process calls an operating-system function or when a hardware interrup is delivered.

That sounded quite amazing to me, surely it must store the process's context before running off into kernel mode to handle an interrupt.. But ok, I'll buy it for now. A few pages on, while describing a process's scheduling context, it said:

Both system calls and interrups that occur while the process is executing will use this stack.

"this stack" being the place where the kernel stores the process's registers and such.

Isn't this a direct contradiction to the first quote? Am I missinterpreting it somehow?

like image 237
user1130005 Avatar asked Jan 16 '12 10:01

user1130005


People also ask

Does system call need context switch?

Processor mode and context switchingA system call does not generally require a context switch to another process; instead, it is processed in the context of whichever process invoked it. In a multithreaded process, system calls can be made from multiple threads.

Why is context switching necessary?

The need for Context switching. A context switching helps to share a single CPU across all processes to complete its execution and store the system's tasks status. When the process reloads in the system, the execution of the process starts at the same point where there is conflicting.

Can a context switch occur without a dual mode switch?

context switch happens only in kernel mode. If context switching happens between two user mode processes, first cpu has to change to kernel mode, perform context switch, return back to user mode and so on. So there has to be a mode switch associated with a context switch. Save this answer.

Is context switching done by dispatcher?

Context switching is a process. It is a consequence of context switching. It is performed by the dispatcher, when initiated by an interrupt.


2 Answers

I think the first quote is referring to the differences between a monolithic kernel and a microkernel.

Linux being monolithic, all its kernel components (device drivers, scheduler, VM manager) run at ring 0. Therefore, no context switch is necessary when performing system calls and handling interrupts.

Contrast microkernels, where components like device drivers and IPC providers run in user space, outside of ring 0. Therefore, this architecture requires additional context switches when performing system calls (because the performing module might reside in user space) and handling interrupts (to relay the interrupts to the device drivers).

like image 106
Frédéric Hamidi Avatar answered Sep 26 '22 03:09

Frédéric Hamidi


"Context switch" could mean one of a couple of things, both relevant: (1) switching from user to kernel mode to process the system call, or an involuntary switch to kernel mode to process an interrupt against the interrupt stack, or (2) switching to run another user process in user space, with a jump to kernel space in between the two.

Any movement from user space to kernel space implies saving enough user-space to return to it reliably. If the kernel-space code decides that - while you're no longer running the user-code for that process - it's time to let another user-process run, it gets in.

So at the least, you're talking 2-3 stacks or places to store a "context": hardware-interrupts need a kernel-level stack to say what to return to; user method/subroutine calls use a standard stack for getting that done. Etc.

The original Unix kernels - and the model isn't that different now for this part - ran the system calls like a short-order cook processing breakfast orders: move this over on the stove to make room for the order of bacon that just arrived, start the bacon, go back to the first order. All in kernel switching context. Was not a huge monitoring application, which probably drove the IBM and DEC software folks mad.

like image 29
Jeff A. Bowles Avatar answered Sep 24 '22 03:09

Jeff A. Bowles