0% found this document useful (0 votes)
6 views42 pages

Os File

Uploaded by

shobhitcr708
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)
6 views42 pages

Os File

Uploaded by

shobhitcr708
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/ 42

KRISHNA ENGINEERING COLLEGE

(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML

INDEX
Student Roll no – 2101611530041 Student name – Rohan Verma
Subject name – Operating System Lab Subject code – KCS 451

Sno Practical name Scheduled Implementation Output Viva Total Signature


date (10 marks) (5 (5 (20
marks) marks) marks)
1

10

11

12

13

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
Session 2022-23 EVEN semester

Name of the practical – Implement FCFS CPU scheduling algorithm.


Student Roll no – 2101611530041 Practical no – 1
th
Student Name – Rohan Verma Semester/Batch – 4 Sem/ 2021-25

Aim: Write a program to implement the First Come – First Serve CPU Schedulling
Algorithm.

About: In FCFS schedulling algorithm, the process coming first i.e. according to their
arrival time, will be executed first.

Code:
#include <stdio.h>
#include <stdlib.h>
/ structure data type for processes linked-list struct fcfs {
int at,bt,st,wt,tat,p;
struct fcfs *next;
}*start=NULL;
/ Swapping data entries of two
/ processes stored in a linked-list node
void swap(struct fcfs *a,struct fcfs *b) {
int x=a->at;
a->at=b->at;
b->at=x;
x=a->bt;
a->bt=b->bt;
b->bt=x;

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
x=a->p;
a->p=b->p;
b->p=x;
x=a->st;
a->st=b->st;
b->st=x;
x=a->wt;
a->wt=b->wt;
b->wt=x;
x=a->tat;
a->tat=b->tat;
b->tat=x;
}
/ Taking BT and AT of processes as input void enter() {
int t,i;
struct fcfs *node,*temp;
printf(" Number of processes: ");
scanf("%d",&t);
node=(struct fcfs*)malloc(sizeof(struct
fcfs)); printf(" Enter AT and BT of process
1: "); scanf("%d%d",&node->at,&node-
>bt); node->st=node->wt=node->tat=0;
node->p=1;
start=temp=node;
for(i=1;i<t;i++) {
node=(struct fcfs*)malloc(sizeof(struct fcfs));
printf(" Enter AT and BT of process %d: ",i+1);
scanf("%d%d",&node->at,&node->bt);

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
node->st=node->wt=node->tat=0;
node->p=i+1;
temp->next=node;
temp=node;
}
}
/ Sorting the processes according
/ to their arrival time
void sortat() {
struct fcfs *f,*t;
f=start;
while(f!=NULL) {
t=f->next;
while(t!=NULL) {
if(t->at < f->at)
swap(t,f);
t=t-> next;
}
f=f->next;
}
}
/ Calculating the values of
/ and TAT for every process void clac() {
if(!start) {
printf(" The list is
empty"); return;
}

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
start->st=start->at;
start->wt=0;
start->tat=start->bt;
struct fcfs *temp=start->next, *prev=start;
while(temp) {
if(prev->st+prev->bt<temp->at)
temp->st=temp->at;
else
temp->st=prev->st+prev->bt;
temp->wt=temp->st-temp->at;
temp->tat=temp->bt+temp->wt;
prev=temp;
temp=temp->next;
}
}
/ Sorting processes according
/ to their order of input
void sortp() {
struct fcfs *f,*t;
f=start;
while(f!=NULL) {
t=f->next;
while(t!=NULL) {
if(t->p < f->p)
swap(t,f);
t=t-> next;
}
f=f->next;
}

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
}
/ printing the required Output table void print() {
struct fcfs *temp;
temp=start;
printf(" Process Arrival Time Burst Time Waiting Time TurnAround Time \n");
while(temp!=NULL) {
printf(" %d %d %d %d %d\n",temp->p,temp-
>at,temp->bt,temp->wt,temp->tat);
temp=temp->next;
}
}
/ Calculating the average values of WT an TAT void avg() {
struct fcfs* temp=start; float
wtavg=0.0,tatavg=0.0; int
i=0;
while(temp)
{ wtavg+=(float)temp-
>wt;
tatavg+=(float)temp->tat;
i++; temp=temp->next;

}
printf("\n WT average = %0.3f\n TAT average = %0.3f",wtavg/i,tatavg/i);
}
/ Calling functions in main()
int main() {
enter();
sortat();

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
clac();
sortp();
print();
avg();
}

Output:

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
Session 2022-23 EVEN semester

Name of the practical – Implement SJF CPU scheduling algorithm.


Student Roll no – 2101611530041 Practical no – 2
th
Student Name – Rohan Verma Semester/Batch – 4 Sem/ 2021-25

Aim: Write a program to implement the Shortest Job First CPU Schedulling Algorithm.

About: In SJF schedulling algorithm, the process with shortest job i.e. according to
their burst time, will be executed first.

Code:
#include<bits/stdc++.h>
using namespace std;
/ structure data type for processes linked-list struct fcfs {
int at,bt,st,wt,tat,p;
struct fcfs *next;
}*start=NULL;
//comparator for maintaining min-heap
class compare {
public:
bool operator()(struct fcfs* a, struct fcfs* b)
{ return a->bt>b->bt;
}
};
/ Swapping data entries of two
/ processes stored in a linked-list node
void swap(struct fcfs *a,struct fcfs *b) {

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
int x=a->at; a->at=b->at; b->at=x;
x=a->bt; a->bt=b->bt; b->bt=x;
x=a->p; a->p=b->p; b->p=x;
x=a->st; a->st=b->st; b->st=x;
x=a->wt; a->wt=b->wt; b->wt=x;
x=a->tat; a->tat=b->tat; b->tat=x;
}
/ Taking BT and AT of processes as input void enter() {
int t,i;
struct fcfs *node,*temp;
printf(" Number of processes: ");
scanf("%d",&t);
node=(struct fcfs*)malloc(sizeof(struct
fcfs)); printf(" Enter AT and BT of process
1: "); scanf("%d%d",&node->at,&node-
>bt); node->st=node->wt=node->tat=0;
node->p=1;
start=temp=node;
for(i=1;i<t;i++) {
node=(struct fcfs*)malloc(sizeof(struct fcfs));
printf(" Enter AT and BT of process %d:
",i+1); scanf("%d%d",&node->at,&node->bt);
node->st=node->wt=node->tat=0; node-
>p=i+1;
temp->next=node;
temp=node;
}
}

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
/ Sorting the processes according
/ to their arrival time
void sortat() {
struct fcfs *f,*t;
f=start;
while(f!=NULL) {
t=f->next;
while(t!=NULL) {
if(t->at < f->at)
swap(t,f);
t=t-> next;
}
f=f->next;
}
}
/ Calculating the values of
/ and TAT for every process void clac() {
priority_queue<struct fcfs*, vector<struct fcfs*>, compare> pq;
int timer=start->at;
struct fcfs* temp=start, *node;
while(!pq.empty()||temp) {
while(temp&&temp->at<=timer) {
pq.push(temp); temp=temp-
>next;
}
if(pq.empty())
{ timer=temp-
>at;
pq.push(temp);

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
temp=temp->next;
}
node=pq.top();
pq.pop();
node->st=timer;
timer+=node->bt;
node->wt=node->st-node->at;
node->tat=node->wt+node->bt;
}
}
/ Sorting processes according
/ to their order of input
void sortp() {
struct fcfs *f,*t;
f=start;
while(f!=NULL) {
t=f->next;
while(t!=NULL) {
if(t->p < f->p)
swap(t,f);
t=t-> next;
}
f=f->next;
}
}
/ printing the required Output table void print() {
struct fcfs *temp;
temp=start;

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML

printf(" Process Arrival Time Burst Time Waiting Time TurnAround Time \n");
while(temp!=NULL) {
printf(" %d %d %d %d %d\n",temp->p,temp-
>at,temp->bt,temp->wt,temp->tat);
temp=temp->next;
}
}
/ Calculating the average values of WT an TAT void avg() {
struct fcfs* temp=start; float
wtavg=0.0,tatavg=0.0; int
i=0;
while(temp)
{ wtavg+=(float)temp-
>wt;
tatavg+=(float)temp->tat;
i++; temp=temp->next;

}
printf("\n WT average = %0.3f\n TAT average = %0.3f",wtavg/i,tatavg/i);
}
/ Calling functions in main function
int main() {
enter();sortat();clac();sortp();print();avg();
}

Output:

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
Session 2022-23 EVEN semester

Name of the practical – Implement SRTF CPU scheduling algorithm.


Student Roll no – 2101611530041 Practical no – 3
Student Name – Rohan Verma Semester/Batch – 4th Sem/ 2021-25

Aim: Write a program to implement the Shortest Remaining Time First CPU
Schedulling Algorithm.

About: In SRTF(Pre-emptive SJF) schedulling algorithm, the process with shortest job
i.e. according to their remaining burst time, will be executed first.

Code:
#include<bits/stdc++.h>
using namespace std;
/ structure data type for processes linked-list struct fcfs {
int at,bt,ct,rt,wt,tat,p;
struct fcfs *next;
}*start=NULL;
//comparator for maintaining min-heap
class compare {
public:
bool operator()(struct fcfs* a, struct fcfs* b)
{ return a->rt>b->rt;
}
};
/ Swapping data entries of two
/ processes stored in a linked-list node

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
void swap(struct fcfs *a,struct fcfs *b) {
int x=a->at; a->at=b->at; b->at=x;
x=a->bt; a->bt=b->bt; b->bt=x;
x=a->p; a->p=b->p; b->p=x;
x=a->ct; a->ct=b->ct; b->ct=x;
x=a->wt; a->wt=b->wt; b->wt=x;
x=a->tat; a->tat=b->tat; b->tat=x;
x=a->rt; a->rt=b->rt; b->rt=x;
}
/ Taking BT and AT of processes as input void enter() {
int t,i;
struct fcfs *node,*temp;
printf("number of processes: ");
scanf("%d",&t);
node=(struct fcfs*)malloc(sizeof(struct
fcfs)); printf("enter at and bt of process 1:
"); scanf("%d%d",&node->at,&node->bt);
node->wt=node->tat=0;
node->p=1; node-
>rt=node->bt; node-
>ct=node->at;
start=temp=node;
for(i=1;i<t;i++) {
node=(struct fcfs*)malloc(sizeof(struct
fcfs)); printf("enter at and bt of process %d:
",i+1); scanf("%d%d",&node->at,&node-
>bt); node->wt=node->tat=0;
node->p=i+1;

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
node->rt=node->bt;
node->ct=node->at;
temp->next=node;
temp=node;
}}
/ Sorting the processes according
/ to their arrival time
void sortat() {
struct fcfs *f,*t;
f=start;
while(f!=NULL) {
t=f->next;
while(t!=NULL) {
if(t->at < f->at)
swap(t,f);
t=t-> next;
}
f=f->next;
}}
/ Calculating the values of
/ and TAT for every process void clac()
{
priority_queue<struct fcfs*, vector<struct fcfs*>, compare> pq;
int timer=start->at,l;
struct fcfs* temp=start, *node;
while(!pq.empty()||temp) {
while(temp&&temp->at<=timer)
{ pq.push(temp);

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
temp=temp->next;
}
if(pq.empty()) {
timer=temp->at;
pq.push(temp);
temp=temp->next;
}
node=pq.top();
node->wt+=timer-node->ct;
if(temp) {
l=temp->at-timer;
if(node->rt>=l) {
node->rt-=l;
timer=temp->at;
}
else {
timer+=node->rt;
node->rt=0;
}}
else {
timer+=node->rt;
node->rt=0;
}
node->ct=timer;
if(!node->rt) {
node->wt+=timer-node->ct;
node->tat=node->wt+node->bt;
pq.pop();
}}}

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
/ Sorting processes according
/ to their order of input
void sortp() {
struct fcfs *f,*t;
f=start;
while(f!=NULL) {
t=f->next;
while(t!=NULL) {
if(t->p < f->p)
swap(t,f);
t=t-> next;
}
f=f->next;
}}
/ printing the required Output table void print() {
struct fcfs *temp;
temp=start;
printf(" Process Arrival Time Burst Time Waiting Time TurnAround Time \n");
while(temp!=NULL) {
printf(" %d %d %d %d %d\n",temp->p,temp-
>at,temp->bt,temp->wt,temp->tat);
temp=temp->next;
}}
/ Calculating the average values of WT an TAT void avg() {
struct fcfs* temp=start;
float wtavg=0.0,tatavg=0.0;
int i=0;

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
while(temp) {
wtavg+=(float)temp->wt;
tatavg+=(float)temp->tat;
i++;
temp=temp->next;
}
printf("\n WT average = %0.3f\n TAT average = %0.3f",wtavg/i,tatavg/i);
}
/ Calling functions in main() int main() {
enter(); sortat(); clac(); sortp(); print(); avg();
}

Output:

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
Session 2022-23 EVEN semester

Name of the practical – Implement Priority(Non Pre-emptive) CPU scheduling


algorithm.
Student Roll no – 2101611530041 Practical no – 4
Student Name – Rohan Verma Semester/Batch – 4th Sem/ 2021-25

Aim: Write a program to find average waiting time and average turnaround time of CPU
using Priority(Non Pre-emptive) CPU Schedulling Algorithm.

About: In Priority(Non Pre-emptive) schedulling algorithm, the process with highest


priority, will be executed first.

Code:
#include<bits/stdc++.h>
using namespace std;
/ structure data type for processes linked-list struct fcfs {
int at,bt,st,wt,tat,p,pr;
struct fcfs *next;
}*start=NULL;
//comparator for maintaining min-
heap class compare {
public:
bool operator()(struct fcfs* a, struct fcfs* b)
{ return a->pr>b->pr;
}
};
/ Swapping data entries of two
/ processes stored in a linked-list node
void swap(struct fcfs *a,struct fcfs *b) {
int x=a->at; a->at=b->at; b->at=x;
x=a->bt; a->bt=b->bt; b->bt=x;
x=a->p; a->p=b->p; b->p=x;
x=a->pr; a->pr=b->pr; b->pr=x;
x=a->st; a->st=b->st; b->st=x;
x=a->wt; a->wt=b->wt; b->wt=x;

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
x=a->tat; a->tat=b->tat; b->tat=x;
}
/ Taking BT and AT of processes as input void enter() {
int t,i;
struct fcfs *node,*temp;
printf(" Number of processes: ");
scanf("%d",&t);
node=(struct fcfs*)malloc(sizeof(struct fcfs)); printf("
Enter AT, BT and Priority of process 1: "); scanf("%d
%d%d",&node->at,&node->bt,&node->pr); node-
>st=node->wt=node->tat=0;
node->p=1;
start=temp=node;
for(i=1;i<t;i++) {
node=(struct fcfs*)malloc(sizeof(struct fcfs));
printf(" Enter AT, BT and Priority of process %d:
",i+1); scanf("%d%d%d",&node->at,&node-
>bt,&node->pr); node->st=node->wt=node->tat=0;
node->p=i+1;
temp->next=node;
temp=node;
}
}
/ Sorting the processes according
/ to their arrival time
void sortat() {
struct fcfs *f,*t;
f=start;
while(f!=NULL) {
t=f->next;
while(t!=NULL) {
if(t->at < f->at)
swap(t,f);
t=t-> next;
}
f=f->next;
}
}
/ Calculating the values of
/ and TAT for every process void clac() {
priority_queue<struct fcfs*, vector<struct fcfs*>, compare>
pq; int timer=start->at;

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
struct fcfs* temp=start, *node;
while(!pq.empty()||temp) {
while(temp&&temp->at<=timer) {
pq.push(temp);
temp=temp->next;
}
if(pq.empty()) {
timer=temp->at;
pq.push(temp);
temp=temp->next;
}
node=pq.top();
pq.pop();
node->st=timer;
timer+=node->bt;
node->wt=node->st-node->at;
node->tat=node->wt+node->bt;
}
}
/ Sorting processes according
/ to their order of input
void sortp() {
struct fcfs *f,*t;
f=start;
while(f!=NULL) {
t=f->next;
while(t!=NULL) {
if(t->p < f->p)
swap(t,f);
t=t-> next;
}
f=f->next;
}
}
/ printing the required Output table void print() {
struct fcfs *temp;
temp=start;
printf(" Process Arrival Time Burst Time Priority Waiting Time TurnAround Time \n");
while(temp!=NULL) {
%d %d
printf(" %d %d %d %d\n",temp->p,temp->at,temp->bt,temp->pr,temp->wt,temp-
>tat);
temp=temp->next;
}

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
}
/ Calculating the average values of WT an TAT void avg() {
struct fcfs* temp=start; float
wtavg=0.0,tatavg=0.0; int
i=0;
while(temp)
{ wtavg+=(float)temp-
>wt;
tatavg+=(float)temp->tat;
i++; temp=temp->next;

}
printf("\n WT average = %0.3f\n TAT average = %0.3f",wtavg/i,tatavg/i);
}
/ Calling functions in main function
int main() {
enter();sortat();clac();sortp();print();avg();
}

Output:

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
Session 2022-23 EVEN semester

Name of the practical – Implement Priority(Pre-emptive) CPU scheduling algorithm.


Student Roll no – 2101611530041 Practical no – 5
Student Name – Rohan Verma Semester/Batch – 4th Sem/ 2021-25

Aim: Write a program to find average waiting time and average turnaround time of CPU
using Priority(Pre-emptive) CPU Schedulling Algorithm.

About: In Priority(Pre-emptive) schedulling algorithm, the process with highest priority,


will be executed first among remaining processes.

Code:
#include<bits/stdc++.h>
using namespace std;
/ structure data type for processes linked-list struct fcfs {
int at,bt,ct,rt,wt,tat,p,pr;
struct fcfs *next;
}*start=NULL;
//comparator for maintaining min-
heap class compare {
public:
bool operator()(struct fcfs* a, struct fcfs* b)
{ return a->pr>b->pr;
}
};
/ Swapping data entries of two processes stored in a linked-list node void swap(struct fcfs *a,struct fcfs *b) {
int x=a->at; a->at=b->at; b->at=x;
x=a->bt; a->bt=b->bt; b->bt=x;
x=a->p; a->p=b->p; b->p=x; x=a-
>pr; a->pr=b->pr; b->pr=x; x=a-
>ct; a->ct=b->ct; b->ct=x; x=a-
>wt; a->wt=b->wt; b->wt=x; x=a-
>tat; a->tat=b->tat; b->tat=x; x=a-
>rt; a->rt=b->rt; b->rt=x;
}

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
/ Taking BT and AT of processes as input void enter() {
int t,i;
struct fcfs *node,*temp;
printf("number of processes:
"); scanf("%d",&t);
node=(struct fcfs*)malloc(sizeof(struct fcfs));
printf("enter at, bt and priority of process 1: ");
scanf("%d%d%d",&node->at,&node->bt,&node->pr);
node->wt=node->tat=0; node->p=1; node->rt=node-
>bt; node->ct=node->at; start=temp=node; for(i=1;i<t;i+
+) {
node=(struct fcfs*)malloc(sizeof(struct fcfs)); printf("enter
at, bt and priority of process %d: ",i+1); scanf("%d%d
%d",&node->at,&node->bt,&node->pr); node->wt=node-
>tat=0; node->p=i+1; node->rt=node->bt; node-
>ct=node->at; temp->next=node; temp=node;
}}
/ Sorting the processes according to their arrival time
void sortat() {
struct fcfs *f,*t;
f=start;
while(f!=NULL) {
t=f->next;
while(t!=NULL) {
if(t->at < f->at)
swap(t,f);
t=t-> next;
}
f=f->next;
}}
/ Calculating the values of WT and TAT for every process void clac()
{
priority_queue<struct fcfs*, vector<struct fcfs*>, compare>
pq; int timer=start->at,l;
struct fcfs* temp=start, *node;
while(!pq.empty()||temp) {
while(temp&&temp->at<=timer) {
pq.push(temp); temp=temp-
>next;
}
if(pq.empty())
{ timer=temp-
>at;

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
pq.push(temp);
temp=temp->next;
}
node=pq.top();
node->wt+=timer-node->ct;
if(temp) {
l=temp->at-timer;
if(node->rt>=l) {
node->rt-=l;
timer=temp->at;
}
else {
timer+=node->rt;
node->rt=0;
}}
else {
timer+=node->rt;
node->rt=0;
}
node->ct=timer;
if(!node->rt) {
node->wt+=timer-node->ct;
node->tat=node->wt+node->bt;
pq.pop();
}}}
/ Sorting processes according to their order of input void sortp() {
struct fcfs *f,*t;
f=start; while(f!
=NULL) {
t=f->next; while(t!
=NULL) {
if(t->p < f->p)
swap(t,f);
t=t-> next;
}
f=f->next;
}}
/ printing the required Output table
void print() {
struct fcfs *temp;
temp=start;
printf(" Process Arrival Time Burst Time Priority Waiting Time TurnAround Time \n");
while(temp!=NULL) {

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
printf(" %d %d %d %d %d %d\n",temp->p,temp->at,temp->bt,temp->pr,temp->wt,temp-
>tat);
temp=temp->next;
}}
/ Calculating the average values of WT and TAT void avg() {
struct fcfs* temp=start; float
wtavg=0.0,tatavg=0.0; int
i=0;
while(temp)
{ wtavg+=(float)temp-
>wt;
tatavg+=(float)temp->tat;
i++; temp=temp->next;

}
printf("\n WT average = %0.3f\n TAT average = %0.3f",wtavg/i,tatavg/i);
}
int main() {
enter(); sortat(); clac(); sortp(); print(); avg();
}

Output:

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
Session 2022-23 EVEN semester

Name of the practical – Implement Round Robin CPU scheduling algorithm.


Student Roll no – 2101611530041 Practical no – 6
Student Name – Rohan Verma Semester/Batch – 4th Sem/ 2021-25

Aim: Write a program to find average waiting time and average turnaround time of CPU
using Round Robin CPU Schedulling Algorithm.

About: In Round Robin schedulling algorithm, the process will be executed according
to their placement in the ready queue.

Code:
#include<bits/stdc++.h>
using namespace std;
/ structure data type for processes linked-list struct fcfs {
int at,bt,ct,rt,wt,tat,p,pr;
struct fcfs *next;
}*start=NULL;
//comparator for maintaining min-heap
class compare {
public:
bool operator()(struct fcfs* a, struct fcfs* b) {
return a->pr>b->pr;
}
};
/ Swapping data entries of two processes stored in a linked-list node void swap(struct fcfs *a,struct fcfs
*b) {
int x=a->at; a->at=b->at; b->at=x;
x=a->bt; a->bt=b->bt; b->bt=x;
x=a->p; a->p=b->p; b->p=x; x=a-
>pr; a->pr=b->pr; b->pr=x;

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
x=a->ct; a->ct=b->ct; b->ct=x;
x=a->wt; a->wt=b->wt; b->wt=x;
x=a->tat; a->tat=b->tat; b->tat=x;
x=a->rt; a->rt=b->rt; b->rt=x;
}
/ Taking BT and AT of processes as input void enter() {
int t,i;
struct fcfs *node,*temp;
printf("number of processes: ");
scanf("%d",&t);
node=(struct fcfs*)malloc(sizeof(struct fcfs));
printf("enter at, bt and priority of process 1: ");
scanf("%d%d%d",&node->at,&node->bt,&node->pr);
node->wt=node->tat=0; node->p=1; node->rt=node->bt;
node->ct=node->at; start=temp=node; for(i=1;i<t;i++) {

node=(struct fcfs*)malloc(sizeof(struct fcfs)); printf("enter


at, bt and priority of process %d: ",i+1); scanf("%d%d
%d",&node->at,&node->bt,&node->pr); node->wt=node-
>tat=0; node->p=i+1; node->rt=node->bt; node-
>ct=node->at; temp->next=node; temp=node;
printf("Enter the value of Time Quantum: ");
scanf("%d",&tc);
}}
/ Sorting the processes according to their arrival time
void sortat() {
struct fcfs *f,*t;
f=start;
while(f!=NULL) {
t=f->next;
while(t!=NULL) {
if(t->at < f->at)
swap(t,f);
t=t-> next;

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
}
f=f->next;
}}
/ Calculating the values of WT and TAT for every process void clac()
{
queue<struct fcfs*> pq;
int timer=start->at;
struct fcfs* temp=start, *node;
while(!pq.empty()||temp){
if(!pq.empty()){
node=pq.front();
pq.pop(); }
else{ while(temp&&temp-
>at<=timer){
pq.push(temp);
temp=temp->next; }
if(pq.empty())
timer=temp->at;
continue;} node-
>wt+=timer-node->ct;
if(node->rt>tc){
timer+=tc; node->rt-=tc;
while(temp&&temp->at<=timer){
pq.push(temp);
temp=temp->next; }
pq.push(node); }
else{
timer+=node->rt;
node->rt=0; }
node->ct=timer;
if(!node->rt){
node->wt+=timer-node->ct; node-
>tat=node->wt+node->bt; }}}

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
/ Calculating the average values of WT and TAT void avg() {
struct fcfs* temp=start; float
wtavg=0.0,tatavg=0.0; int
i=0;
while(temp)
{ wtavg+=(float)temp-
>wt;
tatavg+=(float)temp->tat;
i++; temp=temp->next;

}
printf("\n WT average = %0.3f\n TAT average = %0.3f",wtavg/i,tatavg/i);
}
int main() {
enter(); sortat(); clac(); sortp(); print(); avg();
}
Output:

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
Session 2022-23 EVEN semester

Name of the practical – Implement resource allocation and deadlock detection(


Banker’s) Algorithms.
Student Roll no – 2101611530041 Practical no – 7
Student Name – Rohan Verma Semester/Batch – 4th Sem/ 2021-25

Aim: Write a program to find the safe sequence and implement resource allocation
Algorithm.

About: In Banker’s Algorithm of Deadlock Detection and Resource allocation Algorithm,


The safe sequence and resource allocation is done on the basis of their need matrix.

Code:
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> allocation, maximum, need;
vector<int> available, request;
void display_matrices() {
int n = allocation.size(), m = available.size();
cout << " Processes \t Allocated \t Maximum Need \t Available\n\t\t A B C \t A B C A B C \t A B
C\n";
for (int i = 0; i < n; i++) {
cout<<" P"<<i<<"\t\t ";
for (int j = 0; j < m; j++) {
cout << allocation[i][j] << " ";
}
cout << " ";

for (int j = 0; j < m; j++) {


cout << maximum[i][j] << " ";
}

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
cout << " ";
for (int j = 0; j < m; j++) {
cout << need[i][j] << " ";
}
cout << " ";
if(i==0){
for (int j = 0; j < m; j++) {
cout << available[j] << " ";
}}
cout << endl;
}}

bool isSafeState(){
int numProcesses = need.size(), numResources =
available.size(); vector<bool> isCompleted(numProcesses,
false); vector<int> safeSequence, work = available;
while (true){
bool found = false;
for (int i = 0; i < numProcesses; ++i){
if (!isCompleted[i]){
bool canExecute = true;
for (int j = 0; j < numResources; ++j){
if (need[i][j] > work[j]){
canExecute = false;
break;
}}
if (canExecute){
for (int j = 0; j < numResources; ++j){
work[j] += allocation[i][j];
}
isCompleted[i] = true;
safeSequence.push_back(i);
found = true;
}}}
if (!found){

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
break;
}}
if (safeSequence.size() == numProcesses){
cout << "The system is in a safe state. Safe sequence: ";
for (int i = 0; i < numProcesses - 1; ++i){
cout << safeSequence[i] << " -> ";
}
cout << safeSequence[numProcesses - 1] << endl;
return true;
}
else{
cout << "The system is in an unsafe state." << endl;
return false;
}}

bool requestResources(int processIndex){


int numResources = request.size(), numProcesses = need.size();
for (int i = 0; i < numResources; ++i){
if (request[i] > need[processIndex][i] || request[i] > available[i]){
cout << "The request for this process is invalid. Please try again." << endl;
return false;
}}
for (int i = 0; i < numResources; ++i){
available[i] -= request[i];
allocation[processIndex][i] += request[i];
need[processIndex][i] -= request[i];
}
return isSafeState();
}

void enterData(){
int numProcesses, numResources;
cout << "Enter the number of processes: ";
cin >> numProcesses;
cout << "Enter the number of resources: ";

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
cin >> numResources;
allocation.resize(numProcesses, vector<int>(numResources));
maximum.resize(numProcesses, vector<int>(numResources));
need.resize(numProcesses, vector<int>(numResources));
available.resize(numResources);
for (int i = 0; i < numProcesses; ++i){
cout << "Enter the allocated resources to process " << i + 1 << ":
"; for (int j = 0; j < numResources; ++j){
cin >> allocation[i][j];
}}
for (int i = 0; i < numProcesses; ++i){
cout << "Enter the maximum resources to process " << i + 1 << ": ";
for (int j = 0; j < numResources; ++j){
cin >> maximum[i][j];
need[i][j] = maximum[i][j] - allocation[i][j];
}}
cout << "Enter the resources available: ";
for (int i = 0; i < numResources; ++i){
cin >> available[i];
}
display_matrices();
isSafeState();
int numRequests;
cout << "Enter the number of resource requests: ";
cin >> numRequests;
for (int i = 0; i < numRequests; ++i){
int processIndex;
cout << "Enter the process index and the request of resources in sequence: ";
cin >> processIndex;
if (processIndex >= numProcesses){
cout << "The entered process index is invalid. Please try again." << endl;
--i;
continue;
}
request.resize(numResources);

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
for (int j = 0; j < numResources; ++j){
cin >> request[j];
}
if (!requestResources(processIndex)){
--i;
}}}

int main(){
enterData();
return 0;
}
Output:

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
Session 2022-23 EVEN semester

Name of the practical – Implement First Fit, Best Fit and Worst Fit Algorithms in Fixed
Partitioning Scheme.
Student Roll no – 2101611530041 Practical no – 8
Student Name – Rohan Verma Semester/Batch – 4th Sem/ 2021-25

Aim: Write a program to find the Total Internal Fragmentation in First Fit, Best Fit and
Worst Fit Algorithms in Fixed Partitioning Scheme.

About: In First Fit, Best Fit and Worst Fit Algorithms in Fixed Partitioning Scheme, the
total internal fragmentation is calculated by the sum of all the leftover space in the
allocated partitions.

Code:
#include <stdio.h>
int main(){
int mem[20] , pro[20], par[20];
int i,j,n,m,ch,pos=-1,f,IF = 0;
printf("Enter no. of Process : ");
scanf("%d",&m);
printf("Enter the Process : ");
for (i=0; i<m; i++)
scanf("%d",&mem[i]);
printf("Enter no. of Process : ");
scanf("%d",&n);
printf("Enter the Process : ");
for (i=0; i<n; i++)
scanf("%d",&pro[i]);
printf("1. First Fit\n2. Best Fit\n3. Worst Fit\nENTER YOUR CHOICE: ");
scanf("%d",&ch);
switch(ch){

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
case 1:
for (j=0; j<n; j++) {
for (i=0; i<6; i++) {
if (pro[j] <= mem[i]){
IF += mem[i] - pro[j];
par[i]=j;
mem[i] = -1;
break;
}}}
case 2:
for (j=0; j<n; j++) {
f=0;
for (i=0; i<6; i++) {
if (pro[j] <= mem[i]){
if(!f){
pos=i;
f=1;}
if(mem[pos]>mem[i])
pos=i;
}}
if(pos==-1||mem[pos]==-1)
continue;
IF+=mem[pos]-pro[j];
par[i]=j;
mem[pos]=-1;
}
case 3:
for (j=0; j<n; j++) {
f=0;
for (i=0; i<6; i++) {
if (pro[j] <= mem[i]){
if(!f){
pos=i;
f=1;}
if(mem[pos]<mem[i])

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
pos=i;
}}
if(pos==-1)
continue;
IF+=mem[pos]-pro[j];
par[i]=j;
mem[pos]=-1;
}
default: printf("Invalid Choice!!");
}
printf("process no.\tprocess size\tpartition no.\n");
for (i=0;i<n;i++)
printf("%d\t\t%d\t\t%d\n",i+1, pro[i], par[i]);
printf("Total internal fregmatation : %d", IF);
}
Output:

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
Session 2022-23 EVEN semester

Name of the practical – Implement First Fit, Best Fit and Worst Fit Algorithms in
Dynamic Holes Scheme.
Student Roll no – 2101611530041 Practical no – 9
Student Name – Rohan Verma Semester/Batch – 4th Sem/ 2021-25

Aim: Write a program to find the Total External Fragmentation in First Fit, Best Fit and
Worst Fit Algorithms in Dynamic Holes Scheme.

About: In First Fit, Best Fit and Worst Fit Algorithms in Dynamic Holes Scheme, the
total external fragmentation is calculated by the sum of all the leftover holes.

Code:
#include <stdio.h>
int main(){
int mem[20],pro[20],alloc[20],par[20],flag,i,ch,tex=0,j,m,p;

printf("Enter the no. of memory Holes : "); scanf("%d",&m);

printf("Enter the no. of processes : ");


scanf("%d",&p);
printf("Enter the values of memory Holes : ");
for(i=0;i<m;i++)
scanf("%d", &mem[i]);
printf("Enter the size of processes : ");
for(i=0;i<p;i++)
scanf("%d", &pro[i]);
printf("ENTER YOUR CHOICE \n1.First Fit \n2.Best Fit \n3.Worst Fit");
scanf("%d",&ch);
switch(ch) {
case 1:
for(i=0;i<p;i++){
flag=0;
Faculty Remarks & Signature
KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
for(j=0;j<m;j++){
if(pro[i]<=mem[j]){
alloc[i]=j;
mem[j]=mem[j]-pro[i];
flag=1;
break; }
if(flag==0){
alloc[i]=-1;
}}}
break;
case 2:
for(i=0;i<p;i++){
int low=-1,temp=0;
for(j=0;j<m;j++){
if(pro[i]<=mem[j] && alloc[i]==0){
temp=mem[j]-pro[i];
low=j;
alloc[i]=1; }
if(pro[i]<=mem[j] && temp >= mem[j]-pro[i] && alloc[i]==1){
temp=mem[j]-pro[i];
low=j; }}
if(low!=-1){
par[i]=low;
mem[low]=mem[low]-pro[i];
}}
break;
case 3:
for(i=0;i<p;i++){
int low=-1,temp=0,alloc=0;
for(j=0;j<m;j++){
if(pro[i]<=mem[j] && alloc==0){
temp=mem[j]-pro[i];
low=j;
alloc=1; }
if(pro[i]<=mem[j] && temp <= mem[j]-pro[i] && alloc==1){

Faculty Remarks & Signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)
Department of AI & ML
temp=mem[j]-pro[i];
low=j; }}
if(low!=-1){
par[i]=low;
mem[low]=mem[low]-pro[i]; }
else
par[i]=-1;
}
break;
default :
printf("Enter a valid choice");
}
printf("process no.\tprocess size\tpartition no.\n");
for (i=0; i<p;i++)
printf("%d\t\t%d\t\t%d\n",i+1, pro[i], alloc[i]);
for(i=0;i<m;i++)
tex=tex+mem[i];
printf("total external fragmentation is %d",tex);
return 0;
}
Output:

Faculty Remarks & Signature

You might also like