5.5 FSFC and RR algorithm code

advertisement
The University of British Columbia
Department of Electrical and Computer Engineering
EECE 315: Operating and File Systems
CPU Scheduler
By Scott Hazlett (63215065) –
Zheng Lu (68285063)
Olivani Baisheka (84757061)
1
Table of Contents
1
Introduction ......................................................................................................................... 3
2
Project Requirements .......................................................................................................... 4
3
Program Flowchart Diagram................................................................................................ 5
3.1
Main Scheduler Flowchart............................................................................................... 5
3.2
Flowchart for FCFS and RR Schedules Subroutine........................................................... 6
3.3
Flowchart for Priority-Based Schedules Subroutine........................................................ 7
3.4
Sub Flowchart for SJF and SBF ......................................................................................... 8
4
Member Tasks ..................................................................................................................... 9
4.1
Member Tasks for Programming ..................................................................................... 9
4.2
Member Tasks for Report Writing ................................................................................. 10
5
Solution Description .......................................................................................................... 11
5.1
Create new process class............................................................................................... 11
5.2
Obtain user input........................................................................................................... 11
5.3
Parsing data ................................................................................................................... 11
5.4
Common algorithm code ............................................................................................... 12
5.5
FSFC and RR algorithm code .......................................................................................... 12
5.5.1
FCFS algorithm ....................................................................................................... 12
5.5.2
Round Robin algorithm.......................................................................................... 12
5.6
Priority algorithm code .................................................................................................. 13
5.7
SJF and SPB algorithm code ........................................................................................... 13
6
Gantt chart ........................................................................................................................ 13
7
Notable Features ............................................................................................................... 13
8
Test cases........................................................................................................................... 13
9
Conclusion ......................................................................................................................... 14
2
1 Introduction
Scheduling is a key concept in computer multitasking, multiprocessing operating
system and real-time operating system designs. Scheduling refers to the way processes
are assigned to run on the available CPUs, since there are typically many more processes
running than there are available CPUs. This assignment is carried out by software known
as a scheduler and dispatcher.
In this project a CPU scheduler with seven different algorithms learnt in class is
designed. The project will concentrate on providing useful comparisons of scheduling
policies. We will make the code easy to conceptualize by using five separate queues to
represent the five stages of a process: new, ready, running, waiting, and terminated. As
well, each scheduling policy will share as much common code as possible. The CPU
scheduler will be written in C++.
3
2 Project Requirements
This project has to be written in C++ on either Linux or windows. The scheduler should
ask users’ input. The user will provide interactively:

Name of the text file that contains the workload (a list of the processes with all the data
as described in the next paragraph);
 What algorithm to use to generate the metrics and the Gantt chart;
 Any other necessary parameter for the particular algorithm that the user chooses (like
‘quantum time’, in the case of Round Robin algorithm).
The scheduler should read process information from a TXT file which contains a metrics of data.
Each process in the workload text file will be described by:






its ID code, an integer number;
its time of arrival to the ready queue, an integer number;
its priority code, an integer number;
its "age", an integer number; d) its total number of cpu-bursts;
a list of values corresponding to the durations of each of the cpu bursts;
a list of values corresponding to the durations of each of the io bursts. Each process
description may include any other attribute that you find convenient for your
implementation.
Each line will be of the form:
PID
TARQ
PRIO
TNCPU
CPUl I 0 1
CPU2 I02 C P U
5
3
2,
3
7
2
5
2
0
7
2
5
4
2
7
1
1
2
2
2
3
3
2
Where PID = process ID code, TARQ = time of arrival into the ready queue (the first time, right
when it was created only!), PRIO = priority code, TNCPU = total number of cpu bursts of the
process (the number of IO bursts will always be one less, of course), CPU1 = duration of the first
cpu burst, I01 = duration of the first io burst, CPU2 = duration of the second cpu bursts, etc.
The output should consist of: a Gantt chart of cpu utilization for your entire simulation. The
program should determine also, (a) for each project: the total waiting time and total execution
time, (b) for the entire set of projects: the average waiting time, the throughput, the
turnaround.
4
3 Program Flowchart Diagram
3.1 Main Scheduler Flowchart
START
Legend
Note: In the code, ReadyQ,
RunningQ, WaitingQ, and
TerminatedQ are named
the same. However, NewQ
is named ProcL
Blue Involves operating
upon processes within any
of the 5 queues : NewQ,
ReadyQ, RunningQ,
WaitingQ, TerminatedQ,
OR
Operating on related
variables
Get input from user
Populate NewQ
Start simulation
Note: While there is a
RunningQ, there should
never be more than one
process in this queue. This
is for easier bookkeeping
(and possible extension to
multicore systems)
Green involves moving a
process from one queue to
another queue
For all in NewQ, if time has
reached TARQ, then move
to ReadyQ
There was a
process running in
last cycle?
Yes Decrement
quantum
counter
Process just
completed a
CPU burst?
Reset
quantum
counter
Yes
No
There is IO
burst
remaining?
Yes
No
No
Move to
TerminatedQ
Move to
WaitingQ
Record
turnaround time
No
For each process in
WaitingQ, choose a
process. Is there a
process left?
Yes
Process just Yes
completed
an IO burst?
Decide and move a process
from WaitingQ to ReadyQ
depending on Scheduling
Policy
No
Decide and move a process
from ReadyQ to RunningQ
depending on Scheduling
Policy
See accompanying flowcharts
for details of the steps for the 7
scheduling policies
Age all processes in
ReadyQ
No
For each process in
WaitingQ, choose a
process. Is there a
process left?
Yes
Consume one unit of IO
work remaining
Process just
completed
an IO burst?
Yes
Move process from
WaitingQ to ReadyQ
No
Consume one unit of CPU
work from the process in
RunningQ
Is there at least one
process in any of
NewQ, ReadyQ,
RunningQ,
WaitingQ?
Output Gantt Chart.
END
Figure 1: Flowchart for the Main Loop of Scheduler which contains the Common Code of All Algorithms
5
3.2 Flowchart for FCFS and RR Schedules Subroutine
If the policy
input is 1 or
2
Update all
processes and
find the process
with smallest
arrival time
Move the first
come process
from procL to
readyQ
If the policy
input is 1 or
2
Round Robin
Calculate the
remaining after
time/
If rr equals
to 0 or not
Not 0
Aging
0
Time is up for
the current
process to run.
Put it back to
readyQ and move
the next in
readyQ to
runningQ
Figure 2: Flowchart for Round Robin and First-Come-First-Served Scheduling Policies
6
3.3 Flowchart for Priority-Based Schedules Subroutine
Priority
“Impatient”
Preemptive
Priority “Polite”
Preemptive
Is quantum
counter at
zero?
Yes
Does the preempting
process have a higher
priority than the currently
running process ?
Priority NonPreemptive
Is there a
process in
RunningQ?
Yes
No
No
No
Does the preempting
process have a higher
age than the currently
running process ?
Yes
Yes
Move the running process
in RunningQ to ReadyQ,
No
Move the preempting task
from ReadyQ to RunningQ
Reset the age of the
running task
return
Figure 3: Flowchart for the three Priority Based Scheduling Policies
7
3.4 Sub Flowchart for SJF and SBF
8
4 Member Tasks
4.1 Member Tasks for Programming
Tasks
Member
Completion
Makefile
Scott Hazlett
100%
Process Class
Scott Hazlett
100%
Scheduler Class
Scott Hazlett
100%
Obtain user input
Zheng Lu
100%
Parse the data
Zheng Lu
100%
FCFS algorithm
Zheng Lu
100%
Round Robin algorithm
Zheng Lu
100%
Priority Non-Preemptive algorithm
Scott Hazlett
100%
Priority “Polite” algorithm
Scott Hazlett
100%
Priority “Impatient” algorithm
Scott Hazlett
100%
SJF algorithm
Olivani Baisheka
?
SPB algorithm
Olivani Baisheka
?
Scheduler
main
loop Scott Hazlett
/ common code for all algorithms
Test cases
Scott Hazlett
Zheng Lu
Gant chart output
Olivani Baisheka
100%
50%
50%
?
9
4.2 Member Tasks for Report Writing
Task
Member
Completion
Introduction
Zheng Lu
100%
Project requirements
Zheng Lu
100%
Member Tasks
Zheng
Scott Hazlett
Obtain and parsing data
Zheng Lu
Lu 50%
50%
100%
A PCB per Process
Scheduler
main
/ common code Flowchart
loop Scott Hazlett
100%
FCFS and RR Flowchart
Zheng Lu
100%
Priority Flowchart
Scott Hazlett
100%
SFJ and SPB Flowchart
Olivani Baisheka
?
Scheduler
main
loop
Common code algorithm
/ Scott Hazlett
100%
FCFS and RR algorithms
Zheng Lu
100%
Priority-Based algorithms
Scott Hazlett
100%
SJF and SPB algorithms
Olivani Baisheka
?
Gantt Chart
Olivani Baisheka
?
Notable Features
?
?
Test cases
?
?
Conclusion
Zheng Lu
100%
10
5 Solution Description
5.1 Create new process class
5.2 Obtain user input
This program will obtain two or three input from the user depending on different policy
selected. Before the prompts for policy, we called a display function with only “cout” to
give user a instruction on values representing different policy. Then the program will ask
the user to input filename containing data, policy to use and quantum time if required.
It’s done by using the “cin>>” and “cout<<” function.
5.3 Parsing data
After we saved user’s input in filename, policy and quantum, we will open the txt file
which containing the matrices for loading our process. First we open the file with:
ifstream fout(filename);
Then we read the output buffer line by line in a while loop and read the line number by
number with a sub while loop using:
fout.getline(buf,100)
p = strtok(buf ," \n\t" );
The data will be saved in an array. After the entire loop exit, we starting to create
process using:
temp = new Process(j[1],j[2],j[3], temp_cpub.size(), &temp_iob, &temp_cpub);
procL.push_back( *temp );
The program will continue until it reaches the end of file.
11
5.4 Common algorithm code
5.5 FSFC and RR algorithm code
The common code will create a waiting queue and ready queue shifting rule. After the
user has selected FCFS or Round Robin as the policy, the program will search through all
the process list to find the process with smallest tanQ ( earliest to arrive) then push
such a process into the readyQ and continue to do this until process list is empty. The
sorting is done by the following code:
while ( rdyQI != readyQ.end() )
{if ( firstQ->tarq > rdyQI->tarq )
{ firstQ = rdyQI;}
rdyQI ++;}
5.5.1 FCFS algorithm
If the user selected first come first serve as the policy, our program will just BREAK the
switch case loop because the readyQ is already sorted to be earliest to latest order.
5.5.2 Round Robin algorithm
If the user selected Round Robin as the policy, our program will use a quantum counter
to count how many time unit the process had run. At the very beginning, we will set
quantum counter to equal to quantum time the user imported earlier. Every time unit
passed, the quantum counter will be decremented by one. Then every time a process is
moved from the runningQ into the waitingQ, quantum counter will be reset back to
quantum time. If the quantum counter equals to zero that means the CPU burst had
used all its time. Our program will then push the current process back to the last of
readyQ and move the first one on readyQ into runningQ. At the same time, quantum
counter will be reset to quantum time.
12
5.6 Priority algorithm code
5.7 SJF and SPB algorithm code
6 Gantt chart
7 Notable Features
8
Test cases
13
9
Conclusion
In this project, we designed and tested a CPU scheduler with seven different algorithms.
Our program obtained the user’s input and parsed the data file. Our program simulated
a CPU scheduler’s functionality and output a Gantt chart to show the results. This
project helped us learn how CPU scheduler works, as well as provide meaningful
comparison of the pros and cons of each scheduling policies. Our project is a success.
14
Download