0% found this document useful (0 votes)
2 views

22BLC1104-Lab-05

The document outlines an experiment on priority and preemptive priority CPU scheduling as part of an Operating Systems Lab course. It includes code implementations for both non-preemptive and preemptive scheduling, detailing how to calculate turnaround time, waiting time, response time, and completion time for each process. The document also specifies how to handle processes with the same priority using round-robin scheduling and provides a structure for input and output of the scheduling results.

Uploaded by

dpsvn.gaur12217
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

22BLC1104-Lab-05

The document outlines an experiment on priority and preemptive priority CPU scheduling as part of an Operating Systems Lab course. It includes code implementations for both non-preemptive and preemptive scheduling, detailing how to calculate turnaround time, waiting time, response time, and completion time for each process. The document also specifies how to handle processes with the same priority using round-robin scheduling and provides a structure for input and output of the scheduling results.

Uploaded by

dpsvn.gaur12217
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Date: 18/Feb/2025

PRIORITY AND PREEMPTIVE PRIORITY CPU SCHEDULING


EXPERIMENT – 05

Name: Sahil Ramesh Mane


Reg. No.: 22BLC1104
Course Name: Operating Systems Lab
Slot: L9+L10
Faculty Name: Dr. Revathi M.
Course Code: BCSE303P

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

printf("Enter number of processes: ");

scanf("%d", &n);

Process proc[n];

if (choice == 1) {

for (i = 0; i < n; i++) {

proc[i].id = i + 1;

printf("Enter arrival time, burst time, priority for process %d: ", proc[i].id);

scanf("%d %d %d", &proc[i].arrival_time, &proc[i].burst_time, &proc[i].priority);

proc[i].start_time = -1;

proc[i].finished = 0;

} else {

for (i = 0; i < n; i++) {

proc[i].id = i + 1;

printf("Enter burst time, priority for process %d: ", proc[i].id);

scanf("%d %d", &proc[i].burst_time, &proc[i].priority);

proc[i].arrival_time = 0;

proc[i].start_time = -1;

proc[i].finished = 0;

int completed = 0, current_time = 0;

char schedule[10000] = "";

int sched_index = 0;

while (completed < n) {

int idx = -1, best_priority = 1000000;

for (i = 0; i < n; i++) {


if (proc[i].arrival_time <= current_time && proc[i].finished == 0) {

if (proc[i].priority < best_priority) {

best_priority = proc[i].priority;

idx = i;

} else if (proc[i].priority == best_priority) {

if (proc[i].arrival_time < proc[idx].arrival_time)

idx = i;

if (idx == -1) {

current_time++;

continue;

proc[idx].start_time = current_time;

proc[idx].response_time = proc[idx].start_time - proc[idx].arrival_time;

current_time += proc[idx].burst_time;

proc[idx].completion_time = current_time;

proc[idx].turnaround_time = proc[idx].completion_time - proc[idx].arrival_time;

proc[idx].waiting_time = proc[idx].turnaround_time - proc[idx].burst_time;

proc[idx].finished = 1;

completed++;

sched_index += sprintf(schedule + sched_index, "P%d ", proc[idx].id);

double total_waiting_time = 0, total_turnaround_time = 0;

for (i = 0; i < n; i++) {

total_waiting_time += proc[i].waiting_time;

total_turnaround_time += proc[i].turnaround_time;
}

double avg_waiting_time = total_waiting_time / n;

double avg_turnaround_time = total_turnaround_time / n;

printf("\nProcess\tAT\tBT\tPriority\tCT\tTAT\tWT\tRT\n");

for (i = 0; i < n; i++) {

printf("P%d\t%d\t%d\t%d\t\t%d\t%d\t%d\t%d\n", proc[i].id, proc[i].arrival_time,


proc[i].burst_time, proc[i].priority, proc[i].completion_time, proc[i].turnaround_time,
proc[i].waiting_time, proc[i].response_time);

printf("\nAverage Waiting Time: %.2f", avg_waiting_time);

printf("\nAverage Turnaround Time: %.2f\n", avg_turnaround_time);

printf("\nSequence of Scheduled Processes:\n%s\n", schedule);

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

printf("Enter number of processes: ");

scanf("%d", &n);

Process proc[n];

if (choice == 1) {

for (int i = 0; i < n; i++) {

proc[i].id = i + 1;

printf("Enter arrival time, burst time, priority for process %d: ", proc[i].id);

scanf("%d %d %d", &proc[i].arrival_time, &proc[i].burst_time, &proc[i].priority);

proc[i].remaining_time = proc[i].burst_time;

proc[i].start_time = -1;

} else {

for (int i = 0; i < n; i++) {

proc[i].id = i + 1;

printf("Enter burst time, priority for process %d: ", proc[i].id);

scanf("%d %d", &proc[i].burst_time, &proc[i].priority);

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_process = -1;

int current_quantum = 0;

int last_executed = -1;

char schedule[10000] = "";

int sched_index = 0;

while (completed < n) {

int highest_priority = 1000000;

for (int i = 0; i < n; i++) {

if (proc[i].arrival_time <= current_time && proc[i].remaining_time > 0) {

if (proc[i].priority < highest_priority) {

highest_priority = proc[i].priority;

int candidate = -1;

if (current_process != -1 && proc[current_process].remaining_time > 0 &&

proc[current_process].arrival_time <= current_time &&

proc[current_process].priority == highest_priority && current_quantum < time_slice) {

candidate = current_process;

} else {

int start_index = (last_executed == -1) ? 0 : (last_executed + 1) % n;

for (int i = 0; i < n; i++) {

int idx = (start_index + i) % n;

if (proc[idx].arrival_time <= current_time &&

proc[idx].remaining_time > 0 &&

proc[idx].priority == highest_priority) {

candidate = idx;
break;

current_quantum = 0;

if (candidate == -1) {

current_time++;

continue;

last_executed = candidate;

sched_index += sprintf(schedule + sched_index, "P%d ", proc[candidate].id);

if (proc[candidate].start_time == -1) {

proc[candidate].start_time = current_time;

proc[candidate].response_time = current_time - proc[candidate].arrival_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;

double total_waiting_time = 0, total_turnaround_time = 0;

for (int i = 0; i < n; i++) {

proc[i].turnaround_time = proc[i].completion_time - proc[i].arrival_time;

proc[i].waiting_time = proc[i].turnaround_time - proc[i].burst_time;

total_waiting_time += proc[i].waiting_time;

total_turnaround_time += proc[i].turnaround_time;

double avg_waiting_time = total_waiting_time / n;

double avg_turnaround_time = total_turnaround_time / n;

printf("\nProcess\tAT\tBT\tPriority\tCT\tTAT\tWT\tRT\n");

for (int i = 0; i < n; i++) {

printf("P%d\t%d\t%d\t%d\t\t%d\t%d\t%d\t%d\n", proc[i].id, proc[i].arrival_time,


proc[i].burst_time,

proc[i].priority, proc[i].completion_time, proc[i].turnaround_time,

proc[i].waiting_time, proc[i].response_time);

printf("\nAverage Waiting Time: %.2f", avg_waiting_time);

printf("\nAverage Turnaround Time: %.2f\n", avg_turnaround_time);

printf("\nSequence of Scheduled Processes:\n%s\n", schedule);

return 0;

}
OUTPUT:

Test Case-1:

Test Case-2:

You might also like