report_3

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)
Table of Contents
Table of Contents ..................................................................................................................ii
Introduction ..........................................................................................................................1
1. Project Requirements……………………………………………………………………………………………………2
2. Program Flow Chart.........................................................................................................3
2.1.
Main Scheduler Flow Chart………………………………………………………………………………3
2.2.
Sub Flow Chart for FCFS and RR………………………………………………………………………..4
2.3.
Sub Flow Chart for Priority……………………………………………………………………..5
2.4.
Sub Flow Chart for SJF and SPB…………………………………………………………….6
3. Member Tasks…………………………………………………………………………………………………7
3.1.
Member Tasks for programming………………………………………………………….7
3.2.
Member Tasks for report writing…………………………………………………………8
4. Solution Description
4.1.
Create new process class
4.2.
Obtain user input
4.3.
Parsing data
4.4.
Common algorithm code
4.5.
FCFS and RR algorithm
4.5.1.
FCFS algorithm
4.5.2.
Round Robin algorithm
4.6.
Priority algorithm
4.7.
SJF and SPB algorithm
4.8.
Gantt chart
5. Notable Features
6. Test cases
7. Conclusion
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 efficiency of CPU. In order to save time
and memory, we will use a waiting queue, a really queue and a running queue to
switch between tasks. The CPU scheduler will be written in C++.
1. 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
5
3
2,
2
0
7
1
TNCPU
CPUl
I01
CPU2
I02
CPU
3
7
2
5
3
2
7
2
5
4
2
1
2
2
2
3
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.
2. Program Flowchart Diagram
2.1. Main Scheduler Flowchart
2.2. Sub Flowchart for FCFS and RR
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
2.3. Sub Flowchart for Priority
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
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
Yes
2.4. Sub Flowchart for SJF and SBF
3 .Member Tasks
3.1 Member Tasks for Programming
Tasks
Makefile
Process.cpp
Obtain user input
Parse the data
Scheduler simulator
FCFS algorithm
Round Robin algorithm
Priority algorithm
Polite algorithm
Impatient algorithm
SJF algorithm
SPB algorithm
Common algorithm code
Test cases
Gant chart output
Member
Completion
3.2 Member Tasks for Report Writing
Task
Introduction
Project requirements
Main Flowchart
New process class
Obtain and parsing data
Common algorithm code
FCFS and RR Flowchart
Priority Flowchart
SFJ and SPB Flowchart
Member Tasks
FCFS and RR algorithm
Priority algorithm
SJF and SPB algorithm
Gantt Chart
Notable Features
Test cases
Conclusion
Member
Completion
4. Solution Description
4.1. Create new process class
4.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.
4.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.
4.4. Common algorithm code
4.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 ++;}
4.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.
4.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.
4.6. Priority algorithm code
4.7. SJF and SPB algorithm code
4.8. Gantt chart
5. Notable Features
6. Test cases
7. 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. We have completed the project requirements with high efficiency
scheduler. This project helped us learn how CPU scheduler works. Our project is a
success.
Download