0% found this document useful (0 votes)
69 views22 pages

Lec14 CS604 Pps

Uploaded by

Ibrahim Choudary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views22 pages

Lec14 CS604 Pps

Uploaded by

Ibrahim Choudary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 22

Operating

Systems
Lecture 14
Agenda for Today
 Review of previous lecture
 Short-term scheduler

 Dispatcher

 Reasons for invoking scheduler

 Optimization criteria

 FCFS, SJF, and SRTF


14 September 2019 © 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


14 September 2019 © 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;
}14 September 2019 © 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.
$14 September 2019 © 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);
}
14 September 2019 © 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);
} 14 September 2019 © Copyright Virtual University of
Pakistan
CPU Scheduling

 Scheduling processes in the


ready queue
 Short-term scheduler
 Different types of
schedulers

14 September 2019 © Copyright Virtual University of


Pakistan
Life of a Process

14 September 2019 © Copyright Virtual University of


Pakistan
Histogram of CPU-
burst Times

14 September 2019 © 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
14 September 2019 © 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
14 September 2019 © 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

14 September 2019 © 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
14 September 2019 © Copyright Virtual University of
Pakistan
CPU Scheduler
 Scheduling under 1 and 4 is
nonpreemptive.
 All other scheduling is
preemptive.

14 September 2019 © 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
14 September 2019 © 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)
14 September 2019 © Copyright Virtual University of
Pakistan
Optimization Criteria
 Maximize CPU utilization
 Maximize throughput
 Minimize turnaround time
 Minimize waiting time
 Minimize response time
14 September 2019 © 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
14 September 2019 © 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
14 September 2019 © 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
14 September 2019 © Copyright Virtual University of
Pakistan
Recap of Lecture
 Short-term scheduler
 Examples of Pthreads

 Dispatcher

 Reasons for invoking scheduler

 Optimization criteria

 FCFS
14 September 2019 © Copyright Virtual University of
Pakistan

You might also like