SlideShare a Scribd company logo
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Structure to represent a process
struct Process {
int pid; // Process ID
int arrival_time; // Arrival time
int burst_time; // Burst time
int remaining_time; // Remaining burst time (for preemptive algorithms)
int comes_back_after; // Time to return to the ready queue
int priority; // Priority
bool is_complete; // Completion flag
int waiting_time; // Waiting time
int turnaround_time; // Turnaround time
};
// Function to calculate waiting and turnaround times
void calculate_times(struct Process *processes, int n) {
int total_waiting_time = 0;
int total_turnaround_time = 0;
for (int i = 0; i < n; i++) {
processes[i].turnaround_time = processes[i].burst_time + processes[i].waiting_time;
processes[i].waiting_time = processes[i].turnaround_time - processes[i].burst_time;
total_waiting_time += processes[i].waiting_time;
total_turnaround_time += processes[i].turnaround_time;
}
printf("Average Waiting Time: %.2fn", (float)total_waiting_time / n);
printf("Average Turnaround Time: %.2fn", (float)total_turnaround_time / n);
}
// Sort processes by arrival time (for FCFS)
void sort_by_arrival(struct Process *processes, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (processes[j].arrival_time < processes[i].arrival_time) {
struct Process temp = processes[i];
processes[i] = processes[j];
processes[j] = temp;
}
}
}
}
// First Come First Served (FCFS)
void fcfs(struct Process *processes, int n, int time_limit) {
printf("First Come First Served (FCFS):n");
int current_time = 0;
printf("Gantt Chart:n");
printf("|");
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time > current_time) {
printf(" Idle |");
current_time = processes[i].arrival_time;
}
printf(" P%d (%d-%d) |", processes[i].pid, current_time, current_time +
processes[i].burst_time);
processes[i].waiting_time = current_time - processes[i].arrival_time;
current_time += processes[i].burst_time;
}
printf("n");
calculate_times(processes, n);
}
// Shortest Job First (SJF)
void shortest_job_first(struct Process *processes, int n, int time_limit) {
printf("Shortest Job First (SJF):n");
int current_time = 0;
int completed = 0;
printf("Gantt Chart:n");
while (completed < n) {
int shortest_index = -1;
int shortest_burst = 9999;
// Find the process with the shortest burst time that is ready
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time <= current_time && !processes[i].is_complete) {
if (processes[i].burst_time < shortest_burst) {
shortest_burst = processes[i].burst_time;
shortest_index = i;
}
}
}
if (shortest_index == -1) {
printf("| Idle |");
current_time++;
continue;
}
printf(" P%d (%d-%d) |", processes[shortest_index].pid, current_time, current_time +
processes[shortest_index].burst_time);
processes[shortest_index].waiting_time = current_time -
processes[shortest_index].arrival_time;
current_time += processes[shortest_index].burst_time;
processes[shortest_index].is_complete = true;
completed++;
}
printf("n");
calculate_times(processes, n);
}
// Shortest Remaining Time First (SRTF)
void shortest_remaining_time_first(struct Process *processes, int n, int time_limit) {
printf("Shortest Remaining Time First (SRTF):n");
int current_time = 0;
int completed = 0;
printf("Gantt Chart:n");
while (completed < n && current_time < time_limit) {
int shortest_index = -1;
int shortest_remaining = 9999;
// Find the process with the shortest remaining time
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time <= current_time && !processes[i].is_complete) {
if (processes[i].remaining_time < shortest_remaining) {
shortest_remaining = processes[i].remaining_time;
shortest_index = i;
}
}
}
if (shortest_index == -1) {
printf("| Idle |");
current_time++;
continue;
}
printf(" P%d (%d-%d) |", processes[shortest_index].pid, current_time, current_time + 1);
processes[shortest_index].remaining_time--;
current_time++;
if (processes[shortest_index].remaining_time == 0) {
processes[shortest_index].is_complete = true;
completed++;
}
}
printf("n");
calculate_times(processes, n);
}
// Round Robin with a given quantum
void round_robin(struct Process *processes, int n, int quantum, int time_limit) {
printf("Round Robin (RR) with Quantum %d:n", quantum);
int current_time = 0;
int completed = 0;
printf("Gantt Chart:n");
while (completed < n && current_time < time_limit) {
for (int i = 0; i < n; i++) {
if (!processes[i].is_complete && processes[i].arrival_time <= current_time) {
if (processes[i].remaining_time > quantum) {
printf(" P%d (%d-%d) |", processes[i].pid, current_time, current_time + quantum);
processes[i].remaining_time -= quantum;
current_time += quantum;
} else {
printf(" P%d (%d-%d) |", processes[i].pid, current_time, current_time +
processes[i].remaining_time);
current_time += processes[i].remaining_time;
processes[i].is_complete = true;
completed++;
}
}
}
}
printf("n");
calculate_times(processes, n);
}
// Preemptive Priority Scheduling
void preemptive_priority_scheduling(struct Process *processes, int n, int time_limit) {
printf("Preemptive Priority Scheduling:n");
int current_time = 0;
int completed = 0;
printf("Gantt Chart:n");
while (completed < n && current_time < time_limit) {
int highest_priority_index = -1;
int highest_priority = -1;
for (int i = 0; i < n; i++) {
if (!processes[i].is_complete && processes[i].arrival_time <= current_time) {
if (processes[i].priority > highest_priority) {
highest_priority = processes[i].priority;
highest_priority_index = i;
}
}
}
if (highest_priority_index == -1) {
printf("| Idle |");
current_time++;
continue;
}
printf(" P%d (%d-%d) |", processes[highest_priority_index].pid, current_time, current_time
+ 1);
processes[highest_priority_index].remaining_time--;
current_time++;
if (processes[highest_priority_index].remaining_time == 0) {
processes[highest_priority_index].is_complete = true;
completed++;
}
}
printf("n");
calculate_times(processes, n);
}
// Non-Preemptive Priority Scheduling
void non_preemptive_priority_scheduling(struct Process *processes, int n, int time_limit) {
printf("Non-Preemptive Priority Scheduling:n");
int current_time = 0;
int completed = 0;
printf("Gantt Chart:n");
while (completed < n && current_time < time_limit) {
int highest_priority_index = -1;
int highest_priority = -1;
for (int i = 0; i < n; i++) {
if (!processes[i].is_complete && processes[i].arrival_time <= current_time) {
if (processes[i].priority > highest_priority) {
highest_priority = processes[i].priority;
highest_priority_index = i;
}
}
}
if (highest_priority_index == -1) {
printf("| Idle |");
current_time++;
continue;
}
printf(" P%d (%d-%d) |", processes[highest_priority_index].pid, current_time, current_time
+ processes[highest_priority_index].burst_time);
current_time += processes[highest_priority_index].burst_time;
processes[highest_priority_index].is_complete = true;
completed++;
}
printf("n");
calculate_times(processes, n);
}
// Display the scheduling menu
void display_menu() {
printf("Select a Scheduling Algorithm:n");
printf("1. First Come First Served (FCFS)n");
printf("2. Shortest Job First (SJF)n");
printf("3. Shortest Remaining Time First (SRTF)n");
printf("4. Round Robin (Quantum 5)n");
printf("5. Preemptive Priority Scheduling with Agingn");
printf("6. Non-Preemptive Priority Schedulingn");
}
int main() {
struct Process processes[] = {
{1, 0, 10, 10, 2, 3, 0, 0, 0},
{2, 1, 8, 8, 4, 2, 0, 0, 0},
{3, 3, 14, 14, 6, 3, 0, 0, 0},
{4, 4, 7, 7, 8, 1, 0, 0, 0},
{5, 6, 5, 5, 3, 0, 0, 0},
{6, 7, 4, 4, 6, 1, 0, 0, 0},
{7, 8, 6, 6, 9, 2, 0, 0, 0}
};
int n = sizeof(processes) / sizeof(processes[0]);
// Loop to allow multiple runs
while (true) {
display_menu(); // Display the menu to choose the algorithm
int choice;
printf("Enter your choice (1-6, or 0 to exit): ");
scanf("%d", &choice);
if (choice == 0) {
break; // Exit the loop to end the program
}
// Reset process states for each run
for (int i = 0; i < n; i++) {
processes[i].remaining_time = processes[i].burst_time;
processes[i].is_complete = false;
processes[i].waiting_time = 0;
processes[i].turnaround_time = 0;
}
switch (choice) {
case 1:
fcfs(processes, n, 200); // Simulate FCFS
break;
case 2:
shortest_job_first(processes, n, 200); // Simulate SJF
break;
case 3:
shortest_remaining_time_first(processes, n, 200); // Simulate SRTF
break;
case 4:
round_robin(processes, n, 5, 200); // Simulate RR with Quantum 5
break;
case 5:
preemptive_priority_scheduling(processes, n, 200); // Simulate Preemptive Priority
break;
case 6:
non_preemptive_priority_scheduling(processes, n, 200); // Simulate Non-Preemptive
Priority
break;
default:
printf("Invalid choice. Please enter a number between 1 and 6.n");
break;
}
}
return 0;
}
Output:
22-SE-77 OS_lab_labotary assignmnet#2.pdf

More Related Content

Similar to 22-SE-77 OS_lab_labotary assignmnet#2.pdf

Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
Make Mannan
 
Programs for Operating System
Programs for Operating SystemPrograms for Operating System
Programs for Operating System
LPU
 
JAVA - please help with the error regarding the bold line below- I am.docx
JAVA - please help with the error regarding the bold line below- I am.docxJAVA - please help with the error regarding the bold line below- I am.docx
JAVA - please help with the error regarding the bold line below- I am.docx
BenjaminIjsDaviesq
 
Contiki introduction I.
Contiki introduction I.Contiki introduction I.
Contiki introduction I.
Dingxin Xu
 
Complete Lab 123456789123456789123456789
Complete Lab 123456789123456789123456789Complete Lab 123456789123456789123456789
Complete Lab 123456789123456789123456789
vickyvikas51556
 
Lab program 1234567891234567891234567891
Lab program 1234567891234567891234567891Lab program 1234567891234567891234567891
Lab program 1234567891234567891234567891
akashpunarvi2005
 
lab.123456789123456789123456789123456789
lab.123456789123456789123456789123456789lab.123456789123456789123456789123456789
lab.123456789123456789123456789123456789
Ghh
 
Sysprog 12
Sysprog 12Sysprog 12
Sysprog 12
Ahmed Mekkawy
 
Sysprog 12
Sysprog 12Sysprog 12
Sysprog 12
Ahmed Mekkawy
 
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
katherncarlyle
 
Operating system labs
Operating system labsOperating system labs
Operating system labs
bhaktisagar4
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programs
Kandarp Tiwari
 
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docxcmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
gordienaysmythe
 
Completo-anexo-código-de-programación_2022.pdf
Completo-anexo-código-de-programación_2022.pdfCompleto-anexo-código-de-programación_2022.pdf
Completo-anexo-código-de-programación_2022.pdf
José Cuadros Loayza
 
Lab9 LRU and FIFO.docx
Lab9 LRU and FIFO.docxLab9 LRU and FIFO.docx
Lab9 LRU and FIFO.docx
PremaJain2
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
Prof. Dr. K. Adisesha
 
Zoro123456789123456789123456789123456789
Zoro123456789123456789123456789123456789Zoro123456789123456789123456789123456789
Zoro123456789123456789123456789123456789
Ghh
 
labb123456789123456789123456789123456789
labb123456789123456789123456789123456789labb123456789123456789123456789123456789
labb123456789123456789123456789123456789
Ghh
 
COA_remaining_lab_works_077BCT033.pdf
COA_remaining_lab_works_077BCT033.pdfCOA_remaining_lab_works_077BCT033.pdf
COA_remaining_lab_works_077BCT033.pdf
JavedAnsari236392
 
assign4assign4_part1bonnie.c This is a file system ben.docx
assign4assign4_part1bonnie.c  This is a file system ben.docxassign4assign4_part1bonnie.c  This is a file system ben.docx
assign4assign4_part1bonnie.c This is a file system ben.docx
festockton
 

Similar to 22-SE-77 OS_lab_labotary assignmnet#2.pdf (20)

Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
 
Programs for Operating System
Programs for Operating SystemPrograms for Operating System
Programs for Operating System
 
JAVA - please help with the error regarding the bold line below- I am.docx
JAVA - please help with the error regarding the bold line below- I am.docxJAVA - please help with the error regarding the bold line below- I am.docx
JAVA - please help with the error regarding the bold line below- I am.docx
 
Contiki introduction I.
Contiki introduction I.Contiki introduction I.
Contiki introduction I.
 
Complete Lab 123456789123456789123456789
Complete Lab 123456789123456789123456789Complete Lab 123456789123456789123456789
Complete Lab 123456789123456789123456789
 
Lab program 1234567891234567891234567891
Lab program 1234567891234567891234567891Lab program 1234567891234567891234567891
Lab program 1234567891234567891234567891
 
lab.123456789123456789123456789123456789
lab.123456789123456789123456789123456789lab.123456789123456789123456789123456789
lab.123456789123456789123456789123456789
 
Sysprog 12
Sysprog 12Sysprog 12
Sysprog 12
 
Sysprog 12
Sysprog 12Sysprog 12
Sysprog 12
 
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
 
Operating system labs
Operating system labsOperating system labs
Operating system labs
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programs
 
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docxcmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
 
Completo-anexo-código-de-programación_2022.pdf
Completo-anexo-código-de-programación_2022.pdfCompleto-anexo-código-de-programación_2022.pdf
Completo-anexo-código-de-programación_2022.pdf
 
Lab9 LRU and FIFO.docx
Lab9 LRU and FIFO.docxLab9 LRU and FIFO.docx
Lab9 LRU and FIFO.docx
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
Zoro123456789123456789123456789123456789
Zoro123456789123456789123456789123456789Zoro123456789123456789123456789123456789
Zoro123456789123456789123456789123456789
 
labb123456789123456789123456789123456789
labb123456789123456789123456789123456789labb123456789123456789123456789123456789
labb123456789123456789123456789123456789
 
COA_remaining_lab_works_077BCT033.pdf
COA_remaining_lab_works_077BCT033.pdfCOA_remaining_lab_works_077BCT033.pdf
COA_remaining_lab_works_077BCT033.pdf
 
assign4assign4_part1bonnie.c This is a file system ben.docx
assign4assign4_part1bonnie.c  This is a file system ben.docxassign4assign4_part1bonnie.c  This is a file system ben.docx
assign4assign4_part1bonnie.c This is a file system ben.docx
 

Recently uploaded

Operational amplifiers and oscillators notes
Operational amplifiers and oscillators notesOperational amplifiers and oscillators notes
Operational amplifiers and oscillators notes
ShachiPGowda
 
Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...
Prakhyath Rai
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
ElakkiaU
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
Roger Rozario
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
VANDANAMOHANGOUDA
 
Introduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.pptIntroduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.ppt
Dwarkadas J Sanghvi College of Engineering
 
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
upoux
 
Pressure Relief valve used in flow line to release the over pressure at our d...
Pressure Relief valve used in flow line to release the over pressure at our d...Pressure Relief valve used in flow line to release the over pressure at our d...
Pressure Relief valve used in flow line to release the over pressure at our d...
cannyengineerings
 
Introduction to verilog basic modeling .ppt
Introduction to verilog basic modeling   .pptIntroduction to verilog basic modeling   .ppt
Introduction to verilog basic modeling .ppt
AmitKumar730022
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
nedcocy
 
5G Radio Network Througput Problem Analysis HCIA.pdf
5G Radio Network Througput Problem Analysis HCIA.pdf5G Radio Network Througput Problem Analysis HCIA.pdf
5G Radio Network Througput Problem Analysis HCIA.pdf
AlvianRamadhani5
 
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
PIMR BHOPAL
 
smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...
um7474492
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
bjmsejournal
 
TIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptxTIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptx
CVCSOfficial
 
Generative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdfGenerative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdf
mahaffeycheryld
 
SCALING OF MOS CIRCUITS m .pptx
SCALING OF MOS CIRCUITS m                 .pptxSCALING OF MOS CIRCUITS m                 .pptx
SCALING OF MOS CIRCUITS m .pptx
harshapolam10
 
Mechatronics material . Mechanical engineering
Mechatronics material . Mechanical engineeringMechatronics material . Mechanical engineering
Mechatronics material . Mechanical engineering
sachin chaurasia
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
ijaia
 

Recently uploaded (20)

Operational amplifiers and oscillators notes
Operational amplifiers and oscillators notesOperational amplifiers and oscillators notes
Operational amplifiers and oscillators notes
 
Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
 
Introduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.pptIntroduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.ppt
 
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
 
Pressure Relief valve used in flow line to release the over pressure at our d...
Pressure Relief valve used in flow line to release the over pressure at our d...Pressure Relief valve used in flow line to release the over pressure at our d...
Pressure Relief valve used in flow line to release the over pressure at our d...
 
Introduction to verilog basic modeling .ppt
Introduction to verilog basic modeling   .pptIntroduction to verilog basic modeling   .ppt
Introduction to verilog basic modeling .ppt
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
 
5G Radio Network Througput Problem Analysis HCIA.pdf
5G Radio Network Througput Problem Analysis HCIA.pdf5G Radio Network Througput Problem Analysis HCIA.pdf
5G Radio Network Througput Problem Analysis HCIA.pdf
 
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
VARIABLE FREQUENCY DRIVE. VFDs are widely used in industrial applications for...
 
smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
 
TIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptxTIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptx
 
Generative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdfGenerative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdf
 
SCALING OF MOS CIRCUITS m .pptx
SCALING OF MOS CIRCUITS m                 .pptxSCALING OF MOS CIRCUITS m                 .pptx
SCALING OF MOS CIRCUITS m .pptx
 
Mechatronics material . Mechanical engineering
Mechatronics material . Mechanical engineeringMechatronics material . Mechanical engineering
Mechatronics material . Mechanical engineering
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 

22-SE-77 OS_lab_labotary assignmnet#2.pdf

  • 1.
  • 2. Code: #include <stdio.h> #include <stdlib.h> #include <stdbool.h> // Structure to represent a process struct Process { int pid; // Process ID int arrival_time; // Arrival time int burst_time; // Burst time int remaining_time; // Remaining burst time (for preemptive algorithms) int comes_back_after; // Time to return to the ready queue int priority; // Priority bool is_complete; // Completion flag int waiting_time; // Waiting time int turnaround_time; // Turnaround time }; // Function to calculate waiting and turnaround times void calculate_times(struct Process *processes, int n) { int total_waiting_time = 0; int total_turnaround_time = 0; for (int i = 0; i < n; i++) { processes[i].turnaround_time = processes[i].burst_time + processes[i].waiting_time; processes[i].waiting_time = processes[i].turnaround_time - processes[i].burst_time; total_waiting_time += processes[i].waiting_time; total_turnaround_time += processes[i].turnaround_time; } printf("Average Waiting Time: %.2fn", (float)total_waiting_time / n); printf("Average Turnaround Time: %.2fn", (float)total_turnaround_time / n); } // Sort processes by arrival time (for FCFS) void sort_by_arrival(struct Process *processes, int n) { for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (processes[j].arrival_time < processes[i].arrival_time) { struct Process temp = processes[i]; processes[i] = processes[j]; processes[j] = temp; } } } } // First Come First Served (FCFS) void fcfs(struct Process *processes, int n, int time_limit) {
  • 3. printf("First Come First Served (FCFS):n"); int current_time = 0; printf("Gantt Chart:n"); printf("|"); for (int i = 0; i < n; i++) { if (processes[i].arrival_time > current_time) { printf(" Idle |"); current_time = processes[i].arrival_time; } printf(" P%d (%d-%d) |", processes[i].pid, current_time, current_time + processes[i].burst_time); processes[i].waiting_time = current_time - processes[i].arrival_time; current_time += processes[i].burst_time; } printf("n"); calculate_times(processes, n); } // Shortest Job First (SJF) void shortest_job_first(struct Process *processes, int n, int time_limit) { printf("Shortest Job First (SJF):n"); int current_time = 0; int completed = 0; printf("Gantt Chart:n"); while (completed < n) { int shortest_index = -1; int shortest_burst = 9999; // Find the process with the shortest burst time that is ready for (int i = 0; i < n; i++) { if (processes[i].arrival_time <= current_time && !processes[i].is_complete) { if (processes[i].burst_time < shortest_burst) { shortest_burst = processes[i].burst_time; shortest_index = i; } } } if (shortest_index == -1) { printf("| Idle |"); current_time++; continue; } printf(" P%d (%d-%d) |", processes[shortest_index].pid, current_time, current_time + processes[shortest_index].burst_time);
  • 4. processes[shortest_index].waiting_time = current_time - processes[shortest_index].arrival_time; current_time += processes[shortest_index].burst_time; processes[shortest_index].is_complete = true; completed++; } printf("n"); calculate_times(processes, n); } // Shortest Remaining Time First (SRTF) void shortest_remaining_time_first(struct Process *processes, int n, int time_limit) { printf("Shortest Remaining Time First (SRTF):n"); int current_time = 0; int completed = 0; printf("Gantt Chart:n"); while (completed < n && current_time < time_limit) { int shortest_index = -1; int shortest_remaining = 9999; // Find the process with the shortest remaining time for (int i = 0; i < n; i++) { if (processes[i].arrival_time <= current_time && !processes[i].is_complete) { if (processes[i].remaining_time < shortest_remaining) { shortest_remaining = processes[i].remaining_time; shortest_index = i; } } } if (shortest_index == -1) { printf("| Idle |"); current_time++; continue; } printf(" P%d (%d-%d) |", processes[shortest_index].pid, current_time, current_time + 1); processes[shortest_index].remaining_time--; current_time++; if (processes[shortest_index].remaining_time == 0) { processes[shortest_index].is_complete = true; completed++; } } printf("n");
  • 5. calculate_times(processes, n); } // Round Robin with a given quantum void round_robin(struct Process *processes, int n, int quantum, int time_limit) { printf("Round Robin (RR) with Quantum %d:n", quantum); int current_time = 0; int completed = 0; printf("Gantt Chart:n"); while (completed < n && current_time < time_limit) { for (int i = 0; i < n; i++) { if (!processes[i].is_complete && processes[i].arrival_time <= current_time) { if (processes[i].remaining_time > quantum) { printf(" P%d (%d-%d) |", processes[i].pid, current_time, current_time + quantum); processes[i].remaining_time -= quantum; current_time += quantum; } else { printf(" P%d (%d-%d) |", processes[i].pid, current_time, current_time + processes[i].remaining_time); current_time += processes[i].remaining_time; processes[i].is_complete = true; completed++; } } } } printf("n"); calculate_times(processes, n); } // Preemptive Priority Scheduling void preemptive_priority_scheduling(struct Process *processes, int n, int time_limit) { printf("Preemptive Priority Scheduling:n"); int current_time = 0; int completed = 0; printf("Gantt Chart:n"); while (completed < n && current_time < time_limit) { int highest_priority_index = -1; int highest_priority = -1; for (int i = 0; i < n; i++) { if (!processes[i].is_complete && processes[i].arrival_time <= current_time) { if (processes[i].priority > highest_priority) { highest_priority = processes[i].priority; highest_priority_index = i; } } }
  • 6. if (highest_priority_index == -1) { printf("| Idle |"); current_time++; continue; } printf(" P%d (%d-%d) |", processes[highest_priority_index].pid, current_time, current_time + 1); processes[highest_priority_index].remaining_time--; current_time++; if (processes[highest_priority_index].remaining_time == 0) { processes[highest_priority_index].is_complete = true; completed++; } } printf("n"); calculate_times(processes, n); } // Non-Preemptive Priority Scheduling void non_preemptive_priority_scheduling(struct Process *processes, int n, int time_limit) { printf("Non-Preemptive Priority Scheduling:n"); int current_time = 0; int completed = 0; printf("Gantt Chart:n"); while (completed < n && current_time < time_limit) { int highest_priority_index = -1; int highest_priority = -1; for (int i = 0; i < n; i++) { if (!processes[i].is_complete && processes[i].arrival_time <= current_time) { if (processes[i].priority > highest_priority) { highest_priority = processes[i].priority; highest_priority_index = i; } } } if (highest_priority_index == -1) { printf("| Idle |"); current_time++; continue; } printf(" P%d (%d-%d) |", processes[highest_priority_index].pid, current_time, current_time + processes[highest_priority_index].burst_time); current_time += processes[highest_priority_index].burst_time;
  • 7. processes[highest_priority_index].is_complete = true; completed++; } printf("n"); calculate_times(processes, n); } // Display the scheduling menu void display_menu() { printf("Select a Scheduling Algorithm:n"); printf("1. First Come First Served (FCFS)n"); printf("2. Shortest Job First (SJF)n"); printf("3. Shortest Remaining Time First (SRTF)n"); printf("4. Round Robin (Quantum 5)n"); printf("5. Preemptive Priority Scheduling with Agingn"); printf("6. Non-Preemptive Priority Schedulingn"); } int main() { struct Process processes[] = { {1, 0, 10, 10, 2, 3, 0, 0, 0}, {2, 1, 8, 8, 4, 2, 0, 0, 0}, {3, 3, 14, 14, 6, 3, 0, 0, 0}, {4, 4, 7, 7, 8, 1, 0, 0, 0}, {5, 6, 5, 5, 3, 0, 0, 0}, {6, 7, 4, 4, 6, 1, 0, 0, 0}, {7, 8, 6, 6, 9, 2, 0, 0, 0} }; int n = sizeof(processes) / sizeof(processes[0]); // Loop to allow multiple runs while (true) { display_menu(); // Display the menu to choose the algorithm int choice; printf("Enter your choice (1-6, or 0 to exit): "); scanf("%d", &choice); if (choice == 0) { break; // Exit the loop to end the program } // Reset process states for each run for (int i = 0; i < n; i++) { processes[i].remaining_time = processes[i].burst_time; processes[i].is_complete = false; processes[i].waiting_time = 0; processes[i].turnaround_time = 0; }
  • 8. switch (choice) { case 1: fcfs(processes, n, 200); // Simulate FCFS break; case 2: shortest_job_first(processes, n, 200); // Simulate SJF break; case 3: shortest_remaining_time_first(processes, n, 200); // Simulate SRTF break; case 4: round_robin(processes, n, 5, 200); // Simulate RR with Quantum 5 break; case 5: preemptive_priority_scheduling(processes, n, 200); // Simulate Preemptive Priority break; case 6: non_preemptive_priority_scheduling(processes, n, 200); // Simulate Non-Preemptive Priority break; default: printf("Invalid choice. Please enter a number between 1 and 6.n"); break; } } return 0; } Output: