You are on page 1of 5

Aim: Write a program to simulate the following preemptive and non-preemptive CPU scheduling algorithms to

find turnaround time and waiting time.

Name Of The Scheduling Algorithm: Priority [ Non-Preemptive]

Description: In the Non Preemptive Priority scheduling, The Processes are scheduled according to the priority
number assigned to them. Once the process gets scheduled, it will run till the completion. Generally, the lower the
priority number, the higher is the priority of the process.

Program:
#include <stdio.h>
#include <stdbool.h>
void sort(int p[],int at[],int bt[],int n){
int temp,i,j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (at[j] > at[j + 1]) {
temp=at[j];
at[j]=at[j+1];
at[j+1]=temp;
temp=bt[j];
bt[j]=bt[j+1];
bt[j+1]=temp;
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
}
void calculateWaitingTurnaroundTime(int at[],int bt[], int n, int wt[], int tat[]) {
// Waiting time for the first process is always 0
int i;
wt[0] = 0;

// Calculate waiting time


for (i = 1; i < n; i++) {
wt[i] = bt[i - 1] + wt[i - 1];
}
// Calculate turnaround time
for (i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
}
}
// Function to display Gantt chart
void displayGanttChart(int bt[], int n,int p[]) {
int i;
printf("Gantt Chart:\n");
for (i = 0; i < n; i++) {
printf(" -------");
}
printf("\n");
printf("|");
for (i = 0; i < n; i++) {
printf(" P%d |", p[i]);
}
printf("\n");
for (i = 0; i < n; i++) {
printf(" -------");
}
printf("\n");
printf("0");
int sum=0;
for (i = 0; i < n; i++) {
sum=sum+bt[i];
printf(" %d", sum);
}
printf("\n");
}
int main() {
int n,i; // Number of processes
printf("Enter the number of processes: ");
scanf("%d", &n);
int bt[n],p[n],at[n]; // Burst times for each process
int wt[n]; // Waiting times for each process
int tat[n],ct[n]; // Turnaround times for each process
printf("Enter burst times for each process:\n");
for (i = 0; i < n; i++) {
p[i]=i+1;
printf("Enter priority for P%d: ",i+1);
scanf("%d",&at[i]);
printf("Burst time for P%d: ", i+1);
scanf("%d", &bt[i]);
}
sort(p,at,bt,n);
// Display Gantt chart
displayGanttChart(bt, n,p);

// Perform SJF scheduling


// Calculate waiting and turnaround times
calculateWaitingTurnaroundTime(at,bt, n, wt, tat);
printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < n; i++) {
printf("P%d\t%d\t\t%d\t\t%d\n", p[i], bt[i], wt[i], tat[i]);
}
float wait=0,total=0;
for (i = 0; i < n; i++) {
wait=wait+wt[i];
total=total+tat[i];
}
wait/=n;
total/=n;
printf("Average waiting :%.2f\n",wait);
printf("Average total around time: %.2f",total);
return 0;
}

Output:

Name Of The Scheduling Algorithm: Priority [ Preemptive]

Description:In Preemptive Priority Scheduling, at the time of arrival of a process in the ready queue, its Priority is
compared with the priority of the other processes present in the ready queue as well as with the one which is being
executed by the CPU at that point of time. The One with the highest priority among all the available processes will
be given the CPU next.

Program:
#include<stdio.h>
struct process
{
int WT, AT, BT, TAT, PT;
};
struct process a[10];
int main()
{
int n, temp[10], t, count = 0, short_p;
float total_WT = 0, total_TAT = 0, Avg_WT, Avg_TAT;
printf("Enter the number of processes\n");
scanf("%d", &n);
printf("Enter the arrival time, burst time, and priority of the process\n");
printf("AT BT PT\n");
int i;
for (i = 0; i < n; i++)
{
scanf("%d%d%d", &a[i].AT, &a[i].BT, &a[i].PT);
// Copying the burst time in
// a temp array for further use
temp[i] = a[i].BT;
}
// We initialize the burst time
// of a process with a maximum
a[9].PT = 10000;
printf("Gantt Chart:\n");
printf("| ");
for (t = 0; count != n; t++)
{
short_p = 9;
for (i = 0; i < n; i++)
{
if (a[short_p].PT > a[i].PT && a[i].AT <= t && a[i].BT > 0)
{
short_p = i;
}
}
a[short_p].BT = a[short_p].BT - 1;
// Print the process ID in the Gantt chart
printf("P%d | ", short_p + 1);
// If any process is completed
if (a[short_p].BT == 0)
{
// One process is completed
// so count increases by 1
count++;
a[short_p].WT = t + 1 - a[short_p].AT - temp[short_p];
a[short_p].TAT = t + 1 - a[short_p].AT;
// Total calculation
total_WT = total_WT + a[short_p].WT;
total_TAT = total_TAT + a[short_p].TAT;
}
}
printf("\n\n");
Avg_WT = total_WT / n;
Avg_TAT = total_TAT / n;
// Printing the answer
printf("pid WT TAT\n");
for (i = 0; i < n; i++)
{
printf("%d %d\t%d\n", i + 1, a[i].WT, a[i].TAT);
}
printf("Avg waiting time of the process is %f\n", Avg_WT);
printf("Avg turn around time of the process is %f\n", Avg_TAT);
return 0;
}
Output:

You might also like