0% found this document useful (0 votes)
8 views

Cpu Scheduling

CPU SCHEDULING

Uploaded by

Zurghuna Gul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Cpu Scheduling

CPU SCHEDULING

Uploaded by

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

SESSION:2023-

SCXVCVSS
2024 SE

SUBJECT: COMPUTER
SCIENCE
CPU SCHEDULING
SUBMITTED BY
 Scheduling of processes/work is done to finish the work on time.
 CPU Scheduling is a process that allows one process to use the CPU while another process
is delayed (in standby) due to unavailability of any resources such as I / O etc, thus making
full use of the CPU. LAB:8
 The purpose of CPU Scheduling is to make the system more efficient,
faster, and fairer.
 SUBMISSION DATE: 25th nov
 CPU scheduling is a key part of how an operating system works.
 COURSE:OPERATING
 It decides which task SYSTEM
(or process) the CPU should work on at any given time.
 This is important because a CPU can only handle one task at a time, but there are usually
many tasks that need to be processed. In this article, we are going to discuss CPU
scheduling in detail.
 Whenever the CPU becomes idle, the operating system must select one of the processes in
the line ready for launch. The selection process is done by a temporary (CPU) scheduler.
 The Scheduler selects between memory processes ready to launch and assigns the CPU to
one of them.

TYPES OF CPU SCHEDULING

There are mainly two types of scheduling methods:


 PREEMPTIVE SCHEDULING: Preemptive scheduling is used when a process switches

from running state to ready state or from the waiting state to the ready state.

 NON-PREEMPTIVE SCHEDULING: Non-Preemptive scheduling is used when a

process terminates , or when a process switches from running state to waiting state.

SCHEDULING ALGORITHMS

The technique through which we do scheduling is called scheduling algorithm

There are four types of cpu scheduling

 First-Come First-Serve Scheduling, FCFS


 . Shortest Job First(SJF)
 Priority Scheduling
 Round Robin scheduling

ROUND ROBIN SCHEDULING ALGORITHM


 This algorithm is very special because it is going to remove all the Flaws which
we have detected in the previous CPU Process Scheduling Algorithms.

 There is a lot of popularity for this Round Robin CPU Scheduling is because
Round Robin works only in Pre Emptive state. This makes it very reliable.

IMPORTANT ABBREVIATIONS
1. CPU - - - > Central Processing Unit
2. AT - - - > Arrival Time
3. BT - - - > Burst Time
4. WT - - - > Waiting Time
5. TAT - - - > Turn Around Time
6. CT - - - > Completion Time
7. FIFO - - - > First In First Out
8. TQ - - - > Time Quantum

ROUND ROBIN CPU SCHEDULING


 Round Robin CPU Scheduling is the most important CPU Scheduling Algorithm which is
ever used in the history of CPU Scheduling Algorithms.

 Round Robin CPU Scheduling uses Time Quantum (TQ). The Time Quantum is something
which is removed from the Burst Time and lets the chunk of process to be completed.

 Time Sharing is the main emphasis of the algorithm.

 Each step of this algorithm is carried out cyclically. The system defines a specific time slice,
known as a time quantum.

working
 First, the processes which are eligible to enter the ready queue enter the ready queue.
 After entering the first process in Ready Queue is executed for a Time Quantum chunk of
time. After execution is complete, the process is removed from the ready queue.

 Even now the process requires some time to complete its execution, then the process is added
to Ready Queue.

 The Ready Queue does not hold processes which already present in the Ready Queue. The
Ready Queue is designed in such a manner that it does not hold non unique processes.

 By holding same processes Redundancy of the processes increases.

 After, the process execution is complete, the Ready Queue does not take the completed
process for holding.

ADVANTAGES
The Advantages of Round Robin CPU Scheduling are:

1. A fair amount of CPU is allocated to each job.


2. Because it doesn't depend on the burst time, it can truly be implemented in the system.
3. It is not affected by the convoy effect or the starvation problem as occurred in First Come
First Serve CPU Scheduling Algorithm.
DISADVANTAGES
The Disadvantages of Round Robin CPU Scheduling are:

1. Low Operating System slicing times will result in decreased CPU output.
2. Round Robin CPU Scheduling approach takes longer to swap contexts.
3. Time quantum has a significant impact on its performance.
4. The procedures cannot have priorities established.

EXAMPLE:

S. No Process ID Arrival Time Burst Time

1 P1 0 7
2 P2 1 4
3 P3 2 15
4 P4 3 11
5 P5 4 20
6 P6 4 9

Assume Time Quantum TQ = 5

Ready Queue:

1. P1, P2, P3, P4, P5, P6, P1, P3, P4, P5, P6, P3, P4, P5

Gantt chart:
AVERAGE COMPLETION TIME

1. Average Completion Time = ( 31 +9 + 55 +56 +66 + 50 ) / 6


2. Average Completion Time = 267 / 6
3. Average Completion Time = 44.5
AVERAGE WAITING TIME

1. Average Waiting Time = ( 5 + 26 + 5 + 42 + 42 + 37 ) / 6


2. Average Waiting Time = 157 / 6
3. Average Waiting Time = 26.16667
AVERAGE TURN AROUND TIME

1. Average Turn Around Time = ( 31 + 8 + 53 + 53 + 62 + 46 ) / 6


2. Average Turn Around Time = 253 / 6
3. Average Turn Around Time = 42.16667

CODE
import java.util.*;

// Define the Process class


class Process {
int id;
int burstTime;
int remainingTime;

// Constructor to initialize process with burst time and id


Process(int id, int burstTime) {
this.id = id;
this.burstTime = burstTime;
this.remainingTime = burstTime; // initialize remaining time to burst time
}

// Getter methods to access private variables


public int getId() {
return id;
}

public int getBurstTime() {


return burstTime;
}

public int getRemainingTime() {


return remainingTime;
}

// Setter methods if you want to update the variables


public void setRemainingTime(int remainingTime) {
this.remainingTime = remainingTime;
}
}

// RoundRobin class to manage scheduling


class RoundRobinScheduler {
private List<Process> processList = new ArrayList<>();
private int timeQuantum;

// Constructor to initialize the time quantum


public RoundRobinScheduler(int timeQuantum) {
this.timeQuantum = timeQuantum;
}

// Method to add processes to the list


public void addProcess(int burstTime) {
int id = processList.size() + 1;
processList.add(new Process(id, burstTime));
}

// Method to perform Round Robin scheduling


public void schedule() {
Queue<Process> queue = new LinkedList<>();

// Add all processes to the queue


for (Process process : processList) {
queue.add(process);
}

int currentTime = 0; // Track the current time


while (!queue.isEmpty()) {
Process currentProcess = queue.poll(); // Get the next process in the queue

// Execute process for up to the time quantum or until finished


int timeToRun = Math.min(currentProcess.getRemainingTime(), timeQuantum);
currentProcess.setRemainingTime(currentProcess.getRemainingTime() - timeToRun); //
Update remaining time
currentTime += timeToRun; // Update the current time
System.out.println("Process " + currentProcess.getId() + " ran for " + timeToRun + "
units. Current Time: " + currentTime);

// If process isn't finished, add it back to the queue


if (currentProcess.getRemainingTime() > 0) {
queue.add(currentProcess);
} else {
System.out.println("Process " + currentProcess.getId() + " finished at time " +
currentTime);
}
}
}
}

// Main class to test the Round Robin scheduling


public class Main {
public static void main(String[] args) {
// Create a scanner to get user input
Scanner scanner = new Scanner(System.in);

// Ask user to define the time quantum


System.out.print("Enter the time quantum: ");
int timeQuantum = scanner.nextInt();

// Create a Round Robin scheduler with the user-defined time quantum


RoundRobinScheduler rr = new RoundRobinScheduler(timeQuantum);

// Ask user how many processes they want to add


System.out.print("Enter the number of processes: ");
int numProcesses = scanner.nextInt();
// Get burst times for each process
for (int i = 1; i <= numProcesses; i++) {
System.out.print("Enter the burst time for Process " + i + ": ");
int burstTime = scanner.nextInt();
rr.addProcess(burstTime);
}

// Start scheduling
rr.schedule();

// Close the scanner


scanner.close();
}
}

You might also like