OS Complete
OS Complete
The operating system (OS) manages all of the software and hardware on the computer. It
performs basic tasks such as file, memory and process management, handling input and output,
and controlling peripheral devices such as disk drives and printers.
Most of the time, there are several different computer programs running at the same time, and
they all need to access your computer’s central processing unit (CPU), memory and storage. The
operating system coordinates all of this to make sure each program gets what it needs.
The operating system also manages the start-up and shut-down of the computer, as well as the
management of tasks and processes running on the computer.
Overall, the operating system acts as the central controller of a computer, managing its resources
and communication, and providing a user interface for interacting with the computer. It is
responsible for the overall functioning and stability of a computer and its software applications.
Operating Systems Works As :
1. Interface: Operating systems provide a user-friendly interface to interact with the computer,
such as a graphical user interface (GUI) or command-line interface (CLI).
2. Communication: Operating systems facilitate communication between different devices and
software programs, such as networking and file sharing.
3. Resource management: Operating systems manage and allocate resources such as memory,
processing power, and storage to different programs and processes.
4. Process management: Operating systems manage and control the execution of different
processes, such as multitasking and scheduling.
5. Security: Operating systems provide security features, such as user authentication and
access control, to protect the system from unauthorized access and malware.
6. File management: Operating systems manage and organize files and folders on a computer
or device, allowing users to easily find and access them.
7. Hardware management: Operating systems manage and control the interactions between
software and hardware, such as device drivers and input/output operations.
1. Multitasking :An operating system can manage multiple tasks at once. It enables users to
complete multiple things at the same time.
2. Resource management: Operating systems manage and allocate resources such as
memory, processing power, and storage to different programs and processes running on
the system.
3. Security: Operating systems provide built-in security features to protect the system and
user data from unauthorized access or malicious attacks.
4. User-friendly interface: Operating systems provide an intuitive and user-friendly interface
that makes it easy to navigate and perform common tasks.
5. No Coding Lines : The rise of GUI (graphical user interface) has eliminated the process of
writing complex commands for doing small tasks.
Disadvantages of operating systems:
1. Expensive : When compared to other platforms like Linux, some operating systems are
costly. Microsoft Windows operating system with GUI and other in-built features carry a
costly price.
2. Highly Complex : Operating systems are highly complex, and the language which used to
established these OS are not clear and well defined.
3. Vulnerability to viruses and malware: Operating systems are vulnerable to viruses and
malware, which can compromise system security and cause data loss or corruption.
4. High system requirements: Some operating systems require powerful hardware to run,
which can be expensive and limit their use on older or less powerful devices.
5. Limited customization options: Some operating systems may have limited customization
options, which can be frustrating for users who want to personalize their experience.
Experiment 2
Aim: Introduction to (First Come First serve) Job Scheduling.
Theory:
Introduction:
First Come, First Served (FCFS) is a type of scheduling algorithm used by operating systems and
networks to efficiently and automatically execute queued tasks, processes and requests by the
order of their arrival. An FCFS scheduling algorithm may also be referred to as a first-in, first-out
(FIFO) algorithm or a first-come, first-choice (FCFC) algorithm.
Due to its simplistic nature, an FCFS algorithm is predictable, regardless of the type of tasks or
requests it has to process. Like a grocery store checkout scheme, FCFS algorithms mimic real-life
customer service situations where patrons who arrive first get served first regardless of the size
and complexity of their interaction.
FCFS Algorithm :
FCFS (First Come First Serve) is a scheduling algorithm that processes tasks in the order they are
comes in ready queue, without any priority or preference given to specific tasks. if we have 2 tasks
in ready queue then who has less arrival time will execute first.
#include<iostream>
using namespace std ;
static int count=0 ;
class process {
public :
int p_no,at,bt,ct,wt,tat;
void enter_details() {
p_no=++count;
cout<<"ENTER ARRIVAL TIME AND BURST TIME FOR PROCESS "<<p_no<< " : ";
cin>>at>>bt;
ct=tat=wt=0;
}
};
int main() {
int n ;
cout<<"ENTER NUMBER OF PROCESS : ";
cin>>n;
process p[n], temp ;
for(int i=0;i<n;i++) {
p[i].enter_details();
}
cout<<"p_id\tAT\tBT\tCT\tTAT\tWT"<<endl;
for(int i=0;i<n;i++) {
cout<<p[i].p_no<<"\t"<<p[i].at<<"\t"<<p[i].bt<<"\t"<<p[i].ct<<"\t"<<p[i].tat<<"\t"<<p[i].wt<<endl;
}
for(int i=0;i<n-1;i++) {
for(int j=i+1;j<n;j++) {
if(p[i].at > p[j].at) {
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
float ct_sum=0,tat_sum=0,wt_sum=0;
int cu_t=p[0].at;
for(int i=0;i<n;i++) {
if((cu_t-p[i].at)<0) {
cu_t=p[i].at;
}
cu_t=cu_t+p[i].bt;
p[i].ct=cu_t;
p[i].tat=p[i].ct-p[i].at ;
tat_sum=tat_sum+p[i].tat;
p[i].wt=p[i].tat-p[i].bt ;
wt_sum=wt_sum+p[i].wt;
}
for(int i=0;i<n-1;i++) {
for(int j=i+1;j<n;j++) {
if(p[i].p_no > p[j].p_no) {
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
cout<<"p_id\tAT\tBT\tCT\tTAT\tWT"<<endl;
for(int i=0;i<n;i++) {
cout<<p[i].p_no<<"\t"<<p[i].at<<"\t"<<p[i].bt<<"\t"<<p[i].ct<<"\t"<<p[i].tat<<"\t"<<p[i].wt<<endl;
}
cout<<"average TAT = "<<tat_sum/n<<endl;
cout<<"Average WT = "<<wt_sum/n<<endl;
return 0;
}
Output:
Some Numerical:
A computer system has 5 processes that need to be executed using the First Come First Serve (FCFS)
algorithm. The arrival time and burst time of each process are as follows:
To calculate the completion time, turnaround time, and waiting time for each process, we can use
the following formulas:
Turnaround Time (TAT ) = Completion Time - Arrival Time
Waiting Time (WT ) = Turnaround Time - Burst Time
P1 P2 P3 P4 P5
0 6 8 12 13 16
Using these formulas and Grantt chart, we can calculate the values for each process as follows:
Another advantage is that first come, first serve can be used to manage customer flow. For example,
if a store knows that it will be busy at a certain time of day, it can use first come, first serve to ensure
that customers are served in a timely manner. This can help to reduce wait times for customers and
improve overall customer satisfaction.
Disadvantages of FCFS:
Long Waiting Time : Because FCFS is a non-preemp tive CPU scheduling algorithm, This means that
a subsequent order cannot begin processing until the order before has finished executing. Once a
process has been allocated to the CPU, it will never release the CPU until it is completed. This
means that if the first order placed has a considerable burst time, all orders behind it are forced to
wait for its completion, no matter how small their burst times.
Lower Device Utilization : Because FCFS is so simple, it tends not to be very efficient. This goes
hand-in-hand with extended waiting times. Because the CPU can only handle one order at a time,
FCFS utilizes a minimal portion of your system’s capabilities, rendering it very inefficient.
Experiment 3
Aim: To perform shortest job first (non-primitive scheduling) algorithm .
Theory:
Introduction:
Shortest Job First (SJF) is a scheduling algorithm that assigns the job with the shortest burst time to
the CPU. When a new job arrives, it is inserted into the ready queue in the order of its burst time.
When the CPU is available, it is assigned to the job with the shortest burst time. However, it is very
difficult to predict the burst time needed for a process hence this algorithm is very difficult to
implement in the system.
To implement SJF in a non-primitive scheduling environment, you can follow these steps:
1. Create a ready queue to hold the jobs that are waiting to be executed.
2. When a new job arrives, sort the ready queue in increasing order of burst time.
3. When the CPU is available, assign the job with the shortest burst time to the CPU.
4. Once a job is completed, remove it from the ready queue and repeat steps 2 and 3.
It is important to note that SJF is based on the assumption that the burst time of a job is known in
advance. In reality, this is often not the case, and thus, SJF is not commonly used in practice.
#include<iostream>
using namespace std ;
Some Numerical:
Algorithm in which the process with the shortest burst time is executed first. Here is one numerical
on SJF:
P3 P1 P4 P5 P2
0 2 3 7 8 11 14
Using these formulas and Grantt chart, we can calculate the values for each process as follows:
Job Scheduling: In job scheduling systems, SJF is used to prioritize jobs based on their execution
time. This ensures that jobs with shorter execution times are completed first, reducing the overall
completion time of all jobs.
Cloud Computing: In cloud computing environments, SJF is used to optimize the allocation of
resources to virtual machines. This helps to improve the performance of the cloud environment and
reduce costs.
Network Routing: In network routing, SJF is used to determine the best path for data packets to
travel through the network. This helps to minimize the delay of data packets and improve network
performance.
Advantages of SJF:
1. High throughput: shortest job first (SJF) algorithm ensures that the CPU is always working
on the shortest job, thus maximizing the throughput of the system.
2. Low waiting time: As the shortest job is executed first, the waiting time for other jobs is
minimized. This results in a more efficient use of system resources.
3. High CPU utilization: As the CPU is always working on the shortest job, it is more likely to
be in a busy state, resulting in high CPU utilization.
4. Good for interactive systems: SJF is well suited for interactive systems where the user
expects a quick response.
Disadvantages of SJF:
1. Difficult to predict job length: It is difficult to predict the length of a job, which can lead to
inaccuracies in the scheduling algorithm.
2. Starvation of long jobs: longer jobs may be starved and may not be executed for a long
time, resulting in poor performance.
3. No support for priority: SJF does not consider the priority of a job and only focuses on the
job's length, which may lead to lower-priority jobs being executed before higher-priority
jobs.
4. Non-pre-emptive: SJF is a non-pre-emptive algorithm, meaning that once a job starts, it
cannot be interrupted. This can lead to inefficiency if a shorter job arrives while a longer
job is executing.
Experiment 4
Aim: To perform longest job first (non-primitive scheduling) algorithm .
Theory:
Introduction:
LJF (Longest Job First) is a scheduling algorithm used in operating systems to prioritize the
execution of processes based on their expected or estimated processing time, where the longest
job is given the highest priority
LJF is commonly used in batch processing systems, where the goal is to complete a large number
of jobs in the shortest amount of time. It can also be used in real-time systems, where the goal is
to meet specific deadlines.
However, LJF can lead to a longer waiting time for shorter jobs, which can be a disadvantage in
some systems. Additionally, if a new job arrives with a much longer burst time than the currently
executing job, it can cause a significant delay for that job.
Step 4: Repeat the above three steps until all the processes are executed.
#include<iostream>
using namespace std ;
output
Numerical:
A computer system has 5 processes that need to be executed using the Longest Job First (LJF)
algorithm. The arrival time and burst time of each process are as follows:
To calculate the completion time, turnaround time, and waiting time for each process, we can use
the following formulas:
Turnaround Time (TAT ) = Completion Time - Arrival Time
Waiting Time (WT ) = Turnaround Time - Burst Time
P1 P3 P5 P2 P4
0 6 10 13 15 16
Using these formulas and Grantt chart, we can calculate the values for each process as follows:
Note: The priority of the jobs is not considered in this algorithm as the focus is on completing the
longest jobs first.
Scheduling in Healthcare: The LJF algorithm can also be applied in healthcare to schedule patients
based on the time required for their medical procedures. This helps to optimize the use of medical
resources and ensure that longer procedures are scheduled first to reduce overall wait times.
Overall, the LJF algorithm is useful in any system that requires the processing of a large number of
jobs with different processing requirements, where prioritizing longer jobs can help to optimize
system performance.
#include<iostream>
using namespace std ;
static int count=0 ;
class process {
public :
int p_no,at,bt,ct,wt,tat,pt;
void enter_details() {
p_no=++count;
cout<<"ENTER ARRIVAL TIME AND BURST TIME AND PRIORITY FOR PROCESS
"<<p_no<< " : ";
cin>>at>>bt>>pt;
ct=tat=wt=0;
}
};
int main() {
int n ;
cout<<"ENTER NUMBER OF PROCESS : ";
cin>>n;
process p[n] ;
process temp ;
for(int i=0;i<n;i++) {
p[i].enter_details();
}
cout<<"-------------------"<<endl;
cout<<"p_id\tAT\tBT\tPRIORITY"<<endl;
for(int i=0;i<n;i++) {
cout<<p[i].p_no<<"\t"<<p[i].at<<"\t"<<p[i].bt<<"\t"<<p[i].pt<<endl;
}
cout<<"-------------------"<<endl;
for(int i=0;i<n-1;i++) {
for(int j=i+1;j<n;j++) {
if(p[i].at > p[j].at) {
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
int highestPriority = -1, currentTime=0 ;
float tat_sum=0,wt_sum=0;
// Process each job
for (int i = 0; i < n; i++) {
// Find the highest priority job that has arrived
highestPriority = i;
for (int j = i + 1; j < n; j++) {
if (p[j].at <= currentTime && p[j].pt > p[highestPriority].pt) {
highestPriority = j;
}
}
// Swap current process with the highest priority job
swap(p[i], p[highestPriority]);
// Update waiting and turnaround time
p[i].wt = currentTime - p[i].at;
p[i].tat = p[i].wt + p[i].bt;
// Update current time
currentTime += p[i].bt;
p[i].ct=currentTime;
// Update average waiting and turnaround time
wt_sum += p[i].wt;
tat_sum += p[i].tat;
}
for(int i=0;i<n-1;i++) {
for(int j=i+1;j<n;j++) {
if(p[i].p_no > p[j].p_no) {
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
cout<<"p_id\tAT\tBT\tCT\tTAT\tWT"<<endl;
for(int i=0;i<n;i++) {
cout<<p[i].p_no<<"\t"<<p[i].at<<"\t"<<p[i].bt<<"\t"<<p[i].ct<<"\t"<<p[i].tat<<"\t"<<p[i].wt
<<endl;
}
cout<<"average TAT = "<<tat_sum/n<<endl;
cout<<"Average WT = "<<wt_sum/n<<endl;
return 0;
}
Output
Numerical:
A computer system has 5 processes that need to be executed using the First Come First Serve (FCFS)
algorithm. The arrival time and burst time of each process are as follows:
P2 P1 P3 P4 P5
0 3 7 14 18 20
Using these formulas and Grantt chart, we can calculate the values for each process as follows:
Theory:
Introduction:
HRRN scheduling algorithm is a non-preemptive scheduling algorithm. The response ratio of a
process is defined as (waiting time + burst time) / burst time. The process with the highest
response ratio is selected for execution next. If multiple processes have the same response ratio,
the one with the highest arrival time is selected.
#include <iostream>
#include <algorithm>
using namespace std;
// Process structure to store process information
struct Process {
int id;
int arrival_time;
int burst_time;
int waiting_time;
int turnaround_time;
};
// Function to calculate waiting time and turnaround time for each process
void calculate_times(Process processes[], int n) {
int current_time = 0;
int completed_processes = 0;
while (completed_processes < n) {
// Find the process with the highest response ratio that has arrived and not yet
completed
int highest_response_ratio_index = -1;
double highest_response_ratio = -1;
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time <= current_time && processes[i].burst_time > 0) {
double response_ratio = 1 + ((double)(current_time - processes[i].arrival_time) /
processes[i].burst_time);
if (response_ratio > highest_response_ratio) {
highest_response_ratio_index = i;
highest_response_ratio = response_ratio;
}
}
}
// If a process is found, execute it for 1 unit of time and update waiting time and
turnaround time
if (highest_response_ratio_index != -1) {
processes[highest_response_ratio_index].burst_time--;
current_time++;
if (processes[highest_response_ratio_index].burst_time == 0) {
completed_processes++;
int completion_time = current_time;
int turnaround_time = completion_time -
processes[highest_response_ratio_index].arrival_time;
int waiting_time = turnaround_time -
processes[highest_response_ratio_index].burst_time;
processes[highest_response_ratio_index].waiting_time = waiting_time;
processes[highest_response_ratio_index].turnaround_time = turnaround_time;
}
}
// If no process is found, move to the next unit of time
else {
current_time++;
}
}
}
// Function to print the waiting time and turnaround time for each process
void print_times(Process processes[], int n) {
for (int i = 0; i < n; i++) {
cout << "Process " << processes[i].id << ": Waiting Time = " <<
processes[i].waiting_time << ", Turnaround Time = " << processes[i].turnaround_time <<
endl;
}
}
int main() {
// Example usage
Process processes[] = {{1, 0, 5, 0, 0}, {2, 1, 3, 0, 0}, {3, 2, 8, 0, 0}};
int n = sizeof(processes) / sizeof(processes[0]);
calculate_times(processes, n);
print_times(processes, n);
return 0;
}
OUTPUT
APPLICATIONS:
1. HRRN scheduling algorithm is commonly used in interactive systems where the
response time is a critical factor, such as time-sharing systems and interactive
user interfaces.
2. It is also useful in systems where a process with a higher response ratio should
be given priority over others, such as in real-time systems.
Advantages
1. The HRRN scheduling algorithm provides a balance between the SJF (Shortest
Job First) and FCFS (First Come, First Serve) scheduling algorithms, as it
considers both the waiting time and burst time of processes to determine their
priority.
2. This algorithm guarantees that the process with the highest response ratio will
be executed next, ensuring that the system responds quickly to user requests.
3. The HRRN algorithm avoids the problem of starvation, as processes with longer
waiting times will have higher response ratios and thus higher priority.
Disadvantages
1. The HRRN scheduling algorithm is not suitable for batch processing systems,
where the response time is not a critical factor.
2. Calculating the response ratio for each process requires additional overhead,
which can be time-consuming in large systems with many processes.
3. The HRRN algorithm may suffer from convoy effect, where a long-running
process may cause other processes with higher response ratios to wait for a
longer time.
Experiment 7
Aim: To perform Shortest Remaining Time First scheduling.
Theory:
Introduction:
This Algorithm is the preemptive version of SJF scheduling. In SRTF, the execution of the process can be
stopped after certain amount of time. At the arrival of every process, the short term scheduler schedules
the process with the least remaining burst time among the list of available processes and the running
process.
Once all the processes are available in the ready queue, No preemption will be done and the
algorithm will work as SJF scheduling. The context of the process is saved in the Process Control
Block when the process is removed from the execution and the next process is scheduled. This PCB
is accessed on the next execution of this process.
2. If the burst time of the new process is shorter, the scheduler preempts the currently running
process and starts executing the new process.
3. If the burst time of the new process is longer,
4. the scheduler places the new process in the ready queue.
5. The scheduler continues this process until all the processes have completed their execution.
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
int main() {
int n = 5; // number of processes
int bt[] = {6, 8, 7, 3, 2}; // burst time of each process
int at[] = {0, 2, 3, 5, 6}; // arrival time of each process
int rt[n]; // remaining time of each process
int wt[n]; // waiting time of each process
int tt[n]; // turnaround time of each process
int completed = 0;
int current_time = 0;
// If the process is completed, calculate its waiting time and turnaround time
if (rt[shortest_process] == 0) {
completed++;
wt[shortest_process] = current_time - at[shortest_process] - bt[shortest_process];
tt[shortest_process] = current_time - at[shortest_process];
}
}
}
// Calculate the average waiting time and turnaround time of all processes
float avg_waiting_time = 0;
float avg_turnaround_time = 0;
for (int i = 0; i < n; i++) {
avg_waiting_time += wt[i];
avg_turnaround_time += tt[i];
}
avg_waiting_time /= n;
avg_turnaround_time /= n;
return 0;
}
OUTPUT
Numerical:
Process Id Arrival Time Burst Time
P1 0 5
P2 1 6
P3 2 3
P4 3 1
P5 4 5
To calculate the completion time, turnaround time, and waiting time for each process, we can use
the following formulas:
Turnaround Time (TAT ) = Completion Time - Arrival Time
Waiting Time (WT ) = Turnaround Time - Burst Time
Gantt chart
P1 P1 P3 P4 P3 P1 P5 P2
0 1 2 3 4 6 10 15 21
Using these formulas and Grantt chart, we can calculate the values for each process as follows:
Applications
The SRTF scheduling algorithm is widely used in interactive systems that require fast response times.
Some applications that use SRTF include:
1. Real-time systems: SRTF scheduling is useful in real-time systems, where a process must
complete its execution within a fixed time frame.
2. Multimedia systems: SRTF scheduling is useful in multimedia systems, where a high-quality
display or audio output must be generated in real-time.
3. Interactive systems: SRTF scheduling is useful in interactive systems, such as web servers,
where fast response times are critical.
Advantages
The SRTF scheduling algorithm has several advantages, including:
1. Reduced response time: SRTF scheduling ensures that processes with shorter burst times are
given priority, resulting in reduced response times.
2. Improved efficiency: SRTF scheduling ensures that the CPU is always busy, resulting in
improved efficiency.
3. Better resource utilization: SRTF scheduling ensures that resources are utilized optimally, resulting in
better resource utilization.
Disadvantages
The SRTF scheduling algorithm also has some disadvantages, including:
1. High overhead: The SRTF scheduling algorithm requires frequent context switching, which
results in high overhead.
2. Starvation: The SRTF scheduling algorithm may result in starvation, where a process with a
longer burst time is continuously preempted by processes with shorter burst times.
3. Predictability: The SRTF scheduling algorithm is not very predictable, as the burst time of
processes may vary.
Experiment 8
Aim: To perform Longest Remaining Time First scheduling.
Theory:
Introduction:
Longest Remaining Time First (LRTF) is a CPU scheduling algorithm that is designed to improve the
efficiency of scheduling by prioritizing tasks with the longest remaining processing time. The main
idea behind this algorithm is that it aims to reduce the amount of context switching and maximize
the CPU utilization by scheduling the tasks in the order of their remaining execution time. This
algorithm is useful in real-time systems where tasks with shorter deadlines must be executed first.
4. If a new task arrives with a longer remaining processing time than the current task, preempt
the current task and assign the new task to the CPU.
5. Continue the above steps until all tasks have been processed
#include <iostream>
#include <algorithm>
using namespace std;
// Define a struct to represent a process
struct Process {
int pid; // process ID
int arrival_time; // arrival time
int burst_time; // burst time
int remaining_time;// remaining time for the process
};
// Define a function to compare processes by their remaining time
bool compare_processes(const Process& a, const Process& b) {
if (a.remaining_time == b.remaining_time) {
return [Link] < [Link];
} else {
return a.remaining_time > b.remaining_time;
}
}
int main() {
// Define the number of processes
int n = 5;
// Define the arrival time and burst time for each process
int arrival_time[] = {0, 1, 2, 3, 4};
int burst_time[] = {5, 4, 3, 2, 1};
// Set the flag to indicate that not all processes are complete
all_complete = false;
} else {
// Update the current time to the arrival time of the next process
if (i < n) {
current_time = processes[i].arrival_time;
}
}
}
return 0;
}
OUTPUT
Numerical:
Process Id Arrival Time Burst Time
P1 0 5
P2 1 6
P3 2 3
P4 3 1
P5 4 5
To calculate the completion time, turnaround time, and waiting time for each process, we can use
the following formulas:
P1 P2 P2 P1 P5 P2 P1 P3 P4
0 1 2 3 4 9 13 16 19 20
Using these formulas and Grantt chart, we can calculate the values for each process as follows:
Process Id Arrival Time Burst Completion TAT WT
Time time ( CT )
P1 0 5 16 16 11
P2 1 6 13 12 6
P3 2 3 19 17 14
P4 3 1 20 17 16
P5 4 5 9 5 0
SUM = 67 47
Average Turn Around time = 13.4, Average Waiting time = 9.4
Applications:
The LRTF algorithm is useful in real-time systems, where tasks with shorter deadlines must be
executed first. It is also suitable for systems where the processing time of the tasks is not known
beforehand. Some of the common applications of the LRTF algorithm are:
1. Operating Systems: The LRTF algorithm is used in many operating systems for CPU
scheduling, including Unix, Linux, and Windows.
2. Real-time Systems: The LRTF algorithm is widely used in real-time systems, such as traffic
management systems, industrial automation systems, and medical equipment.
3. Multimedia Systems: The LRTF algorithm is used in multimedia systems to ensure that the
tasks with the longest processing time are executed first. This is important in video streaming
applications, where frames must be displayed at a specific rate.
Advantages
The LRTF algorithm has several advantages over other scheduling algorithms, including:
1. Improved Efficiency: The LRTF algorithm reduces the amount of context switching and
maximizes the CPU utilization by scheduling the tasks in the order of their remaining
execution time.
2. Fairness: The LRTF algorithm ensures that tasks with longer processing times are given higher
priority, which results in a fair allocation of CPU time.
3. Real-time Responsiveness: The LRTF algorithm is useful in real-time systems, where tasks
with shorter deadlines must be executed first.
Disadvantages
The LRTF algorithm has a few disadvantages, including:
1. Starvation: The LRTF algorithm can result in the starvation of short tasks if there are long
tasks in the queue.
2. Complexity: The LRTF algorithm is more complex than other scheduling algorithms, which
makes it difficult to implement and maintain.
3. Predictability: The LRTF algorithm is not very predictable, as the execution time of the tasks
is not known beforehand.
Experiment 9
Aim: To perform Pre-emptive Priority scheduling.
Theory:
Introduction:
Pre-emptive priority CPU scheduling is a popular scheduling algorithm used in operating systems to
determine which process should be executed next. In this algorithm, each process is assigned a
priority, which determines its position in the execution queue. The process with the highest priority
is executed first, and the execution of a process can be pre-empted by a higher-priority process.
#include <iostream>
#include <algorithm>
#include <limits.h>
using namespace std;
int current_time = 0;
int completed_processes = 0;
while (completed_processes < n) {
// Find the highest priority process that has arrived and not yet completed
int highest_priority_index = -1;
int highest_priority = INT_MAX ;
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time <= current_time && processes[i].burst_time > 0 &&
processes[i].priority < highest_priority) {
highest_priority_index = i;
highest_priority = processes[i].priority;
}
}
current_time++;
}
}
int main() {
// Example usage
Process processes[] = {{1, 0, 5, 2}, {2, 1, 3, 1}, {3, 2, 8, 3}};
int n = sizeof(processes) / sizeof(processes[0]);
priority_preemptive(processes, n);
return 0;
}
OUTPUT
Numerical:
Process Id Arrival Time Burst Time Priority
P1 0 3 3
P2 1 4 2
P3 2 6 4
P4 3 4 6
P5 4 2 10
To calculate the completion time, turnaround time, and waiting time for each process, we can use
the following formulas:
P1 P1 P3 P4 P5 P4 P3 P1 P2
0 1 2 3 4 6 9 14 15 17
Using these formulas and Grantt chart, we can calculate the values for each process as follows:
USES:
1. Resource allocation: Priorities can be used to allocate resources such as CPU time and
memory to processes based on their importance.
2. Real-time systems: In real-time systems, priority scheduling is used to ensure that time-
critical tasks are completed within their deadlines.
3. Multitasking: Priority scheduling can be used to manage multiple tasks running
simultaneously, ensuring that high-priority tasks are completed before lower-priority
tasks.
4. Fairness: Priority scheduling can be designed to ensure fairness by aging the priorities
of processes that have been waiting for a long time.
5. Interrupt handling: In systems with hardware interrupts, priority scheduling can be
used to handle the interrupts based on their priority, ensuring that important
interrupts are serviced first.
Advantages
1. The pre-emptive priority CPU scheduling algorithm ensures that high-priority processes are
executed first, which is important in real-time systems.
2. It allows for the efficient utilization of system resources, as the most important processes
are given priority.
3. It can help to minimize the average response time of processes, as higher-priority processes
are executed more quickly.
Disadvantages
1. The pre-emptive priority CPU scheduling algorithm can lead to starvation, where low-priority
processes are never executed.
2. It can also lead to priority inversion, where a low-priority process holds a resource that is
required by a high-priority process, preventing the high-priority process from executing.
3. It can be difficult to determine appropriate priorities for processes, which can lead to
inefficiencies in the system.
Experiment 10
Aim: To implement Round Robin scheduling algorithm .
Theory:
Introduction:
Round-robin scheduling is a CPU scheduling algorithm that is designed for time-sharing systems. It
is a pre-emptive scheduling algorithm, meaning that the CPU is allocated to each process for a
fixed time slice, or quantum, before being preempted and given to the next process in the queue.
In round-robin scheduling, the ready queue is treated as a circular queue. Each process is assigned
a time slice, which is typically a small amount of time, such as 10-100 milliseconds. The scheduler
assigns the CPU to the first process in the queue and allows it to run for its time slice. If the
process completes its execution during the time slice, it is removed from the queue. If not, it is
moved to the end of the queue and the next process in the queue is given a chance to run.
The period of time for which a process or job is allowed to run in a pre-emptive method is called
time quantum.
#include <iostream>
#include <queue>
using namespace std;
// Process structure to store process information
struct Process {
int id;
int arrival_time;
int burst_time;
int remaining_time;
};
// Function to implement Round Robin scheduling
void round_robin(Process processes[], int n, int quantum) {
// Create a queue to store the processes
queue<Process> q;
for (int i = 0; i < n; i++) {
[Link](processes[i]);
}
int current_time = 0;
while (![Link]()) {
Process current_process = [Link]();
[Link]();
// If the process has not finished executing, execute it for the quantum time
if (current_process.remaining_time > quantum) {
current_time += quantum;
current_process.remaining_time -= quantum;
[Link](current_process);
}
// If the process has finished executing, print its details
else {
current_time += current_process.remaining_time;
cout << "Process " << current_process.id << " finished at time " << current_time << endl;
}
}
}
int main() {
// Example usage
Process processes[] = {{1, 0, 5, 5}, {2, 1, 3, 3}, {3, 2, 8, 8}};
int n = sizeof(processes) / sizeof(processes[0]);
int quantum = 2;
round_robin(processes, n, quantum);
return 0;
}
OUTPUT
Numerical:
A computer system has 5 processes that need to be executed using the Round Robin algorithm. The
arrival time and burst time of each process are as follows: time quntum =4
P1 P2 P3 P4 P5 P1 P6 P2 P5
Gantt chart
P1 P2 P3 P4 P5 P1 P6 P2 P5
0 4 8 11 12 16 17 21 23 24
Using these formulas and Grantt chart, we can calculate the values for each process as follows:
Experiment 11
Aim: FCFS Disc Scheduling
Theory:
Introduction:
FCFS (First-Come, First-Served) is a simple disk scheduling algorithm used in operating systems to
manage input/output (I/O) operations on a hard disk. As the name suggests, it prioritizes the I/O
requests in the order in which they arrive, with the first request received being the first to be
serviced.
Under this scheduling algorithm, the operating system maintains a queue of pending I/O requests,
with new requests being added to the end of the queue as they arrive. The operating system then
services these requests one by one in the order they were added to the queue.
While this approach is simple to implement and ensures that all requests are eventually serviced, it
can lead to performance issues in some cases. For example, if a large I/O request arrives first, it can
cause subsequent smaller requests to be delayed, resulting in longer average response times for all
requests.
Overall, FCFS is a straightforward scheduling algorithm that works well for simple I/O workloads but
may not be the most efficient option in more complex scenarios.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
// Example usage
vector<int> requests = {98, 183, 37, 122, 14, 124, 65, 67};
int head_position = 53;
int total_head_movement = calculate_head_movement(requests, head_position);
cout << "Total head movement: " << total_head_movement << endl;
return 0;
}
OUTPUT
Advantages
1. First Come First Serve algorithm has a very simple logic, it executes the process requests
one by one in the sequence they arrive.
2. Thus, First Come First Serve is very simple and easy to understand and implement.
3. In FCFS eventually, each and every process gets a chance to execute, so no starvation
occur.
Disadvantages
1. This scheduling algorithm is nonpreemptive, which means the process can’t be stopped in
middle of execution and will run it’s full course.
2. FCFS being a nonpreemptive scheduling algorithm, the short processes which are at the
back of the queue have to wait for the long process at the front to finish
#include <iostream>
#include <algorithm>
#include <vector>
#include <limits.h>
using namespace std;
int main() {
// Example usage
vector<int> requests = {98, 183, 37, 122, 14, 124, 65, 67};
int head_position = 53;
int total_head_movement = calculate_head_movement(requests, head_position);
cout << "Total head movement: " << total_head_movement << endl;
return 0;
}
OUTPUT
Applications:
1. File systems: SSTF is used in file systems to read and write files from disk. By minimizing the
head movement of the disk arm, SSTF reduces the average seek time and access time for
reading and writing files.
2. Database systems: SSTF is used in database systems to retrieve data from disk. Databases
often store large amounts of data on disk, and the SSTF algorithm helps to optimize disk
access time for faster retrieval of data.
3. Real-time systems: SSTF is used in real-time systems that require low-latency disk access.
For example, in aviation and automotive systems, low-latency access to data from disk is
critical for safety-critical applications, and SSTF helps to optimize disk access time.
Advantages
1. The total seek time is reduced compared to First Come First Serve.
2. SSTF improves and increases throughput.
3. Less average waiting time and response time in SSTF.
Disadvantages
1. In SSTF there is an overhead of finding out the closest request.
2. Starvation may occur for requests far from head.
3. In SSTF high variance is present in response time and waiting time.
4. Frequent switching of the Head’s direction slows the algorithm.