0% found this document useful (0 votes)
5 views5 pages

Program for Sjf

The document is a C++ program that implements the Shortest Job First (SJF) scheduling algorithm for process management. It takes user input for process names, arrival times, and burst times, and calculates completion times, turnaround times, and waiting times for each process. The program also outputs the processes in order of arrival time and displays a Gantt chart representation of the scheduling.
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)
5 views5 pages

Program for Sjf

The document is a C++ program that implements the Shortest Job First (SJF) scheduling algorithm for process management. It takes user input for process names, arrival times, and burst times, and calculates completion times, turnaround times, and waiting times for each process. The program also outputs the processes in order of arrival time and displays a Gantt chart representation of the scheduling.
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

PROGRAM FOR SJF

#include<string.h>

#include<iostream>

using namespace std;

int getmin(int i,int n,int bt[])

int j,d,min=10000;

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

if(min>bt[j])

{min=bt[j];

d=j;

return d;

int main()

int i,j,n;

cout<<"enter the number of processes \n ";

cin>>n;

int at[n],bt[n],ct[n],wt[n],tt[n];

string pn[n];

cout<<"enter the processes name ,arrival time and burst time \n ";
for(i=0;i<n;i++)

cin>>pn[i]>>at[i]>>bt[i];

cout<<"\n The data you have entered is as follows:\n";

cout<<"PROCESS NAME \tARIVAL TIME \tBURST TIME \n";

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

cout<<"\t"<<pn[i]<<"\t\t"<<at[i]<<"\t\t"<<bt[i]<<"\n";

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

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

string k;

int t;

if(at[i]>at[j+1])

t=at[i];

at[i]=at[j+1];

at[j+1]=t;

k=pn[i];

pn[i]=pn[j+1];

pn[j+1]=k;
t=bt[i];

bt[i]=bt[j+1];

bt[j+1]=t;

cout<<"arranged processes by Arival time \n ";

cout<<"PROCESS NAME \t ARIVAL TIME \t BURST TIME \n";

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

cout<<"\t"<<pn[i]<<"\t\t"<<at[i]<<"\t\t"<<bt[i]<<"\n";

cout<<"\n NP SJF with COMPLETION TIME \n";

ct[0]=bt[0];

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

if(ct[i-1]>at[i])

int d;

d=getmin(i,n,bt);

string k;

int t;
t=at[i];

at[i]=at[d];

at[d]=t;

k=pn[i];

pn[i]=pn[d];

pn[d]=k;

t=bt[i];

bt[i]=bt[d];

bt[d]=t;

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

else

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

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

tt[i]=ct[i]-at[i];

wt[i]=tt[i]-bt[i];

}
cout<<"\n NP SJF with Gantt Chart \n\n";

cout<<"0";

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

cout<<" | "<<pn[i]<<" | "<<ct[i]<<" | ";

cout<<"\n\nPROCESS NAME \t ARIVAL TIME \t BURST TIME \t COMPLETION TIME \


t TURN AROUND TIME \t WAITING TIME \t \n";

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

cout<<"\t"<<pn[i]<<"\t\t"<<at[i]<<"\t\t"<<bt[i]<<"\t\t"<<ct[i]<<"\t\t\
t"<<tt[i]<<"\t\t\t"<<wt[i]<<"\n";

return 0;

You might also like