You are on page 1of 38

Department of

CSE (Artificial Intelligence & Machine Learning)

LAB MANUAL
(2022-2023)

Subject Name : Operating System


(SubjectCode) : AL501

Submitted To: Submitted By:


Mr. Mayur Rathi Name: Manas Patidar
Enrollment No.:0827AL201032
Year / Sem: 3 Year / 5 Sem
Class: AIML
INDEX
SNO Name of Experiment Date Grade Sign/Remark

Case study of different OS and evaluation of


1 operating system.

Case study of file systems used in Linux, Unix


2 and windows operating system.

Write a program to implement FCFS CPU


3 scheduling algorithm.

Write a program to implement a priority


4 Scheduling algorithm.

Write a program to implement Shortest Job First


5 Scheduling algorithm

Write a program to implement Round Robin CPU


6 Scheduling Algorithm

Write a program to implement Banker's Algorithm.


7

Write a program to simulate memory allocation


8 strategies (First feed, best feed, worst feed).

Write a program to implement page replacement


9 algorithms.

Write a program to implement various disk


10 scheduling algorithms.
Experiment-1

Objective: Case study of different OS and evaluation of operating system.

Solution:

1. MS-DOS:

MS-DOS which is short for Microsoft Disk Operating System is a non-


graphical command line operating system developed for IBM
compatible computers with x86 microprocessor. The operating system useda
command line interface for the user to input commands to navigate, open and
manipulate files on their computer.
Features:
It is a single user operating system meaning only one user can operate ata
time.
It is a light weight operating system allowing users to have direct accessto the
BIOS and its underlying hardware.
Loads data and programs from external sources and bring them into the
internal memory so they can be used on the computer.
Enables the computer to perform input and output operations such astaking
commands from keyboard, printing information on the screen.
It is very helpful in making file management like creating, editing,
deleting files, etc.
It also controls and manages other external devices such as the printer,
keyboard or external hard drive using various drive utilities.
Drawbacks:

It does not allow multiple users to operate on the system.

It does not support graphical interface hence mouse cannot be used to


operate it.
It does not support multiprogramming meaning it can only have one
process in the ram.
It lacked memory protection which meant no security, and less stability.
It has difficulty in memory access when addressing more than 640 MB ofRAM.

2. Windows Operating System:


Windows is an operating system designed by Microsoft to be used on a standard x86
Intel and AMD processors. It provides an interface, known as a graphical user
interface(GUI) which eliminates the need to memorize commands for the command line
by using a mouse to navigate through menus, dialog boxes, buttons, tabs, and icons.
The operating system was named windows since the programs are displayed in the
shape of a square. This Windows operating systemhas been designed for both a novice
user just using at home as well as for professionals who are into development.
Features:
It is designed to run on any standard x86 Intel and AMD hence most ofthe
hardware vendors make drivers for windows like Dell, HP, etc.
It supports enhanced performance by utilizing multi-core processors.
It comes preloaded with many productivity tools which helps to completeall
types of everyday tasks on your computer.
Windows has a very large user base so there is a much larger selectionof
available software programs, utilities.
Windows is backward compatible meaning old programs can run on
newer versions.
Hardware is automatically detected eliminating need of manually
installing any device drivers.
Drawbacks:
Windows can be expensive since the OS is paid license and majority ofits
applications are paid products.
Windows has high computer resource requirement like it should havehigh
ram capacity, a lot of hard drive space and good graphics card.
Windows slows and hangs up if the user loads up many programs at thesame
time.
Windows includes network sharing that can be useful if user has a
network with many PCs.
Windows is vulnerable to virus attacks since it has a huge user base andusers
have to update OS to keep up-to-date with security patches.

3. LINUX Operating System:


The Linux OS is an open source operating system project that is a freely distributed,
cross-platform operating system developed based on UNIX. This operating system is
developed by Linus Torvalds. The name Linux comes from the Linux kernel. It is
basically the system software on a computer that allows apps and users to perform
some specific task on the computer. The developmentof Linux operating system
pioneered the open source development and became the symbol of software
collaboration.
Features:
Linux is free can be downloaded from the Internet or redistribute it underGNU
licenses and has the best community support.
Linux OS is easily portable which means it can be installed on varioustypes
of devices like mobile, tablet computers.
It is a multi-user, multitasking operating system.
BASH is the Linux interpreter program which can be used to execute
commands.
Linux provides multiple levels of file structures i.e. hierarchical structure in which
all the files required by the system and those that are created by the user are
arranged.
Linux provides user security using authentication features and alsothreat
detection and solution is very fast because Linux is mainly community
driven.

Drawbacks:
There’s no standard edition of Linux hence confusing for users and also
becoming familiar with the Linux may be a problem for new users.
More difficult to find applications to support user needs since Linux doesnot
dominate the market.
Since some applications are developed specifically for Windows and Mac,
those might not be compatible with linux and sometimes users might not
have much of a choice to choose between different applications like in
Windows or Mac since most apps are developed foroperating systems that
have a huge user base.
Some hardware may not be incompatible with Linux since it has patchier
support for drivers which may result in malfunction.
There are plenty of forums to resolve Linux issues, but it may not alwaysmatch
the user’s own level of technical understanding.
4. Android Mobile Operating System:
Android is a Google’s Linux based operating system it is designed primarily fortouch
screen mobile devices such as smart phones and tablet computers. The hardware
which can be used to support android is based on three architecturesnamely ARM,
Intel and MIPS design lets users manipulate the mobile devices. intuitively, with finger
movements that mirror common motions, such as pinching,swiping, and tapping
making these applications comfortable for the users.
Features:
The android operating system is an open source operating system
means that it’s free and any one can use it.
Android offers optimized 2D and 3D graphics, multimedia, GSM
connectivity, multi-tasking.
Android OS is known for its friendly user interface and exceptional
customizable according to the user’s taste.
Huge choice of applications for its users since Playstore offer over onemillion
apps.
Software developers who want to create applications for the Android OScan
download the Android Software Development Kit(SDK) to easily develop
apps for android.
Android would consume very little power but deliver extreme
performance since its hardware is based on ARM architecture.

Drawbacks:
The design and coding of intuitive modern user experiences and
interfaces poses a difficulty because of its dependency on Java.
Most apps tend to run in the background even when closed by the user
draining the battery.
Performance is bound to take a hit as multiple programs run
simultaneously in the background at any given time.
Android phones overheat especially when indulged in hardcore
productivity tasks or heavy graphics.
Apps have lower security profiles and make users more susceptible todata
breaches.

5. iOS Mobile Operating System:


iOS which is short for iPhone OS is a mobile operating system created and
developed by Apple Inc. exclusively for its hardware like A12 Bionic chip that
presently powers many of its mobile devices, including the iPhone, iPad, and iPod.
The iOS user interface is based upon using multi-touch gestures such asswipe, tap,
pinch, and reverse pinch. The purpose of these finger actions is to provide the user
with fast responsive inputs given from multiple fingers to the multi-touch capacitive
screen display.
Features:
It is written in C, C++, Objective-C and Swift and is based on the
Macintosh OS X.
Has excellent and intuitive user interface and very fluid response.
Performance of iOS is unbeatable.
iOS comes with a lot of default apps, including an email client, web
browser, media player and the phone app.
Availability of higher quality apps which can be downloaded from the
Appstore.
Apple has provided its own iOS software development kit (SDK) for the
developers to create applications for Apple mobile devices.
iOS is much safer than other mobile operating systems and has fewer
security breaches as well.
Provides regular updates and security patches.
Drawbacks:

The OS is closed source instead of open source hence beta testing


taking a lot of time since its only available to limited developers.
The amount of memory space the iOS applications occupy is very largewhen
compared with other mobile platforms.
Lack of customization compared to other operating systems.
Doesn’t allow third party installations.
Having intense graphics and animations consumes more power and
causes battery drains.
iOS is resource intensive operating system due to which older devices
struggle to run it.
Experiment-2

Objective:Case study of file systems used in Linux, Unix and windows operating system.

Solution:

Linux File System Structure

Linux file system has a hieíaíchal file stíuctuíe as it contains a íoot diíectoíy and its
subdiíectoíies. All otheí diíectoíies can be accessed fíom the íoot diíectoíy. A paítition
usually has only one file system, but it may have moíe than one file system.

A file system is designed in a way so that it can manage and píovide space foí non- volatile
stoíage data. All file systems íequiíed a namespace that is a naming ando íganizational
methodology. ľhe namespace defines the naming píocess, length of the file name, oí a
subset of chaíacteís that can be used foí the file name. It also defines the logical stíuctuíe
of files on a memoíy segment, such as the use of diíectoíies foí oíganizing the specific
files. Once a namespace is descíibed, a Metadata descíiption must be defined foí that
paíticulaí file.

ľhe data stíuctuíe needs to suppoít a hieíaíchical diíectoíy stíuctuíe; this stíuctuíe is
used to descíibe the available and used disk space foí a paíticulaí block. It also has the
otheí details about the files such as file size, date & time of cíeation, update, and last
modified.

Also, it stoíes advanced infoímation about the section of the disk, such as paítitions
and volumes.

ľhe advanced data and the stíuctuíes that it íepíesents contain the infoímation about
the file system stoíed on the díive; it is distinct and independent of the file system
metadata.

Linux file system contains two-paít file system softwaíe implementation aíchitectuíe.
Consideí the below image:
ľhe file system íequiíes an API (Application píogíamming inteíface) to access the
function calls to inteíact with file system components like files and diíectoíies. API
facilitates tasks such as cíeating, deleting, and copying the files. It facilitates an algoíithm
that defines the aííangement of files on a file system.

ľhe fiíst two paíts of the given file system togetheí called a Linux viítual file system. It
píovides a single set of commands foí the keínel and developeís to access the file
system.
ľhis viítual file system íequiíes the specific system díiveí to give an inteíface to the file
system.

Unix file system:-


Unix file system is a logical method of organizing and storing large amounts of
information in a way that makes it easy to manage. A file is a smallest unit in which the
information is stored. Unix file system has several important features.All data in Unix is
organized into files. All files are organized into directories.
These directories are organized into a tree-like structure called the file system.
Files in Unix System are organized into multi-level hierarchy structure known as a
directory tree. At the very top of the file system is a directory called “root” which is
represented by a “/”. All other files are “descendants” of root.
Types of Unix files – The UNIX files system contains several different types of files :
File systems of Windows
Microsoft Windows employs two major file systems: NTFS, the primaryformat most
modern versions of this OS use by default, and FAT, which
was inherited from old DOS and has exFAT as its later extension. ReFS wasalso
introduced by Microsoft as a new generation format for server
computers starting from Windows Server 2012. HPFS developed by
Microsoft together with IBM can be found only on extremely old machinesrunning
Windows NT up to 3.5.

FAT
FAT (File Allocation Table) is one of the simplest FS types, which has been around
since the 1980s. It consists of the FS descriptor sector (boot sectoror superblock), the
block allocation table (referred to as the File AllocationTable) and plain storage space
for storing data. Files in FAT are stored in directories. Each directory is an array of 32-
byte records, each defining a file or its extended attributes (e.g. a long name). A record
attributes the first block of a file. Any next block can be found through the block
allocation table by using it as a linked list.

NTFS
NTFS (New Technology File System) was introduced in 1993 with WindowsNT and is
currently the most common file system for end user computers based on Windows.
Most operating systems of the Windows Server line use this format as well.

ReFS
ReFS (Resilient File System) is the latest development of Microsoftintroduced
with Windows 8 and now available for Windows 10. Its
architecture absolutely differs from other Windows formats and is mainly organized in a
form of the B+-tree. ReFS has high tolerance to failures dueto new features included
into it.

HPFS
HPFS (High Performance File System) was created by Microsoft in
cooperation with IBM and introduced with OS/2 1.20 in 1989 as a filesystem for
servers that could provide much better performance when compared to FAT. In
contrast to FAT, which simply allocates any first free cluster on the disk for the file
fragment, HPFS seeks to arrange the file in contiguous blocks, or at least ensure
that its fragments (referred to as extents) are placed maximally close to each
other.
Experiment-3

Objective:Write a program to implement FCFS CPU scheduling algorithm.

Solution:
#include<iostream>
using namespace std;

void findWaitingTime(int processes[], int n,int bt[], int wt[])


{
wt[0] = 0;

for (int i = 1; i < n ; i++ )

wt[i] = bt[i-1] + wt[i-1] ;


}
void findTurnAroundTime( int processes[], int n,int bt[], int wt[], int tat[])
{

for (int i = 0; i < n ; i++)

tat[i] = bt[i] + wt[i];


}
void findavgTime( int processes[], int n, int bt[])
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;

findWaitingTime(processes, n, bt, wt);

findTurnAroundTime(processes, n, bt, wt, tat);

cout << "Processes "<< " Burst time "<< " Waiting time " << " Turn around time\n";

for (int i=0; i<n; i++)

total_wt = total_wt + wt[i];

total_tat = total_tat + tat[i];

cout << " " << i+1 << "\t \t" << bt[i] <<"\t \t"<< wt[i] <<"\t\t " << tat[i] <<endl;
}
cout << "Average waiting time = "<< (float)total_wt / (float)n;

cout << "\n Average turn around time = "<< (float)total_tat / (float)n;
}
int main()
{
int processes[] = { 1, 2, 3};

int n = sizeof processes / sizeof processes[0];

int burst_time[] = {10, 5, 8};

findavgTime( processes, n, burst_time);

return 0;
}

Outcomes:
Experiment-4

Objective:Write a program to implement a priority Scheduling algorithm.

Solution:
#include <bits/stdc++.h>
using namespace std;
#define totalprocess 5

struct process
{
int at,bt,pr,pno;
};

process proc[50];

bool comp(process a,process b)


{
if(a.at == b.at)
{
return a.pr<b.pr;
}
else
{
return a.at<b.at;
}
}

void get_wt_time(int wt[])


{

int service[50];
service[0] = proc[0].at;
wt[0]=0;
for(int i=1;i<totalprocess;i++)
{
service[i]=proc[i-1].bt+service[i-1];

wt[i]=service[i]-proc[i].at;

if(wt[i]<0)
{
wt[i]=0;
}
}

void get_tat_time(int tat[],int wt[])


{

for(int i=0;i<totalprocess;i++)
{
tat[i]=proc[i].bt+wt[i];
}

void findgc()
{

int wt[50],tat[50];

double wavg=0,tavg=0;
get_wt_time(wt);
get_tat_time(tat,wt);

int stime[50],ctime[50];

stime[0] = proc[0].at;
ctime[0]=stime[0]+tat[0];
for(int i=1;i<totalprocess;i++)
{
stime[i]=ctime[i-1];
ctime[i]=stime[i]+tat[i]-wt[i];
}

cout<<"Process_no\tStart_time\tComplete_time\tTurn_Around_Time\tWaiting_Time"<<endl;

for(int i=0;i<totalprocess;i++)
{
wavg += wt[i];
tavg += tat[i];

cout<<proc[i].pno<<"\t\t"<<
stime[i]<<"\t\t"<<ctime[i]<<"\t\t"<<
tat[i]<<"\t\t\t"<<wt[i]<<endl;
}

cout<<"Average waiting time is : ";


cout<<wavg/(float)totalprocess<<endl;
cout<<"Average turnaround time : ";
cout<<tavg/(float)totalprocess<<endl;

int main()
{
int arrivaltime[] = { 1, 2, 3, 4, 5 };
int bursttime[] = { 3, 5, 1, 7, 4 };
int priority[] = { 3, 4, 1, 7, 8 };

for(int i=0;i<totalprocess;i++)
{
proc[i].at=arrivaltime[i];
proc[i].bt=bursttime[i];
proc[i].pr=priority[i];
proc[i].pno=i+1;
}

sort(proc,proc+totalprocess,comp);
findgc();

return 0;
}

Outcomes:
Experiment-5

Objective:Write a program to implement Shortest Job First Scheduling algorithm

Solution:
#include <bits/stdc++.h>
using namespace std;
struct Process {
int pid;
int bt;
int art;
};
void findTurnAroundTime(Process proc[], int n, int wt[], int tat[]) {
for (int i = 0; i < n; i++)
tat[i] = proc[i].bt + wt[i];
}

void findWaitingTime(Process proc[], int n, int wt[]) {


int rt[n];
for (int i = 0; i < n; i++)
rt[i] = proc[i].bt;
int complete = 0, t = 0, minm = INT_MAX;
int shortest = 0, finish_time;
bool check = false;
while (complete != n) {
for (int j = 0; j < n; j++) {
if ((proc[j].art <= t) && (rt[j] < minm) && rt[j] > 0) {
minm = rt[j];
shortest = j;
check = true;
}
}
if (check == false) {
t++;
continue;
}

rt[shortest]--;
minm = rt[shortest];
if (minm == 0)
minm = INT_MAX;

if (rt[shortest] == 0) {
complete++;
check = false;
finish_time = t + 1;

wt[shortest] = finish_time -
proc[shortest].bt -
proc[shortest].art;
if (wt[shortest] < 0)
wt[shortest] = 0;
}
t++;
}
}
void findavgTime(Process proc[], int n) {
int wt[n], tat[n], total_wt = 0,
total_tat = 0;
findWaitingTime(proc, n, wt);
findTurnAroundTime(proc, n, wt, tat);
cout << "Processes " << " Burst time " << " Waiting time " << " Turn around time";
for (int i = 0; i < n; i++) {
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << proc[i].pid << "\t\t" << proc[i].bt << "\t\t " << wt[i] << "\t\t " << tat[i] << endl;
}
cout << "Average waiting time = " << (float)total_wt / (float)n;
cout << "Average turn around time = " << (float)total_tat / (float)n;
}
int main()
{
Process proc[] = { { 1, 5, 1 }, { 2, 3, 1 }, { 3, 6, 2 }, { 4, 5, 3 } };
int n = sizeof(proc) / sizeof(proc[0]);
findavgTime(proc, n);
return 0;
}

Outcomes:
Experiment-6

Objective: Write a program to implement Round Robin CPU Scheduling Algorithm

Solution:
#include<iostream>
using namespace std;

void findWaitingTime(int processes[], int n,int bt[], int wt[], int quantum)
{
int rem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];
int t = 0;
while (1)
{
bool done = true;
for (int i = 0 ; i < n; i++)
{
if (rem_bt[i] > 0)
{
done = false;
if (rem_bt[i] > quantum)
{
t += quantum;
rem_bt[i] -= quantum;
}
else
{
t = t + rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0;
}
}
}
if (done == true)
break;
}
}
void findTurnAroundTime(int processes[], int n,int bt[], int wt[], int tat[])
{
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}

void findavgTime(int processes[], int n, int bt[],int quantum)


{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt, quantum);
findTurnAroundTime(processes, n, bt, wt, tat);
cout << "Process No\t"<< "Brust Time \t"
<< "Wating Time \t" << "Turn Around Time\n";
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i+1 << "\t\t" << bt[i] <<"\t\t "
<< wt[i] <<"\t\t " << tat[i] <<endl;
}
cout << "Average waiting time = "
<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}
int main()
{
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
int burst_time[] = {10, 5, 8};
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
return 0;
}

Outcomes:
Experiment-7

Objective: Write a program to implement Banker's Algorithm

Solution:
#include <iostream>
using namespace std;

int main()
{

int n, m, i, j, k;
n = 5;
m = 3;
int alloc[5][3] = { { 0, 1, 0 },
{ 2, 0, 0 },
{ 3, 0, 2 },
{ 2, 1, 1 },
{ 0, 0, 2 } };
int max[5][3] = { { 7, 5, 3 },
{ 3, 2, 2 },
{ 9, 0, 2 },
{ 2, 2, 2 },
{ 4, 3, 3 } };

int avail[3] = { 3, 3, 2 };
int f[n], ans[n], ind = 0;
for (k = 0; k < n; k++) {
f[k] = 0;
}
int need[n][m];
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}
int y = 0;
for (k = 0; k < 5; k++) {
for (i = 0; i < n; i++) {
if (f[i] == 0) {

int flag = 0;
for (j = 0; j < m; j++) {
if (need[i][j] > avail[j]){
flag = 1;
break;
}
}
if (flag == 0) {
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}

int flag = 1;

for(int i = 0;i<n;i++)
{
if(f[i]==0)
{
flag = 0;
cout << "The given sequence is not safe";
break;
}
}

if(flag==1)
{
cout << "Following is the SAFE Sequence" << endl;
for (i = 0; i < n - 1; i++)
cout << " P" << ans[i] << " ->";
cout << " P" << ans[n - 1] <<endl;
}

return (0);
}

Outcomes:
Experiment-8

Objective:Write a program to simulate memory allocation strategies (First feed, best feed, worst
feed).

Solution:
#include<bits/stdc++.h>
using namespace std;
void firstFit(int blockSize[], int m,int processSize[], int n)
{
int allocation[n];
memset(allocation, -1, sizeof(allocation));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (blockSize[j] >= processSize[i])
{
allocation[i] = j;
blockSize[j] -= processSize[i];
break;
}
}
}
cout << "First Fit \nProcess No.\tProcess Size\tBlock no.\n";
for (int i = 0; i < n; i++)
{
cout << " " << i+1 << "\t\t"
<< processSize[i] << "\t\t";
if (allocation[i] != -1)
cout << allocation[i] + 1;
else
cout << "Not Allocated";
cout << endl;
}
}
void bestfit(int bsize[], int m, int psize[], int n) {
int alloc[n];
memset(alloc, -1, sizeof(alloc));
for (int i=0; i<n; i++) {
int bestIdx = -1;
for (int j=0; j<m; j++) {
if (bsize[j] >= psize[i]) {
if (bestIdx == -1)
bestIdx = j;
else if (bsize[bestIdx] > bsize[j])
bestIdx = j;
}
}
if (bestIdx != -1) {
alloc[i] = bestIdx;
bsize[bestIdx] -= psize[i];
}
}
cout << "Best Fit \nProcess No.\tProcess Size\tBlock no."<<endl;
for (int i = 0; i < n; i++) {
cout << " " << i+1 << "\t\t" << psize[i] << "\t\t";
if (alloc[i] != -1)
cout << alloc[i] + 1;
else
cout << "Not Allocated";
cout << endl;
}
}

void worstFit(int blockSize[], int m, int processSize[],int n)


{
int allocation[n];
memset(allocation, -1, sizeof(allocation));
for (int i=0; i<n; i++)
{
int wstIdx = -1;
for (int j=0; j<m; j++)
{
if (blockSize[j] >= processSize[i])
{
if (wstIdx == -1)
wstIdx = j;
else if (blockSize[wstIdx] < blockSize[j])
wstIdx = j;
}
}
if (wstIdx != -1)
{
allocation[i] = wstIdx;
blockSize[wstIdx] -= processSize[i];
}
}
cout << "Wrost Fit\nProcess No.\tProcess Size\tBlock no.\n";
for (int i = 0; i < n; i++)
{
cout << " " << i+1 << "\t\t" << processSize[i] << "\t\t";
if (allocation[i] != -1)
cout << allocation[i] + 1;
else
cout << "Not Allocated";
cout << endl;
}
}

int main()
{
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize) / sizeof(blockSize[0]);
int n = sizeof(processSize) / sizeof(processSize[0]);

firstFit(blockSize, m, processSize, n);


cout<<endl<<endl;
bestfit(blockSize, m, processSize, n);
cout<<endl<<endl;
worstFit(blockSize, m, processSize, n);
return 0 ;
}
Outcomes:
Experiment-9

Objective: Write a program to implement page replacement algorithms.

Solution:
#include <bits/stdc++.h>
using namespace std;
int predict(int page[], vector<int>& fr, int pn, int index) {

int res = -1, farthest = index;


for (int i = 0; i < fr.size(); i++) {
int j;
for (j = index; j < pn; j++) {
if (fr[i] == page[j]) {
if (j > farthest) {
farthest = j;
res = i;
}
break;
}
}

if (j == pn)
return i;
}

return (res == -1) ? 0 : res;


}
bool search(int key, vector<int>& fr) {
for (int i = 0; i < fr.size(); i++)
if (fr[i] == key)
return true;
return false;
}
void opr(int page[], int pn, int fn) {
vector<int> fr;
int hit = 0;
for (int i = 0; i < pn; i++) {

if (search(page[i], fr)) {
hit++;
continue;
}

if (fr.size() < fn)


fr.push_back(page[i]);
else {
int j = predict(page, fr, pn, i + 1);
fr[j] = page[i];
}
}
cout << "Hits = " << hit << endl;
cout << "Misses = " << pn - hit << endl;
}

int main() {
int page[] = { 2, 7, 8, 3, 0, 2, 0, 3, 5, 4, 0, 6, 1 };
int pn = sizeof(page) / sizeof(page[0]);
int fn = 3;
opr(page, pn, fn);
return 0;
}
Outcomes:
Experiment-10

Objective:Write a program to implement various disk scheduling algorithms.

Solution:
#include <bits/stdc++.h>
using namespace std;

int size = 8;
int disk_size = 200;

void FCFS(int arr[], int head)


{
int seek_count = 0;
int distance, cur_track;
for (int i = 0; i < size; i++) {
cur_track = arr[i];
distance = abs(cur_track - head);
seek_count += distance;
head = cur_track;
}
cout << "Total number of seek operations = "<< seek_count << endl;
cout << "Seek Sequence is : ";
for (int i = 0; i < size; i++) {
cout << arr[i] << endl;
}
}
void calculatedifference(int request[], int head,int diff[][2], int n)
{
for(int i = 0; i < n; i++)
{
diff[i][0] = abs(head - request[i]);
}
}
int findMIN(int diff[][2], int n)
{
int index = -1;
int minimum = 1e9;
for(int i = 0; i < n; i++)
{
if (!diff[i][1] && minimum > diff[i][0])
{
minimum = diff[i][0];
index = i;
}
}
return index;
}

void shortestSeekTimeFirst(int request[],int head, int n)


{
if (n == 0)
{
return;
}
int diff[n][2] = { { 0, 0 } };
int seekcount = 0;
int seeksequence[n + 1] = {0};
for(int i = 0; i < n; i++)
{
seeksequence[i] = head;
calculatedifference(request, head, diff, n);
int index = findMIN(diff, n);
diff[index][1] = 1;
seekcount += diff[index][0];
head = request[index];
}
seeksequence[n] = head;
cout << "Total number of seek operations = " << seekcount << endl;
cout << "Seek sequence is : ";
for(int i = 0; i <= n; i++)
{
cout << seeksequence[i] << "\n";
}
}

void SCAN(int arr[], int head, string direction,int size)


{
int seek_count = 0;
int distance, cur_track;
vector<int> left, right;
vector<int> seek_sequence;
if (direction == "left")
left.push_back(0);
else if (direction == "right")
right.push_back(disk_size - 1);

for (int i = 0; i < size; i++) {


if (arr[i] < head)
left.push_back(arr[i]);
if (arr[i] > head)
right.push_back(arr[i]);
}
std::sort(left.begin(), left.end());
std::sort(right.begin(), right.end());
int run = 2;
while (run--) {
if (direction == "left") {
for (int i = left.size() - 1; i >= 0; i--) {
cur_track = left[i];
seek_sequence.push_back(cur_track);
distance = abs(cur_track - head);
seek_count += distance;
head = cur_track;
}
direction = "right";
}
else if (direction == "right") {
for (int i = 0; i < right.size(); i++) {
cur_track = right[i];
seek_sequence.push_back(cur_track);
distance = abs(cur_track - head);
seek_count += distance;
head = cur_track;
}
direction = "left";
}
}
cout << "Total number of seek operations = "<< seek_count << endl;
cout << "Seek Sequence is" << endl;
for (int i = 0; i < seek_sequence.size(); i++) {
cout << seek_sequence[i] << endl;
}
}
void CSCAN(int arr[], int head)
{
int seek_count = 0;
int distance, cur_track;
vector<int> left, right;
vector<int> seek_sequence;
left.push_back(0);
right.push_back(disk_size - 1);
for (int i = 0; i < size; i++) {
if (arr[i] < head)
left.push_back(arr[i]);
if (arr[i] > head)
right.push_back(arr[i]);
}
std::sort(left.begin(), left.end());
std::sort(right.begin(), right.end());
for (int i = 0; i < right.size(); i++) {
cur_track = right[i];
seek_sequence.push_back(cur_track);
distance = abs(cur_track - head);
seek_count += distance;
head = cur_track;
}
head = 0;
seek_count += (disk_size - 1);
for (int i = 0; i < left.size(); i++) {
cur_track = left[i];
seek_sequence.push_back(cur_track);
distance = abs(cur_track - head);
seek_count += distance;
head = cur_track;
}
cout << "Total number of seek operations = " << seek_count << endl;
cout << "Seek Sequence is" << endl;
for (int i = 0; i < seek_sequence.size(); i++) {
cout << seek_sequence[i] << endl;
}
}

void LOOK(int arr[], int head, string direction)


{
int seek_count = 0;
int distance, cur_track;
vector<int> left, right;
vector<int> seek_sequence;
for (int i = 0; i < size; i++) {
if (arr[i] < head)
left.push_back(arr[i]);
if (arr[i] > head)
right.push_back(arr[i]);
}
std::sort(left.begin(), left.end());
std::sort(right.begin(), right.end());
int run = 2;
while (run--) {
if (direction == "left") {
for (int i = left.size() - 1; i >= 0; i--) {
cur_track = left[i];
seek_sequence.push_back(cur_track);
distance = abs(cur_track - head);
seek_count += distance;
head = cur_track;
}
direction = "right";
}
else if (direction == "right") {
for (int i = 0; i < right.size(); i++) {
cur_track = right[i];
seek_sequence.push_back(cur_track);
distance = abs(cur_track - head);
seek_count += distance;
head = cur_track;
}
direction = "left";
}
}
cout << "Total number of seek operations = "<< seek_count << endl;
cout << "Seek Sequence is" << endl;
for (int i = 0; i < seek_sequence.size(); i++) {
cout << seek_sequence[i] << endl;
}
}
void CLOOK(int arr[], int head)
{
int seek_count = 0;
int distance, cur_track;
vector<int> left, right;
vector<int> seek_sequence;
for (int i = 0; i < size; i++) {
if (arr[i] < head)
left.push_back(arr[i]);
if (arr[i] > head)
right.push_back(arr[i]);
}
std::sort(left.begin(), left.end());
std::sort(right.begin(), right.end());
for (int i = 0; i < right.size(); i++) {
cur_track = right[i];
seek_sequence.push_back(cur_track);
distance = abs(cur_track - head);
seek_count += distance;
head = cur_track;
}
seek_count += abs(head - left[0]);
head = left[0];
for (int i = 0; i < left.size(); i++) {
cur_track = left[i];
seek_sequence.push_back(cur_track);
distance = abs(cur_track - head);
seek_count += distance;
head = cur_track;
}
cout << "Total number of seek operations = "<< seek_count << endl;
cout << "Seek Sequence is" << endl;
for (int i = 0; i < seek_sequence.size(); i++) {
cout << seek_sequence[i] << endl;
}
}

int main()
{
int arr[size] = { 176, 79, 34, 60, 92, 11, 41, 114 };
int head = 50;
string direction = "left";

FCFS(arr, head);
cout<<endl;

shortestSeekTimeFirst(arr, head, size);


cout<<endl;

SCAN(arr, head, direction,size);


cout<<endl;

cout << "Initial position of head: " << head << endl;
CSCAN(arr, head);
cout<<endl;

cout << "Initial position of head: "<< head << endl;


LOOK(arr, head, direction);
cout<<endl;

cout << "Initial position of head: " << head << endl;
CLOOK(arr, head);

return 0;
}
Outcomes:

You might also like