Exercise 2.1
Exercise 2.1
1
Priority Scheduling
Round Robin Scheduling
Structures- Create a structure variable and store
values
#include <stdio.h>
#include <string.h>
// create struct
struct Person {
char name[50];
int citNo;
float salary;
} person1;
Structures
int main() {
// assign value to name of person1
strcpy(person1.name, "Hari Priya");
// assign values to other person1 variables
person1.citNo = 1984;
person1. salary = 2500;
printf("Name: %s\n", person1.name);
printf("Citizenship No.: %d\n", person1.citNo);
printf("Salary: %.2f", person1.salary);
return 0;
}
Array of structures- The program creates a structure to store information about students, such as
their name, age, and grade. It then initializes an array of these structures, and prints out the
details for each student
// Define a structure for storing student information
struct Student {
char name[50];
int age;
float grade;
};
printf("\nPID\tPriority\tArrival\tBurst\tCompletion\tTurnaround\tWaiting\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t%d\t%d\t\t%d\t\t%d\n", proc[i].pid, proc[i].priority,
proc[i].arrival, proc[i].burst, proc[i].completion, proc[i].turnaround,
proc[i].waiting);
}
}
Priority Scheduling
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process proc[n];
for (int i = 0; i < n; i++) {
printf("Enter Process ID, Arrival Time, Burst Time, Priority for Process %d: ", i + 1);
scanf("%d %d %d %d", &proc[i].pid, &proc[i].arrival, &proc[i].burst, &proc[i].priority);
}
sortByPriority(proc, n);
calculateTimes(proc, n);
printGanttChart(proc, n);
return 0;}
• Implement Round Robin (RR) scheduling with a given time
quantum.
RR
struct Process {
int id;
int arrival_time;
int burst_time;
int remaining_time;
int completion_time;
int turnaround_time;
int waiting_time;
};
RR
void roundRobin(struct Process processes[], int n, int time_quantum) {
int time = 0, done = 0;
int i;
while (done < n) {
done = 0;
for (i = 0; i < n; i++) {
if (processes[i].remaining_time > 0) {
if (processes[i].remaining_time > time_quantum) {
time += time_quantum;
processes[i].remaining_time -= time_quantum;
} else {
time += processes[i].remaining_time;
processes[i].completion_time = time;
processes[i].turnaround_time = processes[i].completion_time - processes[i].arrival_time;
processes[i].waiting_time = processes[i].turnaround_time - processes[i].burst_time;
processes[i].remaining_time = 0;}
} else {
done++;
} }}
RR
printf("\nProcess\tAT\tBT\tCT\tTAT\tWT\n");
for (i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\t%d\t%d\n", processes[i].id,
processes[i].arrival_time, processes[i].burst_time,
processes[i].completion_time,
processes[i].turnaround_time, processes[i].waiting_time); }}
RR
int main() {
int n, time_quantum, i;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process processes[n];
printf("Enter time quantum: ");
scanf("%d", &time_quantum);
printf("Enter arrival time and burst time for each process:\n");
for (i = 0; i < n; i++) {
processes[i].id = i + 1;
printf("Process %d Arrival Time: ", i + 1);
scanf("%d", &processes[i].arrival_time);
printf("Process %d Burst Time: ", i + 1);
scanf("%d", &processes[i].burst_time);
processes[i].remaining_time = processes[i].burst_time; }
roundRobin(processes, n, time_quantum);