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

Topic:-Cpu Scheduling Algorithms

This document discusses three different CPU scheduling algorithms: first-come, first-served (FCFS), shortest job first (SJF), and priority scheduling. It provides C++ code implementations of each algorithm and functions to calculate waiting times, turnaround times, and average times. The code takes in processes with IDs, burst times, and optionally priorities. It then runs the scheduling algorithm, outputs the order processed, and calculates performance metrics.
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)
40 views5 pages

Topic:-Cpu Scheduling Algorithms

This document discusses three different CPU scheduling algorithms: first-come, first-served (FCFS), shortest job first (SJF), and priority scheduling. It provides C++ code implementations of each algorithm and functions to calculate waiting times, turnaround times, and average times. The code takes in processes with IDs, burst times, and optionally priorities. It then runs the scheduling algorithm, outputs the order processed, and calculates performance metrics.
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
You are on page 1/ 5

Topic :- CPU SCHEDULING ALGORITHMS

# CODE1 cpu scheduling with fcfs

#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 will be 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 findAverageTime( 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;
}

// main function
int main()
{
// process ids
int processes[] = { 1, 2, 3, 4};
int n = sizeof processes / sizeof processes[0];

// burst time of all processes


int burst_time[] = {21, 3, 6, 2};

findAverageTime(processes, n, burst_time);

return 0;
}

# code2 Cpu scheduling with sjf (shortest job first )

// c++ program to implement Shortest Job first

#include<bits/stdc++.h>

using namespace std;

struct Process
{
int pid; // process ID
int bt; // burst Time
};

/*
this function is used for sorting all
processes in increasing order of burst time
*/
bool comparison(Process a, Process b)
{
return (a.bt < b.bt);
}

// 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 findAverageTime(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 << "Average waiting time = "


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

// main function
int main()
{
Process proc[] = {{1, 21}, {2, 3}, {3, 6}, {4, 2}};
int n = sizeof proc / sizeof proc[0];

// sorting processes by burst time.


sort(proc, proc + n, comparison);

cout << "Order in which process gets executed\n";


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

findAverageTime(proc, n);
return 0;
}

Code3 cpu scheduling with priority scheduling

// Implementation of Priority scheduling algorithm


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

struct Process
{
// this is the process ID
int pid;
// the CPU burst time
int bt;
// priority of the process
int priority;
};

// sort the processes based on priority


bool sortProcesses(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, sortProcesses);

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

End
**********************************************************************************
****************//

You might also like