towson university department of computer and information

19
Page 1 of 19 Towson University Department of Computer and Information Sciences A Group Research Paper For Dr. Ramesh Karne COSC 519: Operating System Principles Fall 2018 CPU SCHEDULING SIMULATION Group (5) members: ALSHENAIFI, REEM CHODAVARAM, VIJAYASHREE KONERU, MANOJA PALAMAKULA, SAI NITESH RIVERA, MARIA

Upload: others

Post on 19-Apr-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Towson University Department of Computer and Information

Page 1 of 19

Towson University

Department of Computer and Information Sciences

A Group Research Paper For

Dr. Ramesh Karne

COSC 519: Operating System Principles

Fall 2018

CPU SCHEDULING SIMULATION

Group (5) members: ALSHENAIFI, REEM

CHODAVARAM, VIJAYASHREE KONERU, MANOJA

PALAMAKULA, SAI NITESH

RIVERA, MARIA

Page 2: Towson University Department of Computer and Information

Page 2 of 19

Table of Contents 1. INTRODUCTION TO THE PROJECT ......................................................................................................... 3

1.1 PURPOSE OF THE PROJECT: ........................................................................................................................................ 3 1.2 INTRODUCTION TO CPU SCHEDULING: ........................................................................................................................ 3 1.3 ATTRIBUTES OF PROCESSES: ............................................................................................................................... 4 1.4 MEASURABLES: ...................................................................................................................................................... 4 1.5 LIMITATIONS OF THE PROJECT: ................................................................................................................................... 5

2.SCHEDULING ALGORITHMS USED (FIFO, RR, SJF): ............................................................................. 6

2.1 FIRST-COME, FIRST-SERVED SCHEDULING .................................................................................................................... 6 2.2 SHORTEST-JOB-FIRST SCHEDULING ............................................................................................................................. 6

2.2.1 Non-Pre-emptive Shortest Job First ........................................................................................................... 7 2.3 ROUND-ROBIN SCHEDULING ..................................................................................................................................... 7

3.IMPLEMENTATION: ..................................................................................................................................... 9

3.1 TECHNICAL DETAILS ................................................................................................................................................. 9 3.2 DEVELOPMENT NOTES: ............................................................................................................................................. 9

4. STUDY OF THE RESULTS: ........................................................................................................................ 11

4.1SIMULATOR OUTPUT .............................................................................................................................................. 11 4.1.1 FIFO Algorithm Result Set: ...................................................................................................................... 11 4.1.2 SJF Algorithm Result Set: ........................................................................................................................ 12 4.1.3RR Algorithm Result Set: ........................................................................................................................... 12

4.2 ANALYSIS ............................................................................................................................................................. 14 4.2.1 Analysis of First In First Out (FIFO) ....................................................................................................... 14 4.2.2 Analysis of Shortest Job First (SJF) ......................................................................................................... 15 4.2.3 Analysis of Round Robin (RR) .................................................................................................................. 16 4.2.4 Analysis summary of the 3 algorithms ...................................................................................................... 17

5. CONCLUSION: ............................................................................................................................................ 18

6. BIBLIOGRAPHY: ........................................................................................................................................ 19

6.1 RESEARCH PAPERS: ................................................................................................................................................ 19 6.2 BOOKS: ............................................................................................................................................................... 19 6.3 WEBSITES: ........................................................................................................................................................... 19

Page 3: Towson University Department of Computer and Information

Page 3 of 19

1. Introduction to the project

1.1 Purpose of the Project: The purpose of this project is to understand how CPU scheduling works in an Operating

System. In addition, we will analyze various scheduling algorithms that determine the sequence of

execution by CPU. The simulator shall support three different process scheduling algorithms,

which are First in First out (FIFO), Round Robin, and Shortest Job First (SJF). We

will also compare between the efficiency of each algorithm by measuring the Burst time, waiting

time, and turn-around time.

1.2 Introduction to CPU Scheduling: The Operating System’s principal responsibility is in controlling the execution of

processes. This includes the determination of sequence for execution along with the allocation of

resources to the process. Whenever a program executes, it requires a must resource which is the

CPU. Then the CPU will be utilized by this process (CPU burst time), however, the process does

not utilize the CPU 100% of the time because a process during execution requires I/O operations

(I/O burst time) leaving the CPU idle. So, to overcome wasting of CPU time, scheduling is

performed. In multi-programming environment whenever the CPU is in idle state, the

implementation of scheduling algorithms will be assigned to another process that is loaded already

in the main memory and waiting to be executed. As there are probably many processes waiting in

the ready queue, an efficient scheduling algorithm is needed to control the order in which they will

be executed. The main goal of scheduling is to maximize the utilization of CPU and keep it busy

as much as possible, minimize the waiting time of each process and finish all jobs in less time

possible[1]. Scheduling requires two main parts, the scheduler and the dispatcher. The scheduler

is a software which decides which process will be executed next and allocate the CPU. The

scheduling decision is needed whenever the following states is encountered:

1. Switching from running to waiting state.

2. Switching from running to ready state.

3. Switching from waiting to ready state.

4. Terminates.

CPU scheduling algorithms are divided into two basic types:

Page 4: Towson University Department of Computer and Information

Page 4 of 19

1. Preemptive: it is a method where a running process is temporarily suspended so that the

CPU can be assigned to another ready to be executed process, for e.g. Round Robin,

preemptive SJF.

2. Non-preemptive: it is a running to completion method where the implemented scheduling

algorithm chooses a process to execute and let that process runs until it is done executing

or it’s moving to another state such as from running to waiting state as it waits for some

I/o, for e.g. FCFS, non-preemptive SJF.

1.3 Attributes of Processes: Process ID: a unique value for each process.

When a process is loaded into the CPU, the system will automatically assign a unique ID to each

process. The ID is incremented by 1 every time a new process is created.

Wait Time: The duration of time the process waits to utilize CPU is called wait time. It can be

calculated by the difference of turn-around time and burst time.

Burst Time: The duration of time where the process gets to utilize the CPU is called burst time.

Turn Around Time: The turn-around time the sum of waiting time of the process to get into the

ready queue, execution on CPU and executing input/output.

Arrival Time: It can be given as the time at which the process arrives in the ready queue.

1.4 Measurables: First In First Out:

Arrival time of the process

Burst Time

Total Waiting Time

Total Around Time

Round Robin:

Wait Time

Burst Time

Total Wait time

Average Wait Time

Shortest Job First:

Wait Time

Page 5: Towson University Department of Computer and Information

Page 5 of 19

Burst Time

Arrival time

Average Waiting Time

1.5 Limitations of the Project: 1. The project deals with only Non-Preemptive Shortest Job First Scheduling algorithm and

doesn’t deal with the preemptive SJF scheduling algorithms, where tasks are usually

assigned based on priority.

2. The project deals with only three CPU scheduling algorithms i.e., First in First Out

(FIFO), Shortest Job First (SJF), Round Robin (RR). The other three CPU scheduling

algorithms , which are Priority Scheduling, Multilevel Queue Scheduling, Multilevel

Feedback Queue Scheduling, will not be included in the study.

3. The Simulation doesn’t deal with the measurables like Load Average: Average number of

processes residing in the waiting queue.

4. This project doesn’t deal with Long term Scheduler where the processes are selected

from job pool rather than the ready queue.

Page 6: Towson University Department of Computer and Information

Page 6 of 19

2.Scheduling algorithms used (FIFO, RR, SJF): CPU scheduling works by deciding which processes in the ready queue should be allocated

to the CPU. The algorithms First-come, first serve, Shortest job first, Round Robin are described in this section.

2.1 First-Come, First-Served Scheduling First-come, first-served(FCFS) scheduling algorithm is the simplest CPU scheduling

algorithm. This algorithm works by allocating the CPU to the process that requests the CPU first. The implementation of the FCFS is done using a FIFO queue. The disadvantage is the average waiting time is more. Consider the following set of processes with arrive time 0, and the length of the CPU burst given in milliseconds:

Process Burst Time

P1 24

P2 3

P3 3

If the processes come in the order P1, P2, P3, we get the result shown in the following Gantt chart, which is a bar chart that contains the start and finish times of each of the participating processes

Source: Silberschatz et al.

The waiting time for P1 is 0 milliseconds for the process, P2 is 24 milliseconds, P3 27 (24+3) milliseconds. Thus, the average waiting time is (0 + 24 + 27)/3 = 17 milliseconds.

If the processes arrive in the order P2, P3, P1, The Gantt chart is represented as

Source: Silberschatz et al.

The average waiting time is (6 + 0 + 3)/3 = 3 milliseconds. Thus, the average waiting time changes if the processes’ CPU burst times vary greatly. FCFS is non-preemptive algorithm.

2.2 Shortest-Job-First Scheduling In this algorithm scheduling depends on the length of the next CPU burst of a process, rather than its total length. When the CPU is available, it is allocated to the process that has the smallest next CPU burst. If the next CPU bursts of two processes are the same, FCFS scheduling is used.

Page 7: Towson University Department of Computer and Information

Page 7 of 19

2.2.1 Non-Pre-emptive Shortest Job First consider the following set of processes p1, p2, p3, p4 and their CPU burst given in milliseconds with arrival time as 0 for all the processes.

Process Burst Time

P1 6

P2 8

P3 7

P4 3

Source: Silberschatz et al.

Here out of all process, p4 is having less Burst time so it is allocated first with waiting time 0. Next, out of p1, p2, p3 p1 has less burst time and selected with waiting time (0+3). Then p3 is selected with waiting time (3+6=9). At last p2 is selected with waiting time (9+7=16). Thus, the average waiting time is (3 + 16 + 9 + 0)/4 = 7 milliseconds.

2.3 Round-Robin Scheduling Round Robin is a preemptive scheduling algorithm. It is mainly designed for timesharing systems. A small unit of time, called a time quantum or time slice, with length 10- 100 milliseconds is defined. When the process arrives, they are placed in the circular queue. The CPU scheduler selects the process from the queue and allocates the CPU to each process for a time interval of one quantum. When the new process arrives, it is added to the tail of the queue. The CPU scheduler selects the first process from the queue, sets a timer to interrupt after one quantum, and dispatches the process. Then CPU scheduler assigns the CPU to the next process in the ready queue. After the end of the quantum if the process is not finished, the CPU is preempted, and the process is added to the tail of the queue. If the process is completed before the end of the quantum, the process releases the CPU voluntarily.

If there are n processes in the ready queue and the time quantum is q, then each process is allocated 1/n of the CPU time in chunks of at most q time units. Each process should wait no longer than (n − 1) × q time units until its next time quantum. The performance of the RR algorithm mainly based on the size of the time quantum.

Consider the following set of processes p1, p2, p3 that arrive at time 0, with the length of the CPU burst given in milliseconds and time quantum of 4 milliseconds.

Process Burst Time

Page 8: Towson University Department of Computer and Information

Page 8 of 19

P1 24

P2 3

P3 3

First, p1 gets selected with 4 milliseconds. After time quantum is finished since it requires another 20 milliseconds, it is preempted and added to the tail of the queue. CPU is given to the next process in the queue, process P2. Process P2 completes their execution before time quantum and releases the CPU. The CPU is then given to the next process, process P3. After each process done with their 1-time quantum, the CPU is given to the process P1 for an additional time quantum. The resulting RR schedule is as follows:

Source: Silberschatz et al.

The average waiting time is calculated as follows: P1 waits for 6 milliseconds (10 - 4), P2 waits for 4 milliseconds, and P3 waits for 7 milliseconds. Thus, the average waiting time is 17/3 = 5.66 milliseconds.

n the RR scheduling algorithm, no process is allocated the CPU for more than 1-time quantum in a row (unless it is the only runnable process). It is more like FCFS scheduling, but preemption is added to allow the system to switch between processes.

Page 9: Towson University Department of Computer and Information

Page 9 of 19

3. Implementation:

3.1 Technical Details Language: Java Platform: Linux (open source O/S) 3.2 Development:

The different algorithms which includes the SJF, FIFO and the Round Robin are

implemented using Java as the programming language. For the purpose of the implementation

we have used the attributes which are specified in the attributes of the process. The number of

the processes which are generated are maintained in the form of the queue with the help of the

Array as the data structure. The waiting time for each of the process is calculated and the burst

time is also maintained using the array. For every random set of the processes the average

waiting time is calculated, and it is displayed. We can observe that the number of the processes

execute continuously until it is made to terminate.

Every process is assigned with the unique process id. The random number of the

processes are generated where every process has the burst time. The waiting time is calculated

in the different algorithms which include the SJF, FIFO and the Round Robin as discussed in

section 2. For this random number of the processes the average waiting is calculated and is

displayed. This process of the generating the processes and calculating the average waiting time

for the set of the processes is continuously performed and it is terminated only when the

termination is done by the user.

We have implemented each algorithm so that it continues executing until the user

interrupts the execution of the program or it runs out of memory. The processes will be

automatically generated, each time a new process got generated, it will be assigned a unique

number (PID). The PID is incremented by 1 every time a new process is created. The burst

time of each process is a random number between 1 - 10. Also, the arrival time is a random

number. The first process to get created will have an arrival time between 0-3. Then after that

the arrival time will be as follows: Arrival = a.get(a.size()-1) + random_arrival_time.

* where random_arrival_time is a random number between 0 -3, and a.size()-1 is the arrival

time of the previous process.

Page 10: Towson University Department of Computer and Information

Page 10 of 19

The Output of SJF Algorithm:

Page 11: Towson University Department of Computer and Information

Page 11 of 19

4. Study of the results:

4.1 Simulator Output

4.1.1 FIFO Algorithm Result Set:

Explanation: PID Burst Time Arrival Time

0 8 3

1 5 3

2 3 6

3 2 7

4 3 8

5 8 10

■ The Gantt Chart for the schedule is:

0 3 11 16 19 21 24 32

■ Waiting time for P0 = 0; P1 = 8; P2= 10; P3= 12; P4= 13; P5= 14

■ Average waiting time: (0 + 8 + 10+12+13+14)/6 = 9.5

P0 P1 P2 P3 P4 P5

Page 12: Towson University Department of Computer and Information

Page 12 of 19

4.1.2 SJF Algorithm Result Set:

Explanation: PID Burst Time Arrival Time

0 8 3

1 4 6

2 1 8

3 4 10

9 3 14

10 2 17

■ The Gantt Chart for the schedule is:

0 3 11 12 16 19 21 25

■ Waiting time for P0 = 0; P1 = 6; P2= 3; P3= 11; P9= 2; P10= 2

■ Average waiting time: (0 + 6 + 3+11+2+2)/6 = 4

4.1.3 RR Algorithm Result Set:

P0 P2 P1 P3 P9 P10

Page 13: Towson University Department of Computer and Information

Page 13 of 19

Explanation: PID Burst Time Arrival Time

0 3 0

1 4 0

2 9 0

3 6 4

4 5 10

■ The Gantt Chart for the schedule is:

0 3 7 11 15 19 23 25 26 27

■ Waiting time for P0 = 0; P1 = 3; P2= 18; P3= 15; P4= 11;

■ Average waiting time: (0 + 3 + 18+15+11)/5 = 9

P0 P1 P2 P3 P4 P2 P3 P4 P2

Page 14: Towson University Department of Computer and Information

Page 14 of 19

4.2 Analysis For the study of each scheduling algorithms, a sample of 400 processes completed was

taken, and the correlation between the wait time and burst time, and between wait time and arrival

time was analyzed for each one. A comparison of the average waiting time for FIFO, SJF and RR

was graphical represented, in order to make conclusions about the performance and determine the

advantages and disadvantages of the three different approaches.

4.2.1 Analysis of First In First Out (FIFO)

First In First Out scheduling algorithm assigns the process to the CPU in the order they

arrive to the ready queue. When a process arrives, its PCB is linked onto the end of the queue. The

average waiting time of FIFO is known to be long, and for this reason not a very efficient

algorithm, what is support by this simulation results. (Abhijit A. Rajguru, July 2013). Figure 2

shows how wait time is dependent of arrival time, as it increases linearly when the arrival time

increment too. By the other hand, in Figure 1 the wait time can vary in a range between 0 to 1600

units of time, for all possible burst times.

One of the main disadvantages of FIFO is the Convoy effect, that is when a short process is behind

long process. FIFO exhibit an acceptable performance for long jobs but small number of processes

in the ready queue. (Lalit Kishor, April 2013)

Figure 1.

Page 15: Towson University Department of Computer and Information

Page 15 of 19

Figure 2.

4.2.2 Analysis of Shortest Job First (SJF)

The SJF algorithm implemented in this simulation is non-preempted, this means once the

process is in the CPU, is going to run until completion, even though the arriving process has a

shorter burst time. The process in the queue with the smallest burst time have priority and is going

to run first. The results of the simulation show that SJF provide the minimum average waiting time

as it can been seen in table number__ , the results are similar to others performance analysis of

scheduling algorithms like the one made by (Abhijit A. Rajguru, July 2013), where SJF exhibited

the higher performance.

One of the main limitations of SJF is that the processes with the largest burst times may never run,

what is known as starvation. We can see in Figure 3 that the biggest size of any finished processes

is 6, what means that processes with higher values (7 to 10) are still waiting in the queue for CPU

access, and as more process arrive with possible small burst times, a so higher priority, the ones

with big burst times are going to starve and never be executed. One possible solution for this issue

is known as “Aging”, whereas time progresses the priority of the process increase, this

characteristic was not implemented in this simulation.

In Figure 3 we can also see a clear tendency where the wait time increase as it does the burst time.

For example, the maximum waiting time for process with size 1 is at most 5 units of time, for burst

time 4 is 38 units of time and for burst time 6, the maximum waiting time registered was 250 units

of time.

Page 16: Towson University Department of Computer and Information

Page 16 of 19

Figure 3

Figure 4 show that for SJF there is no correlation between the arrival time and the process

waiting time as expected. Because for this algorithm the priority of execution is completely

independent of the arrival time.

Figure 4

4.2.3 Analysis of Round Robin (RR)

In Round robin algorithm a fixed time slot is predefined and is called quantum. If size of

the process being executed is greater than the given quantum, the process is going to be preempted

from the CPU, is going to be placed at the end of the queue, and the next process in the ready

queue is going to start execution according to the arrival time. (Lalit Kishor, April 2013). It is

evident again in Figure 5 that in RR the wait time can vary in the same range for different burst

Page 17: Towson University Department of Computer and Information

Page 17 of 19

times, as it happened with FIFO, also Figure 6 shows how wait time increase linearly with the

arrival time.

Figure 5

Figure 6

4.2.4 Analysis summary of the 3 algorithms

In the Figure 7 is represented the variations of the average wait time (AVWT) as the

number of processes completed increase for each of the 3 algorithms, and it can be determined that

there is a significant difference in the performance between them. For the 3 approaches the average

waiting time increase with the number of processes completed, but at very different rates, while

for SJF after 400 processes the AVWT is just 20 units of time, for RR and FIFO is 720 and 780

units of time respectively. Although RR and FIFO exhibited a similar tendency, is evident that at

Page 18: Towson University Department of Computer and Information

Page 18 of 19

some point at 150 process approximately, a divergence starts between both, and the AVWT

increase faster in FIFO than in RR.

Figure 7.

The performance of FIFO is arguably the worst as the average wait time increase faster than it

does in RR and SJF. By the other hand SJF has considerably the best performance, although as

discussed previously, processes with high burst time are going to suffer from starvation. Round

Robin reduces the penalty that short processes suffer with FIFO, giving all processes a “fair”

treatment, preempting the CPU after each quantum end, and also avoid the starvation that long

processes suffer with SJF. (Lalit Kishor, April 2013).

5. Conclusion:

CPU scheduling is an important part of any operating system. This project helped us to

comprehend the CPU process scheduling of the Operating System, have a better understanding for

wait time, burst time and turn-around time terminologies and analyzing how they vary depending

on the scheduling algorithms, gain a solid knowledge in simulating a real-world process

management module and to improve out project design skill, strongly our coding skill.

Page 19: Towson University Department of Computer and Information

Page 19 of 19

6. Bibliography:

6.1 Research papers: 1. Kishor, L., & Goyal, D. (2013). Comparative analysis of various scheduling

algorithms. International Journal of Advanced Research in Computer Engineering &

Technology (IJARCET), 2(4), pp-1488.

2. Abhijit A. Rajguru, S. A. (July 2013). A Performance Analysis of Task Scheduling Algorithms

using Qualitative Parameters. International Journal of Computer Applications, 33-38.

6.2 Books: 3. Silberschatz, A., Galvin, P., and Gagne, G., "Operating System Concepts," (Sixth Edition), Wiley

& Sons, 2002.

6.3 Websites: 4. https://www.studytonight.com/operating-system/shortest-job-first

5. http://orion.towson.edu/~karne/teaching/projos/procmgmt2.pdf

6. https://www.geeksforgeeks.org/gate-notes-operating-system-process-scheduling/

7. https://en.wikipedia.org/wiki/Process_management_(computing)

8. https://www.tutorialspoint.com/operating_system/os_process_scheduling.htm