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

Project Code

The document contains source code for implementing different CPU scheduling algorithms including FCFS, SJF preemptive and non-preemptive, priority scheduling preemptive and non-preemptive, and round robin. It includes main files that allow a user to select which algorithm to run, makefiles to compile the code, and cpp files containing the implementation of each algorithm. The code takes processes with different arrival times and burst times as input, runs the selected scheduling algorithm, and outputs the waiting times and turnaround times.

Uploaded by

KUSHAGRA ANAND
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Project Code

The document contains source code for implementing different CPU scheduling algorithms including FCFS, SJF preemptive and non-preemptive, priority scheduling preemptive and non-preemptive, and round robin. It includes main files that allow a user to select which algorithm to run, makefiles to compile the code, and cpp files containing the implementation of each algorithm. The code takes processes with different arrival times and burst times as input, runs the selected scheduling algorithm, and outputs the waiting times and turnaround times.

Uploaded by

KUSHAGRA ANAND
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

MAIN FILE (main.

sh)
echo "loading the program"

make

echo " 1.fcfs"


echo "2.sjfs preemptive"
echo "3.sjfs nonpreemptive"
echo "4.round robin"
echo "5.priority preemptive"
echo "6.priority nonpreemptive"
echo "enter a character :\c"

read var

case $var in
1)
./fcfs
;;
2)
./sjfspre
;;
3)
./sjfsnp
;;
4)
./prioritypre
;;
5)
./prioritynp
;;
6)
./roundrobin
;;

*) echo nothing choosed


;;
esac

echo "loading the program"

make

echo " 1.fcfs"


echo "2.sjfs preemptive"
echo "3.sjfs nonpreemptive"
echo "4.round robin"
echo "5.priority preemptive"
echo "6.priority nonpreemptive"
echo "enter a character :\c"
read var

case $var in
1)
./fcfs
;;
2)
./sjfspre
;;
3)
./sjfsnp
;;
4)
./prioritypre
;;
5)
./prioritynp
;;
6)
./roundrobin
;;

*) echo nothing choosed


;;
esac

MAKE FILE (makefile.sh)


output: fcfs sjfsnp sjfspre prioritynp prioritypre roundrobin

fcfs: fcfs.cpp
g++ -o fcfs fcfs.cpp

sjfsnp: sjfsnp.cpp
g++ -o sjfsnp sjfsnp.cpp

sjfspre: sjfspre.cpp
g++ -o sjfspre sjfspre.cpp

prioritynp: prioritynp.cpp
g++ -o prioritynp prioritynp.cpp

prioritypre: prioritypre.cpp
g++ -o prioritypre prioritypre.cpp

roundrobin: roundrobin.cpp
g++ -o roundrobin roundrobin.cpp
FCFS Algorithm
(fcfs.cpp)
// C++ program for implementation of FCFS
// scheduling with different arrival time

#include<iostream>
using namespace std;

// Function to find the waiting time for all


// processes
void findWaitingTime( int n, int bt[],
int wt[], int at[])
{
int service_time[n];
service_time[0] = 0;
wt[0] = 0;

// calculating waiting time


for (int i = 1; i < n ; i++)
{
// Add burst time of previous processes
service_time[i] = service_time[i-1] + bt[i-1];

// Find waiting time for current process =


// sum - at[i]
wt[i] = service_time[i] - at[i];

// If waiting time for a process is in negative


// that means it is already in the ready queue
// before CPU becomes idle so its waiting time is 0
if (wt[i] < 0)
wt[i] = 0;
}
}

// To calculate turn around time


void findTurnAroundTime( 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 waiting and turn-around


// times.
void findavgTime( int n, int bt[], int at[])
{
int wt[n], tat[n];

// Function to find waiting time of all processes


findWaitingTime( n, bt, wt, at);

// Function to find turn around time for all processes


findTurnAroundTime( n, bt, wt, tat);

// Display processes along with all details


cout << "Processes " << " Burst Time " << " Arrival Time "
<< " Waiting Time " << " Turn-Around Time "
<< " Completion Time \n";
int total_wt = 0, total_tat = 0;
for (int i = 0 ; i < n ; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
int compl_time = tat[i] + at[i];
cout << " " << i+1 << "\t\t" << bt[i] << "\t\t"
<< at[i] << "\t\t" << wt[i] << "\t\t "
<< tat[i] << "\t\t " << compl_time << endl;
}

cout << "Average waiting time = "


<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}

// Main function
int main()
{ int n;
int a[20];

//number of processes present


cout<<"enter the number of processes";
cin>>n;
int processes[n];

// Burst time of all processes


int burst_time[20] ;

cout<<"enter the burst time of the proceses"<<endl;

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

cin>>burst_time[i];

// Arrival time of all processes


int arrival_time[20] ;

cout<<"enter the arrival time of the proceses"<<endl;

for(int j=0;j<n;++j)
cin>> arrival_time[j];

findavgTime( n, burst_time, arrival_time);

return 0;
}

SJSF(Pre-emptive)
(sjsfpre.cpp)
//Implementation fo SHORTEST JOB FIRST(Preemptive)
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

typedef struct proccess


{
int at,bt,ct,ta,wt,btt;
string pro_id;

/*
artime = Arrival time,
bt = Burst time,
ct = Completion time,
ta = Turn around time,
wt = Waiting time
*/

}Schedule;

bool compare(Schedule a,Schedule b)


{
return a.at<b.at;

}
bool compare2(Schedule a,Schedule b)
{
return a.bt<b.bt;

int main()
{
Schedule pro[10];

int n,i,j,pcom;
//n = number of processes, i= iteration variable

cout<<"Enter the number of Process::";


cin>>n;

cout<<"Enter the Process id arrival time burst time :::";

for(i=0;i<n;i++)
{
cin>>pro[i].pro_id;
cin>>pro[i].at;
cin>>pro[i].bt;
pro[i].btt=pro[i].bt;
}

sort(pro,pro+n,compare);

/*sort is a predefined funcion defined in algorithm.h header file,


it will sort the processes according to their arrival time*/

float t=0;
float b=0;
i=0;
pcom=0;
while(pcom<n)
{
for(j=0;j<n;j++)
{
if(pro[j].at>i)
break;
}

// sort function is called again


sort(pro,pro+j,compare2);

if(j>0)
{

for(j=0;j<n;j++)
{
if(pro[j].bt!=0)
break;
}
if(pro[j].at>i)

{
i=pro[j].at;

}
pro[j].ct=i+1;
pro[j].bt--;
}
i++;
pcom=0;
for(j=0;j<n;j++)
{
if(pro[j].bt==0)
pcom++;
}
}

cout<<"ProID\tAtime\tBtime\tCtime\tTtime\tWtime\n";

for(i=0;i<n;i++)
{
pro[i].ta=pro[i].ct-pro[i].at;
pro[i].wt=pro[i].ta-pro[i].btt;

//sum calculation

t= t+pro[i].ta;
b= b+pro[i].wt;
/*Printing the Process id, arrival time, burst time,
completion time, turn around time, waiting time*/

cout<<pro[i].pro_id<<"\t"<<pro[i].at<<"\t"<<pro[i].btt<<"\t"<<pro[i].ct<<"\t"<<pro[i].ta<<"\t"<<
pro[i].wt;
cout<<endl;
}

float avg1=t/n;
float avg2=b/n;

//printing the avg of both turnaround time and waiting time

cout<<" the avg turn around time of the processes is="<<avg1<<endl;


cout<<" the avg waiting time of the processes is="<<avg2<<endl;
return 0;
}

SJSF (Non - Pre


emptive) (sjsfnp.cpp)
//Implementation fo SHORTEST JOB FIRST

#include <iostream>
#include <algorithm>
using namespace std;
int ab;

typedef struct schedule


{
string pro_id;
int at,bt,ct,ta,wt;

/*
artime = Arrival time,
bt = Burst time,
ct = Completion time,
ta = Turn around time,
wt = Waiting time
*/

}schedule;

bool compare(schedule a,schedule b)


{
return a.bt < b.bt;
/* This process will always return TRUE
if above condition comes*/
}

bool compare2(schedule a,schedule b)


{
return a.bt < b.bt && a.at <= ab;
/* This process will always return TRUE
if above condition comes*/
}

int main()
{
schedule pro[10];
//An array of Processes

int n,i,j;
//n = number of processes, i= iteration variable
float t=0;
float b=0;
cout<<"Enter the number of schedule::";
cin>>n;
cout<<"Enter the schedule id and burst time :::";

for(i=0;i<n;i++)
{
cin>>pro[i].pro_id;
cin>>pro[i].bt;
pro[i].at=0;
}

/*sort is a predefined funcion defined in algorithm.h header file,


it will sort the processes according to their arrival time*/

sort(pro,pro+n,compare);
// initial values

pro[0].ct=pro[0].bt+pro[0].at;
pro[0].ta=pro[0].ct-pro[0].at;
pro[0].wt=pro[0].ta-pro[0].bt;

for(i=1;i<n;i++)
{
ab=pro[i-1].ct;
sort(pro+i,pro+n,compare2);

if(pro[i-1].ct<pro[i].at)
{
pro[i].ct=pro[i-1].ct+pro[i].bt+(pro[i].at-pro[i-1].ct);
}
else
{

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

}
pro[i].ta=pro[i].ct-pro[i].at;
pro[i].wt=pro[i].ta-pro[i].bt;

t= t+pro[i].ta;
b= b+pro[i].wt;
}
t=t+pro[0].ta;
b=b+pro[0].wt;
cout<<t;
cout<<b;
float avg1=t/n;
float avg2=b/n;
cout<<"ProID\tAtime\tBtime\tCtime\tTtime\tWtime\n";

for(i=0;i<n;i++)
{
//before executing make it in one statement
cout<<pro[i].pro_id<<"\t"<<pro[i].at<<"\t"<<pro[i].bt
<<"\t"<<pro[i].ct<<"\t"<<pro[i].ta<<"\t"<<pro[i].wt;

cout<<endl;
}

cout<<" the avg turn around time of the processes is="<<avg1<<endl;


cout<<" the avg waiting time of the processes is="<<avg2<<endl;

return 0;
}
Priority
Scheduling (NP)
(prioritynp.cpp)
//Implementation of Priority(Non-Preeemptive) Using C++
#include <iostream>
#include <algorithm>
using namespace std;

typedef struct proccess


{
int at,bt,pr,ct,ta,wt;
string pro_id;

/*
artime = Arrival time,
bt = Burst time,
ct = Completion time,
ta = Turn around time,
wt = Waiting time
*/

}process;

bool compare(process a,process b)


{
return a.at<b.at;
/* This schedule will always return TRUE
if above condition comes*/

bool compare2(process a,process b)


{
return a.pr>b.pr;

int main()
{
process pro[10];
int n,i,j;
cout<<"Enter the number of process::";
cin>>n;
cout<<"Enter the process id arrival time burst time and priority :::";

for(i=0;i<n;i++)
{
cin>>pro[i].pro_id;
cin>>pro[i].at;
cin>>pro[i].bt;
cin>>pro[i].pr;
}

sort(pro,pro+n,compare);
float t=0;
float b=0;

/*sort is a predefined funcion defined in algorithm.h header file,


it will sort the schedulees according to their arrival time*/

pro[0].ct=pro[0].bt+pro[0].at;
pro[0].ta=pro[0].ct-pro[0].at;
pro[0].wt=pro[0].ta-pro[0].bt;
i=1;

while(i<n-1)
{

for(j=i;j<n;j++)
{
if(pro[j].at>pro[i-1].ct)
break;
}
sort(pro+i,pro+i+(j-i),compare2);
pro[i].ct=pro[i-1].ct+pro[i].bt;
pro[i].ta=pro[i].ct-pro[i].at;
pro[i].wt=pro[i].ta-pro[i].bt;
i++;
}
pro[i].ct=pro[i-1].ct+pro[i].bt;
pro[i].ta=pro[i].ct-pro[i].at;
pro[i].wt=pro[i].ta-pro[i].bt;
cout<<"ProID\tAtime\tBtime\tCtime\tTtime\tWtime\tPriority\n";
for(i=0;i<n;i++)
{ t= t+pro[i].ta;
b= b+pro[i].wt;
//desplaying all the values

cout<<pro[i].pro_id<<"\t"<<pro[i].at<<"\t"<<pro[i].bt<<"\t"<<pro[i].ct<<"\t"<<pro[i].ta<<"\t"<<p
ro[i].wt<<"\t"<<pro[i].pr;
cout<<endl;
}
float avg1=t/n;

float avg2=b/n;
cout<<" the avg turn around time of the processes is="<<avg1<<endl;
cout<<" the avg waiting time of the processes is="<<avg2<<endl;

return 0;
}
Priority
Scheduling(Pre)
(prioritypre.cpp)
//Implementation of Priority(Preeemptive)
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;

typedef struct proccess


{
int at,bt,ct,ta,wt,btt,pr;
string pro_id;
/*
artime = Arrival time,
bt = Burst time,
ct = Completion time,
ta = Turn around time,
wt = Waiting time
*/

}schedule;

bool compare(schedule a,schedule b)


{
return a.at<b.at;

bool compare2(schedule a,schedule b)


{
return a.pr>b.pr;

int main()
{
schedule pro[10];

int n,i,j,pcom;

cout<<"Enter the number of process::";


cin>>n;
cout<<"Enter the Process id arrival time burst time and priority :::";
for(i=0;i<n;i++)
{
cin>>pro[i].pro_id;
cin>>pro[i].at;
cin>>pro[i].bt;
pro[i].btt=pro[i].bt;
cin>>pro[i].pr;
}

sort(pro,pro+n,compare);
float t=0;
float b=0;
i=0;
pcom=0;
while(pcom<n)
{
for(j=0;j<n;j++)
{
if(pro[j].at>i)
break;
}
//second sort
sort(pro,pro+j,compare2);

if(j>0)
{
for(j=0;j<n;j++)
{
if(pro[j].bt!=0)
break;
}

if(pro[j].at>i)
{
i+=pro[j].at-i;
}

pro[j].ct=i+1;
pro[j].bt--;
}

i++;
pcom=0;
for(j=0;j<n;j++)
{
if(pro[j].bt==0)
pcom++;
}
}
cout<<"ProID\tAtime\tBtime\tCtime\tTtime\tWtime\tPriority\n";

for(i=0;i<n;i++)
{
pro[i].ta=pro[i].ct-pro[i].at;
pro[i].wt=pro[i].ta-pro[i].btt;
//before executing make it in one statement

// sum calculation
t= t+pro[i].ta;
b= b+pro[i].wt;

cout<<pro[i].pro_id<<"\t"<<pro[i].at<<"\t"<<pro[i].btt<<"\t"<<pro[i].ct<<"\t"<<pro[i].ta<<"\t"<<
pro[i].wt<<"\t"<<pro[i].pr;
cout<<endl;
}

float avg1=t/n;

float avg2=b/n;
cout<<" the avg turn around time of the processes is="<<avg1<<endl;
cout<<" the avg waiting time of the processes is="<<avg2<<endl;

return 0;
}

RoundRobin
(roundrobin.cpp)
//C++ Program to implement Round Robin
//Scheduling CPU Algorithm

#include <iostream>
#include <vector>

/*at = Arrival time,


bt = Burst time,
time_quantum= Quantum time
tat = Turn around time,
wt = Waiting time*/

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;
// assigning the number of process to remain variable

vector<int>at(n);
vector<int>bt(n);
vector<int>rt(n);
//dynamic array declaration using vector method of (STL)
//STL standard template library of C++

cout<<"Enter the Arrival time, Burst time for All the processes"<<endl;
for(i=0;i<n;i++)
{
cin>>at[i];
cin>>bt[i];
rt[i]=bt[i];
}

cout<<"Enter the value of time QUANTUM:"<<endl;


cin>>time_quantum;

cout<<"\n\nProcess\t:Turnaround Time:Waiting Time\n\n";


for(time=0,i=0;remain!=0;)
{
if(rt[i]<=time_quantum && rt[i]>0)
{
time += rt[i];
//Addition using shorthand operators
rt[i]=0;
temps=1;
}

else if(rt[i]>0)
{
rt[i] -= time_quantum;
//Subtraction using shorthand operators
time += time_quantum;
//Addition using shorthand operators
}

if(rt[i]==0 && temps==1)


{
remain--;
//Desplaying the result of wating, turn around time:
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;
}

cout<<"Average waiting time "<<wt*1.0/n<<endl;


cout<<"Average turn around time "<<tat*1.0/n<<endl;;

return 0;
}
Created by :-
1) Anurag Singh (1705489)
2) Kushagra Anand (1705503)

You might also like