Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding CPU Scheduling algorithms #1

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions CPU Scheduling/algorithms/FCFS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Short-Introduction
Given n processes with their burst times, the task is to find average waiting time and average turn around time using FCFS scheduling algorithm.
First in, first out (FIFO), also known as first come, first served (FCFS), is the simplest scheduling algorithm. FIFO simply queues processes in the order that they arrive in the ready queue.
In this, the process that comes first will be executed first and next process starts only after the previous gets fully executed.
(Here we are considering that arrival time for all processes is 0)

## Implementation
- Input the processes along with their burst time (bt).
- Find waiting time (wt) for all processes.
- As first process that comes need not to wait so
waiting time for process 1 will be 0 i.e. wt[0] = 0.
- Find waiting time for all other processes i.e. for
process i ->
wt[i] = bt[i-1] + wt[i-1] .
- Find turnaround time = waiting_time + burst_time
for all processes.
- Find average waiting time =
total_waiting_time / no_of_processes.
- Similarly, find average turnaround time =
total_turn_around_time / no_of_processes.
7 changes: 7 additions & 0 deletions CPU Scheduling/algorithms/SJF.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Introduction
Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next. SJN is a non-preemptive algorithm.

## Implementation
- Sort all the process according to the arrival time.
- Then select that process which has minimum arrival time and minimum Burst time.
- After completion of process make a pool of process which after till the completion of previous process and select that process among the pool which is having minimum Burst time.
14 changes: 14 additions & 0 deletions CPU Scheduling/algorithms/multilevel-feedback-queue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Introduction
This Scheduling is like Multilevel Queue(MLQ) Scheduling but in this process can move between the queues. Multilevel Feedback Queue Scheduling (MLFQ) keep analyzing the behavior (time of execution) of processes and according to which it changes its priority

## Implementation
- When a process starts executing then it first enters queue 1.
- In queue 1 process executes for 4 unit and if it completes in this 4 unit or it gives CPU for I/O operation in this 4 unit than the priority of this process does not change and if it again comes in the ready queue than it again starts its execution in Queue 1.
- If a process in queue 1 does not complete in 4 unit then its priority gets reduced and it shifted to queue 2.
- Above points 2 and 3 are also true for queue 2 processes but the time quantum is 8 unit.In a general case if a process does not complete in a time quantum than it is shifted to the lower priority queue.
- In the last queue, processes are scheduled in FCFS manner.
- A process in lower priority queue can only execute only when higher priority queues are empty.
- A process running in the lower priority queue is interrupted by a process arriving in the higher priority queue.

### Note:
I am assuming queue 1 and 2 follow round robin with time quantum 4 and 8 respectively and queue 3 follow FCFS. But one can use different algorithma for scheduling queue 1 and queue 2.
7 changes: 7 additions & 0 deletions CPU Scheduling/algorithms/multilevel-queue-scheduling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Introduction
It may happen that processes in the ready queue can be divided into different classes where each class has its own scheduling needs. For example, a common division is a foreground (interactive) process and background (batch) processes.These two classes have different scheduling needs. For this kind of situation Multilevel Queue Scheduling is used.Now, let us see how it works.
Ready Queue is divided into separate queues for each class of processes. For example, let us take three different types of process System processes, Interactive processes and Batch Processes. All three process have there own queue

All three different type of processes have there own queue. Each queue have its own Scheduling algorithm. For example, queue 1 and queue 2 uses Round Robin while queue 3 can use FCFS to schedule there processes

Note: One can use any scheduling algorithm for queue1, queue2 and queue2.
7 changes: 7 additions & 0 deletions CPU Scheduling/algorithms/priority-scheduling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Introduction
Priority scheduling is a non-preemptive algorithm and one of the most common scheduling algorithms in batch systems. Each process is assigned first arrival time (less arrival time process first) if two processes have same arrival time, then compare to priorities (highest process first). Also, if two processes have same priority then compare to process number (less process number first). This process is repeated while all process get executed.

## Implementation
- First input the processes with their arrival time, burst time and priority.
- Sort the processes, according to arrival time if two process arrival time is same then sort according process priority if two process priority are same then sort according to process number.
- Now simply apply FCFS algorithm.
30 changes: 30 additions & 0 deletions CPU Scheduling/algorithms/round-robin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## Introduction
Round Robin is a CPU scheduling algorithm where each process is assigned a fixed time slot in a cyclic way.
- It is simple, easy to implement, and starvation-free as all processes get fair share of CPU.
- One of the most commonly used technique in CPU scheduling as a core.
- It is preemptive as processes are assigned CPU only for a fixed slice of time at most.

Completion Time: Time at which process completes its execution.
Turn Around Time: Time Difference between completion time and arrival time. Turn Around Time = Completion Time – Arrival Time
Waiting Time(W.T): Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time

## Implementation
- Create an array rem_bt[] to keep track of remaining
burst time of processes. This array is initially a
copy of bt[] (burst times array)
- Create another array wt[] to store waiting times
of processes. Initialize this array as 0.
- Initialize time : t = 0
- Keep traversing the all processes while all processes
are not done.
Do following for i'th process if it is
not done yet.
- If rem_bt[i] > quantum
(i) t = t + quantum
(ii) bt_rem[i] -= quantum;
- Else // Last cycle for this process
(i) t = t + bt_rem[i];
(ii) wt[i] = t - bt[i]
(ii) bt_rem[i] = 0; // This process is over
- turn around time tat[i] of a process as sum of waiting and burst times, i.e., wt[i] + bt[i]
7 changes: 7 additions & 0 deletions CPU Scheduling/curious-cat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Task for curious cats
- Implement each scheduling algorithm in different programming language like Java, C, CPP, Python etc.
- There are some more scheduling algorithms other than these six, you can add thier introduction and implementation steps.
- Find difference between different scheduling algorithms
- Find time and space complexity of each algorithm.
- Think of more advance algorithm and try to implement it.
- Check which scheduling algorithm (/algorithms) your operating system use.
10 changes: 10 additions & 0 deletions CPU Scheduling/facts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Do you know
- FCFS can cause long waiting times, especially when the first job takes too much CPU time.

- Both SJF and Shortest Remaining time first algorithms may cause starvation. Consider a situation when the long process is there in the ready queue and shorter processes keep coming.

- If time quantum for Round Robin scheduling is very large, then it behaves same as FCFS scheduling.

- SJF is optimal in terms of average waiting time for a given set of processes,i.e., average waiting time is minimum with this scheduling, but problems are, how to know/predict the time of next job.
- SJF is a greedy algorithm
- The disadvantage of Round Robin is more overhead of context switching and large waiting time & Response time.
33 changes: 33 additions & 0 deletions CPU Scheduling/scheduling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# What is CPU Scheduling
CPU scheduling is a process which allows one process to use the CPU while the execution of another process is on hold(in waiting state) due to unavailability of any resource like I/O etc, thereby making full use of CPU. The aim of CPU scheduling is to make the system efficient, fast and fair.
Whenever the CPU becomes idle, the operating system must select one of the processes in the ready queue to be executed. The selection process is carried out by the short-term scheduler (or CPU scheduler). The scheduler selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them.

## Different Scheduling Algorithms
- First Come First Serve(FCFS) Scheduling
- Shortest-Job-First(SJF) Scheduling
- Priority Scheduling
- Round Robin(RR) Scheduling
- Multilevel Queue Scheduling
- Multilevel Feedback Queue Scheduling

### First Come First Serve (FCFS):
Simplest scheduling algorithm that schedules according to arrival times of processes. First come first serve scheduling algorithm states that the process that requests the CPU first is allocated the CPU first. It is implemented by using the FIFO queue. When a process enters the ready queue, its PCB is linked onto the tail of the queue. When the CPU is free, it is allocated to the process at the head of the queue. The running process is then removed from the queue. FCFS is a non-preemptive scheduling algorithm.

### Shortest Job First (SJF):
Process which have the shortest burst time are scheduled first.If two processes have the same bust time then FCFS is used to break the tie. It is a non-preemptive scheduling algorithm.

### Priority Based scheduling (Non-Preemptive):
In this scheduling, processes are scheduled according to their priorities, i.e., highest priority process is scheduled first. If priorities of two processes match, then schedule according to arrival time. Here starvation of process is possible.

### Round Robin Scheduling:
Each process is assigned a fixed time(Time Quantum/Time Slice) in cyclic way.It is designed especially for the time-sharing system. The ready queue is treated as a circular queue. The CPU scheduler goes around the ready queue, allocating the CPU to each process for a time interval of up to 1-time quantum. To implement Round Robin scheduling, we keep the ready queue as a FIFO queue of processes. New processes are added to the tail of the ready queue. The CPU scheduler picks the first process from the ready queue, sets a timer to interrupt after 1-time quantum, and dispatches the process. One of two things will then happen. The process may have a CPU burst of less than 1-time quantum. In this case, the process itself will release the CPU voluntarily. The scheduler will then proceed to the next process in the ready queue. Otherwise, if the CPU burst of the currently running process is longer than 1-time quantum, the timer will go off and will cause an interrupt to the operating system. A context switch will be executed, and the process will be put at the tail of the ready queue. The CPU scheduler will then select the next process in the ready queue.

### Multilevel Queue Scheduling:
According to the priority of process, processes are placed in the different queues. Generally high priority process are placed in the top level queue. Only after completion of processes from top level queue, lower level queued processes are scheduled. It can suffer from starvation.

### Multi level Feedback Queue Scheduling:
It allows the process to move in between queues. The idea is to separate processes according to the characteristics of their CPU bursts. If a process uses too much CPU time, it is moved to a lower-priority queue.

#### Note:
- For pseudocode/algorithm for each scheduling algorithm please go to algorithms directory.
- Don't forget to check facts.md for some useful facts and curious-cat.md for exercises and challenges.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ An operating system, or "OS," is [software](https://techterms.com/definition/sof

### Background

Understand the concepts behind the working principles of any operating system, such as concurrency, threads vs processes, virtual memory, memory leak, etc.
Understand the concepts behind the working principles of any operating system, such as concurrency, threads vs processes, virtual memory, memory leak,CPU scheduling etc.