added OS theory code
This commit is contained in:
parent
2313ae0e01
commit
bb32f2abc5
@ -1,7 +1,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <limits.h>
|
|
||||||
|
|
||||||
# define MAX 4
|
# define MAX 4
|
||||||
|
|
||||||
@ -23,7 +22,6 @@
|
|||||||
*a = *b;
|
*a = *b;
|
||||||
*b = temp;
|
*b = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Function to calculate Completion Time, Turnaround Time, and Waiting Time
|
// Function to calculate Completion Time, Turnaround Time, and Waiting Time
|
||||||
void calculate_times(Process processes[], int n) {
|
void calculate_times(Process processes[], int n) {
|
||||||
@ -32,7 +30,6 @@
|
|||||||
processes[i].wt = processes[i].tat - processes[i].bt;
|
processes[i].wt = processes[i].tat - processes[i].bt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Function to calculate average times
|
// Function to calculate average times
|
||||||
void calculate_averages(Process processes[], int n, float *avg_ct,
|
void calculate_averages(Process processes[], int n, float *avg_ct,
|
||||||
@ -41,7 +38,6 @@
|
|||||||
*avg_tat = 0;
|
*avg_tat = 0;
|
||||||
*avg_wt = 0;
|
*avg_wt = 0;
|
||||||
*avg_rt = 0;
|
*avg_rt = 0;
|
||||||
|
|
||||||
|
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
*avg_ct += processes[i].ct;
|
*avg_ct += processes[i].ct;
|
||||||
@ -49,7 +45,6 @@
|
|||||||
*avg_wt += processes[i].wt;
|
*avg_wt += processes[i].wt;
|
||||||
*avg_rt += processes[i].rt;
|
*avg_rt += processes[i].rt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
*avg_ct /= n;
|
*avg_ct /= n;
|
||||||
*avg_tat /= n;
|
*avg_tat /= n;
|
||||||
@ -64,17 +59,16 @@
|
|||||||
for (int i = 0; i <= timeline[n - 1]; i++) {
|
for (int i = 0; i <= timeline[n - 1]; i++) {
|
||||||
printf("%-3d", i);
|
printf("%-3d", i);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\n-----------------------------------------------------------\n");
|
printf("\n-----------------------------------------------------------\n");
|
||||||
|
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
printf("%-3s", processes[i].pid);
|
printf("%-3s", processes[i].pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\n-----------------------------------------------------------\n");
|
printf("\n-----------------------------------------------------------\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Function to display the process table
|
// Function to display the process table
|
||||||
void display_table(Process processes[], int n) {
|
void display_table(Process processes[], int n) {
|
||||||
printf("--------------------------------------------------------------------"
|
printf("--------------------------------------------------------------------"
|
||||||
@ -94,7 +88,7 @@
|
|||||||
|
|
||||||
// Preemptive SJF
|
// Preemptive SJF
|
||||||
void preemptive_sjf(Process processes[], int n){
|
void preemptive_sjf(Process processes[], int n){
|
||||||
|
|
||||||
// process sort by AT
|
// process sort by AT
|
||||||
for (int i = 0; i < n -1; i++){
|
for (int i = 0; i < n -1; i++){
|
||||||
for(int j = 0; j < n - i - 1; j++){
|
for(int j = 0; j < n - i - 1; j++){
|
||||||
@ -148,45 +142,45 @@
|
|||||||
|
|
||||||
processes[shortest].remaining_bt--;
|
processes[shortest].remaining_bt--;
|
||||||
current_time++;
|
current_time++;
|
||||||
|
|
||||||
|
|
||||||
if (processes[shortest].remaining_bt == 0) {
|
if (processes[shortest].remaining_bt == 0) {
|
||||||
completed++;
|
completed++;
|
||||||
processes[shortest].ct = current_time;
|
processes[shortest].ct = current_time;
|
||||||
processes[shortest].is_completed = 1;
|
processes[shortest].is_completed = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
timeline[timeline_index++] = current_time;
|
timeline[timeline_index++] = current_time;
|
||||||
}
|
}
|
||||||
|
|
||||||
calculate_times(processes, n);
|
calculate_times(processes, n);
|
||||||
|
|
||||||
float avg_ct, avg_tat, avg_wt, avg_rt;
|
float avg_ct, avg_tat, avg_wt, avg_rt;
|
||||||
calculate_averages(processes, n, &avg_ct, &avg_tat, &avg_wt, &avg_rt);
|
calculate_averages(processes, n, &avg_ct, &avg_tat, &avg_wt, &avg_rt);
|
||||||
|
|
||||||
|
|
||||||
printf("\nPreemptive SJF Scheduling:\n");
|
printf("\nPreemptive SJF Scheduling:\n");
|
||||||
display_table(processes, n);
|
display_table(processes, n);
|
||||||
|
|
||||||
|
|
||||||
printf("\nAverage Completion Time: %.2f\n", avg_ct);
|
printf("\nAverage Completion Time: %.2f\n", avg_ct);
|
||||||
printf("Average Turnaround Time: %.2f\n", avg_tat);
|
printf("Average Turnaround Time: %.2f\n", avg_tat);
|
||||||
printf("Average Waiting Time: %.2f\n", avg_wt);
|
printf("Average Waiting Time: %.2f\n", avg_wt);
|
||||||
printf("Average Response Time: %.2f\n", avg_rt);
|
printf("Average Response Time: %.2f\n", avg_rt);
|
||||||
|
|
||||||
|
|
||||||
display_gantt_chart(processes, n, timeline);
|
display_gantt_chart(processes, n, timeline);
|
||||||
free(timeline);
|
free(timeline);
|
||||||
}
|
}
|
||||||
|
|
||||||
void round_robin(Process processes[], int n, int quantum) {
|
void round_robin(Process processes[], int n, int quantum) {
|
||||||
|
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
processes[i].remaining_bt = processes[i].bt;
|
processes[i].remaining_bt = processes[i].bt;
|
||||||
processes[i].rt = -1;
|
processes[i].rt = -1;
|
||||||
processes[i].is_completed = 0;
|
processes[i].is_completed = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int current_time = 0;
|
int current_time = 0;
|
||||||
int completed = 0;
|
int completed = 0;
|
||||||
@ -197,57 +191,57 @@
|
|||||||
perror("Failed to allocate memory for timeline");
|
perror("Failed to allocate memory for timeline");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int timeline_index = 0;
|
int timeline_index = 0;
|
||||||
|
|
||||||
|
|
||||||
while (completed != n) {
|
while (completed != n) {
|
||||||
if (processes[i].remaining_bt > 0 && processes[i].at <= current_time) {
|
if (processes[i].remaining_bt > 0 && processes[i].at <= current_time) {
|
||||||
if (processes[i].rt == -1) {
|
if (processes[i].rt == -1) {
|
||||||
processes[i].rt = current_time - processes[i].at;
|
processes[i].rt = current_time - processes[i].at;
|
||||||
}
|
}
|
||||||
|
|
||||||
int execute_time = (processes[i].remaining_bt > quantum) ? quantum : processes[i].remaining_bt; processes[i].remaining_bt -= execute_time; current_time += execute_time;
|
int execute_time = (processes[i].remaining_bt > quantum) ? quantum : processes[i].remaining_bt; processes[i].remaining_bt -= execute_time; current_time += execute_time;
|
||||||
|
|
||||||
if (processes[i].remaining_bt == 0) {
|
if (processes[i].remaining_bt == 0) {
|
||||||
completed++;
|
completed++;
|
||||||
processes[i].ct = current_time;
|
processes[i].ct = current_time;
|
||||||
processes[i].is_completed = 1;
|
processes[i].is_completed = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
timeline[timeline_index++] = current_time;
|
timeline[timeline_index++] = current_time;
|
||||||
|
|
||||||
} else if (processes[i].at > current_time) {
|
} else if (processes[i].at > current_time) {
|
||||||
current_time++; // if process hasn't arrived, time is incremented (to prevent a stall)
|
current_time++; // if process hasn't arrived, time is incremented (to prevent a stall)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
i = (i + 1) % n;
|
i = (i + 1) % n;
|
||||||
if (current_time > 1000) break;
|
if (current_time > 1000) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
calculate_times(processes, n);
|
calculate_times(processes, n);
|
||||||
|
|
||||||
|
|
||||||
float avg_ct, avg_tat, avg_wt, avg_rt;
|
float avg_ct, avg_tat, avg_wt, avg_rt;
|
||||||
calculate_averages(processes, n, &avg_ct, &avg_tat, &avg_wt, &avg_rt);
|
calculate_averages(processes, n, &avg_ct, &avg_tat, &avg_wt, &avg_rt);
|
||||||
|
|
||||||
|
|
||||||
printf("\nRound Robin Scheduling (Quantum = %d):\n", quantum);
|
printf("\nRound Robin Scheduling (Quantum = %d):\n", quantum);
|
||||||
display_table(processes, n);
|
display_table(processes, n);
|
||||||
|
|
||||||
|
|
||||||
printf("\nAverage Completion Time: %.2f\n", avg_ct);
|
printf("\nAverage Completion Time: %.2f\n", avg_ct);
|
||||||
printf("Average Turnaround Time: %.2f\n", avg_tat);
|
printf("Average Turnaround Time: %.2f\n", avg_tat);
|
||||||
printf("Average Waiting Time: %.2f\n", avg_wt);
|
printf("Average Waiting Time: %.2f\n", avg_wt);
|
||||||
printf("Average Response Time: %.2f\n", avg_rt);
|
printf("Average Response Time: %.2f\n", avg_rt);
|
||||||
|
|
||||||
|
|
||||||
display_gantt_chart(processes, n, timeline);
|
display_gantt_chart(processes, n, timeline);
|
||||||
free(timeline); // Free memory
|
free(timeline); // Free memory
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void non_preemptive_priority(Process processes[], int n) {
|
void non_preemptive_priority(Process processes[], int n) {
|
||||||
for (int i = 0; i < n - 1; i++) {
|
for (int i = 0; i < n - 1; i++) {
|
||||||
@ -260,54 +254,54 @@
|
|||||||
|
|
||||||
int current_time = 0;
|
int current_time = 0;
|
||||||
int completed = 0;
|
int completed = 0;
|
||||||
int *timeline = (int *)malloc((n * 2) * sizeof(int));
|
int *timeline = (int *)malloc((n * 2) * sizeof(int));
|
||||||
|
|
||||||
if (timeline == NULL) {
|
if (timeline == NULL) {
|
||||||
perror("Failed to allocate memory for timeline");
|
perror("Failed to allocate memory for timeline");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int timeline_index = 0;
|
int timeline_index = 0;
|
||||||
|
|
||||||
while (completed != n) {
|
while (completed != n) {
|
||||||
int highest_priority = -1;
|
int highest_priority = -1;
|
||||||
int min_priority = 9999; // Large value
|
int min_priority = 9999; // Large value
|
||||||
|
|
||||||
|
|
||||||
for (int j = 0; j < n; j++) {
|
for (int j = 0; j < n; j++) {
|
||||||
if (processes[j].at <= current_time && processes[j].bt > 0 && processes[j].priority < min_priority) {
|
if (processes[j].at <= current_time && processes[j].bt > 0 && processes[j].priority < min_priority) {
|
||||||
min_priority = processes[j].priority;
|
min_priority = processes[j].priority;
|
||||||
highest_priority = j;
|
highest_priority = j;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (highest_priority == -1) {
|
if (highest_priority == -1) {
|
||||||
current_time++;
|
current_time++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (processes[highest_priority].rt == -1) {
|
if (processes[highest_priority].rt == -1) {
|
||||||
processes[highest_priority].rt =
|
processes[highest_priority].rt =
|
||||||
current_time - processes[highest_priority].at;
|
current_time - processes[highest_priority].at;
|
||||||
}
|
}
|
||||||
|
|
||||||
current_time += processes[highest_priority].bt;
|
current_time += processes[highest_priority].bt;
|
||||||
|
|
||||||
processes[highest_priority].ct = current_time;
|
processes[highest_priority].ct = current_time;
|
||||||
processes[highest_priority].bt = 0; // Mark completed
|
processes[highest_priority].bt = 0; // Mark completed
|
||||||
|
|
||||||
completed++;
|
completed++;
|
||||||
timeline[timeline_index++] = current_time;
|
timeline[timeline_index++] = current_time;
|
||||||
}
|
}
|
||||||
|
|
||||||
calculate_times(processes, n);
|
calculate_times(processes, n);
|
||||||
|
|
||||||
float avg_ct, avg_tat, avg_wt, avg_rt;
|
float avg_ct, avg_tat, avg_wt, avg_rt;
|
||||||
calculate_averages(processes, n, &avg_ct, &avg_tat, &avg_wt, &avg_rt);
|
calculate_averages(processes, n, &avg_ct, &avg_tat, &avg_wt, &avg_rt);
|
||||||
|
|
||||||
printf("\nNon-Preemptive Priority Scheduling:\n");
|
printf("\nNon-Preemptive Priority Scheduling:\n");
|
||||||
display_table(processes, n);
|
display_table(processes, n);
|
||||||
|
|
||||||
printf("\nAverage Completion Time: %.2f\n", avg_ct);
|
printf("\nAverage Completion Time: %.2f\n", avg_ct);
|
||||||
printf("Average Turnaround Time: %.2f\n", avg_tat);
|
printf("Average Turnaround Time: %.2f\n", avg_tat);
|
||||||
printf("Average Waiting Time: %.2f\n", avg_wt);
|
printf("Average Waiting Time: %.2f\n", avg_wt);
|
||||||
@ -316,32 +310,32 @@
|
|||||||
display_gantt_chart(processes, n, timeline);
|
display_gantt_chart(processes, n, timeline);
|
||||||
free(timeline); // Free memory
|
free(timeline); // Free memory
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int n, choice, quantum;
|
int n, choice, quantum;
|
||||||
|
|
||||||
printf("Enter the number of processes: ");
|
printf("Enter the number of processes: ");
|
||||||
scanf("%d", &n);
|
scanf("%d", &n);
|
||||||
|
|
||||||
Process processes[n];
|
Process processes[n];
|
||||||
|
|
||||||
// Input process details
|
// Input process details
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
printf("\nEnter details for process %d:\n", i + 1);
|
printf("\nEnter details for process %d:\n", i + 1);
|
||||||
|
|
||||||
printf("PID: ");
|
printf("PID: ");
|
||||||
scanf("%s", processes[i].pid);
|
scanf("%s", processes[i].pid);
|
||||||
|
|
||||||
printf("Arrival Time: ");
|
printf("Arrival Time: ");
|
||||||
scanf("%d", &processes[i].at);
|
scanf("%d", &processes[i].at);
|
||||||
|
|
||||||
printf("Burst Time: ");
|
printf("Burst Time: ");
|
||||||
scanf("%d", &processes[i].bt);
|
scanf("%d", &processes[i].bt);
|
||||||
|
|
||||||
printf("Priority (lower value = higher priority): ");
|
printf("Priority (lower value = higher priority): ");
|
||||||
scanf("%d", &processes[i].priority);
|
scanf("%d", &processes[i].priority);
|
||||||
|
|
||||||
processes[i].rt = 0; // Initialize response time
|
processes[i].rt = 0; // Initialize response time
|
||||||
processes[i].is_completed = 0; // Initialize completion flag
|
processes[i].is_completed = 0; // Initialize completion flag
|
||||||
}
|
}
|
||||||
@ -351,13 +345,13 @@
|
|||||||
printf("-----------------------\n");
|
printf("-----------------------\n");
|
||||||
printf("| PID | AT | BT |\n");
|
printf("| PID | AT | BT |\n");
|
||||||
printf("-----------------------\n");
|
printf("-----------------------\n");
|
||||||
|
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
printf("| %-5s | %-3d | %-3d |\n", processes[i].pid, processes[i].at, processes[i].bt);
|
printf("| %-5s | %-3d | %-3d |\n", processes[i].pid, processes[i].at, processes[i].bt);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("-----------------------\n");
|
printf("-----------------------\n");
|
||||||
|
|
||||||
// Algorithm Selection Menu with Loop and Exit
|
// Algorithm Selection Menu with Loop and Exit
|
||||||
while (1) {
|
while (1) {
|
||||||
printf("\nChoose a scheduling algorithm:\n");
|
printf("\nChoose a scheduling algorithm:\n");
|
||||||
@ -366,9 +360,9 @@
|
|||||||
printf("3. Non-Preemptive Priority\n");
|
printf("3. Non-Preemptive Priority\n");
|
||||||
printf("4. Exit\n");
|
printf("4. Exit\n");
|
||||||
printf("Enter your choice: ");
|
printf("Enter your choice: ");
|
||||||
|
|
||||||
scanf("%d", &choice);
|
scanf("%d", &choice);
|
||||||
|
|
||||||
switch (choice) {
|
switch (choice) {
|
||||||
case 1:
|
case 1:
|
||||||
preemptive_sjf(processes, n);
|
preemptive_sjf(processes, n);
|
||||||
@ -386,8 +380,8 @@
|
|||||||
exit(0);
|
exit(0);
|
||||||
default:
|
default:
|
||||||
printf("Invalid choice. Please try again.\n");
|
printf("Invalid choice. Please try again.\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user