Operating Systems
Lecture 14
Agenda for Today
of previous lecture Short-term scheduler Dispatcher Reasons for invoking scheduler Optimization criteria FCFS, SJF, and SRTF
1 September 2009 © Copyright Virtual University of Pakistan
Review
Review of Lecture 13
and multi-threaded processes Thread models User- and kernel-level threads Pthreads library Sample multi-threaded code
1 September 2009 © Copyright Virtual University of Pakistan
Single
Example 1
#include #include #include /* Prototype for a function to be passed to our thread */ void* MyThreadFunc(void *arg); int main() { pthread_t aThread; /* Create a thread and have it run the MyThreadFunction */ pthread_create(&aThread, NULL, MyThreadFunc, NULL); /* Parent waits for the aThread thread to exit */ pthread_join(aThread, NULL); printf ("Exiting the main function.\n"); return 0; © Copyright Virtual University of }1 September 2009
Pakistan
Example 1
void* MyThreadFunc(void* arg) { printf ("Hello, world! ... The threaded version.\n"); return NULL; }
$ gcc hello.c –o hello –lpthread –D_REENTRANT $ hello Hello, world! ... The threaded version. Exiting the main function. $ September 2009 1 © Copyright Virtual University of
Pakistan
Example 2
#include #include #define NUM_THREADS 5 void *PrintHello(void *threadid) { printf("\n%d: Hello World!\n", threadid); pthread_exit(NULL); }
1 September 2009 © Copyright Virtual University of Pakistan
Example 2
int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc, t; for (t=0; t < NUM_THREADS; t++) { printf("Creating thread %d\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); if (rc) { printf("ERROR; return code is %d\n", rc); exit(-1); } } pthread_exit(NULL); } 1 September 2009
© Copyright Virtual University of Pakistan
CPU Scheduling
Scheduling processes in the ready queue Short-term scheduler Different types of schedulers
1 September 2009 © Copyright Virtual University of Pakistan
Life of a Process
1 September 2009
© Copyright Virtual University of Pakistan
Histogram of CPUburst Times
1 September 2009
© Copyright Virtual University of Pakistan
CPU Scheduler
Short-term scheduler Selects a process from among the processes in the ready queue Invokes the dispatcher to have the CPU allocated to the selected process
1 September 2009 © Copyright Virtual University of Pakistan
Dispatcher
Dispatcher gives control of the CPU to the process selected by the short-term scheduler; this involves: switching context switching to user mode jumping to the proper location in the user program to start (or restart) it
1 September 2009 © Copyright Virtual University of Pakistan
Dispatcher
Dispatch latency – time it takes for the dispatcher to stop one process and start another running. Typically, a few microseconds
1 September 2009
© Copyright Virtual University of Pakistan
CPU Scheduler
CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state 2. Switches from running to ready state 3. Switches from waiting to ready 4. Terminates
1 September 2009 © Copyright Virtual University of Pakistan
CPU Scheduler
Scheduling under 1 and 4 is nonpreemptive. All other scheduling is preemptive.
1 September 2009
© Copyright Virtual University of Pakistan
Scheduling Criteria
CPU utilization – keep the CPU as busy as possible Throughput – # of processes that complete their execution per time unit Turnaround time – amount of time to execute a particular process
1 September 2009 © Copyright Virtual University of Pakistan
Scheduling Criteria
Waiting time – amount of time a process has been waiting in the ready queue Response time – amount of time it takes from when a request was submitted until the first response is produced, not output (for timesharing environment)
1 September 2009 © Copyright Virtual University of Pakistan
Optimization Criteria
Maximize CPU utilization Maximize throughput Minimize turnaround time Minimize waiting time Minimize response time
1 September 2009 © Copyright Virtual University of Pakistan
FCFS Scheduling
The process that enters the ready queue first is scheduled first, regardless of the size of its next CPU burst Example: Process Burst Time P1 24 P2 3 P3 3 Suppose that processes arrive into the system in the order: P1, P2 , P3
1 September 2009 © Copyright Virtual University of Pakistan
FCFS Scheduling
Processes are served in the order: P1, P2, P3 The Gantt Chart for the schedule is:
P1 P2 P3
0
24
27
30
Waiting times P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0+24+27)/3 = 17
1 September 2009 © Copyright Virtual University of Pakistan
FCFS Scheduling
Suppose that processes arrive in the order: P2 , P3 , P1 . 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 Convoy effect short process behind long process
1 September 2009 © Copyright Virtual University of Pakistan
Recap of Lecture
scheduler Examples of Pthreads Dispatcher Reasons for invoking scheduler Optimization criteria FCFS
1 September 2009 © Copyright Virtual University of Pakistan
Short-term