Os Lab File
Os Lab File
UNIVERSITY OF
TECHNOLOGY
PRACTICAL FILE
Index
1.System calls
fork(),exec(),getpid(),getppid(),wait(),exit().
2.Producer-Consumer
Bounded Buffer Problem
IPC(Inter process communication)
3.CPU Scheduling Algorithms
a)FCFS
b)SJF
c)Preemptive-SJF
d)Priority
e)Preemptive-Priority
f)Round robin
g)Multilevel Feedback queue
4.Critical Section Problem
5.Deadlock Detection & Avoidance Algorithm
6.Memory Allocation methods
a) First fit
b) Best Fit
c) Worst Fit
7.Page replacement Algorithm
a) FIFO
b) LRU
c) LFU
d) Second chance
e) Enhanced Second chance
f) MRU
g) MFU
8.Disk Scheduling Algorithms
a) FCFS
b) SSTF
c) SCAN
d) C-SCAN
e) LOOK
f) C-LOOK
Q1. Write a program for system calls.
Fork()
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
printf("Hello world!\n");
return 0;
}
OUTPUT
Getpid()
#include <iostream>
#include <unistd.h>
using namespace std;
// Driver Code
int main()
{
int pid = fork();
if (pid == 0)
cout << "\nCurrent process id of Process : "
<< getpid() << endl;
return 0;
}
OUTPUT
Getppid()
#include <iostream>
#include <unistd.h>
using namespace std;
// Driver Code
int main()
{
int pid;
pid = fork();
if (pid == 0)
{
cout << "\nParent Process id : "
<< getpid() << endl;
cout << "\nChild Process with parent id : "
<< getppid() << endl;
}
return 0;
}
OUTPUT
Exec()
#include <unistd.h>
int main(void) {
char *binaryPath = "/bin/ls";
char *arg1 = "-lh";
char *arg2 = "/home";
OUTPUT
Wait()
#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<sys/wait.h>
int main()
{
pid_t p;
printf("before fork\n");
p=fork();
if(p==0)//younger
{
printf("I am Rohan my id %d\n",getpid());
printf("My name is Rohit(elder than Rohan) id is %d\n",getppid());
}
else//elder
{
wait(NULL);
printf("Rohan id is %d\n",p);
printf("Rohit id %d\n",getpid());
}
printf("Common\n");
}
OUTPUT
Exit()
#include<iostream>
using namespace std;
int main()
{
int i,num;
cout<<"Enter the number : ";
cin>>num;
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
cout<<"\nNot a prime number!";
exit(0);
}
}
cout<<"\n prime number!";
return 0;
}
OUTPUT
int main() {
cout << "Received message #" << msg.id << ": " << msg.data
<< endl;
}
mq_close(queue);
exit(0);
} else {
cout << "Sent message #" << msg.id << ": " << msg.data <<
endl;
usleep(rand() % 1000000);
}
mq_close(queue);
mq_unlink(QUEUE_NAME);
exit(0);
}
return 0;
}
OUTPUT
PC
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
using namespace std;
const int MAX_BUFFER_SIZE = 10;
queue<int> buffer;
mutex bufferMutex;
condition_variable bufferCV;
void producer() {
for (int i = 0; i < 20; i++) {
int item = rand() % 100;
unique_lock<mutex> lock(bufferMutex);
bufferCV.wait(lock, [] { return buffer.size() <
MAX_BUFFER_SIZE; });
buffer.push(item);
cout << "Produced item: " << item << endl;
bufferCV.notify_all();
}
}
void consumer() {
for (int i = 0; i < 20; i++) {
unique_lock<mutex> lock(bufferMutex);
bufferCV.wait(lock, [] { return !buffer.empty(); });
int item = buffer.front();
buffer.pop();
cout << "Consumed item: " << item << endl;
bufferCV.notify_all();
}
}
int main() {
thread producerThread(producer);
thread consumerThread(consumer);
producerThread.join();
consumerThread.join();
return 0;
}
OUTPUT
Q3. CPU Scheduling Algorithm
FCFS
#include<iostream>
using namespace std;
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
cout<<"Enter total number of processes(maximum 20):";
cin>>n;
cout<<"\nEnter Process Burst Time\n";
for(i=0;i<n;i++)
{
cout<<"P["<<i+1<<"]:";
cin>>bt[i];
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
cout<<"\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time";
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
cout<<"\nP["<<i+1<<"]"<<"\t\t"<<bt[i]<<"\t\t"<<wt[i]<<"\t\t"<<tat[i];
}
avwt/=i;
avtat/=i;
cout<<"\n\nAverage Waiting Time:"<<avwt;
cout<<"\nAverage Turnaround Time:"<<avtat;
return 0;
}
OUTPUT
SJF
#include <iostream>
#include <algorithm>
#include <vector>
struct Process
{
int pid;
int arrival_time;
int burst_time;
};
bool compare_arrival_time(Process a, Process b)
{
return a.arrival_time < b.arrival_time;
}
int main()
{
int n;
cout << "Enter the number of processes: ";
cin >> n;
vector<Process> processes(n);
int current_time = 0;
swap(processes[i], processes[shortest_job_index]);
average_waiting_time += waiting_time[i];
average_turnaround_time += turnaround_time[i];
}
average_waiting_time /= n;
average_turnaround_time /= n;
cout << "\nAverage Waiting Time: " << average_waiting_time << endl;
cout << "Average Turnaround Time: " << average_turnaround_time <<
endl;
return 0;
}
OUTPUT
Premptive SJF
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
int main()
{
int n;
cout << "Enter the number of processes: ";
cin >> n;
vector<Process> processes(n);
int current_time = 0;
int completed_processes = 0;
if (pq.empty())
{
current_time++;
continue;
}
if (processes[process_index].remaining_time ==
processes[process_index].burst_time)
{
processes[process_index].completion_time = current_time +
processes[process_index].burst_time;
}
processes[process_index].remaining_time--;
current_time++;
if (processes[process_index].remaining_time == 0)
{
completed_processes++;
processes[process_index].turnaround_time =
processes[process_index].completion_time -
processes[process_index].arrival_time;
processes[process_index].waiting_time =
processes[process_index].turnaround_time -
processes[process_index].burst_time;
}
}
average_waiting_time += processes[i].waiting_time;
average_turnaround_time += processes[i].turnaround_time;
}
average_waiting_time /= n;
average_turnaround_time /= n;
cout << "\nAverage Waiting Time: " << average_waiting_time << endl;
cout << "Average Turnaround Time: " << average_turnaround_time <<
endl;
return 0;
}
OUTPUT
Round Robin
#include <iostream>
#include <vector>
using namespace std;
int main(){
int i,n,time,remain,temps=0,time_quantum;
int wt=0,tat=0;
cout<<"Enter the total number of process="<<endl;
cin>>n;
remain=n;
vector<int>at(n);
vector<int>bt(n);
vector<int>rt(n);
rt[i]=0;
temps=1;
}
else if(rt[i]>0)
{
rt[i] -= time_quantum;
time += time_quantum;
printf("Process{%d}\t:\t%d\t:\t%d\n",i+1,time-at[i],time-at[i]-bt[i]);
cout<<endl;
wt += time-at[i]-bt[i];
tat += time-at[i];
temps=0;
}
if(i == n-1)
i=0;
else if(at[i+1] <= time)
i++;
else
i=0;
}
return 0;
}
OUTPUT
Priority Scheduling
#include<iostream>
#include<algorithm>
#include<vector>
int n = processes.size();
int waiting_time[n], turnaround_time[n], completion_time[n];
double total_waiting_time = 0, total_turnaround_time = 0;
total_waiting_time += waiting_time[i];
total_turnaround_time += turnaround_time[i];
}
int main() {
int n; // number of processes
cout << "Enter the number of processes: ";
cin >> n;
vector<process> processes(n);
return 0;
}
OUTPUT
struct Process {
int pid;
int arrivalTime;
int burstTime;
int priority;
};
struct ComparePriority {
bool operator()(const Process& p1, const Process& p2) {
return p1.priority > p2.priority;
}
};
int currentTime = 0;
priority_queue<Process, vector<Process>, ComparePriority> pq;
cout << "PID " << currentProcess.pid << " executed at time
" << currentTime
<< " with priority " << currentProcess.priority <<
".\n";
cout << "Waiting time: " << waitingTime << ", Turnaround
time: " << turnaroundTime
<< "\n";
currentTime += currentProcess.burstTime;
}
int main() {
int numProcesses;
vector<Process> processes;
cout << "Enter the number of processes: ";
cin >> numProcesses;
scheduleProcesses(processes);
return 0;
}
Feedback Queue
#include<stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
int main()
{
int p[20],bt[20], su[20], wt[20],tat[20],i, k, n, temp;
float wtavg, tatavg;
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i] = i;
printf("Enter the Burst Time of Process%d:", i);
scanf("%d",&bt[i]);
printf("System/User Process (0/1) ? ");
scanf("%d", &su[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(su[i] > su[k])
{
temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=su[i];
su[i]=su[k];
su[k]=temp;
}
wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\nPROCESS\t\t SYSTEM/USER PROCESS \tBURST TIME\tWAITING
TIME\tTURNAROUND TIME");
for(i=0;i<n;i++)
printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d
",p[i],su[i],bt[i],wt[i],tat[i]);
printf("\nAverage Waiting Time is --- %f",wtavg/n);
printf("\nAverage Turnaround Time is --- %f",tatavg/n);
return 0;
}
OUTPUT
Q4. Critical Section Problem
#include <iostream>
#include <thread>
#include <mutex>
std::mutex mtx; // mutex for critical section
void thread_func(int thread_id, int num_iterations) {
for (int i = 0; i < num_iterations; ++i) {
mtx.lock(); // enter critical section
std::cout << "Thread " << thread_id << " is in the critical
section.\n";
mtx.unlock(); // exit critical section
}
}
int main() {
int num_threads, num_iterations;
std::cout << "Enter the number of threads: ";
std::cin >> num_threads;
std::cout << "Enter the number of iterations per thread: ";
std::cin >> num_iterations;
std::thread* threads = new std::thread[num_threads];
for (int i = 0; i < num_threads; ++i) {
threads[i] = std::thread(thread_func, i, num_iterations);
}
for (int i = 0; i < num_threads; ++i) {
threads[i].join();
}
delete[] threads;
return 0;
}
OUTPUT
Q5. Deadlock Detection and Deadlock Avoidance
Algorithm
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<math.h>
#include<time.h>
using namespace std;
int no_printer,no_fax,no_scanner,no_tapedrive;
int sum=0,sum1=0,sum2=0,sum3=0;
int r1,r2,r3,r4;
int fraction[4];
int fraction1[4];
int fraction2[4];
int fraction3[4];
int request[4];
int request1[4];
int request2[4];
int request3[4];
int available[4];
void Calculate();
int main()
{
cout<<"Enter the number of printers your company
has"<<endl;
cin>>no_printer;
cout<<"Enter the number of fax your company has"<<endl;
cin>>no_fax;
cout<<"Enter the number of scanner your company has"<<endl;
cin>>no_scanner;
cout<<"Enter the number of tapedrive your company
has"<<endl;
cin>>no_tapedrive;
cout<<"Enter the printer,fax,scanner,tapedrive requirements
for Kumar ";
for(int i=0;i<4;i++)
{
cin>>request[i];
}
cout<<"Enter the printer,fax,scanner,tapedrive requirements
for Harsh ";
for(int i=0;i<4;i++)
{
cin>>request1[i];
}
cout<<"Enter the printer,fax,scanner,tapedrive requirements
for Vipul ";
for(int i=0;i<4;i++)
{
cin>>request2[i];
}
cout<<"Enter the printer,fax,scanner,tapedrive requirements
for Ramesh ";
for(int i=0;i<4;i++)
{
cin>>request3[i];
}
srand(time(0));
int randnum[4];
int randnum1[4];
int randnum2[4];
int randnum3[4];
for (int i = 0; i < 4; i++)
{
randnum[i]=((double) rand() / (RAND_MAX))*no_printer;
randnum1[i]=((double) rand() / (RAND_MAX))*no_fax;
randnum2[i]=((double) rand() / (RAND_MAX))*no_scanner;
randnum3[i]=((double) rand() /
(RAND_MAX))*no_tapedrive;
sum+=randnum[i];
sum1+=randnum1[i];
sum2+=randnum2[i];
sum3+=randnum3[i];
}
r1=fraction[0]+fraction[1]+fraction[2]+fraction[3];
r2=fraction1[0]+fraction1[1]+fraction1[2]+fraction1[3];
r3=fraction2[0]+fraction2[1]+fraction2[2]+fraction2[3];
r4=fraction3[0]+fraction3[1]+fraction3[2]+fraction3[3];
cout<<"\n
"<<"**********************************************************\n\n"<<"
";
available[0]=no_printer-r1;
available[1]=no_fax-r2;
available[2]=no_scanner-r3;
available[3]=no_tapedrive-r4;
Calculate();
return 0;
}
void Calculate()
{
int c1,c2,c3,c4;
c1=0,c2=0,c3=0,c4=0;
do
{
c1=0,c2=0,c3=0,c4=0;
for(int i=0;i<4;i++)
{
if(request[i]<=available[i])
c1+=1;
else
c1=-1;
if(c1==4)
{
for(int i=0;i<4;i++)
{
available[i]+=request[i];
}
}
}
for(int i=0;i<4;i++)
{
if(request1[i]<=available[i])
c2+=1;
else
c2=-1;
if(c2==4)
{
for(int i=0;i<4;i++)
{
available[i]+=request1[i];
}
}
}
for(int i=0;i<4;i++)
{
if(request2[i]<=available[i])
c3+=1;
else
c3=-1;
if(c3==4)
{
for(int i=0;i<4;i++)
{
available[i]+=request2[i];
}
}
}
for(int i=0;i<4;i++)
{
if(request3[i]<=available[i])
c4+=1;
else
c4=-1;
if(c4==4)
{
for(int i=0;i<4;i++)
{
available[i]+=request3[i];
}
}
}
if(c1!=4 && c2!=4 && c3!=4 && c4!=4)
{
cout<<"The system is in deadlock state";
break;
}
else{
cout<<"The system is not in deadlock";
break;
}
}while(c1==4||c2==4||c3==4||c4==4);
}
OUTPUT
Deadlock Avoidance
#include <iostream>
using namespace std;
const int P = 5;
const int R = 3;
void calculateNeed(int need[P][R], int maxm[P][R],
int allot[P][R])
{
for (int i = 0; i < P; i++)
for (int j = 0; j < R; j++)
need[i][j] = maxm[i][j] - allot[i][j];
}
bool isSafe(int processes[], int avail[], int maxm[][R],
int allot[][R])
{
int need[P][R];
calculateNeed(need, maxm, allot);
bool finish[P] = {0};
int safeSeq[P];
int work[R];
for (int i = 0; i < R; i++)
work[i] = avail[i];
int count = 0;
while (count < P)
{
bool found = false;
for (int p = 0; p < P; p++)
{
if (finish[p] == 0)
{
int j;
for (j = 0; j < R; j++)
if (need[p][j] > work[j])
break;
if (j == R)
{
for (int k = 0; k < R; k++)
work[k] += allot[p][k];
safeSeq[count++] = p;
finish[p] = 1;
found = true;
}
}
}
if (found == false)
{
cout << "System is not in safe state";
return false;
}
}
cout << "System is in safe state.\nSafe"
" sequence is: ";
for (int i = 0; i < P; i++)
cout << safeSeq[i] << " ";
return true;
}
int main()
{
int processes[] = {0, 1, 2, 3, 4};
int avail[] = {3, 3, 2};
int maxm[][R] = {{7, 5, 3},
{3, 2, 2},
{9, 0, 2},
{2, 2, 2},
{4, 3, 3}};
int allot[][R] = {{0, 1, 0},
{2, 0, 0},
{3, 0, 2},
{2, 1, 1},
{0, 0, 2}};
isSafe(processes, avail, maxm, allot);
return 0;
}
OUTPUT
OUTPUT
Best Fit
#include<bits/stdc++.h>
using namespace std;
int main()
{
int nb,np,temp,lowest=9999;
int a1[20],a2[20],b[20],p[20];
for(int t=1;t<=np;t++)
{
for(int q=1;q<=nb;q++)
{
if(a1[q]!=1)
{
temp=b[q]-p[t];
if(temp>=0)
if(lowest>temp)
{
a2[t]=q;
lowest=temp;
}
}
}
a1[a2[t]]=1;
lowest=10000;
}
OUTPUT
Worst Fit
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
int memsize;
int allocp=-1;
int pos;
int allocSize;
}m[200];
bool posSort(node a,node b){
return a.pos < b.pos;
}
bool memSort(node a,node b){
return a.memsize > b.memsize;
}
int main(){
int nm,np,choice, i, j, p[200];
cout<<"Enter number of blocks\n";
cin>>nm;
cout<<"Enter block size\n";
for(i=0;i<nm;i++){
cin>>m[i].memsize;
m[i].pos=i;
}
cout<<"Enter number of processes\n";
cin>>np;
cout<<"Enter process size\n";
for(i=0;i<np;i++){
cin>>p[i];
}
cout<<"\n\n";
sort(m,m+nm,memSort);
int globalFlag=0;
for(i=0;i<np;i++){
int flag=0;
for(j=0;j<nm;j++){
if(p[i]<=m[j].memsize && m[j].allocp==-1){
m[j].allocp=i;
m[j].allocSize=p[i];
flag=1;
break;
}
}
if(flag==0){
cout<<"Unallocated Process P"<<i+1<<"\n";
globalFlag=1;
}
}
sort(m,m+nm,posSort);
cout<<"\n";
int intFrag=0,extFrag=0;
cout<<"Memory\t\t";
for(i=0;i<nm;i++){
cout<<m[i].memsize<<"\t";
}
cout<<"\n";
cout<<"P. Alloc.\t";
for(i=0;i<nm;i++){
if(m[i].allocp!=-1){
cout<<"P"<<m[i].allocp+1<<"\t";
}
else{
cout<<"Empty\t";
}
}
cout<<"\n";
cout<<"Int. Frag.\t";
for(i=0;i<nm;i++){
if(m[i].allocp!=-1){
cout<<m[i].memsize-m[i].allocSize<<"\t";
intFrag+=m[i].memsize-m[i].allocSize;
}
else{
extFrag+=m[i].memsize;
cout<<"Empty\t";
}
}
cout<<"\n";
cout<<"\n";
if(globalFlag==1)
cout<<"Total External Fragmentation: "<<extFrag<<"\n";
else{
cout<<"Available Memory: "<<extFrag<<"\n";
}
cout<<"Total Internal Fragmentation: "<<intFrag<<"\n";
return 0;
}
OUTPUT
OUTPUT
LRU
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m,i,j,k,hit=0;
cout<<"Enter number of frames\n";
cin>>n;
cout<<"Enter number of processes\n";
cin>>m;
vector<int> p(m);
vector<int> hi(m);
cout<<"Enter processes\n";
for(i=0;i<m;i++){
cin>>p[i];
}
vector<vector<int>> a(n);
for(i=0;i<n;i++){
a[i]=vector<int>(m,-1);
}
map <int, int> mp;
for(i=0;i<m;i++){
vector<pair<int,int>> c;
for(auto q: mp){
c.push_back({q.second,q.first});
}
sort(c.begin(),c.end());
bool hasrun=false;
for(j=0;j<n;j++){
if(a[j][i]==p[i]){
hit++;
hi[i]=1;
mp[p[i]]=1;
hasrun=true;
break;
}
if(a[j][i]==-1){
for(k=i;k<m;k++)
a[j][k]=p[i];
mp[p[i]]++;
hasrun=true;
break;
}
}
if(j==n||hasrun==false){
for(j=0;j<n;j++){
if(a[j][i]==c[c.size()-1].second){
mp.erase(a[j][i]);
for(k=i;k<m;k++)
a[j][k]=p[i];
mp[p[i]]++;
break;
}
}
}
for(auto q:mp){
if(q.first!=p[i]){
mp[q.first]++;
}
}
}
cout<<"Process ";
for(i=0;i<m;i++){
cout<<p[i]<<" ";
}
cout<<'\n';
for(i=0;i<n;i++){
cout<<"Frame "<<i<<" ";
for(j=0;j<m;j++){
if(a[i][j]==-1)
cout<<"E ";
else
cout<<a[i][j]<<" ";
}
cout<<'\n';
}
for(i=0;i<m;i++){
if(hi[i]==0)
cout<<" ";
else
cout<<hi[i]<<" ";
}
cout<<"\n";
cout<<"Hit "<<hit<<'\n'<<"Page Fault "<<m-hit<<'\n';
return 0;
}
OUTPUT
LFU
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m,i,j,k;
cout<<"Enter number of frames\n";
cin>>n;
cout<<"Enter number of processes\n";
cin>>m;
vector<int> p(m);
cout<<"Enter processes\n";
for(i=0;i<m;i++){
cin>>p[i];
}
vector<vector<int>> a(n,vector<int>(m,-1));
map <int, int> mp,lfmp;
for(i=0;i<m;i++){
vector<int> op;
vector<pair<int,int>> c,lf;
for(auto q: mp){
c.push_back({q.second,q.first});
}
for(auto q:lfmp){
lf.push_back({q.second,q.first});
}
sort(lf.begin(),lf.end());
bool dontCall=true;
if(lf.size()>2){
if(lf[0].first!=lf[1].first){
dontCall=false;
}
}
sort(c.begin(),c.end());
bool hasrun=false;
for(j=0;j<n;j++){
if(a[j][i]==p[i]){
mp[p[i]]++;
lfmp[p[i]]++;
hasrun=true;
break;
}
if(a[j][i]==-1){
for(k=i;k<m;k++)
a[j][k]=p[i];
mp[p[i]]++;
lfmp[p[i]]++;
hasrun=true;
break;
}
}
if(j==n||hasrun==false){
for(j=0;j<n;j++){
if(dontCall==true){
int q;
if(lf[lf.size()-1].second==c[c.size()-1].second&&lf[lf.size()-1].first>
1){
if(a[j][i]==c[c.size()-2].second){
mp.erase(a[j][i]);
lfmp.erase(a[j][i]);
for(k=i;k<m;k++)
a[j][k]=p[i];
mp[p[i]]++;
lfmp[p[i]]++;
break;
}
}
else{
if(a[j][i]==c[c.size()-1].second){
mp.erase(a[j][i]);
lfmp.erase(a[j][i]);
for(k=i;k<m;k++)
a[j][k]=p[i];
mp[p[i]]++;
lfmp[p[i]]++;
break;
}
}
}
else if(dontCall==false){
if(a[j][i]==lf[0].second){
mp.erase(a[j][i]);
lfmp.erase(a[j][i]);
for(k=i;k<m;k++)
a[j][k]=p[i];
mp[p[i]]++;
lfmp[p[i]]++;
break;
}
}
}
}
for(auto q:mp){
if(q.first!=p[i]){
mp[q.first]++;
}
}
}
int hit=0;
vector<int> hitv(m);
for(i=1;i<m;i++){
for(j=0;j<n;j++){
if(p[i]==a[j][i-1]){
hit++;
hitv[i]=1;
break;
}
}
}
cout<<"Process ";
for(i=0;i<m;i++){
cout<<p[i]<<" ";
}
cout<<'\n';
for(i=0;i<n;i++){
cout<<"Frame "<<i<<" ";
for(j=0;j<m;j++){
if(a[i][j]==-1)
cout<<"E ";
else
cout<<a[i][j]<<" ";
}
cout<<'\n';
}
cout<<"HIT ";
for(i=0;i<hitv.size();i++){
if(hitv[i]==0)
cout<<" ";
else
cout<<hitv[i]<<" ";
}
cout<<"\n";
cout<<"Hit "<<hit<<'\n'<<"Page Fault "<<m-hit<<'\n';
return 0;
}
OUTPUT
Second Chance
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n, i, j, f, pos = 0, hit = 0, miss = 0;
cout<<"Enter the number of frames: ";
cin>>f;
cout<<"Enter the number of pages: ";
cin>>n;
int frames[f], ref[n], use[f] = {0};
cout<<"Enter the reference string: ";
for(i = 0; i < n; i++)
cin>>ref[i];
for(i = 0; i < f; i++)
frames[i] = -1;
for(i = 0; i < n; i++)
{
int found = 0;
for(j = 0; j < f; j++)
{
if(frames[j] == ref[i])
{
hit++;
use[j] = 1;
found = 1;
break;
}
}
if(found == 0)
{
while(use[pos] == 1)
{
use[pos] = 0;
pos++;
if(pos == f)
pos = 0;
}
frames[pos] = ref[i];
use[pos] = 1;
pos++;
if(pos == f)
pos = 0;
miss++;
}
}
cout<<"Number of hits: "<<hit<<endl;
cout<<"Number of misses: "<<miss<<endl;
cout<<"Page Fault Ratio: "<<(float)miss/n<<endl;
return 0;
}
OUTPUT
MRU
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n, i, j, f, pos = 0, hit = 0, miss = 0;
cout<<"Enter the number of frames: ";
cin>>f;
cout<<"Enter the number of pages: ";
cin>>n;
int frames[f], ref[n], mru[f] = {0};
cout<<"Enter the reference string: ";
for(i = 0; i < n; i++)
cin>>ref[i];
for(i = 0; i < f; i++)
frames[i] = -1;
for(i = 0; i < n; i++)
{
int found = 0;
for(j = 0; j < f; j++)
{
if(frames[j] == ref[i])
{
hit++;
mru[j] = i+1;
found = 1;
break;
}
}
if(found == 0)
{
int maxmru = -1, maxpos;
for(j = 0; j < f; j++)
{
if(mru[j] > maxmru)
{
maxmru = mru[j];
maxpos = j;
}
}
frames[maxpos] = ref[i];
mru[maxpos] = i+1;
miss++;
}
}
cout<<"Number of hits: "<<hit<<endl;
cout<<"Number of misses: "<<miss<<endl;
cout<<"Page Fault Ratio: "<<(float)miss/n<<endl;
return 0;
}
OUTPUT
MFU
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n, i, j, f, pos = 0, hit = 0, miss = 0;
cout<<"Enter the number of frames: ";
cin>>f;
cout<<"Enter the number of pages: ";
cin>>n;
int frames[f], ref[n], mfu[f] = {0};
cout<<"Enter the reference string: ";
for(i = 0; i < n; i++)
cin>>ref[i];
for(i = 0; i < f; i++)
frames[i] = -1;
for(i = 0; i < n; i++)
{
int found = 0;
for(j = 0; j < f; j++)
{
if(frames[j] == ref[i])
{
hit++;
mfu[j]++;
found = 1;
break;
}
}
if(found == 0)
{
int maxmfu = -1, maxpos;
for(j = 0; j < f; j++)
{
if(mfu[j] > maxmfu)
{
maxmfu = mfu[j];
maxpos = j;
}
}
frames[maxpos] = ref[i];
mfu[maxpos] = 1;
miss++;
}
}
cout<<"Number of hits: "<<hit<<endl;
cout<<"Number of misses: "<<miss<<endl;
cout<<"Page Fault Ratio: "<<(float)miss/n<<endl;
return 0;
}
OUTPUT
int temp=h;
cout<<temp;
for(i=0;i<n;i++){
cout<<" -> "<<a[i]<<' ';
sum+=abs(a[i]-temp);
temp=a[i];
}
cout<<'\n';
cout<<"Total head movements = "<< sum<<'\n';
return 0;
}
OUTPUT
SSTF
#include<bits/stdc++.h>
using namespace std;
int main(){
int i,j,k,n,m,sum=0,x,y,h;
cout<<"Enter the size of disk\n";
cin>>m;
cout<<"Enter number of requests\n";
cin>>n;
cout<<"Enter the requests\n";
vector <int> a(n),b;
map <int,int> mp;
for(i=0;i<n;i++){
cin>>a[i];
mp[a[i]]++;
}
for(i=0;i<n;i++){
if(a[i]>m){
cout<<"Error, Unknown position "<<a[i]<<"\n";
return 0;
}
}
cout<<"Enter the head position\n";
cin>>h;
int temp=h;
int ele;
b.push_back(h);
int count=0;
while(count<n){
int diff=999999;
for(auto q:mp){
if(abs(q.first-temp)<diff){
ele=q.first;
diff=abs(q.first-temp);
}
}
mp[ele]--;
if(mp[ele]==0){
mp.erase(ele);
}
b.push_back(ele);
temp=ele;
count++;
}
cout<<b[0];
temp=b[0];
for(i=1;i<b.size();i++){
cout<<" -> "<<b[i];
sum+=abs(b[i]-temp);
temp=b[i];
}
cout<<'\n';
cout<<"Total head movements = "<< sum<<'\n';
cout<<"Average head movement = "<<(float)sum/n<<'\n';
return 0;
}
OUTPUT
SCAN
#include<bits/stdc++.h>
using namespace std;
int main(){
int i,j,k,n,m,sum=0,x,y,h;
cout<<"Enter the size of disk\n";
cin>>m;
cout<<"Enter number of requests\n";
cin>>n;
cout<<"Enter the requests\n";
vector <int> a(n),b;
for(i=0;i<n;i++){
cin>>a[i];
}
for(i=0;i<n;i++){
if(a[i]>m){
cout<<"Error, Unknown position "<<a[i]<<"\n";
return 0;
}
}
cout<<"Enter the head position\n";
cin>>h;
int temp=h;
a.push_back(h);
a.push_back(m);
a.push_back(0);
sort(a.begin(),a.end());
for(i=0;i<a.size();i++){
if(h==a[i])
break;
}
k=i;
if(k<n/2){
for(i=k;i<a.size();i++){
b.push_back(a[i]);
}
for(i=0;i<=k-1;i++){
b.push_back(a[i]);
}
}
else{
for(i=k;i>=0;i--){
b.push_back(a[i]);
}
for(i=a.size()-1;i>=k+1;i--){
b.push_back(a[i]);
}
}
temp=b[0];
cout<<temp;
for(i=1;i<b.size();i++){
cout<<" -> "<<b[i];
sum+=abs(b[i]-temp);
temp=b[i];
}
cout<<'\n';
cout<<"Total head movements = "<< sum<<'\n';
cout<<"Average head movement = "<<(float)sum/n<<'\n';
return 0;
}
OUTPUT
C-SCAN
#include<bits/stdc++.h>
using namespace std;
int main(){
int i,j,k,n,m,sum=0,x,y,h;
cout<<"Enter the size of disk\n";
cin>>m;
cout<<"Enter number of requests\n";
cin>>n;
cout<<"Enter the requests\n";
vector <int> a(n),b;
for(i=0;i<n;i++){
cin>>a[i];
}
for(i=0;i<n;i++){
if(a[i]>m){
cout<<"Error, Unknown position "<<a[i]<<"\n";
return 0;
}
}
cout<<"Enter the head position\n";
cin>>h;
int temp=h;
a.push_back(h);
a.push_back(m);
a.push_back(0);
sort(a.begin(),a.end());
for(i=0;i<a.size();i++){
if(h==a[i])
break;
}
k=i;
if(k<n/2){
for(i=k;i<a.size();i++){
b.push_back(a[i]);
}
for(i=0;i<=k-1;i++){
b.push_back(a[i]);
}
}
else{
for(i=k;i>=0;i--){
b.push_back(a[i]);
}
for(i=a.size()-1;i>=k+1;i--){
b.push_back(a[i]);
}
}
temp=b[0];
cout<<temp;
for(i=1;i<b.size();i++){
cout<<" -> "<<b[i];
sum+=abs(b[i]-temp);
temp=b[i];
}
cout<<'\n';
cout<<"Total head movements = "<< sum<<'\n';
cout<<"Average head movement = "<<(float)sum/n<<'\n';
return 0;
}
OUTPUT
LOOK
#include<bits/stdc++.h>
using namespace std;
int main(){
int i,j,k,n,m,sum=0,x,y,h;
cout<<"Enter the size of disk\n";
cin>>m;
cout<<"Enter number of requests\n";
cin>>n;
cout<<"Enter the requests\n";
vector <int> a(n),l;
for(i=0;i<n;i++){
cin>>a[i];
}
for(i=0;i<n;i++){
if(a[i]>m){
cout<<"Error, Unknown position\n";
return 0;
}
}
cout<<"Enter the head position\n";
cin>>h;
a.push_back(h);
sort(a.begin(),a.end());
for(i=0;i<a.size();i++){
if(h==a[i])
break;
}
k=i;
if(k<n/2){
for(i=k;i<a.size();i++){
l.push_back(a[i]);
}
for(i=k-1;i>=0;i--){
l.push_back(a[i]);
}
}
else{
for(i=k;i>=0;i--){
l.push_back(a[i]);
}
for(i=k+1;i<a.size();i++){
l.push_back(a[i]);
}
}
int temp=l[0];
cout<<temp;
for(i=1;i<l.size();i++){
cout<<" -> "<<l[i]<<' ';
sum+=abs(l[i]-temp);
temp=a[i];
}
cout<<'\n';
cout<<"Total head movements = "<< sum<<'\n';
return 0;
}
OUTPUT
C-LOOK
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n, i, j, pos,sum=0,disk_size, start;
cout<<"Enter the size of disk: ";
cin>>disk_size;
cout<<"Enter the starting position: ";
cin>>start;
cout<<"Enter the number of requests: ";
cin>>n;
int a[n];
cout<<"Enter the requests: ";
for(i = 0; i < n; i++)
cin>>a[i];
sort(a, a+n);
for(i = 0; i < n; i++)
{
if(a[i] == start)
{
pos = i;
break;
}
}
for(i = pos; i < n; i++)
{
sum += abs(start - a[i]);
start = a[i];
}
sum += abs(start-disk_size);
start = 0;
for(i = 0; i < pos; i++)
{
sum += abs(start - a[i]);
start = a[i];
}
cout<<"Total head movement: "<<sum<<endl;
return 0;
}
OUTPUT