Write the program to simulate 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 remaining; // Remaining burst time
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 preemptive priority scheduling
void preemptivePriorityScheduling(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]);
}
}
}
// Initialize variables
int currentTime = 0;
int totalWaiting = 0;
int totalTurnaround = 0;
int completed = 0;
int isExecuting = 0; // Process currently executing
int timeQuantum = 1; // Time quantum for each process
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
while (completed < n) {
int selected = -1; // Index of the process to execute
int highestPriority = 99999; // Highest priority value (lower is higher priority)
// Find the process with the highest priority that has arrived
for (int i = 0; i < n; i++) {
if (processes[i].arrival <= currentTime && processes[i].remaining > 0 && processes[i].priority < highestPriority) {
selected = i;
highestPriority = processes[i].priority;
}
}
if (selected != -1) {
if (!isExecuting) {
isExecuting = 1;
printf("%3d |", currentTime);
}
// Execute the selected process for the time quantum or until it completes
int timeToExecute = (processes[selected].remaining < timeQuantum) ? processes[selected].remaining : timeQuantum;
processes[selected].remaining -= timeToExecute;
currentTime += timeToExecute;
// Print Gantt chart
printf(" P%d |", processes[selected].pid);
// Check if the process has completed
if (processes[selected].remaining == 0) {
// Calculate turnaround time
processes[selected].turnaround = currentTime - processes[selected].arrival;
totalTurnaround += processes[selected].turnaround;
// Calculate waiting time
processes[selected].waiting = processes[selected].turnaround - processes[selected].burst;
totalWaiting += processes[selected].waiting;
completed++;
isExecuting = 0;
}
} else {
// No process can execute at the moment, increment the current time
currentTime++;
}
}
// 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);
processes[i].remaining = processes[i].burst;
}
// Perform preemptive priority scheduling
preemptivePriorityScheduling(processes, n);
return 0;
}

Comments
Post a Comment