Top Banner
1 P r o c e s s e s , C o n t e x t S w i t c h e s a n d I n t e r r u p t s Steve Goddard [email protected] http://www.cse.unl.edu/~goddard/Courses/CSCE351 C S C E 3 5 1 O p e r a t i n g S y s t e m K e r n e l s 2 P r o c e s s e s The basic agent of work, the basic building block Process characterization » Program code » Processor/Memory state » Execution state The state transition diagram Running Ready Waiting
14

CSCE 351 Operating System Kernels Processes, Context …cse.unl.edu/~goddard/Courses/CSCE351/Lectures/Lecture2.pdf · 1 Processes, Context Switches and Interrupts Steve Goddard [email protected]

Aug 26, 2018

Download

Documents

hatuong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSCE 351 Operating System Kernels Processes, Context …cse.unl.edu/~goddard/Courses/CSCE351/Lectures/Lecture2.pdf · 1 Processes, Context Switches and Interrupts Steve Goddard goddard@cse.unl.edu

1

Processes, Context Switchesand Inter rupts

Steve [email protected]

http://www.cse.unl.edu/~goddard/Courses/CSCE351

CSCE 351Operating System Kernels

2

Processes

◆ The basic agent of work, the basic building block◆ Process characterization

» Program code» Processor/Memory state» Execution state

◆ The state transition diagram

RunningReady

Waiting

Page 2: CSCE 351 Operating System Kernels Processes, Context …cse.unl.edu/~goddard/Courses/CSCE351/Lectures/Lecture2.pdf · 1 Processes, Context Switches and Interrupts Steve Goddard goddard@cse.unl.edu

3

Process Actions

◆ Create and Delete

◆ Suspend and Resume

◆ Process synchronization

◆ Process communication

4

Multiprogramming

Page 3: CSCE 351 Operating System Kernels Processes, Context …cse.unl.edu/~goddard/Courses/CSCE351/Lectures/Lecture2.pdf · 1 Processes, Context Switches and Interrupts Steve Goddard goddard@cse.unl.edu

5

Physical v. Logical ConcurrencyWhy is logical concurrency useful?

◆ Structuring of computation

◆ Performance

» Single process I/O

process Pbegin

:Read(var):

end P

system call Read()beginStartIO(input device)WaitIO(interrupt)EndIO(input device):

end Read

6

Physical v. Logical ConcurrencyPerformance considerations

◆ Multithreaded I/O

process Pbegin

:StartRead()<compute>Read(var):

end P

system process Read()beginloopWaitForRequest()System_Read(var)WaitForRequestor():

end loopend Read

system call StartRead()beginRequestIO(input device)

end StartRead

system call Read()beginSignalReader(input device)

end Read

Page 4: CSCE 351 Operating System Kernels Processes, Context …cse.unl.edu/~goddard/Courses/CSCE351/Lectures/Lecture2.pdf · 1 Processes, Context Switches and Interrupts Steve Goddard goddard@cse.unl.edu

7

Process Creation Paradigms

◆ COBEGIN/COEND

◆ FORK/JOIN

◆ Explicit processcreation begin

:P:

end

begin:fork(foo):join(foo):

end

cobeginS1 ||

S2 ||

:Sn

coend

procedure foo()begin

::

end foo

process Pbegin

::

end P

8

Threads

◆ 3 processes» Each with one thread

◆ 1 process» Three threads

Page 5: CSCE 351 Operating System Kernels Processes, Context …cse.unl.edu/~goddard/Courses/CSCE351/Lectures/Lecture2.pdf · 1 Processes, Context Switches and Interrupts Steve Goddard goddard@cse.unl.edu

9

Process SchedulingImplementing and managing state transitions

RunningReady

Waiting

Head

Tailready queue

Head

Tail

device/conditionqueues

prev ptr

reg values

mem ptrs

next ptr

...

prev ptr

reg values

mem ptrs

next ptr

...

prev ptr

reg values

mem ptrs

next ptr

...

...

...Tail

Head

name name name

10

Why Schedule?Scheduling goals

◆ Example: two processes execute concurrently

◆ Performance without scheduling

◆ Performance with scheduling

process P1beginfor i := 1 to 5 do<read a char><process a char>

end forend P1

process P2begin<execute for 1 sec >

end P2

1 100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600 1700 1800 1900 2000

P1

P2

1 100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600 1700 1800 1900 2000

P1

P2

Page 6: CSCE 351 Operating System Kernels Processes, Context …cse.unl.edu/~goddard/Courses/CSCE351/Lectures/Lecture2.pdf · 1 Processes, Context Switches and Interrupts Steve Goddard goddard@cse.unl.edu

11

Types of Schedulers

◆ Long term schedulers» adjust the level of multiprogramming through

admission control

◆ Medium term schedulers» adjust the level of multiprogramming by suspending

processes

◆ Short term schedulers» determine which process should execute next

12

Shor t Term SchedulingWhen to schedule

When a process makes a transition...1. from running to waiting2. from running to ready3. from waiting to ready

(3a. a process is created)4. from running to terminated

RunningReady

Waiting

Head

Tailready queue

Head

Tail

device/conditionqueues

Page 7: CSCE 351 Operating System Kernels Processes, Context …cse.unl.edu/~goddard/Courses/CSCE351/Lectures/Lecture2.pdf · 1 Processes, Context Switches and Interrupts Steve Goddard goddard@cse.unl.edu

13

Shor t Term SchedulingHow to schedule — Implementing a context switch

context_switch(queue : system_queue)var next : process_idbeginDISABLE_INTSinsert_queue(queue, runningProcess)next := remove_queue(readyQueue)dispatch(next)ENABLE_INTS

end context_switch

dispatch(proc : process_id)begin<save memory image of runningProcess><save processor state of runningProcess><load memory image of proc><load processor state of proc>runningProcess := proc

end dispatch

RunningReady

Waiting

Head

Tailready queue

Head

Tail

device/conditionqueues

14

◆ Case 1: Yield

“P2: running”

main()

read()

startIO()

switch()

dispatch()

waitIO()

main()

Implementing a Context SwitchDispatching

dispatch()begin<save state of P2> <load state of P1>

:end dispatch

P2’s dispatch:“P1”

Page 8: CSCE 351 Operating System Kernels Processes, Context …cse.unl.edu/~goddard/Courses/CSCE351/Lectures/Lecture2.pdf · 1 Processes, Context Switches and Interrupts Steve Goddard goddard@cse.unl.edu

15

main()

read()

startIO()

switch()

dispatch()

waitIO()

main()

deposit()

wait()

switch()

dispatch()

dispatch()begin<save state of P1> <load state of P2>

:end dispatch

◆ Case 1: Yield

“P1: running” “P2”P1’s dispatch:

Implementing a Context SwitchDispatching

16

“P1” “P2 : running”

dispatch()begin:RunningProcess:= P2

end dispatch

P2’s dispatch:

main()

read()

startIO()

switch()

dispatch()

waitIO()

main()

deposit()

wait()

switch()

dispatch()

dispatch()begin<save state of P1> <load state of P2>

:end dispatch

◆ Case 1: Yield

P1’s dispatch:

Implementing a Context SwitchDispatching

Page 9: CSCE 351 Operating System Kernels Processes, Context …cse.unl.edu/~goddard/Courses/CSCE351/Lectures/Lecture2.pdf · 1 Processes, Context Switches and Interrupts Steve Goddard goddard@cse.unl.edu

17

main()

read()

startIO()

switch()

waitIO()

“P2: running”

main()

deposit()

wait()

switch()

dispatch()

◆ Case 1: Yield

Implementing a Context SwitchDispatching

“P1”context_switch(queue : system_queue)var next : process_idbeginDISABLE_INTSinsert_queue(queue, runningProcess)next := remove_queue(readyQueue)dispatch(next)ENABLE_INTS

end context_switch

18

“P1” “P2 : running”

main()

read()

startIO()

waitIO()

main()

deposit()

wait()

switch()

dispatch()

◆ Case 1: Yield

Implementing a Context SwitchDispatching

Page 10: CSCE 351 Operating System Kernels Processes, Context …cse.unl.edu/~goddard/Courses/CSCE351/Lectures/Lecture2.pdf · 1 Processes, Context Switches and Interrupts Steve Goddard goddard@cse.unl.edu

19

“P1” “P2 : running”

main()

read()

startIO()

main()

deposit()

wait()

switch()

dispatch()

◆ Case 1: Yield

Implementing a Context SwitchDispatching

20

“P1” “P2 : running”

main()

read()

main()

deposit()

wait()

switch()

dispatch()

◆ Case 1: Yield

Implementing a Context SwitchDispatching

Page 11: CSCE 351 Operating System Kernels Processes, Context …cse.unl.edu/~goddard/Courses/CSCE351/Lectures/Lecture2.pdf · 1 Processes, Context Switches and Interrupts Steve Goddard goddard@cse.unl.edu

21

dispatch()begin<save state of P2> <load state of P1>

:end dispatch

◆ Case 2: Preemption

main() main()

bar()

timerInt()

switch()

dispatch()

“P1” “P2: running”P2’s dispatch:

Implementing a Context SwitchDispatching

22

main()

foo()

timerInt()

switch()

dispatch()

dispatch()begin<save state of P1> <load state of P2>

:end dispatch

◆ Case 2: Preemption

main()

bar()

timerInt()

switch()

dispatch()

“P1: running” “P2”P1’s dispatch:

Implementing a Context SwitchDispatching

Page 12: CSCE 351 Operating System Kernels Processes, Context …cse.unl.edu/~goddard/Courses/CSCE351/Lectures/Lecture2.pdf · 1 Processes, Context Switches and Interrupts Steve Goddard goddard@cse.unl.edu

23

main()

foo()

timerInt()

switch()

dispatch()

dispatch()begin<save state of P1> <load state of P2>

:end dispatch

◆ Case 2: Preemption

main()

bar()

timerInt()

switch()

dispatch()

“P1” “P2 : running”P1’s dispatch:

Implementing a Context SwitchDispatching

dispatch()begin:RunningProcess:= P2

end dispatch

P2’s dispatch:

24

main()

foo()

timerInt()

switch()

dispatch()

◆ Case 2: Preemption

main()

bar()

“P1” “P2 : running”

Implementing a Context SwitchDispatching

Page 13: CSCE 351 Operating System Kernels Processes, Context …cse.unl.edu/~goddard/Courses/CSCE351/Lectures/Lecture2.pdf · 1 Processes, Context Switches and Interrupts Steve Goddard goddard@cse.unl.edu

25

Inter rupts

◆ Device sends a signal to an interrupt controller

◆ Controller interrupts the CPU via the INT pin

26

Kernel response to an Inter rupt - sketch

◆ CPU stacks PC and other key registers

◆ CPU loads new PC from interrupt vector table

◆ Assembly language procedure saves registers

◆ Assembly language procedure sets up INT stack

◆ C ISR runs (usually reads and buffers input)

◆ Scheduler marks any newly ready tasks

◆ Scheduler decides which process will run next

◆ C procedure returns to the assembly code

◆ Assembly language procedure switches to new current process

Page 14: CSCE 351 Operating System Kernels Processes, Context …cse.unl.edu/~goddard/Courses/CSCE351/Lectures/Lecture2.pdf · 1 Processes, Context Switches and Interrupts Steve Goddard goddard@cse.unl.edu

27

Response to an Inter rupt -details for Intel processors

◆ Controller interrupts the CPU via the INT pin

◆ CPU disables interrupts and pushes PC and other key registers onto the current process stack

◆ CPU signals the controller via INTA (interrupt acknowledge) signal to put interrupt number on the system data bus

◆ CPU reads the system data bus and uses that value as an index into the interrupt vector table to find the pointer of the interrupt handler, which is an assembly routine wrapper for the ISR (i.e., an indirect jump)

◆ The interrupt handler fills out the stack frame with general registers, switches to an interrupt stack and calls the C ISR

◆ When the ISR completes, the handler switches to a process stack frame, pops the general registers, and executes the iretd (return from interrupt) instruction to pop the remaining instructions in the stack frame to restore the system state

28

Inter rupts vs System Calls

Interrupt System Call