Top Banner
Process Scheduling CSC501 Operating Systems Principles 1
25

lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Jun 20, 2020

Download

Documents

dariahiddleston
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: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Process Scheduling

CSC501 Operating Systems Principles

1

Page 2: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Last Lecture

q Process Lifecyclesq Context Switching

2

Page 3: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Process Scheduling

q Key stepsQ Examine processes eligible for executionQ Select one based on a certain scheduling policyQ Switch CPU to selected item

q Two-level scheduling possibleQ Select ProcessQ Select thread within Process

3

Page 4: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Scheduling Policy

q Fundamental OS policyq Determines when process selected for executionq May depend onQ Process priorityQ Time process waitsQ Process owner (user)

4

Page 5: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Process Scheduling Queues

q Ready queueQ Set of processes residing in main memory,

ready, and waiting to executeq Job queueQ Set of all processes in the system

q Device queuesQ Set of processes waiting for an I/O device

q Process migration between the various queues

5

Page 6: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Ready Queue And Various I/O Device Queues

Page 7: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Representation of Process Scheduling

Page 8: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Schedulers

q Long-term scheduler (or job scheduler)Q Which processes should be brought into the

ready queueQ Invoked very infrequently (seconds, minutes) ⇒

(may be slow)Q Controls the degree of multiprogramming

q Short-term scheduler (or CPU scheduler)Q Which process should execute next (allocates

CPU)Q Invoked very frequently (milliseconds) ⇒ (must

be fast)

8

Page 9: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Schedulers (Cont.)

q Processes can be described as either:Q I/O-bound processv spends more time doing I/O than computations, v many short CPU bursts

Q CPU-bound processv spends more time doing computationsv few very long CPU bursts

9

Page 10: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Example Scheduling in XINU

q Each process assigned a priorityQ Non-negative integer valueQ Initialized when process createdQ Can be changed

q Scheduler chooses process with highest priorityQ Processes with the same priority are scheduled

in a round-robin fashionq Policy enforced as a system-wide invariant

10

Page 11: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

The XINU Scheduling Invariant

q At any time, the CPU must run the highest priority eligible process. Among processes with equal priority, scheduling is round robin.

q Invariant enforced duringQ System callQ Interrupt

11

Page 12: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Implementation of Scheduling

q Process eligible if state isQ ready or current

q To avoid searching process tableQ Keep ready processes on linked list called ready

listQ Order ready list by priorityQ Selection in constant time

12

Page 13: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Example Scheduler Code

13

Page 14: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Puzzle #1

q Invariant says that at any time, one process must be executing

q Context switch code moves from one process to another

q Question: which process executes the context switch code?

14

Page 15: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Solution to Puzzle #1

q ‘‘Old’’ processQ Executes first half of context switchQ Is suspended

q ‘‘New’’ processQ Continues executing where previously suspendedQ Usually runs second half of context switch

15

Page 16: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Puzzle #2

q Invariant says that at any time, one process must be executing

q All user processes may be idle (e.g., applications all wait for input)

q Which process executes?

16

Page 17: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Solution to Puzzle #2

q OS needs an extra processQ Called NULL processQ Never terminatesQ Cannot make a system call that takes it out of

ready or current stateQ Typically an infinite loop

17

Page 18: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Lab 1 – Process Scheduling

q Part IQ The chprio() function contains a bug (sys/chprio.c)

q Part IIQ The resched() function contains a limitation

(sys/resched.c), i.e., a process with a lower priority could suffer from starvation

18

Page 19: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Lab 1 – Process Scheduling

q Part IQ The chprio() function contains a bug (sys/chprio.c)

19

Page 20: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Lab 1 – Process Scheduling

q Part IIQ The process scheduling policy has a limitation,

namely process starvationQ You are asked to implement two different policiesv Random schedulerv Proportional sharing scheduler

20

Page 21: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Random Scheduler

q Total N processes Pi (i=0..N-1) in the ready queue

q Each PiQ Priority: PRIOiQ Probability: PRIOi/sum(PRIOj) (j=0..N-1)

21

Page 22: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Proportional Sharing Scheduler

q Two factors:Q The priority and execution time

q Timer interrupt handlerQ Related files: sys/clkint.S sys/clkinit.cQ Interrupt rate – based on clock timer v ctr1000: 1ms

Q Scheduling rate: v Interrupt rate * QUANTUM

Q Othersv preempt: preemption counter

22

Page 23: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Lab 1 – Process Scheduling

q Read relevant source code in XinuQ Process queue managementv h/q.h sys/queue.c sys/insert.c, …

Q Proc. creation/suspension/resumption/termination: v sys/create.c, sys/suspend.c sys/resume.c, sys/kill.c

Q Priority changev sys/chprio.c

Q Process schedulingv sys/resched.c

Q Other initialization codev sys/initialize.c

23

Page 24: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Process Control Block (proc.h)

24

Page 25: lec5-process3 - NC State Computer Science · Example Scheduling in XINU qEach process assigned a priority QNon-negative integer value QInitialized when process created QCan be changed

Next Lecture

q Process Synchronization

25