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

Program Os

The document contains three programs that implement different CPU scheduling algorithms: First Come First Serve (FCFS), Shortest Job First (SJF), and Priority-based scheduling. Each program includes user input for process burst times and calculates waiting times and turnaround times, displaying the results along with average waiting and turnaround times. The code is written in C++ and demonstrates the logic for each scheduling method.

Uploaded by

itsrealkhokhar01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Program Os

The document contains three programs that implement different CPU scheduling algorithms: First Come First Serve (FCFS), Shortest Job First (SJF), and Priority-based scheduling. Each program includes user input for process burst times and calculates waiting times and turnaround times, displaying the results along with average waiting and turnaround times. The code is written in C++ and demonstrates the logic for each scheduling method.

Uploaded by

itsrealkhokhar01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Program:1

AIM : Write a program to implement First Come First Scheduling(FCFS).

Code :

#include<iostream>

using namespace std;

int main()

{ int n,bt[20],wt[20],tat[20],avg_wt=0,avg_tat=0,i,j;

cout<<"Enter total number of processes:";

cin>>n;

cout<<"\nEnter Process Burst Time 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\tTurn
around Time";

//calculating turnaround time

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

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

avg_wt+=wt[i];

avg_tat+=tat[i];

cout<<"\nP["<<i+1<<"]"<<"\t\t\t"<<bt[i]<<"\t\t\t\t"<<wt[i
]<<"\t\t\t\t"<<tat[i];

avg_wt/=i;

avg_tat/=i;

cout<<"\n\nAverage Waiting Time:"<<avg_wt;

cout<<"\nAverage Turnaround Time:"<<avg_tat;

return 0;

}
Program:2
AIM : Write a program to implement Shortest Job First scheduling(SJF).

Code:

#include <iostream>

using namespace std;

int main()

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;

}
Program:3
AIM : Write a program to implement priority based scheduling.

Code :

#include<iostream>

using namespace std;

int main()

int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,

temp,avg_wt,avg_tat;

cout<<"Enter Total Number of Process:";

cin>>n;

cout<<"\nEnter Burst Time and Priority\n";

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

cout<<"\nP["<<i+1<<"]\n";

cout<<"Burst Time:";

cin>>bt[i];

cout<<"Priority:";

cin>>pr[i];

p[i]=i+1; //contains process number

//sorting burst time, priority and process number in


ascending order using selection sort

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

pos=i;

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

if(pr[j]<pr[pos])

pos=j;

temp=pr[i];

pr[i]=pr[pos];

pr[pos]=temp;

temp=bt[i];

bt[i]=bt[pos];

bt[pos]=temp;

temp=p[i];

p[i]=p[pos];

p[pos]=temp;

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


is zero

//calculate waiting time

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

wt[i]=0;

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

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

avg_wt=total/n; //average waiting time

total=0;

cout<<"\nProcess\t Burst Time \tWaiting


Time\tTurnaround Time";

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

tat[i]=bt[i]+wt[i]; //calculate turnaround time

total+=tat[i];

cout<<"\nP["<<i+1<<"]"<<"\t\t\t"<<bt[i]<<"\t\t\t\t"<<wt[i
]<<"\t\t\t\t"<<tat[i];

avg_tat=total/n; //average turnaround time

cout<<"\n\nAverage Waiting Time="<<avg_wt;

cout<<"\nAverage Turnaround Time="<<avg_tat;

return 0;

You might also like