14b Scheduling
14b Scheduling
14b Scheduling
Announcements
Midterm vote:
Keep current date (10/17)? Advance prior to 10/12 (drop off deadline)?
Request re-grades promptly (by e-mailing cs241help or asking a grader, TA, or instructor during office hours) Check that you dont have missing (not recorded) grades
2
Solutions to Quiz 2
Q1) Which pthreads function is the closest analogy to fork(), applied to threads? pthread_create() (1 point)
Q2) Which pthreads function is the closest analogy to wait(), applied to threads? pthread_join() (1 point) Q3) What does the Test-and-Set instruction do? It tests a Boolean (returning its current value), and sets it to one atomically (1 point) Q4) Answer true or false. Correct if wrong. (2 points) A) All UNIX processes running on the same computer are descendants of the same process. TRUE B) A child process becomes a zombie if it executes exit() but the parent never issues a wait(). TRUE C) If a parent process sets some global variable x equal to 1 then creates a child process who increments x, the parent will see the incremented value of x only after the child returns. FALSE. It never sees the update. D) fork() always returns 0 if the operation is successful. FALSE: It can 3 also return child PID.
Problems with FIFO (FCFS) Shortest job first Round Robin Priority Scheduling Understand how your program is executed on the machine together with other programs
4
Goals:
Process that requests the CPU FIRST is allocated the CPU FIRST. Also called FIFO Is it preemptive or non-preemptive? Used in batch systems Implementation
FIFO queues A new process enters at the tail of the queue An unblocked process re-enters at the tail of the queue The schedule selects from the head of the queue.
Assume 1 process CPU bounded and many I/O bounded processes Result: Convoy effect, low CPU and I/O Device utilization Why?
6
Consider n-1 jobs in system that are I/O bound and 1 job that is CPU bound. I/O bound jobs pass quickly through the ready queue and suspend themselves waiting for I/O. CPU bound job arrives at head of queue and executes until complete. I/O bound jobs re-join ready queue and wait for CPU bound job to complete. I/O devices idle until CPU bound job completes. When CPU bound job completes, other processes rush to wait on I/O again. CPU becomes idle. 7
Usually preemptive
Performance Criteria
Time is sliced into quantum (time intervals) Scheduling decision is also made at the beginning of each quantum Average response time Fairness (or proportional resource allocation) Priority-based Round-robin
Representative algorithms:
Round-robin
One of the oldest, simplest, most commonly used scheduling algorithm Select process/thread from ready queue in a round-robin fashion (take turns)
Round-robin: Example
Process Duration Order Arrival Time
0 0 0
10
Too many context switches (overheads) Inefficient CPU utilization 70-80% of jobs block within time-slice
10-100 ms (depends on job priority)
11
Heuristic:
Typical time-slice
Schedule the job with the shortest computation time first Scheduling in Batch Systems Two types:
Non-preemptive Preemptive
Optimal if all jobs are available simultaneously: Gives the best possible AWT (average waiting time)
12
Duration 6 8 7
P1 (6) 9
Order 1 2 3
P3 (7)4 16
P4
The total time is: 24 The average waiting time (AWT): (0+3+9+16)/4 = 7
13
Comparing to FCFS
Process P1 P2 P3 Duration 6 8 7
P2 (8)3 6 14
Order 1 2 3 4
Arrival Time 0 0 0
P3 (7)
Do it yourself P4 P1 (6) 0
0
21
P4 (3) 24
The total time is the same. The average waiting time (AWT): (0+6+14+21)/4 = 10.25 (comparing to 7)
14
Preemptive SJF
Shortest job runs first. A job that arrives and is shorter than the running job will preempt it
15
Starvation
Process A with elapse time of 1 hour arrives at time 0 But every 1 minute, a short process with elapse time of 2 minutes arrives Result of SJF: A never gets to run
Priority Scheduling
Each job is assigned a priority. FCFS within each priority level. Select highest priority job over lower ones. Rational: higher priority jobs are more mission-critical
Example: DVD movie player vs. send email May not give the best AWT Starvation of lower priority processes
Problems:
17
Priority 4 1 3
P3 (7)2 18
Do it yourself P2 (8) P4 0
The average waiting time (AWT): (0+8+11+18)/4 = 9.25 (worse than SJF)
18
Set Priority
Every process has a default priority User can also change a process priority
19
20
Introduction to Signals
notification to a process of an event. Why do we need Signals? In systems we need to enable asynchronous events
Examples of asynchronous events:
Email message arrives on my machine mailing agent (user) process should retrieve Invalid memory access happens OS should inform scheduler to remove process from the processor Alarm clock goes on process which sets the alarm should catch it
21
Signal is generated when the event that causes it occurs. Signal is delivered when a process receives it. The lifetime of a signal is the interval between its generation and delivery. Signal that is generated but not delivered is pending. Process catches signal if it executes a signal handler when the signal is delivered. Alternatively, a process can ignore a signal when it is delivered, that is to take no action. Process can temporarily prevent signal from being delivered by blocking it. Signal Mask contains the set of signals currently blocked. 22
Signal Handler
Signal Mask
SIGALRM
SIGBUS SIGCHLD SIGILL SIGINT SIGKILL
alarm clock
access undefined part of memory object child terminated, stopped or continued invalid hardware instruction interactive attention signal (usually ctrl-C) terminated (cannot be caught or ignored)
abnormal termination
implementation dependent ignore implementation dependent abnormal termination abnormal termination
24
Description
Invalid memory reference Execution stopped termination Terminal stop Background process attempting read Background process attempting write High bandwidth data available on socket
default action
implementation dependent stop Abnormal termination stop stop stop ignore
SIGUSR1
User-defined signal 1
abnormal termination
25
Generating Signals
Signal has a symbolic name starting with SIG Signal names are defined in signal.h Users can generate signals (e.g., SIGUSR1) OS generates signals when certain errors occur (e.g., SIGSEGV invalid memory reference) Specific calls generate signals such as alarm (e.g., SIGALARM)
26
You can send a signal to a process from the command line using kill kill -l will list the signals the system understands kill [-signal] pid will send a signal to a process.
28
alarm(20) creates SIGALRM to calling process after 20 real time seconds. Calls are not stacked alarm(0) cancels alarm
29
Summary
Important Issues to remember: How to compare different policies? What are the pros and cons of each scheduling policy? What are signals and why are they important? What does it mean to catch a signal? What are the different ways to generate signals?
30