Review First, operating systems solves time-sharing multi-tasking - - PowerPoint PPT Presentation

review
SMART_READER_LITE
LIVE PREVIEW

Review First, operating systems solves time-sharing multi-tasking - - PowerPoint PPT Presentation

Review First, operating systems solves time-sharing multi-tasking context = memory address space + stack pointer + instruction pointer IBM360 uses context-switch for time-sharing multi-tasking Second, operating systems solves


slide-1
SLIDE 1

Review

  • First, operating systems solves time-sharing multi-tasking
  • context = memory address space + stack pointer + instruction pointer
  • IBM360 uses context-switch for time-sharing multi-tasking
  • Second, operating systems solves interprocess communication (IPC)
  • AT&T UNIX V provides message queue, shared memory and semaphore
  • Third, operating systems handles exception control flow (today’s lecture).
slide-2
SLIDE 2

Exception Control Flow (ECF)

slide-3
SLIDE 3

Exception happens due to divide 0!

slide-4
SLIDE 4

Exception happens due to Ctrl-C!

slide-5
SLIDE 5

More examples of exception

Who initiates exception? Who handles exception? Examples CPU / Hardware Operating System Timer interrupt, I/O interrupt User Application Operating System Divide zero, Ctrl-C interrupt, kill a process User Application User Application Try-catch in C++ or Java

slide-6
SLIDE 6

Control flow is the sequence of instructions executed by one CPU.

CPU executes instructions sequentially: I1, I2, I3, I4, . . . .

slide-7
SLIDE 7

Normal control flow

is the current CPU instruction, is the expected next CPU instruction

Icurr Inext

* Image from CSAPP: Computer Systems A Programmer’s Perspective

slide-8
SLIDE 8

General picture of exception control flow

Key of ECF: an event occurs between and !

Icurr Inext

* Image from CSAPP: Computer Systems A Programmer’s Perspective

Who initiates exception? Who handles exception?

slide-9
SLIDE 9

Step1: CPU executes normally till

Icurr

* Image from CSAPP: Computer Systems A Programmer’s Perspective

is the current CPU instruction, is the expected next CPU instruction

Icurr Inext

slide-10
SLIDE 10

Step2: an exception is initiated at

Icurr

* Image from CSAPP: Computer Systems A Programmer’s Perspective

is the current CPU instruction, is the expected next CPU instruction

Icurr Inext

slide-11
SLIDE 11

Step3: exception is being handled

* Image from CSAPP: Computer Systems A Programmer’s Perspective

is the current CPU instruction, is the expected next CPU instruction

Icurr Inext

slide-12
SLIDE 12

Step4: CPU (may) switch back to Inext

* Image from CSAPP: Computer Systems A Programmer’s Perspective

is the current CPU instruction, is the expected next CPU instruction

Icurr Inext

slide-13
SLIDE 13
  • Step1: CPU executes normally (normal control flow).
  • Step2: An event occurs between

and , the CPU control flow transfers to an exception handler.

  • Step3: Exception is being handled.
  • Step4: CPU may switch control flow back to

Icurr Inext Inext

General steps of exception control flow

slide-14
SLIDE 14

Exception control flow enables preemptive context-switch.

Who initiates exception? Who handles exception? Examples CPU / Hardware Operating System Timer interrupt

slide-15
SLIDE 15

CPU executes thread #1

Who initiates exception? Who handles exception? Examples CPU / Hardware Operating System Timer interrupt

Thread #1

slide-16
SLIDE 16

Timer hardware sends an interrupt to CPU

Who initiates exception? Who handles exception? Examples CPU / Hardware Operating System Timer interrupt

Thread #1 Timer event

slide-17
SLIDE 17

OS can decide to do context-switch

Who initiates exception? Who handles exception? Examples CPU / Hardware Operating System Timer interrupt

Thread #1 Timer event Decide to do a context-switch.

slide-18
SLIDE 18

OS switches context to thread #2

Who initiates exception? Who handles exception? Examples CPU / Hardware Operating System Timer interrupt

Thread #1 Thread #2 Timer event Decide to do a context-switch.

slide-19
SLIDE 19

Switching stack pointer? Switching instruction pointer? Switching memory address space? Switching kernel/user mode?

4411 P1

User-level Threads

Yes No No No

Beyond 4411 P1

Kernel-level Threads

Yes Yes No Yes

Processes

Yes Yes Yes Yes

The two “Yes” is due to exception control flow

slide-20
SLIDE 20

Exception control flow enables preemptive context-switch and also system calls.

slide-21
SLIDE 21

Exception also happens here! Surprise? System calls also incur exception control flow

slide-22
SLIDE 22

CPU executes thread #1 till

Icurr

Who initiates exception? Who handles exception? Examples User Application Operating System System Call

Thread #1

slide-23
SLIDE 23

is a syscall instruction within printf

Icurr

Thread #1 System call incurred by printf

Who initiates exception? Who handles exception? Examples User Application Operating System System Call

slide-24
SLIDE 24

OS helps thread #1 print on screen

Thread #1 Print a string

  • n the screen.

Who initiates exception? Who handles exception? Examples User Application Operating System System Call

System call incurred by printf

slide-25
SLIDE 25

Thread #1 continues to execute Inext

Thread #1 Thread #1 Print a string

  • n the screen.

Who initiates exception? Who handles exception? Examples User Application Operating System System Call

System call incurred by printf

slide-26
SLIDE 26

Exception control flow enables preemptive context-switch, system calls and also safe crash of user application.

slide-27
SLIDE 27

Exception happens due to divide 0!

slide-28
SLIDE 28

Who initiates exception? Who handles exception? Examples User Application Operating System Divide-zero

Thread #1

CPU executes thread #1 till

Icurr

slide-29
SLIDE 29

is a divide-zero instruction

Icurr

Thread #1 Thread1 executes a divide-zero instruction.

Who initiates exception? Who handles exception? Examples User Application Operating System Divide-zero

slide-30
SLIDE 30

OS terminates thread #1

Thread #1 OS terminates thread #1 and context-switch

Who initiates exception? Who handles exception? Examples User Application Operating System Divide-zero

Thread1 executes a divide-zero instruction.

slide-31
SLIDE 31

CPU executes some other thread

Thread #1 Thread #2

Who initiates exception? Who handles exception? Examples User Application Operating System Divide-zero

OS terminates thread #1 and context-switch Thread1 executes a divide-zero instruction.

slide-32
SLIDE 32

Lesson: exception control flow enables preemptive context-switch, system calls and safe crash of user application.

These exceptions are handled by a handler function in the OS.

slide-33
SLIDE 33

Question: how does the CPU know the context of exception handler?

CPU is in the context of an application program. CPU is in the context of OS’s exception handler.

slide-34
SLIDE 34

CPU

Exception stack pointer Exception instruction pointer During initialization, OS record in these registers the pointers to the code & stack of its exception handler function.

CPU has special registers for exception

slide-35
SLIDE 35

CPU

Exception stack pointer Exception instruction pointer

Transfer to exception handler

Stack pointer Instruction pointer

replace replace

Transfer to the exception handler (the red arrow in left picture) is done by the two “replace” in the below picture.

slide-36
SLIDE 36

exception type instruction pointer before exception

Exception handler stack

stack frame of exception handler stack pointer before exception

Pushed to the stack by CPU stack pointer when executing

Icurr

address of

Icurr

Timer? System call? Divide-zero?

slide-37
SLIDE 37

Exception handler in EGOS

slide-38
SLIDE 38

Summary

  • Control flow is a sequence of instructions.
  • An event can cause a CPU to switch from normal control flow to exception

control flow, which looks like the picture below.

  • Exception control flow enables preemptive context-switch, system calls

and safe crash of user application.

  • Exception control flow is made possible by both the OS exception handler

function and the related CPU registers.

slide-39
SLIDE 39

Homework

  • P1 is due on Oct 2.
  • P2 will be released today and due on Oct 23. Implement

the concepts of preemptive context-switch and the MLFQ scheduling algorithm (next lecture).

  • Further reading: the concept of IRQ: https://

en.wikipedia.org/wiki/Interrupt_request_(PC_architecture)