How does the OS run a ? How does it a ? How does it switch from one process to the other?

Operating Systems

Minati De

Department of Mathematics, Indian Institute of Technology Delhi, India.

Lecture 4: Mechanism of Process Execution

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Table of Contents

1 How does the OS run a process?

2 How does it handle a system call?

3 How does it context switch from one process to the other?

M. De Operating Systems Points CPU (PC) to current instruction – Other registers may store operands, return values etc. After setup, OS is out of the way and process executes directly on CPU

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Process Execution

OS allocates memory and creates memory image Code and data (from Code exe) Stack and heap Data Stack

Heap

M. De Operating Systems After setup, OS is out of the way and process executes directly on CPU

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Process Execution

OS allocates memory and creates memory image CPU Memory Code and data (from exe) PC Code Stack and heap Data Points CPU program counter (PC) to current SP Stack instruction – Other registers may store operands, return values etc. Heap

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Process Execution

OS allocates memory and creates memory image CPU Memory Code and data (from exe) PC Code Stack and heap Data Points CPU program counter (PC) to current SP Stack instruction – Other registers may store operands, return values etc. After setup, OS is out of Heap the way and process executes directly on CPU

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Process Execution

M. De Operating Systems A new stack frame pushed to stack and stack pointer (SP) updated Old value of PC (return value) pushed to stack and PC updated Stack frame contains return value, function arguments etc.

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? A simple function call

CPU A function call translates Memory to a jump instruction PC fun();Code fun(){...} Data SP Stack

M. De Operating Systems Old value of PC (return value) pushed to stack and PC updated Stack frame contains return value, function arguments etc.

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? A simple function call

A function call translates CPU Memory to a jump instruction PC fun();Code A new stack frame pushed fun(){...} to stack and stack pointer (SP) updated Data SP Stack

M. De Operating Systems Stack frame contains return value, function arguments etc.

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? A simple function call

A function call translates CPU Memory to a jump instruction PC fun();Code A new stack frame pushed to stack and stack pointer fun(){...} (SP) updated Data Old value of PC (return SP Stack value) pushed to stack Old PC value, reg and PC updated

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? A simple function call

A function call translates CPU Memory to a jump instruction PC fun();Code A new stack frame pushed to stack and stack pointer fun(){...} (SP) updated Data Old value of PC (return SP Stack value) pushed to stack Old PC value, reg and PC updated Stack frame contains return value, function arguments etc.

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Table of Contents

1 How does the OS run a process?

2 How does it handle a system call?

3 How does it context switch from one process to the other?

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? How is a system call different?

CPU hardware has multiple privilege levels One to run user code: user mode One to run OS code like system calls: kernel mode Some instructions execute only in kernel mode Kernel does not trust user stack— Uses a separate kernel stack when in kernel mode

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? How is a system call different?

Kernel does not trust user provided addresses to jump to Kernel sets up Descriptor Table (IDT) at boot time IDT has addresses of kernel functions to run for system calls and other events

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Mechanism of system call: instruction

When system call must be made, a special trap instruction is run (usually hidden from user by libc) Trap instruction execution Move CPU to higher privilege level Switch to kernel stack Save context (old PC, registers) on kernel stack Look up address in IDT and jump to trap handler function in OS code

M. De Operating Systems System call (program needs OS service) Program fault (program does something illegal, e.g., access memory it doesn’t have access to) Interrupt (external device needs attention of OS, e.g., a network packet has arrived on network card) Across all cases, the mechanism is: save context on kernel stack and switch to OS address in IDT IDT has many entries: which to use? ? System calls/ store a number in a CPU register before calling trap, to identify which IDT entry to use

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? More on the trap instruction

Trap instruction is executed on hardware in following cases:

M. De Operating Systems Program fault (program does something illegal, e.g., access memory it doesn’t have access to) Interrupt (external device needs attention of OS, e.g., a network packet has arrived on network card) Across all cases, the mechanism is: save context on kernel stack and switch to OS address in IDT IDT has many entries: which to use? ? System calls/interrupts store a number in a CPU register before calling trap, to identify which IDT entry to use

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? More on the trap instruction

Trap instruction is executed on hardware in following cases: System call (program needs OS service)

M. De Operating Systems Interrupt (external device needs attention of OS, e.g., a network packet has arrived on network card) Across all cases, the mechanism is: save context on kernel stack and switch to OS address in IDT IDT has many entries: which to use? ? System calls/interrupts store a number in a CPU register before calling trap, to identify which IDT entry to use

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? More on the trap instruction

Trap instruction is executed on hardware in following cases: System call (program needs OS service) Program fault (program does something illegal, e.g., access memory it doesn’t have access to)

M. De Operating Systems Across all cases, the mechanism is: save context on kernel stack and switch to OS address in IDT IDT has many entries: which to use? ? System calls/interrupts store a number in a CPU register before calling trap, to identify which IDT entry to use

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? More on the trap instruction

Trap instruction is executed on hardware in following cases: System call (program needs OS service) Program fault (program does something illegal, e.g., access memory it doesn’t have access to) Interrupt (external device needs attention of OS, e.g., a network packet has arrived on network card)

M. De Operating Systems IDT has many entries: which to use? ? System calls/interrupts store a number in a CPU register before calling trap, to identify which IDT entry to use

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? More on the trap instruction

Trap instruction is executed on hardware in following cases: System call (program needs OS service) Program fault (program does something illegal, e.g., access memory it doesn’t have access to) Interrupt (external device needs attention of OS, e.g., a network packet has arrived on network card) Across all cases, the mechanism is: save context on kernel stack and switch to OS address in IDT

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? More on the trap instruction

Trap instruction is executed on hardware in following cases: System call (program needs OS service) Program fault (program does something illegal, e.g., access memory it doesn’t have access to) Interrupt (external device needs attention of OS, e.g., a network packet has arrived on network card) Across all cases, the mechanism is: save context on kernel stack and switch to OS address in IDT IDT has many entries: which to use? ? System calls/interrupts store a number in a CPU register before calling trap, to identify which IDT entry to use

M. De Operating Systems Restore context of CPU registers from kernel stack Change CPU privilege from kernel mode to user mode Restore PC and jump to user code after trap User process unaware that it was suspended, resumes execution as always

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Return from trap

When OS is done handling system call or interrupt, it calls a special instruction return-from-trap

M. De Operating Systems Change CPU privilege from kernel mode to user mode Restore PC and jump to user code after trap User process unaware that it was suspended, resumes execution as always

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Return from trap

When OS is done handling system call or interrupt, it calls a special instruction return-from-trap Restore context of CPU registers from kernel stack

M. De Operating Systems Restore PC and jump to user code after trap User process unaware that it was suspended, resumes execution as always

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Return from trap

When OS is done handling system call or interrupt, it calls a special instruction return-from-trap Restore context of CPU registers from kernel stack Change CPU privilege from kernel mode to user mode

M. De Operating Systems User process unaware that it was suspended, resumes execution as always

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Return from trap

When OS is done handling system call or interrupt, it calls a special instruction return-from-trap Restore context of CPU registers from kernel stack Change CPU privilege from kernel mode to user mode Restore PC and jump to user code after trap

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Return from trap

When OS is done handling system call or interrupt, it calls a special instruction return-from-trap Restore context of CPU registers from kernel stack Change CPU privilege from kernel mode to user mode Restore PC and jump to user code after trap User process unaware that it was suspended, resumes execution as always

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Handling System Call

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Table of Contents

1 How does the OS run a process?

2 How does it handle a system call?

3 How does it context switch from one process to the other?

M. De Operating Systems No Before returning to user mode, OS checks if it must switch to another process

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other?

always return to the same user process from kernel mode?

M. De Operating Systems Before returning to user mode, OS checks if it must switch to another process

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other?

always return to the same user process from kernel mode? No

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other?

always return to the same user process from kernel mode? No Before returning to user mode, OS checks if it must switch to another process

M. De Operating Systems The process has run for too long Must timeshare CPU with other processes

Process has exited or must be terminated (e.g., segfault) Process has made a blocking system call Sometimes, the OS does not want to return back to the same process

In such cases, OS performs a context switch to switch from one process to another

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Why switch between processes?

Sometimes when OS is in kernel mode, it cannot return back to the same process it left

M. De Operating Systems The process has run for too long Must timeshare CPU with other processes

Process has made a blocking system call Sometimes, the OS does not want to return back to the same process

In such cases, OS performs a context switch to switch from one process to another

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Why switch between processes?

Sometimes when OS is in kernel mode, it cannot return back to the same process it left Process has exited or must be terminated (e.g., segfault)

M. De Operating Systems The process has run for too long Must timeshare CPU with other processes

Sometimes, the OS does not want to return back to the same process

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Why switch between processes?

Sometimes when OS is in kernel mode, it cannot return back to the same process it left Process has exited or must be terminated (e.g., segfault) Process has made a blocking system call

In such cases, OS performs a context switch to switch from one process to another

M. De Operating Systems The process has run for too long Must timeshare CPU with other processes

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Why switch between processes?

Sometimes when OS is in kernel mode, it cannot return back to the same process it left Process has exited or must be terminated (e.g., segfault) Process has made a blocking system call Sometimes, the OS does not want to return back to the same process

In such cases, OS performs a context switch to switch from one process to another

M. De Operating Systems Must timeshare CPU with other processes

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Why switch between processes?

Sometimes when OS is in kernel mode, it cannot return back to the same process it left Process has exited or must be terminated (e.g., segfault) Process has made a blocking system call Sometimes, the OS does not want to return back to the same process The process has run for too long

In such cases, OS performs a context switch to switch from one process to another

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Why switch between processes?

Sometimes when OS is in kernel mode, it cannot return back to the same process it left Process has exited or must be terminated (e.g., segfault) Process has made a blocking system call Sometimes, the OS does not want to return back to the same process The process has run for too long Must timeshare CPU with other processes In such cases, OS performs a context switch to switch from one process to another

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Switching Between Processes

The OS scheduler OS scheduler has two parts Policy to pick which process to run (next lecture) Mechanism to switch to that process (this lecture)

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Switching Between Processes

How can the OS regain control of the CPU so that it can switch between processes? A cooperative Approach: Wait for system calls A Non-Cooperative Approach: The OS takes control

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? A cooperative Approach:

Processes periodically give up the CPU by making system calls. The OS decides to run some other task. Application also transfer control to the OS when they do something illegal. Divide by zero Try to access memory that it shouldn’t be able to access

Example Early versions of the Macintosh OS, The old Xerox Alto system

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? A cooperative Approach:

Disadvantage A process gets stuck in an infinite loop?

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? A cooperative Approach:

Disadvantage A process gets stuck in an infinite loop?—Reboot the machine

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? A Non-Cooperative Approach: OS Takes Control

A timer interrupt During the boot sequence, the OS start the timer. The timer raise an interrupt every so many milliseconds. When the interrupt is raised : The currently running process is halted. Save enough of the state of the program A pre-configured in the OS runs.

A timer interrupt gives OS the ability to run again on a CPU.

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Saving and Restoring Context

Scheduler makes a decision: Whether to continue running the current process, or switch to a different one. If the decision is made to switch, the OS executes context switch.

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Context Switch

A low-level piece of assembly code Save a few register values for the current process onto its kernel stack General purpose registers PC kernel stack pointer Restore a few for the soon-to-be-executing process from its kernel stack Switch to the kernel stack for the soon-to-be-executing process

M. De Operating Systems OS did, when it switched out B in the past

Save context (PC, registers, kernel stack pointer) of A on kernel stack Switch SP to kernel stack of B Restore context from B’s kernel stack Who has saved registers on B’s kernel stack?

Now, CPU is running B in kernel mode, return-from-trap to switch to user mode of B

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Context Switch: Example

Process A has moved from user to kernel mode, OS decides it must switch from A to B

M. De Operating Systems OS did, when it switched out B in the past

Switch SP to kernel stack of B Restore context from B’s kernel stack Who has saved registers on B’s kernel stack?

Now, CPU is running B in kernel mode, return-from-trap to switch to user mode of B

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Context Switch: Example

Process A has moved from user to kernel mode, OS decides it must switch from A to B Save context (PC, registers, kernel stack pointer) of A on kernel stack

M. De Operating Systems OS did, when it switched out B in the past

Restore context from B’s kernel stack Who has saved registers on B’s kernel stack?

Now, CPU is running B in kernel mode, return-from-trap to switch to user mode of B

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Context Switch: Example

Process A has moved from user to kernel mode, OS decides it must switch from A to B Save context (PC, registers, kernel stack pointer) of A on kernel stack Switch SP to kernel stack of B

M. De Operating Systems OS did, when it switched out B in the past

Who has saved registers on B’s kernel stack?

Now, CPU is running B in kernel mode, return-from-trap to switch to user mode of B

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Context Switch: Example

Process A has moved from user to kernel mode, OS decides it must switch from A to B Save context (PC, registers, kernel stack pointer) of A on kernel stack Switch SP to kernel stack of B Restore context from B’s kernel stack

M. De Operating Systems OS did, when it switched out B in the past Now, CPU is running B in kernel mode, return-from-trap to switch to user mode of B

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Context Switch: Example

Process A has moved from user to kernel mode, OS decides it must switch from A to B Save context (PC, registers, kernel stack pointer) of A on kernel stack Switch SP to kernel stack of B Restore context from B’s kernel stack Who has saved registers on B’s kernel stack?

M. De Operating Systems Now, CPU is running B in kernel mode, return-from-trap to switch to user mode of B

How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Context Switch: Example

Process A has moved from user to kernel mode, OS decides it must switch from A to B Save context (PC, registers, kernel stack pointer) of A on kernel stack Switch SP to kernel stack of B Restore context from B’s kernel stack Who has saved registers on B’s kernel stack? OS did, when it switched out B in the past

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Context Switch: Example

Process A has moved from user to kernel mode, OS decides it must switch from A to B Save context (PC, registers, kernel stack pointer) of A on kernel stack Switch SP to kernel stack of B Restore context from B’s kernel stack Who has saved registers on B’s kernel stack? OS did, when it switched out B in the past Now, CPU is running B in kernel mode, return-from-trap to switch to user mode of B

M. De Operating Systems How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? Context Switch: Example

M. De Operating Systems