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

exno4

The document outlines C programs for implementing various CPU scheduling algorithms including First Come First Serve, Shortest Job First, Priority Scheduling, Round Robin, and Multilevel Queue Scheduling. Each section includes an algorithm description, a C program implementation, and the expected output format. The programs calculate waiting time, turnaround time, and average metrics for the processes scheduled.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

exno4

The document outlines C programs for implementing various CPU scheduling algorithms including First Come First Serve, Shortest Job First, Priority Scheduling, Round Robin, and Multilevel Queue Scheduling. Each section includes an algorithm description, a C program implementation, and the expected output format. The programs calculate waiting time, turnaround time, and average metrics for the processes scheduled.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Write C programs to implement the various CPU Scheduling Algorithms

Exp.no: 4 a

Date:

Aim:

To write C programs to implement the various CPU Scheduling Algorithms using First Come
First Serve algorithm.

Algorithm:

Step 1: Initialize the total waiting time and total turnaround time to 0.

Step 2: Read the number of processes to be scheduled.

Step 3: For each process, read in its arrival time and burst time.

Step 4: Sort the processes in increasing order of arrival time, so that the first process to arrive is
first in the queue.

Step 5: For each process, calculate its waiting time as the sum of the burst times of all
processes that arrived before it. For the first process, its waiting time is 0.

Step 6: For each process, calculate its turnaround time as the sum of its waiting time and its
burst time.

Step 7: Calculate the average waiting time and the average turnaround time by dividing the
total waiting time and total turnaround time by the number of processes.

Step 8: Print out the results.

Downloaded by Ms. Shanmugapriya - NIET


Program:

#include <stdio.h>

int main() {
int n, i;
float avg_wt, avg_tat;
int burst_time[20], waiting_time[20], turnaround_time[20];

printf("Enter the number of processes: ");


scanf("%d", &n);

printf("Enter the burst times for each process:\n");


for (i = 0; i < n; i++) {
scanf("%d", &burst_time[i]);
}

// Calculate waiting time for the first process


waiting_time[0] = 0;

// Calculate waiting time for each subsequent process


for (i = 1; i < n; i++) {
waiting_time[i] = waiting_time[i - 1] + burst_time[i - 1];
}

// Calculate turnaround time for each process


for (i = 0; i < n; i++) {
turnaround_time[i] = waiting_time[i] + burst_time[i];
}

// Calculate average waiting time and average turnaround time


float total_wt = 0, total_tat = 0;
for (i = 0; i < n; i++) {
total_wt += waiting_time[i];
total_tat += turnaround_time[i];
}
avg_wt = total_wt / n;
avg_tat = total_tat / n;

Downloaded by Ms. Shanmugapriya - NIET


// Print the results
printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\n");

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


printf("%d\t%d\t\t%d\t\t%d\n", i+1, burst_time[i], waiting_time[i], turnaround_time[i]);
}
printf("Average Waiting Time: %.2f\n", avg_wt);
printf("Average Turnaround Time: %.2f\n", avg_tat);

return 0;
}

Output:

Result:

Downloaded by Ms. Shanmugapriya - NIET


Write C programs to implement the various CPU Scheduling Algorithms

Exp.no: 4 b

Date:

Aim:

To write C programs to implement the various CPU Scheduling Algorithms using Shortest Job
First algorithm.

Algorithm:

Step 1: When a process arrives, its expected processing time is estimated, based on its past
behaviour or other factors.

Step 2: The process with the shortest expected processing time is assigned the CPU. If there are
multiple processes with the same expected processing time, any one of them may be selected.

Step 3: The process runs on the CPU until it completes, or until it is pre-empted by a higher-
priority process.

Step 4: When a new process arrives, its expected processing time is estimated, and it is added
to the ready queue.

Step 5: When the current process completes or is pre-empted, the process with the shortest
expected processing time in the ready queue is selected and assigned the CPU.

Program:

#include <stdio.h>

int main() {
int n, i, j, temp, time = 0, total_time = 0;
float avg_waiting_time, avg_turnaround_time;
int burst_time[20], process[20], waiting_time[20], turnaround_time[20];

printf("Enter the number of processes: ");


scanf("%d", &n);

Downloaded by Ms. Shanmugapriya - NIET


for (i = 0; i < n; i++) {
printf("Enter the burst time for process %d: ", i + 1);
scanf("%d", &burst_time[i]);
process[i] = i + 1;
}

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


for (j = i + 1; j < n; j++) {
if (burst_time[i] > burst_time[j]) {
temp = burst_time[i];
burst_time[i] = burst_time[j];
burst_time[j] = temp;
temp = process[i];
process[i] = process[j];
process[j] = temp;
}
}
}

waiting_time[0] = 0;

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


waiting_time[i] = 0;
for (j = 0; j < i; j++) {
waiting_time[i] += burst_time[j];
}
total_time += waiting_time[i];
}

avg_waiting_time = (float)total_time / n;
total_time = 0;

printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");

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


turnaround_time[i] = burst_time[i] + waiting_time[i];
total_time += turnaround_time[i];
printf("\n%d\t\t%d\t\t%d\t\t%d", process[i], burst_time[i], waiting_time[i],
turnaround_time[i]);

Downloaded by Ms. Shanmugapriya - NIET


avg_turnaround_time = (float)total_time / n;
printf("\n\nAverage Waiting Time = %0.2f", avg_waiting_time);
printf("\nAverage Turnaround Time = %0.2f", avg_turnaround_time);

return 0;
}

Output:

Result:

Downloaded by Ms. Shanmugapriya - NIET


Write C programs to implement the various CPU Scheduling Algorithms

Exp.no: 4 c

Date:

Aim:

To write C programs to implement the various CPU Scheduling Algorithms using Priority
Scheduling algorithm.

Algorithm:

Step 1: Assign a priority to each process. This priority can be based on various criteria, such as
the amount of CPU time needed, the amount of memory required, or the importance of the
process.

Step 2: Initialize the ready queue with all the processes that are ready to run.

Step 3: Find the process with the highest priority in the ready queue.

Step 4: Assign the CPU to that process.

Step 5: If the scheduling is non-pre-emptive, the process will continue to run until it completes
or voluntarily gives up the CPU. If the scheduling is pre-emptive, the process will be
interrupted if a new process with a higher priority arrives in the ready queue.

Step 6: When a process completes or gives up the CPU, remove it from the system.

Step 7: If a new process arrives in the system, add it to the ready queue according to its priority.

Step 8: Repeat steps 3 to 7 until all processes have completed.

Downloaded by Ms. Shanmugapriya - NIET


Program:

#include<stdio.h>

int main() {
int n, i, j, temp, total_wt = 0, total_tat = 0;
float avg_wt, avg_tat;
int burst_time[20], priority[20], process[20];

// Taking input from the user


printf("Enter the number of processes: ");
scanf("%d", &n);

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


printf("Enter burst time and priority for process %d: ", i + 1);
scanf("%d %d", &burst_time[i], &priority[i]);
process[i] = i + 1;
}

// Sorting processes based on priority


for(i = 0; i < n - 1; i++) {
for(j = i + 1; j < n; j++)
{ if(priority[i] > priority[j]) {
temp = priority[i];
priority[i] = priority[j];
priority[j] = temp;

temp = burst_time[i];
burst_time[i] = burst_time[j];
burst_time[j] = temp;

temp = process[i];
process[i] = process[j];
process[j] = temp;
}
}
}

Downloaded by Ms. Shanmugapriya - NIET


// Calculating waiting time and turnaround time
int wt[20], tat[20];
wt[0] = 0;
for(i = 1; i < n; i++)
{ wt[i] = 0;
for(j = 0; j < i; j++)
{ wt[i] +=
burst_time[j];
}
total_wt += wt[i];
}

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


tat[i] = burst_time[i] + wt[i];
total_tat += tat[i];
}

// Displaying results
avg_wt = (float)total_wt / n;
avg_tat = (float)total_tat / n;

printf("\nProcess\tBurst Time\tPriority\tWaiting Time\tTurnaround Time");


for(i = 0; i < n; i++) {
printf("\n%d\t%d\t\t%d\t\t%d\t\t%d", process[i], burst_time[i], priority[i], wt[i], tat[i]);
}

printf("\n\nAverage Waiting Time = %f", avg_wt); printf("\


nAverage Turnaround Time = %f", avg_tat);

return 0;
}

Downloaded by Ms. Shanmugapriya - NIET


Output:

Result:

Downloaded by Ms. Shanmugapriya - NIET


Write C programs to implement the various CPU Scheduling Algorithms

Exp.no: 4 d

Date:

Aim:

To write C programs to implement the various CPU Scheduling Algorithms using Round Robin
algorithm.

Algorithm:

Step 1: Initialize a queue Q to hold the processes that are ready to run.

Step 2: Set the time slice, or quantum, for each process.

Step 3: While there are processes in the queue:

Step 4: Dequeue the first process from the queue.

Step 5: If the process has not completed, and its remaining execution time is less than or equal
to the time slice:

Step 6: Run the process for its remaining execution time.

Step 7: Update the process's state to "completed".

Step 8: Record the process's turnaround time and wait time.

Step 9: Otherwise, if the process has not completed:

Step 10: Run the process for the time slice.

Step 11: Update the process's remaining execution time.

Step 12: Enqueue the process back onto the end of the queue.

Step 13: Calculate the average turnaround time and average wait time for all completed
processes.

Downloaded by Ms. Shanmugapriya - NIET


Program:

#include<stdio.h>

int main() {
int i, n, t, j, count=0, quantum;
int waiting_time = 0, turnaround_time = 0;
int arrival_time[10], burst_time[10], temp_burst_time[10];
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the time quantum: ");
scanf("%d", &quantum);
for (i = 0; i < n; i++) {
printf("Enter the arrival time and burst time of process %d: ", i+1);
scanf("%d %d", &arrival_time[i], &burst_time[i]);
temp_burst_time[i] = burst_time[i];
}
t = 0;
while (1) {
int flag = 1;
for (i = 0; i < n; i++) {
if (temp_burst_time[i] > 0)
{ flag = 0;
if (temp_burst_time[i] > quantum)
{ t += quantum;
temp_burst_time[i] -= quantum;
}
else {
t = t + temp_burst_time[i];
waiting_time += t - arrival_time[i] - burst_time[i];
temp_burst_time[i] = 0;
turnaround_time += t - arrival_time[i];
}
}
}
if (flag == 1)
break;
}

Downloaded by Ms. Shanmugapriya - NIET


printf("\nProcess\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time");
for (i = 0; i < n; i++) {
printf("\nP%d\t\t%d\t\t%d\t\t%d\t\t%d", i+1, arrival_time[i], burst_time[i], waiting_time/n,
turnaround_time/n);
}
return 0;
}

Output:

Result:

Downloaded by Ms. Shanmugapriya - NIET


Write C programs to implement the various CPU Scheduling Algorithms

Exp.no: 4 e

Date:

Aim:

To write C programs to implement the various CPU Scheduling Algorithms using Multilevel
Queue Scheduling algorithm.

Algorithm:

Step 1: Divide the ready queue into multiple queues based on some criteria, such as process
priority or type of task.

Step 2: Assign a different scheduling algorithm to each queue, such as FCFS or SJF.

Step 3: When a new process arrives, it is assigned to the appropriate queue based on its
characteristics.

Step 4: The process in the highest priority queue is executed first, and if there are multiple
processes in the same queue, the scheduling algorithm for that queue is used to determine which
process is executed next.

Step 5: If a process in a lower-priority queue becomes ready to run, it will only be executed if
there are no processes in the higher-priority queues.

Step 6: When a process completes or is blocked, it is removed from the queue and the next
process in the queue is executed.

Step 7: The process in the highest-priority queue may pre-empty a process in a lower-priority
queue if it becomes ready to run.

Step 8: The processes in each queue are scheduled independently of each other.

Step 9: The system can be configured with a different number of queues and different
scheduling algorithms for each queue, depending on the requirements of the system.

Downloaded by Ms. Shanmugapriya - NIET


Program:

#include <stdio.h>

#define MAX_QUEUE_SIZE 100

// Process structure
typedef struct {
int pid;
int burst_time;
int priority;
} Process;

// Queue structure
typedef struct {
int front;
int rear;
Process arr[MAX_QUEUE_SIZE];
} Queue;

// Initialize queue
void init_queue(Queue *q) {
q->front = -1;
q->rear = -1;
}

// Check if queue is empty


int is_empty(Queue *q)
{ return q->front == -1;
}

// Check if queue is full


int is_full(Queue *q) {
return q->rear == MAX_QUEUE_SIZE - 1;
}

// Add process to queue


void enqueue(Queue *q, Process p)
{ if (is_full(q)) {
printf("Queue is full\n");
return;

Downloaded by Ms. Shanmugapriya - NIET


}
if (is_empty(q)) {
q->front = q->rear = 0;
} else {
q->rear++;
}
q->arr[q->rear] = p;
}

// Remove process from queue


Process dequeue(Queue *q) {
if (is_empty(q))
{ printf("Queue is empty\
n"); Process p = { -1, -1, -
1 }; return p;
}
Process p = q->arr[q->front];
if (q->front == q->rear) {
q->front = q->rear = -1;
} else {
q->front++;
}
return p;
}

// Main function
int main() {
// Initialize queues
Queue q1, q2, q3;
init_queue(&q1);
init_queue(&q2);
init_queue(&q3);

// Create processes
Process p1 = { 1, 6, 1 };
Process p2 = { 2, 4, 2 };
Process p3 = { 3, 8, 3 };
Process p4 = { 4, 3, 1 };
Process p5 = { 5, 5, 2 };
Process p6 = { 6, 2, 3 };

// Add processes to queues based on priority

Downloaded by Ms. Shanmugapriya - NIET


enqueue(&q1, p1);
enqueue(&q2, p2);
enqueue(&q3, p3);
enqueue(&q1, p4);
enqueue(&q2, p5);
enqueue(&q3, p6);

// Run processes in each queue


printf("Queue 1:\n");
while (!is_empty(&q1))
{ Process p =
dequeue(&q1);
printf("Running process %d with burst time %d\n", p.pid, p.burst_time);
}

printf("Queue 2:\n");
while (!is_empty(&q2)) {
Process p = dequeue(&q2);
printf("Running process %d with burst time %d\n", p.pid, p.burst_time);
}

printf("Queue 3:\n");
while (!is_empty(&q3)) {
Process p = dequeue(&q3);
printf("Running process %d with burst time %d\n", p.pid, p.burst_time);
}

return 0;
}

Downloaded by Ms. Shanmugapriya - NIET


Output:

Result:

Downloaded by Ms. Shanmugapriya - NIET


Downloaded by Ms. Shanmugapriya - NIET

You might also like