exno4
exno4
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 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.
#include <stdio.h>
int main() {
int n, i;
float avg_wt, avg_tat;
int burst_time[20], waiting_time[20], turnaround_time[20];
return 0;
}
Output:
Result:
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];
waiting_time[0] = 0;
avg_waiting_time = (float)total_time / n;
total_time = 0;
return 0;
}
Output:
Result:
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 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.
#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];
temp = burst_time[i];
burst_time[i] = burst_time[j];
burst_time[j] = temp;
temp = process[i];
process[i] = process[j];
process[j] = temp;
}
}
}
// Displaying results
avg_wt = (float)total_wt / n;
avg_tat = (float)total_tat / n;
return 0;
}
Result:
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 5: If the process has not completed, and its remaining execution time is less than or equal
to the time slice:
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.
#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;
}
Output:
Result:
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.
#include <stdio.h>
// 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;
}
// 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 };
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;
}
Result: