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

20CSE038

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 views16 pages

20CSE038

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/ 16

Problem 1:

#include<iostream>

using namespace std;

float roundRobinAvgWaitingTime;

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];

// Function to calculate average time

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

int wt[n], tat[n], total_wt = 0, total_tat = 0;


// Function to find waiting time of all processes

findWaitingTime(processes, n, bt, wt, quantum);

// Function to find turn around time for all processes

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

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

total_wt = total_wt + wt[i];

total_tat = total_tat + tat[i];

roundRobinAvgWaitingTime = (float)total_wt / (float)n; // Change the variable name

cout << "Average waiting time for round robin = "

<< roundRobinAvgWaitingTime << endl;

int main()

int n = 5, bt[20] = {10, 29, 3, 7, 12}, wt[20], tat[20], avwt = 0, avtat = 0, i, j;

wt[0] = 0;

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

wt[i] = 0;

for (j = 0; j < i; j++)

wt[i] += bt[j];
}

//calculating turnaround time

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

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

avwt += wt[i];

avtat += tat[i];

avwt /= i;

cout << "\n\nAverage Waiting Time for FCFS:" << avwt << endl;

int processes[] = {1, 2, 3, 4, 5};

int burst_time[] = {10, 29, 3, 7, 12};

int quantum = 10;

findavgTime(processes, n, burst_time, quantum);

int A[100][4];

int total = 0, index, temp;

float avg_wt, avg_tat;

A[0][1] = 10;

A[1][1] = 29;

A[2][1] = 3;

A[3][1] = 7;

A[4][1] = 12;

A[0][0] = 1;
A[1][0] = 2;

A[2][0] = 3;

A[3][0] = 4;

A[4][0] = 5;

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

index = i;

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

if (A[j][1] < A[index][1])

index = j;

temp = A[i][1];

A[i][1] = A[index][1];

A[index][1] = temp;

temp = A[i][0];

A[i][0] = A[index][0];

A[index][0] = temp;

A[0][2] = 0;

// Calculation of Waiting Times

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

A[i][2] = 0;

for (j = 0; j < i; j++)


A[i][2] += A[j][1];

total += A[i][2];

avg_wt = (float)total / n;

total = 0;

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

A[i][3] = A[i][1] + A[i][2];

total += A[i][3];

avg_tat = (float)total / n;

cout << "Average Waiting Time for SJF= " << avg_wt << endl;

if (roundRobinAvgWaitingTime < avg_wt && roundRobinAvgWaitingTime < avwt)

cout << "Round Robin gives minimum average waiting time\n";

else if (roundRobinAvgWaitingTime > avg_wt && avg_wt < avwt)

cout << "SJF gives minimum average waiting time\n";

else

{
cout << "FCFS gives minimum average waiting time\n";

return 0;

Problem 2:
#include<bits/stdc++.h>

#include <stdlib.h>

using namespace std;

struct Resource {

int A;

int B;

int C;

int D;

};

struct Allocation {

int A1;

int B1;

int C1;

int D1;

};
struct Max {

int A2;

int B2;

int C2;

int D2;

};

void compute();

int allocation[10][4], need[10][4], Max[10][4], available[10][4], p1[1][4];

int p, current[4];

bool executed[10], deadlock;

int main() {

p = 5;

FILE *fip;

fip = fopen("resource.txt", "r");

if (fip == NULL) {

perror("Error Occurred\n");

exit(EXIT_FAILURE);

int i = 0;

while (fscanf(fip, "%d\t%d\t%d\t%d", &current[0], &current[1], &current[2], &current[3]) != EOF) {

i++;

}
fclose(fip);

FILE *fip1;

fip1 = fopen("allocation.txt", "r");

if (fip1 == NULL) {

perror("Error Occurred\n");

exit(EXIT_FAILURE);

i = 0;

while (fscanf(fip1, "%d\t%d\t%d\t%d", &allocation[i][0], &allocation[i][1], &allocation[i][2],


&allocation[i][3]) != EOF) {

i++;

fclose(fip1);

FILE *fip2;

fip2 = fopen("Max.txt", "r");

if (fip2 == NULL) {

perror("Error Occurred\n");

exit(EXIT_FAILURE);

i = 0;

while (fscanf(fip2, "%d\t%d\t%d\t%d", &Max[i][0], &Max[i][1], &Max[i][2], &Max[i][3]) != EOF) {

i++;

fclose(fip2);
p1[0][0] = 0;

p1[0][1] = 4;

p1[0][2] = 2;

p1[0][3] = 0;

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

need[i][0] = Max[i][0] - allocation[i][0];

need[i][1] = Max[i][1] - allocation[i][1];

need[i][2] = Max[i][2] - allocation[i][2];

need[i][3] = Max[i][3] - allocation[i][3];

cout << "\n\n\t\t\tTable for Bankers Algo\n\n";

cout << "Initial Available Resources: " << current[0] << " " << current[1] << " " << current[2] << " "
<< current[3] << "\n\n";

cout << "Process Max Allocation Need\n";

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

cout << " P" << i + 1 << " ";

cout << " " << Max[i][0] << " " << Max[i][1] << " " << Max[i][2] << " " << Max[i][3] << " ";

cout << " " << allocation[i][0] << " " << allocation[i][1] << " " << allocation[i][2] << " " <<
allocation[i][3] << " ";

cout << " " << need[i][0] << " " << need[i][1] << " " << need[i][2] << " " << need[i][3];

cout << "\n";

cout << "\n\n";

compute();

if (p1[0][0] <= current[0] && p1[0][1] <= current[1] && p1[0][2] <= current[2] && p1[0][3] <=
current[3]) {
cout << "\n\nIf a request from p1 arrives for (0,4,2,0) Then it's granted immediately" << endl;

} else {

cout << "\n\nIf a request from p1 arrives for (0,4,2,0) Then it's not granted immediately" << endl;

return 0;

void compute() {

deadlock = false;

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

executed[i] = false;

int i, j;

for (i = 0; i < p; ++i) {

for (j = 0; j < p; j++) {

while (executed[j] && j < p - 1) {

j++;

if (need[j][0] <= current[0] && need[j][1] <= current[1] && need[j][2] <= current[2] && need[j]
[3] <= current[3]) {

if (!executed[j]) {

executed[j] = true;

current[0] += allocation[j][0];

current[1] += allocation[j][1];

current[2] += allocation[j][2];
current[3] += allocation[j][3];

cout << "\nProcess P" << j + 1;

cout << "\nCurrent: " << current[0] << " " << current[1] << " " << current[2] << " " <<
current[3] << "\n";

cout << "\nProcess executed without deadlock";

deadlock = true;

break;

if (!deadlock) {

cout << "\n\t\t\tDeadlock\n\n";

break;

} else {

deadlock = false;

Problem 3:
#include <bits/stdc++.h>

#define ll long long int

using namespace std;

ll pages[100005],n;

void opr()

ll sz = 2;

for(; sz < 10; sz++){

vector<ll>frame;

ll faults=0;

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

ll it;

for (it=0; it<frame.size(); it++)

if(pages[i]==frame[it])

break;

if (it==frame.size())

if (sz>frame.size())

frame.push_back(pages[i]);
}

else

ll pos=i+1;

ll in=INT_MIN,high=pos;

for (ll k=0;k<frame.size();k++)

ll j;

for (j=pos;j<n;j++)

if (frame[k]==pages[j])

if (j>high)

high = j;

in = k;

break;

if (j==n)

in=k;

break;

}
}

frame[in]=pages[i];

faults++;

if(faults == 9)cout<<"Total Page Faults: "<< faults <<"\nFrame number is " <<
sz <<endl;

int main()

#ifndef ONLINE_JUDGE

freopen("input.txt", "r", stdin);

freopen("output.txt", "w", stdout);

#endif

cin>>n;

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

cin>>pages[i];

}
opr();

return 0;

You might also like