Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do OSes Handle context switching?

As I can understand, every OS need to have some mechanism to periodically check if it should run some tasks and suspend others.

One way would be some kind of timer on whose expiry the OS will check if it should run/suspend some task.

Generally, say on a ARM system that would probably be some kind of ISR.

My real question, is that I've been ABLE to only visualize this and not see it somewhere. Could some one point to some free/open RTOS code where I can actually see the code that handles the preemption/scheduling?

like image 506
Holysmoke Avatar asked Jul 14 '12 11:07

Holysmoke


People also ask

How context switching is used in multiprogramming?

Context switching is used to achieve multitasking i.e. multiprogramming with time-sharing(learn more about multitasking from here). Multitasking gives an illusion to the users that more than one process are being executed at the same time.

How does PCB help in context switch?

Role of PCB in Context Switch Sometimes, several factors such as interrupt signals or operating system calls interrupt a running process, and the process preempts its execution. When this happens, the operating system saves the current execution statistics in the PCB of the process.

How multitasking can be used in a context switch give an example?

Example of Context Switching Suppose that multiple processes are stored in a Process Control Block (PCB). One process is running state to execute its task with the use of CPUs. As the process is running, another process arrives in the ready queue, which has a high priority of completing its task using CPU.


1 Answers

freertos.org. The entire OS is open source, and right there for you to see. And there are dozens of different ports to compare and contrast. For the context switch code, you will want to look in the ports directory, in any one of many files called port.c, port.asm, etc. And yes, in the case of freertos all context switches are performed in interrupts (a tick timer ISR, or any other SysCall interrupt).

A context switch is very-much processor specific, as the list of registers to save and the assembly code to save them varies between processor families, and sometimes within a given family. As a result each port has a separate file for this code.

The scheduling (selection of next task to run), on the other hand, is done in a file called tasks.c, which is common to all ports and references the port-specific code.

like image 147
Nathan Wiebe Avatar answered Oct 03 '22 16:10

Nathan Wiebe