Microsoft Word - RK21PKB55 - Assignment - Simulation Based - CSE316 - Os Assignment Ca355
Microsoft Word - RK21PKB55 - Assignment - Simulation Based - CSE316 - Os Assignment Ca355
Microsoft Word - RK21PKB55 - Assignment - Simulation Based - CSE316 - Os Assignment Ca355
Q1
#include <stdio.h>
#include <stdlib.h>
struct Process {
int pid;
int burst_time;
int priority;
int waiting_time;
int turnaround_time;
};
int main() {
int num_processes, i, j;
float avg_waiting_time = 0, avg_turnaround_time = 0;
struct Process *processes, temp;
}
// Sort the processes based on priority
for (i = 0; i < num_processes - 1; i++) {
for (j = i+1; j < num_processes; j++) {
if (processes[i].priority < processes[j].priority) {
temp = processes[i];
processes[i] = processes[j];
processes[j] = temp;
}
}
}
processes[0].waiting_time = 0;
processes[0].turnaround_time = processes[0].burst_time;
avg_waiting_time /= num_processes;
avg_turnaround_time /= num_processes;
printf("\nAverage Waiting Time: %.2f\n", avg_waiting_time);
printf("Average Turnaround Time: %.2f\n", avg_turnaround_time);
free(processes);
return 0;
}
This program first prompts the user to enter the number of processes, and then takes input
for the burst time and priority of each process. It then sorts the processes in descending
order of priority using a simple selection sort algorithm.
Q2-5
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_PROCESSES 10
#define MAX_PRIORITY 10
struct process {
int pid;
int arrival_time;
int burst_time;
int priority;
int remaining_time;
int waiting_time;
int completion_time;
};
void generate_processes(struct process *processes, int n) {
int i;
srand(time(NULL));
processes[i].pid = i;
processes[i].remaining_time = processes[i].burst_time;
int i;
printf("PID\tArrival\tBurst\tPriority\n");
processes[i].burst_time, processes[i].priority);
int time = 0;
int i, j, highest_priority_index;
int done = 0;
// Find highest priority process that has arrived and not completed
highest_priority_index = -1;
if (highest_priority_index == -1 ||
highest_priority_index = i;
if (highest_priority_index == -1) {
time++;
} else {
current_process = &processes[highest_priority_index];
current_process->remaining_time--;
if (current_process->remaining_time == 0) {
done++;
current_process->completion_time = time + 1;
current_process = NULL;
j != highest_priority_index) {
processes[j].waiting_time++;
}
time++;
}
int main() {
int n = MAX_PROCESSES;
generate_processes(processes, n);
print_processes(processes, n);
priority_preemptive(processes, n);
int i;
avg_waiting_time += processes[i].waiting_time;
avg_waiting_time /= n;
avg_completion_time /= n;
return 0;
IMPLEMENTATION OF CODE:
Q6
Priority scheduling algorithm is a commonly used algorithm for scheduling tasks in a
computer system. It is used to assign priority to different tasks and schedule them
accordingly. Priority scheduling algorithm can be implemented in two ways, preemption and
non-preemption. In preemption, a higher priority task can interrupt a lower priority task in
execution, while in non-preemption, a lower priority task must complete its execution
before a higher priority task can start. In this report, we present the results of a simulation
program that implements the priority scheduling algorithm with preemption and compare it
with the non-preemptive priority algorithm.
However, one disadvantage of the priority scheduling algorithm with preemption is that it
can lead to starvation of lower priority tasks. If a high priority task keeps preempting a lower
priority task, the lower priority task may never get a chance to execute. Additionally, the
preemption feature of the algorithm can add overhead to the system, as the operating
system must constantly check for higher priority tasks and interrupt the currently running
task.
In this project, we have implemented the Priority with Preemption algorithm for process
scheduling in a computer system. The simulation program was implemented using the C
programming language, where a set of processes were generated with random arrival times
and CPU burst times. The Priority with Preemption algorithm was then applied to schedule
these processes based on their priority, ensuring that system-critical tasks were executed
first and user tasks were executed later.
The simulation program was run for a set amount of time, and the average waiting time and
completion time for each process were recorded. The results showed that the Priority with
Preemption algorithm provided better performance compared to the Priority (Non-
Preemptive) algorithm, as it allowed higher priority tasks to be executed promptly and
provided equal access to system resources to all user tasks. The average waiting time and
completion time were lower for the Priority with Preemption algorithm, indicating that it
was able to schedule processes more efficiently.
However, the Priority with Preemption algorithm has some disadvantages, such as the
possibility of lower priority tasks being starved due to the continuous execution of higher
priority tasks. This could lead to reduced overall system performance, as lower priority tasks
may not get the resources they need. Another disadvantage is the overhead associated with
context switching, as the CPU has to save the state of the currently running process and
restore the state of the next process.
In conclusion, the Priority with Preemption algorithm is a more efficient and flexible
approach for process scheduling compared to the Priority (Non-Preemptive) algorithm.
However, it has some limitations and trade-offs that should be considered when
implementing it in real-world systems. Further improvements could be made by
incorporating dynamic priority assignment and scheduling, taking into account various
factors such as task resource requirements, task history, and system load. This would ensure
that the system is able to adapt to changing conditions and provide optimal performance.