Write the program to simulate Non-preemptive Priority scheduling. The arrival time and first CPU-burst and priority for different n number of processes should be input to the algorithm. Assume the fixed IO waiting time (2 units). The next CPU-burst should be generated randomly. The output should give Gantt chart, turnaround time and waiting time for each process. Also find the average waiting time and turnaround time.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Structure to represent a process
typedef struct Process {
int pid; // Process ID
int arrival; // Arrival time
int burst; // CPU burst time
int priority; // Priority
int waiting; // Waiting time
int turnaround; // Turnaround time
} Process;
// Function to swap two processes
void swap(Process *a, Process *b) {
Process temp = *a;
*a = *b;
*b = temp;
}
// Function to perform non-preemptive priority scheduling
void priorityScheduling(Process *processes, int n) {
// Sort processes based on arrival time
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (processes[j].arrival > processes[j + 1].arrival) {
swap(&processes[j], &processes[j + 1]);
}
}
}
// Variables to keep track of time and total waiting/turnaround time
int currentTime = 0;
int totalWaiting = 0;
int totalTurnaround = 0;
printf("\nGantt Chart:\n");
printf("--------------------------------------------------\n");
printf("Time |");
for (int i = 0; i < n; i++) {
printf(" P%d |", processes[i].pid);
}
printf("\n--------------------------------------------------\n");
// Perform scheduling
for (int i = 0; i < n; i++) {
if (currentTime < processes[i].arrival) {
currentTime = processes[i].arrival;
}
// Calculate turnaround time
processes[i].turnaround = currentTime - processes[i].arrival + processes[i].burst;
totalTurnaround += processes[i].turnaround;
// Calculate waiting time
processes[i].waiting = processes[i].turnaround - processes[i].burst;
totalWaiting += processes[i].waiting;
// Print Gantt chart
printf("%3d |", currentTime);
for (int j = 0; j < n; j++) {
if (j == i) {
printf(" %d* |", currentTime + processes[i].burst);
currentTime += processes[i].burst;
} else {
printf(" |");
}
}
printf("\n");
}
// Print turnaround time and waiting time for each process
printf("\nProcess | Turnaround Time | Waiting Time\n");
printf("----------------------------------------\n");
for (int i = 0; i < n; i++) {
printf(" P%d %5d %5d\n", processes[i].pid, processes[i].turnaround, processes[i].waiting);
}
// Print average turnaround time and waiting time
printf("\nAverage Turnaround Time: %.2f\n", (float)totalTurnaround / n);
printf("Average Waiting Time: %.2f\n", (float)totalWaiting / n);
}
int main() {
srand(time(NULL));
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
// Initialize an array of processes
Process processes[n];
for (int i = 0; i < n; i++) {
processes[i].pid = i + 1;
printf("Enter arrival time, burst time, and priority for P%d: ", i + 1);
scanf("%d %d %d", &processes[i].arrival, &processes[i].burst, &processes[i].priority);
}
// Perform non-preemptive priority scheduling
priorityScheduling(processes, n);
return 0;
}

Comments
Post a Comment