Lec09 Schedule
Lec09 Schedule
Synchronization (con’t)
Scheduling Review
Interactive is important!
Ask Questions!
• Condition Variables
– Creation: Can be created either statically or dynamically
» Static: pthread_cond_t mycond = PTHREAD_COND_INITIALIZER;
» Dynamic: pthread_mutex_init(&cond,&attr);
pthread_mutex_destroy(&cond);
Here, attr has only one element: process-shared
– Use: Simple locking and unlocking (and “try”):
» pthread_cond_wait(&cond);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&cond);
• Barriers:
– Creation: pthread_barrier_init(&barrier,&attr,count);
pthread_barrier_destroy(&barrier);
– Use: pthread_barrier_wait(&barrier);
DEFINE_SPINLOCK(my_lock);
spin_lock(&mr_lock);
// Cricical section
spin_unlock(&mr_lock);
2/27/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 9.9
Linux Synchronization (in Kernel), Con’t
• Spin Locks:
– Simple Use: include <asm/spinlock.h>
include <linux/spinlock.h>
DEFINE_SPINLOCK(my_lock);
spin_lock(&mr_lock);
// Cricical section
spin_unlock(&mr_lock);
– In Interrupt handlers:
DEFINE_SPINLOCK(my_lock);
unsigned long flags;
// Cricical section
spin_unlock_irqrestore(&mr_lock, flags);
Thread 1 Thread 2
a = 3;
mb();
b=4; c = b;
rmb();
d = a;
Time
2/27/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 9.16
Assumption: CPU Bursts
0 24 27 30
– Waiting time for P1 = 0; P2 = 24; P3 = 27
– Average waiting time: (0 + 24 + 27)/3 = 17
– Average Completion time: (24 + 27 + 30)/3 = 27
• Convoy effect: short process behind long process
2/27/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 9.19
FCFS Scheduling (Cont.)
• Example continued:
– Suppose that processes arrive in order: P2 , P3 , P1
Now, the Gantt chart for the schedule is:
P2 P3 P1
0 3 6 30
– Waiting time for P1 = 6; P2 = 0; P3 = 3
– Average waiting time: (6 + 0 + 3)/3 = 3
– Average Completion time: (3 + 6 + 30)/3 = 13
• In second case:
– average waiting time is much better (before it was 17)
– Average completion time is better (before it was 27)
• FIFO Pros and Cons:
– Simple (+)
– Short jobs get stuck behind long ones (-)
» Safeway: Getting milk, always stuck behind cart full of
small items. Upside: get to read about space aliens!
2/27/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 9.20
Round Robin (RR)
• FCFS Scheme: Potentially bad for short jobs!
– Depends on submit order
– If you are first in line at supermarket with milk, you
don’t care who is behind you, on the other hand…
• Round Robin Scheme
– Each process gets a small unit of CPU time
(time quantum), usually 10-100 milliseconds
– After quantum expires, the process is preempted
and added to the end of the ready queue.
– n processes in ready queue and time quantum is q
» Each process gets 1/n of the CPU time
» In chunks of at most q time units
» No process waits more than (n-1)q time units
• Performance
– q large FCFS
– q small Interleaved (really small hyperthreading?)
– q must be large with respect to context switch,
otherwise overhead is too high (all overhead)
2/27/13 Kubiatowicz CS194-24 ©UCB Fall 2013 Lec 9.21
Example of RR with Time Quantum = 20
• Example: Process Burst Time
P1 53
P2 8
P3 68
P4 24
– The Gantt chart is:
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
A or B C
SRTF
C’s C’s
I/O I/O
Long-Running Compute
Tasks Demoted to
Low Priority