Operating Systems Lecture 14
© Copyright Virtual University of Pakistan Agenda for Today Review of previous lecture Short-term scheduler Dispatcher Reasons for invoking scheduler Optimization criteria FCFS, SJF, and SRTF 2 December 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Review of Lecture 13 Single and multi-threaded processes Thread models User- and kernel-level threads Pthreads library Sample multi-threaded code 2 December 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Example 1 #include <stdio.h> #include <stdlib.h> #include <pthread.h> /* 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; } 2 December 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of 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. $ 2 December 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Example 2 #include <pthread.h> #include <stdio.h> #define NUM_THREADS 5 void *PrintHello(void *threadid) { printf("\n%d: Hello World!\n", threadid); pthread_exit(NULL); } 2 December 2018 © Copyright Virtual University of Pakistan
© 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); 2 December 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan CPU Scheduling Scheduling processes in the ready queue Short-term scheduler Different types of schedulers 2 December 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Life of a Process 2 December 2018 © Copyright Virtual University of Pakistan
Histogram of CPU-burst Times 2 December 2018 © Copyright Virtual University of Pakistan
© 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 2 December 2018 © Copyright Virtual University of Pakistan
© 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 2 December 2018 © Copyright Virtual University of Pakistan
© 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 2 December 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan CPU Scheduler CPU scheduling decisions may take place when a process: Switches from running to waiting state Switches from running to ready state Switches from waiting to ready Terminates 2 December 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan CPU Scheduler Scheduling under 1 and 4 is nonpreemptive. All other scheduling is preemptive. 2 December 2018 © Copyright Virtual University of Pakistan
© 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 2 December 2018 © Copyright Virtual University of Pakistan
© 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 time-sharing environment) 2 December 2018 © Copyright Virtual University of Pakistan
Optimization Criteria Maximize CPU utilization Maximize throughput Minimize turnaround time Minimize waiting time Minimize response time 2 December 2018 © Copyright Virtual University of Pakistan
© 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 2 December 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan FCFS Scheduling Processes are served in the order: P1, P2, P3 The Gantt Chart for the schedule is: Waiting times P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0+24+27)/3 = 17 P1 P2 P3 24 27 30 2 December 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan FCFS Scheduling Suppose that processes arrive in the order: P2 , P3 , P1 . The Gantt chart for the schedule is: Waiting time for P1 = 6; P2 = 0; P3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3 Convoy effect short process behind long process P1 P3 P2 6 3 30 2 December 2018 © Copyright Virtual University of Pakistan
© Copyright Virtual University of Pakistan Recap of Lecture Short-term scheduler Examples of Pthreads Dispatcher Reasons for invoking scheduler Optimization criteria FCFS 2 December 2018 © Copyright Virtual University of Pakistan