Os File
Os File
(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
10
11
12
13
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;
}
printf("\n WT average = %0.3f\n TAT average = %0.3f",wtavg/i,tatavg/i);
}
/ Calling functions in main()
int main() {
enter();
sortat();
Output:
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) {
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:
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
Output:
Aim: Write a program to find average waiting time and average turnaround time of CPU
using Priority(Non Pre-emptive) CPU Schedulling Algorithm.
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;
}
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:
Aim: Write a program to find average waiting time and average turnaround time of CPU
using Priority(Pre-emptive) CPU Schedulling Algorithm.
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;
}
}
printf("\n WT average = %0.3f\n TAT average = %0.3f",wtavg/i,tatavg/i);
}
int main() {
enter(); sortat(); clac(); sortp(); print(); avg();
}
Output:
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;
}
printf("\n WT average = %0.3f\n TAT average = %0.3f",wtavg/i,tatavg/i);
}
int main() {
enter(); sortat(); clac(); sortp(); print(); avg();
}
Output:
Aim: Write a program to find the safe sequence and implement resource allocation
Algorithm.
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 << " ";
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){
void enterData(){
int numProcesses, numResources;
cout << "Enter the number of processes: ";
cin >> numProcesses;
cout << "Enter the number of resources: ";
int main(){
enterData();
return 0;
}
Output:
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){
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;