Anika12
unknown
plain_text
2 years ago
22 kB
4
Indexable
OS SCHEDULLING FCFS #include <stdio.h> #include <stdlib.h> #define MAX 100 typedef struct { int pid; int burst_time; int waiting_time; int turnaround_time; } Process; void print_table(Process p[], int n); void print_gantt_chart(Process p[], int n); int main() { Process p[MAX]; int i, j, n; int sum_waiting_time = 0, sum_turnaround_time; printf("Enter total number of process: "); scanf("%d", &n); printf("Enter burst time for each process:\n"); for(i=0; i<n; i++) { p[i].pid = i+1; printf("P[%d] : ", i+1); scanf("%d", &p[i].burst_time); p[i].waiting_time = p[i].turnaround_time = 0; } // calculate waiting time and turnaround time p[0].turnaround_time = p[0].burst_time; for(i=1; i<n; i++) { p[i].waiting_time = p[i-1].waiting_time + p[i-1].burst_time; p[i].turnaround_time = p[i].waiting_time + p[i].burst_time; } // calculate sum of waiting time and sum of turnaround time for(i=0; i<n; i++) { sum_waiting_time += p[i].waiting_time; sum_turnaround_time += p[i].turnaround_time; } // print table puts(""); // Empty line print_table(p, n); puts(""); // Empty Line printf("Total Waiting Time : %-2d\n", sum_waiting_time); printf("Average Waiting Time : %-2.2lf\n", (double)sum_waiting_time / (double) n); printf("Total Turnaround Time : %-2d\n", sum_turnaround_time); printf("Average Turnaround Time : %-2.2lf\n", (double)sum_turnaround_time / (double) n); // print Gantt chart puts(""); // Empty line puts(" GANTT CHART "); puts(" *********** "); print_gantt_chart(p, n); return 0; } void print_table(Process p[], int n) { int i; puts("+ + + + +"); puts("| PID | Burst Time | Waiting Time | Turnaround Time |"); puts("+ + + + +"); for(i=0; i<n; i++) { printf("| %2d | %2d | %2d | %2d |\n" , p[i].pid, p[i].burst_time, p[i].waiting_time, p[i].turnaround_time ); puts("+ + + + +"); } } void print_gantt_chart(Process p[], int n) { int i, j; // print top bar printf(" "); for(i=0; i<n; i++) { for(j=0; j<p[i].burst_time; j++) printf("--"); printf(" "); } printf("\n|"); // printing process id in the middle for(i=0; i<n; i++) { for(j=0; j<p[i].burst_time - 1; j++) printf(" "); printf("P%d", p[i].pid); for(j=0; j<p[i].burst_time - 1; j++) printf(" "); printf("|"); } printf("\n "); // printing bottom bar for(i=0; i<n; i++) { for(j=0; j<p[i].burst_time; j++) printf("--"); printf(" "); } printf("\n"); // printing the time line printf("0"); for(i=0; i<n; i++) { for(j=0; j<p[i].burst_time; j++) printf(" "); if(p[i].turnaround_time > 9) printf("\b"); // backspace : remove 1 space printf("%d", p[i].turnaround_time); } printf("\n"); } RR SCHEDULLING #include<stdio.h> struct times { int p,art,but,wtt,tat,rnt; }; void sortart(struct times a[],int pro) { int i,j; struct times temp; for(i=0;i<pro;i++) { for(j=i+1;j<pro;j++) { if(a[i].art > a[j].art) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } return; } int main() { int i,j,pro,time,remain,flag=0,ts; struct times a[100]; float avgwt=0,avgtt=0; printf("Round Robin Scheduling Algorithm\n"); printf("Note -\n1. Arrival Time of at least on process should be 0\n2. CPU should never be idle\n"); printf("Enter Number Of Processes : "); scanf("%d",&pro); remain=pro; for(i=0;i<pro;i++) { printf("Enter arrival time and Burst time for Process P%d : ",i); scanf("%d%d",&a[i].art,&a[i].but); a[i].p = i; a[i].rnt = a[i].but; } sortart(a,pro); printf("Enter Time Slice OR Quantum Number : "); scanf("%d",&ts); printf("\n***************************************\n"); printf("Gantt Chart\n"); printf("0"); for(time=0,i=0;remain!=0;) { if(a[i].rnt<=ts && a[i].rnt>0) { time = time + a[i].rnt; printf(" -> [P%d] <- %d",a[i].p,time); a[i].rnt=0; flag=1; } else if(a[i].rnt > 0) { a[i].rnt = a[i].rnt - ts; time = time + ts; printf(" -> [P%d] <- %d",a[i].p,time); } if(a[i].rnt==0 && flag==1) { remain--; a[i].tat = time-a[i].art; a[i].wtt = time-a[i].art-a[i].but; avgwt = avgwt + time-a[i].art-a[i].but; avgtt = avgtt + time-a[i].art; flag=0; } if(i==pro-1) i=0; else if(a[i+1].art <= time) i++; else i=0; } printf("\n\n"); printf("***************************************\n"); printf("Pro\tArTi\tBuTi\tTaTi\tWtTi\n"); printf("***************************************\n"); for(i=0;i<pro;i++) { printf("P%d\t%d\t%d\t%d\t%d\n",a[i].p,a[i].art,a[i].but,a[i].tat,a[i].wtt); } printf("***************************************\n"); avgwt = avgwt/pro; avgtt = avgtt/pro; printf("Average Waiting Time : %.2f\n",avgwt); printf("Average Turnaround Time : %.2f\n",avgtt); return 0; } PRIORITY SCHEDULLING #include<stdio.h> #define MAX 50 struct process { int pid; int burst_time; int priority; int waiting_time; int turnaround_time; }; void display_gantt_chart(struct process proc[], int n) { int i, j; printf("\n\nGANTT CHART\n"); printf(" ---\n"); printf("|"); for(i = 0; i < n; i++) { for(j = 0; j < proc[i].burst_time; j++) printf(" "); printf("P%d", proc[i].pid); for(j = 0; j < proc[i].burst_time; j++) printf(" "); printf("|"); } printf("\n -----\n"); printf("0"); for(i = 0; i < n; i++) { for(j = 0; j < proc[i].burst_time; j++) printf(" "); printf(" %d", proc[i].turnaround_time); for(j = 0; j < proc[i].burst_time; j++) printf(" "); } printf("\n"); } void priority_scheduling(struct process proc[], int n) { int i, j; struct process temp; float avg_waiting_time = 0.0, avg_turnaround_time = 0.0; for(i = 0; i < n; i++) { for(j = i + 1; j < n; j++) { if(proc[i].priority > proc[j].priority) { temp = proc[i]; proc[i] = proc[j]; proc[j] = temp; } } } proc[0].waiting_time = 0; proc[0].turnaround_time = proc[0].burst_time; for(i = 1; i < n; i++) { proc[i].waiting_time = proc[i-1].turnaround_time; proc[i].turnaround_time = proc[i].waiting_time + proc[i].burst_time; } printf("\nProcess ID\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n"); for(i = 0; i < n; i++) { printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", proc[i].pid, proc[i].burst_time, proc[i].priority, proc[i].waiting_time, proc[i].turnaround_time); avg_waiting_time += proc[i].waiting_time; avg_turnaround_time += proc[i].turnaround_time; } printf("\nAverage waiting time: %.2f", (avg_waiting_time/n)); printf("\nAverage turnaround time: %.2f", (avg_turnaround_time/n)); display_gantt_chart(proc, n); } int main() { int i, j, n; struct process proc[MAX], temp; printf("Enter the number of processes: "); scanf("%d", &n); for(i = 0; i < n; i++) { printf("\nEnter details of process %d:\n", (i+1)); printf("Enter process ID: "); scanf("%d", &proc[i].pid); printf("Enter burst time: "); scanf("%d", &proc[i].burst_time); printf("Enter priority: "); scanf("%d", &proc[i].priority); } priority_scheduling(proc, n); return 0; } SHORTEST JOB FIRST #include <stdio.h> #include <stdlib.h> struct Process { int pid; int burst_time; int waiting_time; int turnaround_time; }; int main() { int n, i, j, time = 0, total_waiting_time = 0, total_turnaround_time = 0, min, pos; float avg_waiting_time, avg_turnaround_time; printf("Enter the number of processes: "); scanf("%d", &n); struct Process p[n], temp; for(i = 0; i < n; i++) { printf("\nEnter the burst time of process %d: ", i+1); scanf("%d", &p[i].burst_time); p[i].pid = i+1; } // Sorting the processes in ascending order of burst time for(i = 0; i < n-1; i++) { min = p[i].burst_time; pos = i; for(j = i+1; j < n; j++) { if(min > p[j].burst_time) { min = p[j].burst_time; pos = j; } } temp = p[i]; p[i] = p[pos]; p[pos] = temp; } // Calculating waiting and turnaround time for each process for(i = 0; i < n; i++) { p[i].waiting_time = time; p[i].turnaround_time = p[i].waiting_time + p[i].burst_time; time = p[i].turnaround_time; total_waiting_time += p[i].waiting_time; total_turnaround_time += p[i].turnaround_time; } // Calculating average waiting and turnaround time avg_waiting_time = (float) total_waiting_time / n; avg_turnaround_time = (float) total_turnaround_time / n; // Displaying Gantt chart with process number, burst time and completion time printf("\nGantt Chart:\n"); printf("+"); for(i = 0; i < n; i++) { int k; for(k = 0; k < p[i].burst_time; k++) { printf("-"); } printf("+"); } printf("\n|"); for(i = 0; i < n; i++) { int k; for(k = 0; k < p[i].burst_time-1; k++) { printf(" "); } printf("P%d", p[i].pid); for(k = 0; k < p[i].burst_time-1; k++) { printf(" "); } printf("|"); } printf("\n+"); for(i = 0; i < n; i++) { int k; for(k = 0; k < p[i].burst_time; k++) { printf("-"); } printf("+"); } printf("\n0"); for(i = 0; i < n; i++) { int k; for(k = 0; k < p[i].burst_time-1; k++) { printf(" "); } printf("%d", p[i].turnaround_time); } printf("\n\n"); // Displaying process details in a table printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\n"); for(i = 0; i < n; i++) { printf("%d\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].burst_time, p[i].waiting_time, p[i].turnaround_time); } // Displaying average waiting and turnaround time printf("\nAverage Waiting Time: %.2f\n", avg_waiting_time); printf("Average Turnaround Time: %.2f\n", avg_turnaround_time); return 0; } DEADLOCK DETECTION #include <stdio.h> #define MAX_PROCESS 10 #define MAX_RESOURCE 10 int main() { int num_process, num_resource; int allocation[MAX_PROCESS][MAX_RESOURCE]; int max[MAX_PROCESS][MAX_RESOURCE]; int need[MAX_PROCESS][MAX_RESOURCE]; int available[MAX_RESOURCE]; int work[MAX_RESOURCE]; int finish[MAX_PROCESS]; int safe_seq[MAX_PROCESS]; printf("Enter number of processes: "); scanf("%d", &num_process); printf("Enter number of resources: "); scanf("%d", &num_resource); // Accept the Allocation Matrix from user printf("Enter the Allocation Matrix:\n"); for (int i = 0; i < num_process; i++) { printf("%d: ", i + 1); for (int j = 0; j < num_resource; j++) { scanf("%d", &allocation[i][j]); } } // Accept the Max Matrix from user printf("\nEnter the Max Matrix:\n"); for (int i = 0; i < num_process; i++) { printf("%d: ", i + 1); for (int j = 0; j < num_resource; j++) { scanf("%d", &max[i][j]); } } // Accept the Available Matrix from user printf("\nEnter the Available Matrix:\n"); for (int i = 0; i < num_resource; i++) { scanf("%d", &available[i]); } // Calculate the Need Matrix for (int i = 0; i < num_process; i++) { for (int j = 0; j < num_resource; j++) { need[i][j] = max[i][j] - allocation[i][j]; } } // Initialize work and finish arrays for (int i = 0; i < num_resource; i++) { work[i] = available[i]; } for (int i = 0; i < num_process; i++) { finish[i] = 0; } // Find a safe sequence, if one exists int count = 0; while (count < num_process) { int found = 0; for (int i = 0; i < num_process; i++) { if (finish[i] == 0) { int j; for (j = 0; j < num_resource; j++) { if (need[i][j] > work[j]) break; } if (j == num_resource) { for (int k = 0; k < num_resource; k++) { work[k] += allocation[i][k]; } safe_seq[count++] = i + 1; finish[i] = 1; found = 1; } } } if (found == 0) break; } // Print the results printf("\nProcess\t Max\t Allocation\t Need\n"); for (int i = 0; i < num_process; i++) { printf("%d\t", i + 1); for (int j = 0; j < num_resource; j++) { printf("%d ", max[i][j]); } printf("\t"); for (int j = 0; j < num_resource; j++) { printf("%d ", allocation[i][j]); } printf("\t"); for (int j = 0; j < num_resource; j++) { printf("%d ", need[i][j]); } printf("\n"); } if (count == num_process) { printf("\nSystem is in Safe State.\nSafe Sequence: "); for (int i = 0; i < num_process; i++) { printf("%d ", safe_seq[i]); } printf("\nNeed Matrix:\n"); for (int i = 0; i < num_process; i++) { printf("%d: ", i + 1); for (int j = 0; j < num_resource; j++) { printf("%d ", need[i][j]); } printf("\n"); } } else { printf("\nSystem is in Deadlock State.\n"); } return 0; } PRODUCER CONSUMER #include <stdio.h> #include <stdlib.h> int mutex = 1; int full = 0; int empty = 10, x = 0; void producer(){ --mutex; ++full; --empty; x++; printf("\nProducer produces" "item %d",x); ++mutex; } void consumer(){ --mutex; --full; ++empty; printf("\nConsumer consumes " "item %d", x); x--; ++mutex; } int main(){ int n, i; printf("\n1. Press 1 for Producer" "\n2. Press 2 for Consumer" "\n3. Press 3 for Exit"); #pragma omp critical for (i = 1; i > 0; i++) { printf("\nEnter your choice:"); scanf("%d", &n); switch (n) { case 1: if ((mutex == 1) && (empty != 0)) { producer(); } else { printf("Buffer is full!"); } break; case 2: if ((mutex == 1) && (full != 0)) { consumer(); } else { printf("Buffer is empty!"); } break; case 3: exit(0); break; } } } READER WRITER #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int wait(int n) { return n-1; } int signal(int n) { return n+1; } int main() { int readers; printf("Input the number of readers "); scanf("%d",&readers); int numberOfReaders = 0; int reading = 1; // more readers can go into the process while(true) { if(reading == 1) { while(numberOfReaders+1 <= readers) { printf("%d readers is inside\n",numberOfReaders); numberOfReaders = signal(numberOfReaders); printf("Writer is trying to enter\n"); } reading = 0; } else { while(numberOfReaders-1 >= 0) { printf("%d readers is leaving\n",numberOfReaders); numberOfReaders = wait(numberOfReaders); printf("Writer is trying to enter\n"); } break; } } printf("Writer has entered\n"); printf("Writer is leaving\n"); } FIRST FIT #include <stdio.h> #include <stdlib.h> #define MAX_BLOCKS 50 int main() { int num_processes, num_blocks, block_size; int i, j; printf("Enter number of processes: "); scanf("%d", &num_processes); printf("Enter number of blocks: "); scanf("%d", &num_blocks); printf("Enter size of each block: "); scanf("%d", &block_size); // Allocate memory for process table and block table int *process_table = (int *) malloc(num_processes * sizeof(int)); int *block_table = (int *) malloc(num_blocks * sizeof(int)); // Initialize block table to indicate all blocks are free for (i = 0; i < num_blocks; i++) { block_table[i] = -1; } // Assign blocks to processes using First Fit algorithm for (i = 0; i < num_processes; i++) { int process_size; printf("Enter size of process %d: ", i); scanf("%d", &process_size); // Find the first free block that can fit the process for (j = 0; j < num_blocks; j++) { if (block_table[j] == -1 && block_size >= process_size) { block_table[j] = i; process_table[i] = j; block_size -= process_size; break; } } } // Print block table printf("\nBlock Table:\n"); printf("+ + + +\n"); printf("| Block Num | Size | Process Num |\n"); printf("+ + + +\n"); for (i = 0; i < num_blocks; i++) { if (block_table[i] == -1) { printf("| %10d | %4d | Free |\n", i, block_size); } else { printf("| %10d | %4d | %10d |\n", i, block_size, block_table[i]); } } printf("+ + + +\n"); // Print free space and used space int free_space = 0; for (i = 0; i < num_blocks; i++) { if (block_table[i] == -1) { free_space += block_size; } } int used_space = num_blocks * block_size - free_space; printf("\nFree space: %d\n", free_space); printf("Used space: %d\n", used_space); // Deallocate memory free(process_table); free(block_table); return 0; } BEST FIT #include <stdio.h> #include <stdlib.h> #define MAX_BLOCKS 50 int main() { int num_processes, num_blocks, block_size; int i, j; printf("Enter number of processes: "); scanf("%d", &num_processes); printf("Enter number of blocks: "); scanf("%d", &num_blocks); printf("Enter size of each block: "); scanf("%d", &block_size); // Allocate memory for process table and block table int *process_table = (int *) malloc(num_processes * sizeof(int)); int *block_table = (int *) malloc(num_blocks * sizeof(int)); // Initialize block table to indicate all blocks are free for (i = 0; i < num_blocks; i++) { block_table[i] = -1; } // Assign blocks to processes using First Fit algorithm for (i = 0; i < num_processes; i++) { int process_size; printf("Enter size of process %d: ", i); scanf("%d", &process_size); // Find the first free block that can fit the process for (j = 0; j < num_blocks; j++) { if (block_table[j] == -1 && block_size >= process_size) { block_table[j] = i; process_table[i] = j; block_size -= process_size; break; } } } // Print block table printf("\nBlock Table:\n"); printf("+ + + +\n"); printf("| Block Num | Size | Process Num |\n"); printf("+ + + +\n"); for (i = 0; i < num_blocks; i++) { if (block_table[i] == -1) { printf("| %10d | %4d | Free |\n", i, block_size); } else { printf("| %10d | %4d | %10d |\n", i, block_size, block_table[i]); } } printf("+ + + +\n"); // Print free space and used space int free_space = 0; for (i = 0; i < num_blocks; i++) { if (block_table[i] == -1) { free_space += block_size; } } int used_space = num_blocks * block_size - free_space; printf("\nFree space: %d\n", free_space); printf("Used space: %d\n", used_space); // Deallocate memory free(process_table); free(block_table); return 0; } WORST FIT #include <stdio.h> int main() { int n, n1, i; printf("Enter the number of processes: "); scanf("%d", &n); int process[n]; printf("\nEnter the size of processes:\n"); for (i = 0; i < n; i++) { scanf("%d", &process[i]); } printf("Enter the number of memory blocks: "); scanf("%d", &n1); int blocks[n1]; printf("\nEnter the size of blocks:\n"); int total = 0; for (i = 0; i < n1; i++) { scanf("%d", &blocks[i]); total = total + blocks[i]; } int process1[n1]; int job[n1]; int frag[n1]; int check[n1]; for (i = 0; i < n1; i++) { check[i] = 0; } int j, used = 0; i = 0; while (i < n) { int max = -1, j1 = -1, k = -1, max1; for (j = 0; j < n1; j++) { max1 = blocks[j]; if (max1 >= max && check[j] == 0 && max1 >= process[i]) { max = max1; j1 = j; } else { if (check[j] == 0) { process1[j] = 0; job[j] = 0; frag[j] = blocks[j]; } } } if (k != j1) { process1[j1] = process[i]; job[j1] = i + 1; frag[j1] = blocks[j1] - process[i]; used = used + process[i]; check[j1] = 1; } i++; } printf("\nBlock Size\tProcess Size\tProcess No.\tFragmentation\n"); for (i = 0; i < n1; i++) { printf("%d\t\t%d\t\t%d\t\t%d\n", blocks[i], process1[i], job[i], frag[i]); } int free_memory = total - used; printf("\nTotal Memory Allocation: %d\n", total); printf("Memory Used: %d\n", used); printf("Free Memory: %d\n", free_memory); return 0; } PAGE REPLACEMENT FIRST IN FIRST OUT #include <stdio.h> int main() { int referenceString[10], pageFaults = 0, pageHits = 0, m, n, s, pages, frames; printf("Enter the number of pages: "); scanf("%d", &pages); printf("Enter reference string values: \n"); for (m = 0; m < pages; m++) { printf("Value No. [%d]: ", m + 1); scanf("%d", &referenceString[m]); } printf("What are the total number of frames: "); scanf("%d", &frames); int temp[frames]; for (m = 0; m < frames; m++) { temp[m] = -1; } for (m = 0; m < pages; m++) { s = 0; for (n = 0; n < frames; n++) { if (referenceString[m] == temp[n]) { s++; pageHits++; } } pageFaults++; if ((pageFaults <= frames) && (s == 0)) { temp[m] = referenceString[m]; } else if (s == 0) { temp[(pageFaults - 1) % frames] = referenceString[m]; } printf("\n"); for (n = 0; n < frames; n++) { if (temp[n] != -1) { printf("%d\t", temp[n]); } } } printf("\nTotal Page Faults: %d\n", pageFaults); printf("\nTotal Page Hits: %d\n", pageHits); return 0; } LEAST RECENTLY USED #include <stdio.h> int findLRU(int time[], int n) { int i, minimum = time[0], pos = 0; for (i = 1; i < n; ++i) { if (time[i] < minimum) { minimum = time[i]; pos = i; } } return pos; } int main() { int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i, j, pos, faults = 0, hits = 0; printf("Enter number of frames: "); scanf("%d", &no_of_frames); printf("Enter number of pages: "); scanf("%d", &no_of_pages); printf("Enter reference string: "); for (i = 0; i < no_of_pages; ++i) { scanf("%d", &pages[i]); } for (i = 0; i < no_of_frames; ++i) { frames[i] = -1; } for (i = 0; i < no_of_pages; ++i) { flag1 = flag2 = 0; for (j = 0; j < no_of_frames; ++j) { if (frames[j] == pages[i]) { counter++; time[j] = counter; flag1 = flag2 = 1; hits++; // Increment page hits counter break; } } if (flag1 == 0) { for (j = 0; j < no_of_frames; ++j) { if (frames[j] == -1) { counter++; faults++; frames[j] = pages[i]; time[j] = counter; flag2 = 1; break; } } } if (flag2 == 0) { pos = findLRU(time, no_of_frames); counter++; faults++; frames[pos] = pages[i]; time[pos] = counter; } printf("\n"); for (j = 0; j < no_of_frames; ++j) { printf("%d\t", frames[j]); } } printf("\nTotal Page Faults = %d\n", faults); printf("Total Page Hits = %d\n", hits); return 0; } OPTIMAL #include <stdio.h> #define MAX_FRAMES 5 int main() { int referenceString[20]; int frames[MAX_FRAMES] = { -1, -1, -1, -1, -1 }; int pageFaults = 0; int pageHits = 0; printf("Enter the reference string (20 numbers): "); for (int i = 0; i < 20; i++) { scanf("%d", &referenceString[i]); } printf("\nOptimal Page Replacement Algorithm\n"); for (int i = 0; i < 20; i++) { int currentPage = referenceString[i]; int pageFound = 0; int replaceIndex = -1; // Check if the current page is already in the frame for (int j = 0; j < MAX_FRAMES; j++) { if (frames[j] == currentPage) { pageFound = 1; pageHits++; break; } } // If the current page is not found in the frame if (!pageFound) { // Find the index of the page to be replaced for (int j = 0; j < MAX_FRAMES; j++) { int futureIndex = -1; for (int k = i + 1; k < 20; k++) { if (frames[j] == referenceString[k]) { futureIndex = k; break; } } if (futureIndex == -1) { replaceIndex = j; break; } else if (replaceIndex == -1 || futureIndex > replaceIndex) { replaceIndex = j; } } // Replace the page at the appropriate index frames[replaceIndex] = currentPage; pageFaults++; } // Print the current state of the frame printf("Reference: %d | Frames: ", currentPage); for (int j = 0; j < MAX_FRAMES; j++) { printf("%d ", frames[j]); } printf("\n"); } printf("\nPage Faults: %d\n", pageFaults); printf("Page Hits: %d\n", pageHits); return 0; }
Editor is loading...