review
play

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


  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).

  2. Exception Control Flow (ECF)

  3. Exception happens due to divide 0!

  4. Exception happens due to Ctrl-C!

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

  6. Control flow is the sequence of instructions executed by one CPU. CPU executes instructions sequentially: I 1 , I 2 , I 3 , I 4 , . . . .

  7. Normal control flow is the current CPU instruction, is the expected next CPU instruction I curr I next * Image from CSAPP: Computer Systems A Programmer’s Perspective

  8. General picture of exception control flow Who handles exception? Who initiates exception? Key of ECF: an event occurs between and ! I curr I next * Image from CSAPP: Computer Systems A Programmer’s Perspective

  9. Step1: CPU executes normally till I curr is the current CPU instruction, is the expected next CPU instruction I curr I next * Image from CSAPP: Computer Systems A Programmer’s Perspective

  10. Step2: an exception is initiated at I curr is the current CPU instruction, is the expected next CPU instruction I curr I next * Image from CSAPP: Computer Systems A Programmer’s Perspective

  11. Step3: exception is being handled is the current CPU instruction, is the expected next CPU instruction I curr I next * Image from CSAPP: Computer Systems A Programmer’s Perspective

  12. Step4: CPU (may) switch back to I next is the current CPU instruction, is the expected next CPU instruction I curr I next * Image from CSAPP: Computer Systems A Programmer’s Perspective

  13. General steps of exception control flow • Step1: CPU executes normally (normal control flow). • Step2: An event occurs between and , the CPU control flow I curr I next transfers to an exception handler. • Step3: Exception is being handled. • Step4: CPU may switch control flow back to I next

  14. Exception control flow enables preemptive context-switch. Who initiates Who handles Examples exception? exception? CPU / Hardware Operating System Timer interrupt

  15. CPU executes thread #1 Thread #1 Who initiates Who handles Examples exception? exception? CPU / Hardware Operating System Timer interrupt

  16. Timer hardware sends an interrupt to CPU Thread #1 Timer event Who initiates Who handles Examples exception? exception? CPU / Hardware Operating System Timer interrupt

  17. OS can decide to do context-switch Thread #1 Timer event Decide to do a context-switch. Who initiates Who handles Examples exception? exception? CPU / Hardware Operating System Timer interrupt

  18. OS switches context to thread #2 Thread #1 Timer event Decide to do a context-switch. Thread #2 Who initiates Who handles Examples exception? exception? CPU / Hardware Operating System Timer interrupt

  19. The two “Yes” is due to exception control flow Switching Switching Switching Switching memory stack instruction kernel/user address pointer? pointer? mode? space? User-level Yes No No No 4411 P1 Threads Kernel-level Yes Yes No Yes Threads Beyond 4411 P1 Yes Yes Yes Yes Processes

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

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

  22. CPU executes thread #1 till I curr Thread #1 Who initiates Who handles Examples exception? exception? User Application Operating System System Call

  23. is a syscall instruction within printf I curr Thread #1 System call incurred by printf Who initiates Who handles Examples exception? exception? User Application Operating System System Call

  24. OS helps thread #1 print on screen Thread #1 System call incurred by printf Print a string on the screen. Who initiates Who handles Examples exception? exception? User Application Operating System System Call

  25. Thread #1 continues to execute I next Thread #1 System call incurred by printf Print a string on the screen. Thread #1 Who initiates Who handles Examples exception? exception? User Application Operating System System Call

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

  27. Exception happens due to divide 0!

  28. CPU executes thread #1 till I curr Thread #1 Who initiates Who handles Examples exception? exception? User Application Operating System Divide-zero

  29. is a divide-zero instruction I curr Thread #1 Thread1 executes a divide-zero instruction. Who initiates Who handles Examples exception? exception? User Application Operating System Divide-zero

  30. OS terminates thread #1 Thread #1 Thread1 executes a divide-zero instruction. OS terminates thread #1 and context-switch Who initiates Who handles Examples exception? exception? User Application Operating System Divide-zero

  31. CPU executes some other thread Thread #1 Thread1 executes a divide-zero instruction. OS terminates thread #1 and context-switch Thread #2 Who initiates Who handles Examples exception? exception? User Application Operating System Divide-zero

  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.

  33. Question: how does the CPU know the context of exception handler? CPU is in the context of CPU is in the context of an application program. OS’s exception handler.

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

  35. Transfer to exception handler Transfer to the exception handler (the red arrow in left picture) is done by the two “replace” in the below picture. CPU replace Exception stack pointer Stack pointer replace Exception instruction pointer Instruction pointer

  36. Exception handler stack stack pointer when stack pointer before exception executing I curr instruction pointer Pushed to the stack by CPU address of I curr before exception Timer? System call? exception type Divide-zero? stack frame of exception handler

  37. Exception handler in EGOS

  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.

  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)

Recommend


More recommend