Processes & ECF CS 351: Systems Programming Michael Saelee - - PowerPoint PPT Presentation

processes ecf
SMART_READER_LITE
LIVE PREVIEW

Processes & ECF CS 351: Systems Programming Michael Saelee - - PowerPoint PPT Presentation

Processes & ECF CS 351: Systems Programming Michael Saelee <lee@iit.edu> Computer Science Science Agenda - Definition & OS responsibilities - Exceptional control flow - synch vs. asynch exceptions - exception handling


slide-1
SLIDE 1

Processes & ECF

CS 351: Systems Programming Michael Saelee <lee@iit.edu>

slide-2
SLIDE 2

Computer Science Science

Agenda

  • Definition & OS responsibilities
  • Exceptional control flow
  • synch vs. asynch exceptions
  • exception handling procedure
slide-3
SLIDE 3

Computer Science Science

§Definition & OS responsibilities

slide-4
SLIDE 4

Computer Science Science

a process is a program in execution

slide-5
SLIDE 5

Computer Science Science

programs describe what we want done, processes carry out what we want done

slide-6
SLIDE 6

Computer Science Science

a process comprises ... { code (program) + runtime data (global, local, 
 dynamic) + PC, SP , FP & other registers }

slide-7
SLIDE 7

Computer Science Science

essential to program execution is predictable, logical control flow which requires that nothing disrupt the program mid-execution

main() { fnA();
 }
 
 fnA() {
 fnB();
 } fnB() { loop { } }

slide-8
SLIDE 8

Computer Science Science

easiest way to guarantee this is for a process to “own” the CPU for its entire duration ... downsides?

slide-9
SLIDE 9

Computer Science Science

1.No multitasking! 2.A malicious (or badly written) program can “take over” the CPU forever 3.An idle process (e.g., waiting for input) will underutilize the CPU

slide-10
SLIDE 10

Computer Science Science

the operating system presents each process with a simulated, seamless logical control flow many of which can be taking place concurrently on one or more CPUs

slide-11
SLIDE 11

Computer Science Science

Logical control flow

Time Process A Process B Process C

slide-12
SLIDE 12

Computer Science Science

Physical flow (1 CPU)

Time Process A Process B Process C

slide-13
SLIDE 13

Computer Science Science

to do this, we need (1) a hardware mechanism to periodically interrupt the current process to load the OS , (2) an OS procedure that decides which processes to run, in what order , and (3) a routine for seamlessly transitioning between processes

slide-14
SLIDE 14

Computer Science Science

(1) is the periodic clock interrupt; 
 (2) is the OS scheduler;
 (3) is the context switch

slide-15
SLIDE 15

Computer Science Science

Context switches

Process A Process B User code Kernel code User code Kernel code User code Time Context switch Context switch read Disk interrupt Return from read

Need new diagram that shows context switches triggered by the clock interrupt.

slide-16
SLIDE 16

Computer Science Science

to implement scheduling and carry out context switches, the OS must maintain a wealth of per-process metadata

slide-17
SLIDE 17

Computer Science Science

a process comprises ... { code (program) + runtime data (global, local, 
 dynamic) + PC, SP , FP & other registers + “process control block” (OS metadata) }

slide-18
SLIDE 18

Computer Science Science

a process comprises ... { code (program) + runtime data (global, local, 
 dynamic) + PC, SP , FP & other registers + e.g., PID, mem/CPU usage, pending syscalls }

slide-19
SLIDE 19

Computer Science Science

context switches are external to a process’s logical control flow (dictated by user program) — part of exceptional control flow

slide-20
SLIDE 20

Computer Science Science

§Exceptional Control Flow

slide-21
SLIDE 21

Computer Science Science

int main() { while (1) { printf("hello world!\n"); } return 0; }

slide-22
SLIDE 22

Computer Science Science

int main() { while (1) { printf("hello world!\n"); } return 0; }

logical c.f.

slide-23
SLIDE 23

Computer Science Science

int main() { while (1) { printf("hello world!\n"); } return 0; }

logical c.f. exception!

slide-24
SLIDE 24

Computer Science Science

int main() { while (1) { printf("hello world!\n"); } return 0; }

logical c.f. exception!

?

slide-25
SLIDE 25

Computer Science Science

int main() { while (1) { printf("hello world!\n"); } return 0; }

logical c.f. exception!

?

slide-26
SLIDE 26

Computer Science Science

Two classes of exceptions:

  • I. synchronous

II.asynchronous

slide-27
SLIDE 27

Computer Science Science

  • I. synchronous exceptions are caused

by the currently executing instruction

slide-28
SLIDE 28

Computer Science Science

3 subclasses of synchronous exceptions:

  • 1. traps
  • 2. faults
  • 3. aborts
slide-29
SLIDE 29

Computer Science Science

  • 1. traps

traps are intentionally triggered by a process e.g., to invoke a system call

slide-30
SLIDE 30

Computer Science Science

char *str = "hello world"; int len = strlen(str); write(1, str, len); mov edx, len mov ecx, str mov ebx, 1 mov eax, 4 ; syscall #4 int 0x80 ; trap to OS

slide-31
SLIDE 31

Computer Science Science

return from trap (if it happens) resumes execution at the next logical instruction

slide-32
SLIDE 32

Computer Science Science

  • 2. faults

faults are usually unintentional, and may be recoverable or irrecoverable e.g., segmentation fault, protection fault, page fault, div-by-zero

slide-33
SLIDE 33

Computer Science Science

  • ften, return from fault will result in

retrying the faulting instruction — esp. if the handler “fixes” the problem

slide-34
SLIDE 34

Computer Science Science

  • 3. aborts

aborts are unintentional and irrecoverable i.e., abort = program/OS termination e.g., memory ECC error

slide-35
SLIDE 35

Computer Science Science

  • II. asynchronous exceptions are caused

by events external to the current instruction

slide-36
SLIDE 36

Computer Science Science

int main() { while (1) { printf("hello world!\n"); } return 0; } hello world! hello world! hello world! hello world! ^C $

slide-37
SLIDE 37

Computer Science Science

hardware initiated asynchronous exceptions are known as interrupts

slide-38
SLIDE 38

Computer Science Science

e.g., ctrl-C, ctrl-alt-del, power switch

slide-39
SLIDE 39

Computer Science Science

interrupts are associated with specific processor (hardware) pins

  • checked after every CPU cycle
  • associated with interrupt handlers
slide-40
SLIDE 40

Computer Science Science

1 2 . . . int # interrupt vector

  • int. handler 0 code

(system) memory

slide-41
SLIDE 41

Computer Science Science

interrupt procedure (typical)

  • save context (e.g., user process)
  • load OS context
  • execute handler
  • load context (for …?)
  • return
slide-42
SLIDE 42

Computer Science Science

important: after switching context to the OS (for exception handling), there is no guarantee if/when a process will be
 switched back in!

slide-43
SLIDE 43

Computer Science Science

P0 P1 P2 P3 P4 OS (kernel)

slide-44
SLIDE 44

Computer Science Science

P0 P1 P2 P3 P4 OS (kernel)

trap

slide-45
SLIDE 45

Computer Science Science

P0 P1 P2 P3 P4 OS (kernel)

handler trap

slide-46
SLIDE 46

Computer Science Science

P0 P1 P2 P3 P4 OS (kernel)

handler

slide-47
SLIDE 47

Computer Science Science

P0 P1 P2 P3 P4 OS (kernel)

slide-48
SLIDE 48

Computer Science Science

P0 P1 P2 P3 P4 OS (kernel)

handler trap

slide-49
SLIDE 49

Computer Science Science

P0 P1 P2 P3 P4 OS (kernel)

handler

slide-50
SLIDE 50

Computer Science Science

switching context to the kernel is potentially very expensive — but the only way to invoke system calls and access I/O

slide-51
SLIDE 51

Computer Science Science

moral (to be reinforced ad nauseum): use system calls (traps) sparingly!