updated week7, week8
This commit is contained in:
parent
31ebd76c78
commit
d53e55ce7c
BIN
OS/C/Week6/menu
Executable file
BIN
OS/C/Week6/menu
Executable file
Binary file not shown.
@ -4,90 +4,87 @@
|
|||||||
|
|
||||||
# define MAX 4
|
# define MAX 4
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char pid[5]; // Process ID (string)
|
char pid[5]; // Process ID (string)
|
||||||
int at; // Arrival Time
|
int at; // Arrival Time
|
||||||
int bt; // Burst Time
|
int bt; // Burst Time
|
||||||
int priority; // Priority (lower value = higher priority)
|
int priority; // Priority (lower value = higher priority)
|
||||||
int ct; // Completion Time
|
int ct; // Completion Time
|
||||||
int tat; // Turnaround Time
|
int tat; // Turnaround Time
|
||||||
int wt; // Waiting Time
|
int wt; // Waiting Time
|
||||||
int rt; // Response Time
|
int rt; // Response Time
|
||||||
int remaining_bt; // Remaining Burst Time (for preemptive sjf)
|
int remaining_bt; // Remaining Burst Time (for preemptive sjf)
|
||||||
int is_completed; // completion flag
|
int is_completed; // completion flag
|
||||||
} Process;
|
} Process;
|
||||||
|
|
||||||
void swap(Process *a, Process *b) {
|
void swap(Process *a, Process *b) {
|
||||||
Process temp = *a;
|
Process temp = *a;
|
||||||
*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) {
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
processes[i].tat = processes[i].ct - processes[i].at;
|
processes[i].tat = processes[i].ct - processes[i].at;
|
||||||
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,
|
||||||
float *avg_tat, float *avg_wt, float *avg_rt) {
|
float *avg_tat, float *avg_wt, float *avg_rt) {
|
||||||
*avg_ct = 0;
|
*avg_ct = 0;
|
||||||
*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;
|
||||||
*avg_tat += processes[i].tat;
|
*avg_tat += processes[i].tat;
|
||||||
*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;
|
||||||
*avg_wt /= n;
|
*avg_wt /= n;
|
||||||
*avg_rt /= n;
|
*avg_rt /= n;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to display the Gantt chart
|
// Function to display the Gantt chart
|
||||||
void display_gantt_chart(Process processes[], int n, int timeline[]) {
|
void display_gantt_chart(Process processes[], int n, int timeline[], int timeline_index) {
|
||||||
printf("\nGantt Chart:\n");
|
printf("\nGantt Chart:\n");
|
||||||
printf("-----------------------------------------------------------\n");
|
printf("-----------------------------------------------------------\n");
|
||||||
for (int i = 0; i <= timeline[n - 1]; i++) {
|
int last_process = -1;
|
||||||
printf("%-3d", i);
|
for(int i = 0; i < timeline_index; i++) {
|
||||||
}
|
if(timeline[i] != last_process) {
|
||||||
|
printf("%s ", processes[timeline[i]].pid);
|
||||||
|
last_process = timeline[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("\n-----------------------------------------------------------\n");
|
||||||
|
}
|
||||||
|
|
||||||
printf("\n-----------------------------------------------------------\n");
|
// Function to display the process table
|
||||||
|
void display_table(Process processes[], int n) {
|
||||||
|
printf("--------------------------------------------------------------------"
|
||||||
|
"------\n");
|
||||||
|
printf("| PID | AT | BT | CT | TAT | WT | RT |\n");
|
||||||
|
printf("--------------------------------------------------------------------"
|
||||||
|
"------\n");
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
printf("| %-5s | %-3d | %-3d | %-3d | %-3d | %-3d | %-3d |\n",
|
||||||
|
processes[i].pid, processes[i].at, processes[i].bt,
|
||||||
|
processes[i].ct, processes[i].tat, processes[i].wt,
|
||||||
|
processes[i].rt);
|
||||||
|
}
|
||||||
|
printf("--------------------------------------------------------------------"
|
||||||
|
"------\n");
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < n; i++) {
|
// Preemptive SJF
|
||||||
printf("%-3s", processes[i].pid);
|
void preemptive_sjf(Process processes[], int n){
|
||||||
}
|
|
||||||
|
|
||||||
printf("\n-----------------------------------------------------------\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function to display the process table
|
|
||||||
void display_table(Process processes[], int n) {
|
|
||||||
printf("--------------------------------------------------------------------"
|
|
||||||
"------\n");
|
|
||||||
printf("| PID | AT | BT | CT | TAT | WT | RT |\n");
|
|
||||||
printf("--------------------------------------------------------------------"
|
|
||||||
"------\n");
|
|
||||||
for (int i = 0; i < n; i++) {
|
|
||||||
printf("| %-5s | %-3d | %-3d | %-3d | %-3d | %-3d | %-3d |\n",
|
|
||||||
processes[i].pid, processes[i].at, processes[i].bt,
|
|
||||||
processes[i].ct, processes[i].tat, processes[i].wt,
|
|
||||||
processes[i].rt);
|
|
||||||
}
|
|
||||||
printf("--------------------------------------------------------------------"
|
|
||||||
"------\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preemptive SJF
|
|
||||||
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++){
|
||||||
@ -110,13 +107,14 @@
|
|||||||
int completed = 0;
|
int completed = 0;
|
||||||
int shortest = -1;
|
int shortest = -1;
|
||||||
|
|
||||||
int *timeline = (int *)malloc((n*2)*sizeof(int));
|
int *timeline = (int *)malloc((n*100)*sizeof(int));
|
||||||
if (timeline == NULL) {
|
if (timeline == NULL) {
|
||||||
perror ("MemAlloc Error");
|
perror ("MemAlloc Error");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int timeline_index = 0;
|
int timeline_index = 0;
|
||||||
|
int last_process = -1;
|
||||||
|
|
||||||
// setting to large values to prevent issues
|
// setting to large values to prevent issues
|
||||||
while (completed != n)
|
while (completed != n)
|
||||||
@ -140,248 +138,248 @@
|
|||||||
processes[shortest].rt = current_time - processes[shortest].at;
|
processes[shortest].rt = current_time - processes[shortest].at;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(shortest != last_process) {
|
||||||
|
timeline[timeline_index++] = shortest;
|
||||||
|
last_process = shortest;
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 Response Time: %.2f\n", avg_rt);
|
|
||||||
|
|
||||||
|
|
||||||
display_gantt_chart(processes, n, timeline);
|
|
||||||
free(timeline);
|
|
||||||
}
|
|
||||||
|
|
||||||
void round_robin(Process processes[], int n, int quantum) {
|
|
||||||
|
|
||||||
for (int i = 0; i < n; i++) {
|
|
||||||
processes[i].remaining_bt = processes[i].bt;
|
|
||||||
processes[i].rt = -1;
|
|
||||||
processes[i].is_completed = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int current_time = 0;
|
|
||||||
int completed = 0;
|
|
||||||
int i = 0;
|
|
||||||
int *timeline = (int *)malloc((n * 2) * sizeof(int)); // memory for timeline
|
|
||||||
|
|
||||||
if (timeline == NULL) {
|
|
||||||
perror("Failed to allocate memory for timeline");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int timeline_index = 0;
|
|
||||||
|
|
||||||
|
|
||||||
while (completed != n) {
|
|
||||||
if (processes[i].remaining_bt > 0 && processes[i].at <= current_time) {
|
|
||||||
if (processes[i].rt == -1) {
|
|
||||||
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;
|
|
||||||
|
|
||||||
if (processes[i].remaining_bt == 0) {
|
|
||||||
completed++;
|
|
||||||
processes[i].ct = current_time;
|
|
||||||
processes[i].is_completed = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
timeline[timeline_index++] = current_time;
|
|
||||||
|
|
||||||
} else if (processes[i].at > current_time) {
|
|
||||||
current_time++; // if process hasn't arrived, time is incremented (to prevent a stall)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
i = (i + 1) % n;
|
|
||||||
if (current_time > 1000) break;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
calculate_times(processes, n);
|
|
||||||
|
|
||||||
|
|
||||||
float 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);
|
|
||||||
display_table(processes, n);
|
|
||||||
|
|
||||||
|
|
||||||
printf("\nAverage Completion Time: %.2f\n", avg_ct);
|
|
||||||
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, timeline_index);
|
||||||
free(timeline); // Free memory
|
free(timeline);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void round_robin(Process processes[], int n, int quantum) {
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
processes[i].remaining_bt = processes[i].bt;
|
||||||
|
processes[i].rt = -1;
|
||||||
|
processes[i].is_completed = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void non_preemptive_priority(Process processes[], int n) {
|
int current_time = 0;
|
||||||
for (int i = 0; i < n - 1; i++) {
|
int completed = 0;
|
||||||
for (int j = 0; j < n - i - 1; j++) {
|
int i = 0;
|
||||||
if (processes[j].at > processes[j + 1].at) {
|
int *timeline = (int *)malloc((n * 100) * sizeof(int));
|
||||||
swap(&processes[j], &processes[j + 1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int current_time = 0;
|
|
||||||
int completed = 0;
|
|
||||||
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;
|
||||||
|
int last_process = -1;
|
||||||
|
|
||||||
while (completed != n) {
|
while (completed != n) {
|
||||||
int highest_priority = -1;
|
if (processes[i].remaining_bt > 0 && processes[i].at <= current_time) {
|
||||||
int min_priority = 9999; // Large value
|
if (processes[i].rt == -1) {
|
||||||
|
processes[i].rt = current_time - processes[i].at;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(i != last_process) {
|
||||||
|
timeline[timeline_index++] = i;
|
||||||
|
last_process = i;
|
||||||
|
}
|
||||||
|
|
||||||
for (int j = 0; j < n; j++) {
|
int execute_time = (processes[i].remaining_bt > quantum) ? quantum : processes[i].remaining_bt;
|
||||||
if (processes[j].at <= current_time && processes[j].bt > 0 && processes[j].priority < min_priority) {
|
processes[i].remaining_bt -= execute_time;
|
||||||
min_priority = processes[j].priority;
|
current_time += execute_time;
|
||||||
highest_priority = j;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (highest_priority == -1) {
|
if (processes[i].remaining_bt == 0) {
|
||||||
current_time++;
|
completed++;
|
||||||
continue;
|
processes[i].ct = current_time;
|
||||||
}
|
processes[i].is_completed = 1;
|
||||||
|
}
|
||||||
|
|
||||||
if (processes[highest_priority].rt == -1) {
|
} else if (processes[i].at > current_time) {
|
||||||
processes[highest_priority].rt =
|
current_time++;
|
||||||
current_time - processes[highest_priority].at;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
current_time += processes[highest_priority].bt;
|
i = (i + 1) % n;
|
||||||
|
if (current_time > 1000) break;
|
||||||
|
}
|
||||||
|
|
||||||
processes[highest_priority].ct = current_time;
|
calculate_times(processes, n);
|
||||||
processes[highest_priority].bt = 0; // Mark completed
|
|
||||||
|
|
||||||
completed++;
|
float avg_ct, avg_tat, avg_wt, avg_rt;
|
||||||
timeline[timeline_index++] = current_time;
|
calculate_averages(processes, n, &avg_ct, &avg_tat, &avg_wt, &avg_rt);
|
||||||
}
|
|
||||||
|
|
||||||
calculate_times(processes, n);
|
printf("\nRound Robin Scheduling (Quantum = %d):\n", quantum);
|
||||||
|
display_table(processes, n);
|
||||||
|
|
||||||
float avg_ct, avg_tat, avg_wt, avg_rt;
|
printf("\nAverage Completion Time: %.2f\n", avg_ct);
|
||||||
calculate_averages(processes, n, &avg_ct, &avg_tat, &avg_wt, &avg_rt);
|
printf("Average Turnaround Time: %.2f\n", avg_tat);
|
||||||
|
printf("Average Waiting Time: %.2f\n", avg_wt);
|
||||||
|
printf("Average Response Time: %.2f\n", avg_rt);
|
||||||
|
|
||||||
printf("\nNon-Preemptive Priority Scheduling:\n");
|
display_gantt_chart(processes, n, timeline, timeline_index);
|
||||||
display_table(processes, n);
|
free(timeline);
|
||||||
|
}
|
||||||
|
|
||||||
printf("\nAverage Completion Time: %.2f\n", avg_ct);
|
void non_preemptive_priority(Process processes[], int n) {
|
||||||
printf("Average Turnaround Time: %.2f\n", avg_tat);
|
for (int i = 0; i < n - 1; i++) {
|
||||||
printf("Average Waiting Time: %.2f\n", avg_wt);
|
for (int j = 0; j < n - i - 1; j++) {
|
||||||
printf("Average Response Time: %.2f\n", avg_rt);
|
if (processes[j].at > processes[j + 1].at) {
|
||||||
|
swap(&processes[j], &processes[j + 1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
display_gantt_chart(processes, n, timeline);
|
int current_time = 0;
|
||||||
free(timeline); // Free memory
|
int completed = 0;
|
||||||
}
|
int *timeline = (int *)malloc((n * 100) * sizeof(int));
|
||||||
|
|
||||||
|
if (timeline == NULL) {
|
||||||
|
perror("Failed to allocate memory for timeline");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int timeline_index = 0;
|
||||||
int n, choice, quantum;
|
int last_process = -1;
|
||||||
|
|
||||||
printf("Enter the number of processes: ");
|
while (completed != n) {
|
||||||
scanf("%d", &n);
|
int highest_priority = -1;
|
||||||
|
int min_priority = 9999;
|
||||||
|
|
||||||
Process processes[n];
|
for (int j = 0; j < n; j++) {
|
||||||
|
if (processes[j].at <= current_time && processes[j].bt > 0 && processes[j].priority < min_priority) {
|
||||||
|
min_priority = processes[j].priority;
|
||||||
|
highest_priority = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Input process details
|
if (highest_priority == -1) {
|
||||||
for (int i = 0; i < n; i++) {
|
current_time++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (processes[highest_priority].rt == -1) {
|
||||||
|
processes[highest_priority].rt = current_time - processes[highest_priority].at;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(highest_priority != last_process) {
|
||||||
|
timeline[timeline_index++] = highest_priority;
|
||||||
|
last_process = highest_priority;
|
||||||
|
}
|
||||||
|
|
||||||
|
current_time += processes[highest_priority].bt;
|
||||||
|
processes[highest_priority].ct = current_time;
|
||||||
|
processes[highest_priority].bt = 0;
|
||||||
|
completed++;
|
||||||
|
}
|
||||||
|
|
||||||
|
calculate_times(processes, n);
|
||||||
|
|
||||||
|
float 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");
|
||||||
|
display_table(processes, n);
|
||||||
|
|
||||||
|
printf("\nAverage Completion Time: %.2f\n", avg_ct);
|
||||||
|
printf("Average Turnaround Time: %.2f\n", avg_tat);
|
||||||
|
printf("Average Waiting Time: %.2f\n", avg_wt);
|
||||||
|
printf("Average Response Time: %.2f\n", avg_rt);
|
||||||
|
|
||||||
|
display_gantt_chart(processes, n, timeline, timeline_index);
|
||||||
|
free(timeline);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int n, choice, quantum;
|
||||||
|
|
||||||
|
printf("Enter the number of processes: ");
|
||||||
|
scanf("%d", &n);
|
||||||
|
|
||||||
|
Process processes[n];
|
||||||
|
|
||||||
|
// Input process details
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display initial table
|
// Display initial table
|
||||||
printf("\nInitial Process Table:\n");
|
printf("\nInitial Process Table:\n");
|
||||||
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");
|
||||||
printf("1. Preemptive SJF\n");
|
printf("1. Preemptive SJF\n");
|
||||||
printf("2. Round Robin\n");
|
printf("2. Round Robin\n");
|
||||||
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);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
printf("Enter the time quantum: ");
|
printf("Enter the time quantum: ");
|
||||||
scanf("%d", &quantum);
|
scanf("%d", &quantum);
|
||||||
round_robin(processes, n, quantum);
|
round_robin(processes, n, quantum);
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
non_preemptive_priority(processes, n);
|
non_preemptive_priority(processes, n);
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
printf("Exiting program.\n");
|
printf("Exiting program.\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
default:
|
default:
|
||||||
printf("Invalid choice. Please try again.\n");
|
printf("Invalid choice. Please try again.\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
// Write a C program to solve the Dining-Philosophers problem.
|
134
OS/C/Week8/q1.c
Normal file
134
OS/C/Week8/q1.c
Normal file
@ -0,0 +1,134 @@
|
|||||||
|
// Develop a program to simulate banker’s algorithm. (Consider safety and resource-request algorithms)
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define MAX_PROCESSES 10
|
||||||
|
#define MAX_RESOURCES 10
|
||||||
|
|
||||||
|
int processes, resources;
|
||||||
|
int available[MAX_RESOURCES];
|
||||||
|
int maximum[MAX_PROCESSES][MAX_RESOURCES];
|
||||||
|
int allocation[MAX_PROCESSES][MAX_RESOURCES];
|
||||||
|
int need[MAX_PROCESSES][MAX_RESOURCES];
|
||||||
|
|
||||||
|
int safeSequence[MAX_PROCESSES];
|
||||||
|
|
||||||
|
void initialize() {
|
||||||
|
printf("Enter number of processes: ");
|
||||||
|
scanf("%d", &processes);
|
||||||
|
|
||||||
|
printf("Enter number of resources: ");
|
||||||
|
scanf("%d", &resources);
|
||||||
|
|
||||||
|
printf("\nEnter available resources:\n");
|
||||||
|
for(int i=0; i<resources; i++) {
|
||||||
|
scanf("%d", &available[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\nEnter maximum matrix:\n");
|
||||||
|
for(int i=0; i<processes; i++) {
|
||||||
|
for(int j=0; j<resources; j++) {
|
||||||
|
scanf("%d", &maximum[i][j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\nEnter allocation matrix:\n");
|
||||||
|
for(int i=0; i<processes; i++) {
|
||||||
|
for(int j=0; j<resources; j++) {
|
||||||
|
scanf("%d", &allocation[i][j]);
|
||||||
|
need[i][j] = maximum[i][j] - allocation[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int isSafe() {
|
||||||
|
int work[MAX_RESOURCES];
|
||||||
|
int finish[MAX_PROCESSES] = {0};
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
for(int i=0; i<resources; i++)
|
||||||
|
work[i] = available[i];
|
||||||
|
|
||||||
|
while(count < processes) {
|
||||||
|
int found = 0;
|
||||||
|
for(int p=0; p<processes; p++) {
|
||||||
|
if(!finish[p]) {
|
||||||
|
int j;
|
||||||
|
for(j=0; j<resources; j++) {
|
||||||
|
if(need[p][j] > work[j])
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(j == resources) {
|
||||||
|
for(int k=0; k<resources; k++)
|
||||||
|
work[k] += allocation[p][k];
|
||||||
|
safeSequence[count] = p;
|
||||||
|
finish[p] = 1;
|
||||||
|
count++;
|
||||||
|
found = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!found) return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void resourceRequest(int process) {
|
||||||
|
int request[MAX_RESOURCES];
|
||||||
|
|
||||||
|
printf("\nEnter resource request for process %d:\n", process);
|
||||||
|
for(int i=0; i<resources; i++) {
|
||||||
|
scanf("%d", &request[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if request is valid
|
||||||
|
for(int i=0; i<resources; i++) {
|
||||||
|
if(request[i] > need[process][i]) {
|
||||||
|
printf("Error: Request exceeds maximum claim\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(request[i] > available[i]) {
|
||||||
|
printf("Error: Resources not available\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to allocate
|
||||||
|
for(int i=0; i<resources; i++) {
|
||||||
|
available[i] -= request[i];
|
||||||
|
allocation[process][i] += request[i];
|
||||||
|
need[process][i] -= request[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
if(isSafe()) {
|
||||||
|
printf("Request granted\n");
|
||||||
|
} else {
|
||||||
|
printf("Request denied - unsafe state\n");
|
||||||
|
// Rollback
|
||||||
|
for(int i=0; i<resources; i++) {
|
||||||
|
available[i] += request[i];
|
||||||
|
allocation[process][i] -= request[i];
|
||||||
|
need[process][i] += request[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
initialize();
|
||||||
|
|
||||||
|
if(isSafe()) {
|
||||||
|
printf("\nSystem is in safe state.\nSafe sequence: ");
|
||||||
|
for(int i=0; i<processes; i++)
|
||||||
|
printf("P%d ", safeSequence[i]);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
int process;
|
||||||
|
printf("\nEnter process number (0-%d) to request resources: ", processes-1);
|
||||||
|
scanf("%d", &process);
|
||||||
|
resourceRequest(process);
|
||||||
|
} else {
|
||||||
|
printf("\nSystem is not in safe state!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user