22BLC1104-Lab-05
22BLC1104-Lab-05
Q) Implement (i) priority and (ii) preemptive priority CPU Scheduling and print turnaround
time, waiting time, response time and completion time of each process, average waiting time
and average turn around time The system runs the processes with same priority using round-
robin scheduling. Get the time slice from the use. Display the sequence of scheduled process.
Lower the number higher the priority.
(i) Non-Preemptive:
CODE:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int id;
int arrival_time;
int burst_time;
int priority;
int start_time;
int completion_time;
int turnaround_time;
int waiting_time;
int response_time;
int finished;
} Process;
int main() {
int n, choice, i;
printf("Enter test case type (1: arrival time given, 2: no arrival time): ");
scanf("%d", &choice);
scanf("%d", &n);
Process proc[n];
if (choice == 1) {
proc[i].id = i + 1;
printf("Enter arrival time, burst time, priority for process %d: ", proc[i].id);
proc[i].start_time = -1;
proc[i].finished = 0;
} else {
proc[i].id = i + 1;
proc[i].arrival_time = 0;
proc[i].start_time = -1;
proc[i].finished = 0;
int sched_index = 0;
best_priority = proc[i].priority;
idx = i;
idx = i;
if (idx == -1) {
current_time++;
continue;
proc[idx].start_time = current_time;
current_time += proc[idx].burst_time;
proc[idx].completion_time = current_time;
proc[idx].finished = 1;
completed++;
total_waiting_time += proc[i].waiting_time;
total_turnaround_time += proc[i].turnaround_time;
}
printf("\nProcess\tAT\tBT\tPriority\tCT\tTAT\tWT\tRT\n");
return 0;
OUTPUT:
Test Case-1:
Test Case-2:
(i) Preemptive:
CODE:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int id;
int arrival_time;
int burst_time;
int remaining_time;
int priority;
int start_time;
int completion_time;
int response_time;
int waiting_time;
int turnaround_time;
} Process;
int main() {
int n, choice;
printf("Enter test case type (1: arrival time given, 2: no arrival time): ");
scanf("%d", &choice);
scanf("%d", &n);
Process proc[n];
if (choice == 1) {
proc[i].id = i + 1;
printf("Enter arrival time, burst time, priority for process %d: ", proc[i].id);
proc[i].remaining_time = proc[i].burst_time;
proc[i].start_time = -1;
} else {
proc[i].id = i + 1;
proc[i].arrival_time = 0;
proc[i].remaining_time = proc[i].burst_time;
proc[i].start_time = -1;
int time_slice;
printf("Enter time slice for round robin (for processes with same priority): ");
scanf("%d", &time_slice);
int current_time = 0, completed = 0;
int current_quantum = 0;
int sched_index = 0;
highest_priority = proc[i].priority;
candidate = current_process;
} else {
proc[idx].priority == highest_priority) {
candidate = idx;
break;
current_quantum = 0;
if (candidate == -1) {
current_time++;
continue;
last_executed = candidate;
if (proc[candidate].start_time == -1) {
proc[candidate].start_time = current_time;
proc[candidate].remaining_time--;
current_time++;
current_quantum++;
if (proc[candidate].remaining_time == 0) {
proc[candidate].completion_time = current_time;
completed++;
current_process = -1;
current_quantum = 0;
} else {
if (current_quantum == time_slice) {
current_process = -1;
current_quantum = 0;
} else {
current_process = candidate;
total_waiting_time += proc[i].waiting_time;
total_turnaround_time += proc[i].turnaround_time;
printf("\nProcess\tAT\tBT\tPriority\tCT\tTAT\tWT\tRT\n");
proc[i].waiting_time, proc[i].response_time);
return 0;
}
OUTPUT:
Test Case-1:
Test Case-2: