diff --git a/Operating Systems/CPU Scheduling Algorithms/FirstComeFirstServe.c b/Operating Systems/CPU Scheduling Algorithms/FirstComeFirstServe.c new file mode 100644 index 00000000..762d3bff --- /dev/null +++ b/Operating Systems/CPU Scheduling Algorithms/FirstComeFirstServe.c @@ -0,0 +1,39 @@ +#include +int main() +{ + char pn[10][10]; + int arr[10], bur[10], star[10], finish[10], tat[10], wt[10], i, n; + int totwt=0, tottat=0; + printf("Enter the number of processes: "); + scanf("%d",&n); + for(i=0;i +#include +#define MAX_PROCESSES 10 +typedef struct +{ + int pid; // process ID + int burst_time; // burst time (in milliseconds) + int priority; // priority (higher number means higher priority) + int waiting_time; // waiting time (in milliseconds) + int turnaround_time; // turnaround time (in milliseconds) +} Process; +int main() +{ + Process processes [MAX_PROCESSES]; + int num_processes, i, j; + printf("Enter the number of processes: "); + scanf("%d", &num_processes); + for (i = 0; i < num_processes; i++) { + printf("\nEnter the burst time for process %d (in milliseconds): ", i+1); + scanf("%d", &processes[i].burst_time); + printf("Enter the priority for process %d (higher number means higher priority): ", i+1); + scanf("%d", &processes[i].priority); + processes[i].pid = i+1; + } + for (i = 0; i < num_processes - 1; i++) + { + for (j = i+1; j < num_processes; j++) + { + if (processes[i].priority < processes[j].priority) + { + Process temp = processes[i]; + processes[i] = processes[j]; + processes[j] = temp; + } + } + } + int total_waiting_time = 0, total_turnaround_time = 0; + for (i = 0; i < num_processes; i++) + { + if (i == 0) + { + processes[i].waiting_time = 0; + } + else + { + processes[i].waiting_time = processes[i-1].turnaround_time; + } + processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time; + total_waiting_time += processes[i].waiting_time; + total_turnaround_time += processes[i].turnaround_time; + } + printf("\nPID\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n"); + for (i = 0; i < num_processes; i++) + { + printf("%d\t%d ms\t\t%d\t\t%d ms\t\t%d ms\n", processes[i].pid, processes[i].burst_time, processes[i].priority, processes[i].waiting_time, processes[i].turnaround_time); + } + printf("\nAverage waiting time: %.2f ms", (float)total_waiting_time/num_processes); + printf("\nAverage turnaround time: %.2f ms\n", + (float)total_turnaround_time/num_processes); + return 0; +} \ No newline at end of file diff --git a/Operating Systems/CPU Scheduling Algorithms/RoundRobin.c b/Operating Systems/CPU Scheduling Algorithms/RoundRobin.c new file mode 100644 index 00000000..5e35fb36 --- /dev/null +++ b/Operating Systems/CPU Scheduling Algorithms/RoundRobin.c @@ -0,0 +1,57 @@ +#include +struct process { + int burst, wait, comp, f; +} p[20]={0,0}; +int main( ) { + int n, i, j, totalwait=0, totalturn=0, quantum,flag=1, time=0; + printf("\nEnter the no. of processes: "); + scanf("%d",&n); + printf("\nEnter the Quantum time (in ms) : "); + scanf("%d",&quantum); + for(i=0;iquantum) + { + p[i].comp+=quantum; + } + else + { + p[i].wait=time-p[i].comp; + j=p[i].burst-p[i].comp; + p[i].comp=p[i].burst; + p[i].f=0; + } + printf("\nProcess # %-3d %-10d %-10d %-10d", i+1, time, + time+j, p[i].burst-p[i].comp); + time+=j; + } + } + } + printf("\n\n--------------------"); + printf("\nProcess \t Waiting Time TurnAround Time "); + for(i=0;i +#include +int main() +{ + int i=0, pno[10], bt[10], n, wt[10], temp=0, j, tt[10]; + float sum,at; + printf("\nEnter the no. of process: "); + scanf("\n %d",&n); + printf("\nEnter the burst time of each process: \n"); + for(i=0;ibt[j]) + { + temp=bt[i]; + bt[i]=bt[j]; + bt[j]=temp; + temp=pno[i]; + pno[i]=pno[j]; + pno[j]=temp; + } + } + } + wt[0]=0; + for(i=1;i +void main() { + int a[20], p[20], i, j, n, m, temp, b[20], temp1, temp2, c[20]; + printf("\nMEMORY MANAGEMENT SCHEME - BEST FIT \n"); + printf("Enter No. of Blocks: "); + scanf("%d",&n); + for(i=0;ia[j]) + { + temp=a[i]; + a[i]=a[j]; + a[j]=temp; + } + if(p[i]>p[j]) + { + temp=p[i]; + p[i]=p[j]; + p[j]=temp; + } + } + } + printf("\nProcess\tBlock Size\n"); + for(i=0;i +void main() +{ + int a[20], p[20], i, j, n, m, temp, b[20], temp1, temp2, c[20]; + printf("\nMEMORY MANAGEMENT SCHEME - FIRST FIT \n"); + printf("Enter No. of Blocks: "); + scanf("%d",&n); + for(i=0;i +void main() +{ + int a[20], p[20], i, j, n, m, temp, b[20], temp1, temp2, c[20]; + printf("\nMEMORY MANAGEMENT SCHEME - WORST FIT \n"); + printf("Enter No. of Blocks: "); + scanf("%d",&n); + for(i=0;ia[j]) + { + temp=a[i]; + a[i]=a[j]; + a[j]=temp; + } + if(p[i] +void main() { + int a[5], b[20], n, p=0, q=0, m=0, h, k, i, q1=1; + char f='F'; + printf("Enter the Number of Pages: "); + scanf("%d",&n); + printf("Enter %d Page Numbers: ",n); + for(i=0;i=3) + q=0; + a[q]=b[i]; q++; + if(q1<3) { + q1=q; + } + } + printf("\n%d",b[i]); + printf("\t"); + for(h=0;h%c",f); + m++; + } + p=0; + for(k=0;k +int main() +{ + int a[5], b[20], p=0, q=0, m=0, h, k, i, q1=1, j, u, n; + char f='F'; + printf("Enter the number of pages: "); + scanf("%d",&n); + printf("Enter %d Page Numbers: ",n); + for(i=0;i=3) + q=0; + a[q]=b[i]; + q++; + if(q1<3) + q1=q; + } + printf("\n%d",b[i]); + printf("\t"); + for(h=0;h%c",f); + m++; + } + p=0; + if(q1==3) + { + for(k=0;k=(i-1)&&(k>=0)) + { + if(b[k]==a[j]) + u++; + k--; + } + if(u==0) + q=j; + } + } + else + { + for(k=0;k +#include +#define MAX_PAGES 100 +#define MAX_FRAMES 10 +int main() +{ + int pages[MAX_PAGES], frames[MAX_FRAMES]; + int num_pages, num_frames, num_hits = 0, num_misses = 0; + int i, j, k, pos, max_pos, max_dist; + printf("Enter the number of pages: "); + scanf("%d", &num_pages); + printf("Enter the page references: "); + for (i = 0; i < num_pages; i++) { + scanf("%d", &pages[i]); + } + printf("Enter the number of frames: "); + scanf("%d", &num_frames); + for (i = 0; i < num_frames; i++) { + frames[i] = -1; + } + for (i = 0; i < num_pages; i++) { + for (j = 0; j < num_frames; j++) { + if (frames[j] == pages[i]) { + num_hits++; + break; + } + } + if (j == num_frames) + { + num_misses++; + max_pos = -1; + max_dist = -1; + for (j = 0; j < num_frames; j++) + { + pos = -1; + for (k = i+1; k < num_pages; k++) + { + if (frames[j] == pages[k]) + { + pos = k; + break; + } + } + if (pos == -1) + { + max_pos = j; + break; + } + else if (pos > max_dist) + { + max_pos = j; + max_dist = pos; + } + } + frames[max_pos] = pages[i]; + } + } + printf("Number of page hits: %d\n", num_hits); + printf("Number of page misses: %d\n", num_misses); + printf("Hit ratio: %.2f\n", (float)num_hits/num_pages); + printf("Miss ratio: %.2f\n", (float)num_misses/num_pages); + return 0; +} \ No newline at end of file