lec5-process3 - nc state computer science · example scheduling in xinu qeach process assigned a...
TRANSCRIPT
Process Scheduling
CSC501 Operating Systems Principles
1
Last Lecture
q Process Lifecyclesq Context Switching
2
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
Scheduling Policy
q Fundamental OS policyq Determines when process selected for executionq May depend onQ Process priorityQ Time process waitsQ Process owner (user)
4
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
Ready Queue And Various I/O Device Queues
Representation of Process Scheduling
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
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
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
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
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
Example Scheduler Code
13
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
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
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
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
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
Lab 1 – Process Scheduling
q Part IQ The chprio() function contains a bug (sys/chprio.c)
19
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
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
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
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
Process Control Block (proc.h)
24
Next Lecture
q Process Synchronization
25