Lecture 4: Mechanism of Process Execution

Lecture 4: Mechanism of Process Execution

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? 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 program counter (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()f:::g 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()f:::g 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()f:::g (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()f:::g (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 Interrupt 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: trap 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/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: 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.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    52 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us