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

Assignment 2

Uploaded by

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

Assignment 2

Uploaded by

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

a.

Write generic code in C of CPU Scheduling Algorithms, FCFS, SJF, SRTF, RR,
nonPreemptive Priority, and Preemptive Priority. In the program first user will input
data in sequence like, process no., arrival time, burst time, and priority and then select
any one of the algorithm to get TAT, WT and RT for each process and their averages.
(If RR is selected by user then also take time quantum as input).

FCFS code:
// C++ program for implement FCFS
#include<bits/stdc++.h>
using namespace std

struct Process

int pid; // Process ID


int bt; // CPU Burst time required
int priority; // Priority of this process
}

// Function to sort the Process acc. to priority


bool comparison(Process a, Process b

return (a.priority > b.priority)

// Function to find the waiting time for all


// processes
void findWaitingTime(Process proc[], int n
int wt[]

// waiting time for first process is 0


wt[0] = 0

// calculating waiting time


for (int i = 1; i < n ; i++
wt[i] = proc[i-1].bt + wt[i-1]

// Function to calculate turn around time


void findTurnAroundTime( Process proc[], int n
int wt[], int tat[]

// calculating turnaround time by adding


// bt[i] + wt[i]
for (int i = 0; i < n ; i++
tat[i] = proc[i].bt + wt[i]
{

//Function to calculate average time


void findavgTime(Process proc[], int n

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

//Function to find waiting time of all processes


findWaitingTime(proc, n, wt)

//Function to find turn around time for all processes


findTurnAroundTime(proc, n, wt, tat)

//Display processes along with all details


cout << "\nProcesses "<< " 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 << " " << proc[i].pid << "\t\t"
<< proc[i].bt << "\t " << wt[i
<< "\t\t " << tat[i] <<endl

cout << "\nAverage waiting time = "


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

void priorityScheduling(Process proc[], int n

// Sort processes by priority


sort(proc, proc + n, comparison)

cout<< "Order in which processes gets executed \n"


for (int i = 0 ; i < n; i++
cout << proc[i].pid <<" "

findavgTime(proc, n)

// Driver code
int main(

Process proc[] = {{1, 10, 2}, {2, 5, 0}, {3, 8, 1}}


int n = sizeof proc / sizeof proc[0]
{

priorityScheduling(proc, n)
return 0

SJF Code:
// C++ program to implement Shortest Job first with Arrival
// Time
#include <iostream>
using namespace std
int mat[10][6]

void swap(int* a, int* b

int temp = *a
*a = *b
*b = temp

void arrangeArrival(int num, int mat[][6]

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


for (int j = 0; j < num - i - 1; j++)
if (mat[j][1] > mat[j + 1][1])
for (int k = 0; k < 5; k++)
swap(mat[j][k], mat[j + 1][k])

void completionTime(int num, int mat[][6]

int temp, val


mat[0][3] = mat[0][1] + mat[0][2]
mat[0][5] = mat[0][3] - mat[0][1]
mat[0][4] = mat[0][5] - mat[0][2]

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


temp = mat[i - 1][3]
int low = mat[i][2]
for (int j = i; j < num; j++)
if (temp >= mat[j][1] && low >= mat[j][2])
low = mat[j][2]
val = j
}

mat[val][3] = temp + mat[val][2]


mat[val][5] = mat[val][3] - mat[val][1]
mat[val][4] = mat[val][5] - mat[val][2]
for (int k = 0; k < 6; k++)
swap(mat[val][k], mat[i][k])

int main(

int num, temp

cout << "Enter number of Process: "


cin >> num

cout << "...Enter the process ID...\n"


for (int i = 0; i < num; i++)
cout << "...Process " << i + 1 << "...\n"
cout << "Enter Process Id: "
cin >> mat[i][0]
cout << "Enter Arrival Time: "
cin >> mat[i][1]
cout << "Enter Burst Time: "
cin >> mat[i][2]

cout << "Before Arrange...\n"


cout << "Process ID\tArrival Time\tBurst Time\n"
for (int i = 0; i < num; i++)
cout << mat[i][0] << "\t\t" << mat[i][1] << "\t\t"
<< mat[i][2] << "\n"

arrangeArrival(num, mat)
completionTime(num, mat)
cout << "Final Result...\n"
cout << "Process ID\tArrival Time\tBurst Time\tWaiting "
"Time\tTurnaround Time\n"
for (int i = 0; i < num; i++)
cout << mat[i][0] << "\t\t" << mat[i][1] << "\t\t"
<< mat[i][2] << "\t\t" << mat[i][4] << "\t\t"
<< mat[i][5] << "\n"
}

SRTF Code:

#include<iostream>

using namespace std


int main(

int a[10],b[10],x[10]
int waiting[10],turnaround[10],completion[10]
int i,j,smallest,count=0,time,n
double avg=0,tt=0,end

cout<<"\nEnter the number of Processes: "; //input


cin>>n
for(i=0; i<n; i++

cout<<"\nEnter arrival time of process: "; //input


cin>>a[i]

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

cout<<"\nEnter burst time of process: "; //input


cin>>b[i]

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


x[i]=b[i]

b[9]=9999
for(time=0; count!=n; time++

smallest=9
for(i=0; i<n; i++

if(a[i]<=time && b[i]<b[smallest] && b[i]>0


smallest=i

b[smallest]--

if(b[smallest]==0

count++
end=time+1
completion[smallest] = end
waiting[smallest] = end - a[smallest] - x[smallest]
turnaround[smallest] = end - a[smallest]
{

cout<<"Process"<<"\t"<< "burst-time"<<"\t"<<"arrival-
time" <<"\t"<<"waiting-time" <<"\t"<<"turnaround-
time"<< "\t"<<"completion-time"<<endl
for(i=0; i<n; i++

cout<<"p"<<i+1<<"\t\t"<<x[i]<<"\t\t"<<a[i]<<"\t\t"<<waiting[i]<
<"\t\t"<<turnaround[i]<<"\t\t"<<completion[i]<<endl
avg = avg + waiting[i]
tt = tt + turnaround[i]

cout<<"\n\nAverage waiting time ="<<avg/n


cout<<" Average Turnaround time ="<<tt/n<<endl

RR code:

#include<stdio.h>
#include<conio.h>

void main()
{
// initlialize the variable name
int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], t
emp[10];
float avg_wt, avg_tat;
printf(" Total number of process in the system: ");
scanf("%d", &NOP);
y = NOP; // Assign the number of process to variable y

// Use for loop to enter the details of the process like Arrival time a
nd the Burst Time
for(i=0; i<NOP; i++)
{
printf("\n Enter the Arrival and Burst time of the Process[%d]
\n", i+1);
printf(" Arrival time is: \t"); // Accept arrival time
scanf("%d", &at[i]);
printf(" \nBurst time is: \t"); // Accept the Burst time
scanf("%d", &bt[i]);
temp[i] = bt[i]; // store the burst time in temp array
}
// Accept the Time qunat
printf("Enter the Time Quantum for the process: \t");
scanf("%d", &quant);
// Display the process No, burst time, Turn Around Time and the waiting
time
printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
}

for(sum=0, i = 0; y!=0; )
{
if(temp[i] <= quant && temp[i] > 0) // define the conditions
{
sum = sum + temp[i];
temp[i] = 0;
count=1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - quant;
sum = sum + quant;
}
if(temp[i]==0 && count==1)
{
y--; //decrement the process no.
printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[
i], sum-at[i], sum-at[i]-bt[i]);
wt = wt+sum-at[i]-bt[i];
tat = tat+sum-at[i];
count =0;
}
if(i==NOP-1)
{
i=0;
}
else if(at[i+1]<=sum)
{
i++;
}
else
{
i=0;
}
}
// represents the average waiting time and Turn Around time
avg_wt = wt * 1.0/NOP;
avg_tat = tat * 1.0/NOP;
printf("\n Average Turn Around Time: \t%f", avg_wt);
printf("\n Average Waiting Time: \t%f", avg_tat);
getch();
}

Non Premptive priority scheduling

#include<stdio.h>
 

int main(

int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_w
t,avg_tat
printf("Enter Total Number of Process:")
scanf("%d",&n)

printf("\nEnter Burst Time and Priority\n")


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

printf("\nP[%d]\n",i+1)
printf("Burst Time:")
scanf("%d",&bt[i])
printf("Priority:")
scanf("%d",&pr[i])
p[i]=i+1; //contains process number

//
sorting burst time, priority and process number in ascending order usin
g 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

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Tim


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

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


total+=tat[i]
printf("\nP[%d]
\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i])

avg_tat=total/n; //average turnaround time


printf("\n\nAverage Waiting Time=%d",avg_wt)
printf("\nAverage Turnaround Time=%d\n",avg_tat)

return 0

Premptive Priority scheduling:

// CPP program to implement preemptive priority scheduling


#include <bits/stdc++.h>
using namespace std

struct Process
int processID
int burstTime
int tempburstTime
int responsetime
int arrivalTime
int priority
int outtime
int intime
}

// It is used to include all the valid and eligible


// processes in the heap for execution. heapsize defines
// the number of processes in execution depending on
// the current time currentTime keeps a record of
// the current CPU time.
 

void insert(Process Heap[], Process value, int* heapsize


int* currentTime

int start = *heapsize, i


Heap[*heapsize] = value
if (Heap[*heapsize].intime == -1
Heap[*heapsize].intime = *currentTime
++(*heapsize)

// Ordering the Heap


while (start != 0 && Heap[(start - 1) / 2].priority
Heap[start].priority)
Process temp = Heap[(start - 1) / 2]
Heap[(start - 1) / 2] = Heap[start]
Heap[start] = temp
start = (start - 1) / 2

// It is used to reorder the heap according to


// priority if the processes after insertion
// of new process.
void order(Process Heap[], int* heapsize, int start

int smallest = start


int left = 2 * start + 1
int right = 2 * start + 2
if (left < *heapsize && Heap[left].priority
Heap[smallest].priority
smallest = left
if (right < *heapsize && Heap[right].priority
Heap[smallest].priority
smallest = right

// Ordering the Heap


if (smallest != start)
Process temp = Heap[smallest]
Heap[smallest] = Heap[start]
Heap[start] = temp
order(Heap, heapsize, smallest)

// This function is used to find the process with


// highest priority from the heap. It also reorders
// the heap after extracting the highest priority process.
Process extractminimum(Process Heap[], int* heapsize
int* currentTime

Process min = Heap[0]


{

<

<

>

if (min.responsetime == -1
min.responsetime = *currentTime - min.arrivalTime
--(*heapsize)
if (*heapsize >= 1)
Heap[0] = Heap[*heapsize]
order(Heap, heapsize, 0)

return min

// Compares two intervals according to staring times.


bool compare(Process p1, Process p2

return (p1.arrivalTime < p2.arrivalTime)

// This function is responsible for executing


// the highest priority extracted from Heap[].
void scheduling(Process Heap[], Process array[], int n
int* heapsize, int* currentTime

if (heapsize == 0
return

Process min = extractminimum(Heap, heapsize, currentTime)


min.outtime = *currentTime + 1
--min.burstTime
printf("process id = %d current time = %d\n"
min.processID, *currentTime)

// If the process is not yet finished


// insert it back into the Heap*/
if (min.burstTime > 0)
insert(Heap, min, heapsize, currentTime)
return

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


if (array[i].processID == min.processID)
array[i] = min
break

// This function is responsible for


// managing the entire execution of the
// processes as they arrive in the CPU
// according to their arrival time.
void priority(Process array[], int n
}

sort(array, array + n, compare)

int totalwaitingtime = 0, totalbursttime = 0


totalturnaroundtime = 0, i, insertedprocess = 0
heapsize = 0, currentTime = array[0].arrivalTime
totalresponsetime = 0

Process Heap[4 * n]

// Calculating the total burst time


// of the processes
for (int i = 0; i < n; i++)
totalbursttime += array[i].burstTime
array[i].tempburstTime = array[i].burstTime

// Inserting the processes in Heap


// according to arrival time
do
if (insertedprocess != n)
for (i = 0; i < n; i++)
if (array[i].arrivalTime == currentTime)
++insertedprocess
array[i].intime = -1
array[i].responsetime = -1
insert(Heap, array[i], &heapsize, ¤tTime)

scheduling(Heap, array, n, &heapsize, ¤tTime)


++currentTime
if (heapsize == 0 && insertedprocess == n
break
} while (1)

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


totalresponsetime += array[i].responsetime
totalwaitingtime += (array[i].outtime - array[i].intime
array[i].tempburstTime)
totalbursttime += array[i].burstTime

printf("Average waiting time = %f\n"


((float)totalwaitingtime / (float)n))
printf("Average response time =%f\n"
((float)totalresponsetime / (float)n))
printf("Average turn around time = %f\n"
((float)(totalwaitingtime + totalbursttime) / (float)n))

// Driver code
}

int main(

int n, i
Process a[5]
a[0].processID = 1
a[0].arrivalTime = 4
a[0].priority = 2
a[0].burstTime = 6
a[1].processID = 4
a[1].arrivalTime = 5
a[1].priority = 1
a[1].burstTime = 3
a[2].processID = 2
a[2].arrivalTime = 5
a[2].priority = 3
a[2].burstTime = 1
a[3].processID = 3
a[3].arrivalTime = 1
a[3].priority = 4
a[3].burstTime = 2
a[4].processID = 5
a[4].arrivalTime = 3
a[4].priority = 5
a[4].burstTime = 4
priority(a, 5)
return 0

2.
Code:
// C++ program for implementation of RR 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[], int quantum

// Make a copy of burst times bt[] to store remaining


// burst times.
int rem_bt[n]
for (int i = 0 ; i < n ; i++
rem_bt[i] = bt[i]
{

int t = 0; // Current time

// Keep traversing processes in round robin manner


// until all of them are not done.
while (1

bool done = true

// Traverse all processes one by one repeatedly


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

// If burst time of a process is greater than 0


// then only need to process further
if (rem_bt[i] > 0

done = false; // There is a pending process

if (rem_bt[i] > quantum

// Increase the value of t i.e. shows


// how much time a process has been processed
t += quantum

// Decrease the burst_time of current process


// by quantum
rem_bt[i] -= quantum

// If burst time is smaller than or equal to


// quantum. Last cycle for this process
else

// Increase the value of t i.e. shows


// how much time a process has been processed
t = t + rem_bt[i]

// Waiting time is current time minus time


// used by this process
wt[i] = t - bt[i]

// As the process gets fully executed


// make its remaining burst time = 0
rem_bt[i] = 0

// If all processes are done


if (done == true
break
{

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

// 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,4,5}
}

int n = sizeof processes / sizeof processes[0]

// Burst time of all processes


int burst_time[] = {10, 4, 6,7,2}

// Time quantum
int quantum = 2
findavgTime(processes, n, burst_time, quantum)
return 0
}

You might also like