Scheduling and Synchronization
- semaphores
- pre-emptive scheduling
- scheduler API
- user-level scheduling
- real-time scheduling
Semaphores
- primitives:
- create with given count
- acquire
- release
- threads trying to acquire a semaphore block if the count is zero,
otherwise decrement count
- implementation:
- integer variable in memory, and queue of blocked processes
- must read (to see if zero) and write (to decrement) atomically
- can be done by disabling interrupts
- can be done with special read-modify-write instructions
Synchronization Example
...
lock(listmutex);
list * p = listhead;
while (p != NULL) {
if (p->head >= 5) {
int i = p->head;
unlock (listmutex);
return i;
}
p = p->next;
}
unlock (listmutex);
...
Pre-emption
- it is possible to write a scheduler without interrupts
- context switches only occur when processes voluntarily yield
- synchronization becomes a lot simpler
- such a system is called non-preemptive or co-operative multitasking
- example: MacOS, Windows 3.1
- with interrupts, we have pre-emption
- pre-emption lets us stop an uncooperative process and start another
Scheduler API
- fork, spawn, create
- yield
- exit
- kill
- sleep(t), pause(t) (scheduler needs notion of time)
- interrupt (called by timer interrupt)
- setsignalhandler
- suspend, resume
- synchronization primitives or primitives to suspend/resume interrupts
User-Level Scheduling
- threads within a process can be created, managed by a user process
(see homework)
- system scheduler schedules a single system thread
- time for system thread shared among user-level threads
- non-preemptive (co-operative) scheduling is easy
- preemptive scheduling requires interrupts:
- Unix signals are user-level interrupts
- signals can be made to happen periodically (see "man setitimer")
- signals can be made to happen when file descriptors are ready
for I/O
Real-Time Scheduling
- "real time" means the results of a computation are needed by
a given deadline
- example: robotic control (airplanes, manufacturing, weapons)
- example: process control (chemical plants)
- in a non-real-time system, occasional slow performance is
undesirable but does not affect the correctness of the system
- in a real-time system, a missed deadline could be uncomfortable
to catastrophic
Real-Time Scheduler
- input: a list of processes/threads
- input: a deadline (or min/max deadline) for each process/thread
- possible input: an estimate (upper bound) of how long each
thread will execute for
- possible input: thread priorities
- output: a decision of which thread to execute next
Earliest Deadline First
- start executing thread with earliest deadline, execute to completion
- if deadline is missed, it would have been missed by any other
scheduling algorithm
- does not need estimate of how long each thread will execute for