You are on page 1of 5

LAB - 6

Name of the Scheduling Algorithm :- ROUND-ROBIN PREEMPTIVE SCHEDULING


ALGORITHM
Description :RoundRobin scheduling algorithm is a preemptive scheduling algorithm.In
which each process is assigned a fixed time quantum or time slice.The CPU scheduler then
allocates CPU to each process and allowing each process to execute for its time quantum.If
process execution time is longer than the time quantum.If a process’s execution time is longer than the time
quantum ,it is moved to the back of the ready queue,and the next process in line is
given a chance to execute.
Program :
#include <stdio.h>
#include <stdbool.h>
struct Process {
int id;
int arrival_time;
int burst_time;
int remaining_time;
int waiting_time;
int turnaround_time;
};
void printGanttChart(struct Process processes[], int n, int timeline[]) {
printf("Gantt Chart:\n");
int i;
for (i = 0; i < n; i++) {
printf("| P%d", timeline[i]);
}
printf("|\n");
}
void findAverageTime(struct Process processes[], int n, int quantum) {
int waiting_time[n], turnaround_time[n];
int timeline[n * n]; // Store the order of processes execution
int i;
for (i = 0; i < n; i++) {waiting_time[i] = 0;
turnaround_time[i] = 0;
}
int total_burst_time = 0;
for ( i = 0; i < n; i++) {
total_burst_time += processes[i].burst_time;
}
int t = 0;
int timeline_index = 0;
while (total_burst_time > 0) {
int i;
for (i = 0; i < n; i++) {
if (processes[i].arrival_time <= t && processes[i].remaining_time > 0) {
int execute_time = (processes[i].remaining_time < quantum) ?
processes[i].remaining_time : quantum;
processes[i].remaining_time -= execute_time;
total_burst_time -= execute_time;
t += execute_time;
timeline[timeline_index++] = i + 1;
if (processes[i].remaining_time == 0) {
turnaround_time[i] = t - processes[i].arrival_time;
waiting_time[i] = turnaround_time[i] - processes[i].burst_time;
}
}
}
}float avg_waiting_time = 0;
float avg_turnaround_time = 0;
for ( i = 0; i < n; i++) {
avg_waiting_time += waiting_time[i];
avg_turnaround_time += turnaround_time[i];
}
avg_waiting_time /= n;
avg_turnaround_time /= n;
printGanttChart(processes, n, timeline);
printf("Average Waiting Time: %.2f\n", avg_waiting_time);
printf("Average Turnaround Time: %.2f\n", avg_turnaround_time);
}
int main() {
int n, quantum;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the time quantum: ");
scanf("%d", &quantum);
struct Process processes[n];
int i;
for ( i = 0; i < n; i++) {
processes[i].id = i + 1;
printf("Enter arrival time for process P%d: ", i + 1);scanf("%d", &processes[i].arrival_time);
printf("Enter burst time for process P%d: ", i + 1);
scanf("%d", &processes[i].burst_time);
processes[i].remaining_time = processes[i].burst_time;
}
findAverageTime(processes, n, quantum);
return 0;
}

Output:
AIM : Write a c program to implement Producer-Consumer Problem.
Name: Producer-Consumer problem
Description:The producer-consumer problem is a classic synchronization and
concurrency problem in computer science and operating systems. It illustrates a scenario
where two types of processes, known as producers and consumers, share a common,
finite-size buffer or data structure.
Producer: These are processes or threads responsible for generating data or items and
placing them into a shared buffer. Producers work independently and may produce items at
varying rates.
Consumer: Consumers are processes or threads that retrieve and process the items from the
shared buffer. Like producers, consumers work independently and may consume items at
different rates.
Shared Buffer: The shared buffer is a finite-size data structure where producers deposit
items and consumers retrieve items. It acts as a bridge between producers and consumers,
allowing them to communicate and synchronize their activities.
Program:
#include <stdio.h>
#include <stdlib.h>
int mutex = 1;
int full = 0;
int empty = 10, x = 0;
void producer()
{
--mutex;
++full;
--empty;
x++;
printf("\nProducer produces" "item %d",x);
++mutex;}
void consumer()
{
--mutex;
--full;
++empty;
printf("\nConsumer consumes ""item %d",
x);
x--;
++mutex;
}
int main()
{
int n, i;
printf("\n1. Press 1 for Producer"
"\n2. Press 2 for Consumer"
"\n3. Press 3 for Exit");
#pragma omp critical
for (i = 1; i > 0; i++) {
printf("\nEnter your choice:");
scanf("%d", &n);
switch (n) {
case 1:
if ((mutex == 1)
&& (empty != 0)) {
producer();
}
else {
printf("Buffer is full!");
}break;
case 2:
if ((mutex == 1)
&& (full != 0)) {
consumer();
}
else {
printf("Buffer is empty!");
}
break;
case 3:
exit(0);
break;
}
}
}
Output:

You might also like