updated week7, week8

This commit is contained in:
hello 2025-02-20 10:16:27 +05:30
parent 31ebd76c78
commit d53e55ce7c
4 changed files with 395 additions and 262 deletions

BIN
OS/C/Week6/menu Executable file

Binary file not shown.

View File

@ -53,19 +53,16 @@
} }
// 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");
for (int i = 0; i < n; i++) {
printf("%-3s", processes[i].pid);
} }
printf("\n-----------------------------------------------------------\n"); printf("\n-----------------------------------------------------------\n");
} }
@ -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,17 +138,19 @@
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);
@ -169,7 +169,7 @@
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(timeline);
} }
@ -185,7 +185,7 @@
int current_time = 0; int current_time = 0;
int completed = 0; int completed = 0;
int i = 0; int i = 0;
int *timeline = (int *)malloc((n * 2) * sizeof(int)); // memory for timeline int *timeline = (int *)malloc((n * 100) * sizeof(int));
if (timeline == NULL) { if (timeline == NULL) {
perror("Failed to allocate memory for timeline"); perror("Failed to allocate memory for timeline");
@ -193,7 +193,7 @@
} }
int timeline_index = 0; int timeline_index = 0;
int last_process = -1;
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) {
@ -201,7 +201,14 @@
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; if(i != last_process) {
timeline[timeline_index++] = i;
last_process = i;
}
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++;
@ -209,40 +216,31 @@
processes[i].is_completed = 1; processes[i].is_completed = 1;
} }
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++;
} }
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, timeline_index);
display_gantt_chart(processes, n, timeline); free(timeline);
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++) {
for (int j = 0; j < n - i - 1; j++) { for (int j = 0; j < n - i - 1; j++) {
@ -254,7 +252,7 @@
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 * 100) * sizeof(int));
if (timeline == NULL) { if (timeline == NULL) {
perror("Failed to allocate memory for timeline"); perror("Failed to allocate memory for timeline");
@ -262,11 +260,11 @@
} }
int timeline_index = 0; int timeline_index = 0;
int last_process = -1;
while (completed != n) { while (completed != n) {
int highest_priority = -1; int highest_priority = -1;
int min_priority = 9999; // Large value int min_priority = 9999;
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) {
@ -281,17 +279,18 @@
} }
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; }
if(highest_priority != last_process) {
timeline[timeline_index++] = highest_priority;
last_process = highest_priority;
} }
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;
completed++; completed++;
timeline[timeline_index++] = current_time;
} }
calculate_times(processes, n); calculate_times(processes, n);
@ -307,11 +306,10 @@
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);
} }
int main() { int main() {
int n, choice, quantum; int n, choice, quantum;

View File

@ -0,0 +1 @@
// Write a C program to solve the Dining-Philosophers problem.

134
OS/C/Week8/q1.c Normal file
View File

@ -0,0 +1,134 @@
// Develop a program to simulate bankers 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;
}