0% found this document useful (0 votes)
17 views

code

Uploaded by

Fayera Ababa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

code

Uploaded by

Fayera Ababa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

// C++ program for implementation of FCFS

// scheduling

#include<iostream>

using namespace std;

// Function to find the waiting time for all

// processes

void findWaitingTime(int processes[], int n,

int bt[], int wt[])

// waiting time for first process is 0

wt[0] = 0;

// calculating waiting time

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

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

// Function to calculate turn around time

void findTurnAroundTime( int processes[], int n,

int bt[], int wt[], int tat[])

// calculating turnaround time by adding

// bt[i] + wt[i]

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 wt[n], tat[n], total_wt = 0, total_tat = 0;

//Function to find waiting time of all processes

findWaitingTime(processes, n, bt, wt);

//Function to find turn around time for all processes

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

//Display processes along with all details

cout << "Processes "<< " Burst time "

<< " Waiting time " << " Turn around time\n";

// Calculate total waiting time and total turn

// around time

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

// Driver code

int main()

//process id's

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

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

//Burst time of all processes

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

findavgTime(processes, n, burst_time);

return 0;

}
#include <iostream>
using namespace std;

int main() {

// Matrix for storing Process Id, Burst


// Time, Average Waiting Time & Average
// Turn Around Time.
int A[100][4];
int i, j, n, total = 0, index, temp;
float avg_wt, avg_tat;

cout << "Enter number of process: ";


cin >> n;

cout << "Enter Burst Time:" << endl;

// User Input Burst Time and alloting Process Id.


for (i = 0; i < n; i++) {
cout << "P" << i + 1 << ": ";
cin >> A[i][1];
A[i][0] = i + 1;
}

// Sorting process according to their Burst Time.


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;
cout << "P BT WT TAT" << endl;

// Calculation of Turn Around Time and printing the


// data.
for (i = 0; i < n; i++) {
A[i][3] = A[i][1] + A[i][2];
total += A[i][3];
cout << "P" << A[i][0] << " " << A[i][1] << " " << A[i][2]
<< " " << A[i][3] << endl;
}

avg_tat = (float)total / n;
cout << "Average Waiting Time= " << avg_wt << endl;
cout << "Average Turnaround Time= " << avg_tat << endl;
}
Sjf

#include <iostream>

using namespace std;

int main() {

// Matrix for storing Process Id, Burst

// Time, Average Waiting Time & Average

// Turn Around Time.

int A[100][4];

int i, j, n, total = 0, index, temp;

float avg_wt, avg_tat;

cout << "Enter number of process: ";

cin >> n;

cout << "Enter Burst Time:" << endl;

// User Input Burst Time and alloting Process Id.

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

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

cin >> A[i][1];


A[i][0] = i + 1;

// Sorting process according to their Burst Time.

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;

cout << "P BT WT TAT" << endl;

// Calculation of Turn Around Time and printing the

// data.

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

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

total += A[i][3];

cout << "P" << A[i][0] << " " << A[i][1] << " " << A[i][2] << " " << A[i][3] << endl;

avg_tat = (float)total / n;

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

cout << "Average Turnaround Time= " << avg_tat << endl;

Priority

#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 aka DURATION \n";

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

cout<<"P["<<i+1<<"]:";

cin>>bt[i];

wt[0]=0; //waiting time for first process is 0

//calculating waiting time

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

//calculating turnaround 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;

RR

#include <climits>

#include <iostream>

using namespace std;

struct Process
{

int AT, BT, ST[20], WT, FT, TAT, pos;

};

int quant;

int main()

int n, i, j;

// Taking Input

cout << "Enter the no. of processes: ";

cin >> n;

Process p[n];

cout << "Enter the quantum: " << endl;

cin >> quant;

cout << "Enter the process numbers: " << endl;

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

cin >> p[i].pos;

cout << "Enter the Arrival time of processes: " << endl;

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

cin >> p[i].AT;


cout << "Enter the Burst time of processes: " << endl;

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

cin >> p[i].BT;

// Declaring variables

int c = n, s[n][20];

float time = 0, mini = INT_MAX, b[n], a[n];

// Initializing burst and arrival time arrays

int index = -1;

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

b[i] = p[i].BT;

a[i] = p[i].AT;

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

s[i][j] = -1;

int tot_wt, tot_tat;

tot_wt = 0;

tot_tat = 0;

bool flag = false;


while (c != 0)

mini = INT_MAX;

flag = false;

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

float p = time + 0.1;

if (a[i] <= p && mini > a[i] && b[i] > 0)

index = i;

mini = a[i];

flag = true;

// if at =1 then loop gets out hence set flag to false

if (!flag)

time++;

continue;

// calculating start time


j = 0;

while (s[index][j] != -1)

j++;

if (s[index][j] == -1)

s[index][j] = time;

p[index].ST[j] = time;

if (b[index] <= quant)

time += b[index];

b[index] = 0;

else

time += quant;

b[index] -= quant;

if (b[index] > 0)
{

a[index] = time + 0.1;

// calculating arrival, burst, final times

if (b[index] == 0)

c--;

p[index].FT = time;

p[index].WT = p[index].FT - p[index].AT - p[index].BT;

tot_wt += p[index].WT;

p[index].TAT = p[index].BT + p[index].WT;

tot_tat += p[index].TAT;

} // end of while loop

// Printing output

cout << "Process number ";

cout << "Arrival time ";

cout << "Burst time ";

cout << "\tStart time";

j = 0;

while (j != 10)

j += 1;
cout << " ";

cout << "\t\tFinal time";

cout << "\tWait Time ";

cout << "\tTurnAround Time" << endl;

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

cout << p[i].pos << "\t\t";

cout << p[i].AT << "\t\t";

cout << p[i].BT << "\t";

j = 0;

int v = 0;

while (s[i][j] != -1)

cout << p[i].ST[j] << " ";

j++;

v += 3;

while (v != 40)

cout << " ";

v += 1;

cout << p[i].FT << "\t\t";


cout << p[i].WT << "\t\t";

cout << p[i].TAT << endl;

// Calculating average wait time and turnaround time

double avg_wt, avg_tat;

avg_wt = tot_wt / static_cast<double>(n);

avg_tat = tot_tat / static_cast<double>(n);

// Printing average wait time and turnaround time

cout << "The average wait time is: " << avg_wt << endl;

cout << "The average TurnAround time is: " << avg_tat << endl;

return 0;

You might also like