0% found this document useful (0 votes)
37 views16 pages

Exercise 2.1

The document provides code examples for implementing priority scheduling and round-robin scheduling in C programming. It includes the definition of structures for processes and students, functions to calculate scheduling metrics, and methods to print process details. The main functions demonstrate how to gather input for processes and execute the scheduling algorithms.
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)
37 views16 pages

Exercise 2.1

The document provides code examples for implementing priority scheduling and round-robin scheduling in C programming. It includes the definition of structures for processes and students, functions to calculate scheduling metrics, and methods to print process details. The main functions demonstrate how to gather input for processes and execute the scheduling algorithms.
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/ 16

Exercise 2.

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

// Function to print the details of a student


void printStudent(struct Student s) {
printf("Name: %s\n", s.name);
printf("Age: %d\n", s.age);
printf("Grade: %.2f\n", s.grade);
printf("------------------------\n");}
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
int main() {
// Create an array of structures to hold information about 3 students
struct Student students[3];
// Initialize the details of the first student
strcpy(students[0].name, "Alice");
students[0].age = 20;
students[0].grade = 85.5;

// Initialize the details of the second student


strcpy(students[1].name, "Bob");
students[1].age = 22;
students[1].grade = 90.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

// Initialize the details of the third student


strcpy(students[2].name, "Charlie");
students[2].age = 21;
students[2].grade = 78.0;
// Print the details of each student
for (int i = 0; i < 3; i++) {
printStudent(students[i]);
}
return 0;
}
Priority Scheduling
struct Process {
int pid; // Process ID
int arrival; // Arrival Time
int burst; // Burst Time
int priority; // Priority
int completion; // Completion Time
int turnaround; // Turnaround Time
int waiting; // Waiting Time
};

// Function to swap two processes


void swap(struct Process *a, struct Process *b) {
struct Process temp = *a;
*a = *b;
*b = temp;}
Priority Scheduling

// Function to sort processes by priority (and arrival time if priorities


are the same)
void sortByPriority(struct Process proc[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (proc[j].priority < proc[i].priority ||
(proc[j].priority == proc[i].priority && proc[j].arrival <
proc[i].arrival)) {
swap(&proc[i], &proc[j]);
} } }}
Priority Scheduling

// Function to calculate completion, turnaround, and waiting times


void calculateTimes(struct Process proc[], int n) {
int currentTime = 0;
for (int i = 0; i < n; i++) {
if (currentTime < proc[i].arrival) {
currentTime = proc[i].arrival;
}
proc[i].completion = currentTime + proc[i].burst;
proc[i].turnaround = proc[i].completion - proc[i].arrival;
proc[i].waiting = proc[i].turnaround - proc[i].burst;
currentTime = proc[i].completion;}}
Priority Scheduling
// Function to print Gantt Chart, process details, and summary
void printGanttChart(struct Process proc[], int n) {
printf("Gantt Chart:\n");
for (int i = 0; i < n; i++) {
printf("| P%d ", proc[i].pid);
}
printf("|\n");

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

You might also like