Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 3214 Computer Systems Lecture 9 Godmar Back.

Similar presentations


Presentation on theme: "CS 3214 Computer Systems Lecture 9 Godmar Back."— Presentation transcript:

1 CS 3214 Computer Systems Lecture 9 Godmar Back

2 Announcements Exercise 4 will be posted today Project 3 coming
CS 3214 Fall 2011

3 Part 1 Threads and Processes CS 3214 Fall 2011

4 Processes Def: An instance of a program in execution
OS provides each process with key abstractions Logical control flow 1 flow – single-threaded process Multiple flows – multi-threaded process Private address space Abstracted resources: e.g., stdout/stdin file descriptors These abstractions create the illusion that each process has access to its own CPU (or CPUs for multi-threaded processes) Memory Devices: e.g., terminal CS 3214 Fall 2011

5 Context Switching Historical motivation for processes was introduction of multi-programming: Load multiple processes into memory, and switch to another process if current process is (momentarily) blocked This required protection and isolation between these processes, implemented by a privileged kernel: dual-mode operation. Time-sharing: switch to another process periodically to make sure all processes make equal progress Switch between processes is called a context switch CS 3214 Fall 2011

6 Dual-Mode Operation Two fundamental modes:
“kernel mode” – privileged aka system, supervisor or monitor mode Intel calls its PL0, Privilege Level 0 on x86 “user mode” – non-privileged PL3 on x86 Bit in CPU – controls operation of CPU Privileged operations can only be performed in kernel mode. Example: hlt Must carefully control transition between user & kernel mode int main() { asm(“hlt”); } CS 3214 Fall 2011

7 Mode Switching User  Kernel mode External (aka hardware) interrupt:
For reasons external or internal to CPU External (aka hardware) interrupt: timer/clock chip, I/O device, network card, keyboard, mouse asynchronous (with respect to the executing program) Internal interrupt (aka software interrupt, trap, or exception) are synchronous can be intended (“trap”): for system call (process wants to enter kernel to obtain services) or unintended (usually): (“fault/exception”) (division by zero, attempt to execute privileged instruction in user mode, memory access violation, invalid instruction, alignment error, etc.) Kernel  User mode switch on iret instruction CS 3214 Fall 2011

8 A Context Switch Scenario
Timer interrupt: P1 is preempted, context switch to P2 I/O device interrupt: P2’s I/O complete switch back to P2 Process 1 Process 2 user mode kernel mode System call: (trap): P2 starts I/O operation, blocks context switch to process 1 Timer interrupt: P2 still has time left, no context switch Kernel CS 3214 Fall 2011

9 Context Switching, Details
intr_entry: (saves entire CPU state) (switches to kernel stack) intr_exit: (restore entire CPU state) (switch back to user stack) iret Process 1 Process 2 user mode kernel mode Kernel switch_threads: (in) (saves caller’s state) switch_threads: (out) (restores caller’s state) (kernel stack switch) CS 3214 Fall 2011

10 System Calls User processes access kernel services by trapping into the kernel, executing kernel code to perform the service, then returning – very much like a library call. Unless the system call cannot complete immediately, this does not involve a context switch. Process 1 user mode kernel mode Kernel Kernel’s System Call Implementation CS 3214 Fall 2011

11 Syscall example: write(2)
32-bit Linux /* gcc -static -O -g -Wall write.c -o write */ #include <unistd.h> int main() { const char msg[] = "Hello, World\n"; return write(1, msg, sizeof msg); } /usr/include/asm/unistd.h: …. #define __NR_write a <__write_nocancel>: 805005a: push %ebx 805005b: 8b mov 0x10(%esp),%edx #arg2 805005f: 8b 4c 24 0c mov 0xc(%esp),%ecx # arg1 : 8b 5c mov 0x8(%esp),%ebx # arg0 : b mov $0x4,%eax # syscall no 805006c: cd int $0x80 805006e: 5b pop %ebx 805006f: 3d 01 f0 ff ff cmp $0xfffff001,%eax : 0f e jae ed0 <__syscall_error> 805007a: c ret CS 3214 Fall 2011

12 Kernel Threads Most OS support kernel threads that never run in user mode – these threads typically perform book keeping or other supporting tasks. They do not service system calls or faults. Process 1 Process 2 user mode kernel mode Kernel Kernel Thread Careful: “kernel thread” not the same as kernel-level thread (KLT) – more on KLT later CS 3214 Fall 2011

13 Context vs Mode Switching
Mode switch guarantees kernel gains control when needed To react to external events To handle error situations Entry into kernel is controlled Not all mode switches lead to context switches Kernel decides when – subject of scheduling policies Mode switch does not change the identity of current process/thread See blue/yellow colors in slide on ctxt switch details Hardware knows about modes, does not (typically) know about contexts CS 3214 Fall 2011

14 Bottom Up View: Exceptions
An exception is a transfer of control to the OS in response to some event (i.e., change in processor state) User Process OS event current exception next exception processing by exception handler exception return (optional) CS 3214 Fall 2011


Download ppt "CS 3214 Computer Systems Lecture 9 Godmar Back."

Similar presentations


Ads by Google