FIFO PAGE #include #define MAX_PAGES 3 // Maximum number of pages memory can hold int main() { int memory[MAX_PAGES]; // Array to represent memory int pages[MAX_PAGES]; // Array to represent pages int num_pages, page_faults = 0; printf("Enter the number of pages: "); scanf("%d", &num_pages); printf("Enter the sequence of pages: "); for (int i = 0; i < num_pages; i++) scanf("%d", &pages[i]); // Initialize memory with -1 indicating empty slots for (int i = 0; i < MAX_PAGES; i++) memory[i] = -1; // Simulate FIFO page replacement int index = 0; // Index to keep track of oldest page in memory for (int i = 0; i < num_pages; i++) { int page = pages[i]; int found = 0; // Flag to check if page is already in memory // Check if page is already in memory for (int j = 0; j < MAX_PAGES; j++) { if (memory[j] == page) { found = 1; // Page found in memory break; } } // If page is not found in memory, replace the oldest page with the new one if (!found) { memory[index] = page; // Replace the oldest page with the new one index = (index + 1) % MAX_PAGES; // Update index for next replacement page_faults++; // Increase page fault count } // Print current memory state printf("Memory: "); for (int j = 0; j < MAX_PAGES; j++) { if (memory[j] == -1) printf(" - "); else printf(" %d ", memory[j]); } printf(" "); } printf("Total page faults: %d ", page_faults); return 0; } LRU #include #define MAX_PAGES 3 // Maximum number of pages memory can hold // Function to find the index of the least recently used page in memory int findLRU(int time[], int n) { int minimum = time[0], pos = 0; for (int i = 1; i < n; ++i) { if (time[i] < minimum) { minimum = time[i]; pos = i; } } return pos; } int main() { int memory[MAX_PAGES]; // Array to represent memory int pages[MAX_PAGES]; // Array to represent pages int num_pages, page_faults = 0, time = 0; printf("Enter the number of pages: "); scanf("%d", &num_pages); printf("Enter the sequence of pages: "); for (int i = 0; i < num_pages; i++) scanf("%d", &pages[i]); // Initialize memory with -1 indicating empty slots for (int i = 0; i < MAX_PAGES; i++) memory[i] = -1; // Array to keep track of the time when each page was last accessed int access_time[MAX_PAGES] = {0}; // Simulate LRU page replacement for (int i = 0; i < num_pages; i++) { int page = pages[i]; int found = 0; // Flag to check if page is already in memory // Check if page is already in memory for (int j = 0; j < MAX_PAGES; j++) { if (memory[j] == page) { found = 1; // Page found in memory access_time[j] = ++time; // Update access time break; } } // If page is not found in memory, replace the least recently used page with the new one if (!found) { int lru = findLRU(access_time, MAX_PAGES); memory[lru] = page; // Replace the least recently used page with the new one access_time[lru] = ++time; // Update access time page_faults++; // Increase page fault count } // Print current memory state printf("Memory: "); for (int j = 0; j < MAX_PAGES; j++) { if (memory[j] == -1) printf(" - "); else printf(" %d ", memory[j]); } printf(" "); } printf("Total page faults: %d ", page_faults); return 0; } CSCAN #include #include #define SIZE 100 // Function to sort the request array void sort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } // Function to implement SCAN disk scheduling algorithm void SCAN(int arr[], int head, int size) { int total_seek_operations = 0; int direction = 1; // 1 for right, -1 for left int prev_head = head; int min_track = 0; int max_track = 199; sort(arr, size); // Sort the request array int i = 0; while (i < size && (arr[i] < head)) i++; printf("Sequence of disk access: %d ", head); total_seek_operations += abs(head - prev_head); if (head > max_track / 2) { direction = -1; min_track = 0; max_track = head; } else { direction = 1; min_track = head; max_track = 199; } if (direction == 1) { // Moving towards the higher tracks for (; i < size; i++) { if (arr[i] <= max_track) { printf(" %d ", arr[i]); total_seek_operations += abs(arr[i] - prev_head); prev_head = arr[i]; } } if (max_track != 199) { printf(" %d ", max_track); total_seek_operations += abs(max_track - prev_head); prev_head = max_track; } for (i = size - 1; i >= 0; i--) { if (arr[i] >= min_track) { printf(" %d ", arr[i]); total_seek_operations += abs(arr[i] - prev_head); prev_head = arr[i]; } } } else { // Moving towards the lower tracks for (i = size - 1; i >= 0; i--) { if (arr[i] >= min_track) { printf(" %d ", arr[i]); total_seek_operations += abs(arr[i] - prev_head); prev_head = arr[i]; } } if (min_track != 0) { printf(" %d ", min_track); total_seek_operations += abs(min_track - prev_head); prev_head = min_track; } for (; i < size; i++) { if (arr[i] <= max_track) { printf(" %d ", arr[i]); total_seek_operations += abs(arr[i] - prev_head); prev_head = arr[i]; } } } printf(" Total seek operations: %d ", total_seek_operations); } int main() { int n, head, requests[SIZE]; printf("Enter the number of requests: "); scanf("%d", &n); printf("Enter the requests: "); for (int i = 0; i < n; i++) { scanf("%d", &requests[i]); } printf("Enter the initial head position: "); scanf("%d", &head); SCAN(requests, head, n); return 0; } NON-PRE EMPTIVE FCFS #include struct process { int id; int at; int bt; int wt; int tat; int ct; }p; void swap(struct process *a,struct process *b) //function to swap two process { struct process c=*a; *a=*b; *b=c; } int main() { int current_time=0; float total_tat,total_wt; int n; printf("Enter the no. of process : "); scanf("%d",&n); struct process p[n]; for(int i=0;ip[j+1].at) swap(&p[j],&p[j+1]); //bubble sort to sort process acc. to at } } for(int i=0;icurrent_time) { current_time++; } p[i].ct=current_time+p[i].bt; current_time=p[i].ct; } for(int i=0;ip[j+1].id) swap(&p[j],&p[j+1]); //bubble sort to sort process acc. to id for clear output badia badia } } printf("ID AT BT CT TAT WT "); for(int i=0;i struct process { int ex; int id; int at; int bt; int wt; int tat; int ct; }p; void swap(struct process *a,struct process *b) //function to swap two process { struct process c=*a; *a=*b; *b=c; } int main() { int current_time=0; float total_tat,total_wt; int n; printf("Enter the no. of process : "); scanf("%d",&n); struct process p[n]; for(int i=0;ip[j+1].bt) swap(&p[j],&p[j+1]); //bubble sort to sort process acc. to bt } } int completed=0; while(completedp[j+1].id) swap(&p[j],&p[j+1]); //bubble sort to sort process acc. to id for clear output badia badia } } printf("ID AT BT CT TAT WT "); for(int i=0;i struct process { int ex; int id; int at; int bt; int wt; int tat; int ct; }p; void swap(struct process *a,struct process *b) //function to swap two process { struct process c=*a; *a=*b; *b=c; } int main() { int current_time=0; float total_tat,total_wt; int n; printf("Enter the no. of process : "); scanf("%d",&n); struct process p[n]; for(int i=0;ip[j+1].id) swap(&p[j],&p[j+1]); //bubble sort to sort process acc. to id for clear output badia badia } } printf("ID AT BT CT TAT WT "); for(int i=0;i struct process { int priority; int ex; int id; int at; int bt; int wt; int tat; int ct; }p; void swap(struct process *a,struct process *b) //function to swap two process { struct process c=*a; *a=*b; *b=c; } int main() { int current_time=0; float total_tat,total_wt; int n; printf("Enter the no. of process : "); scanf("%d",&n); struct process p[n]; for(int i=0;ip[j+1].id) swap(&p[j],&p[j+1]); //bubble sort to sort process acc. to id for clear output badia badia } } printf("ID Pro AT BT CT TAT WT "); for(int i=0;i struct process { int remaining_time; int ex; int id; int at; int bt; int wt; int tat; int ct; } p[50]; void swap(struct process *a, struct process *b) { struct process c = *a; *a = *b; *b = c; } int main() { int current_time = 0; float total_tat = 0, total_wt = 0; int n; printf("Enter the number of processes: "); scanf("%d", &n); for (int i = 0; i < n; i++) { p[i].ex = 0; printf("Enter process id: "); scanf("%d", &p[i].id); printf("Enter the arrival time of process %d: ", p[i].id); scanf("%d", &p[i].at); printf("Enter the burst time of process %d: ", p[i].id); scanf("%d", &p[i].bt); p[i].remaining_time = p[i].bt; } int completed = 0; while (completed < n) { int shortest_job = -1; for (int i = 0; i < n; i++) { if(p[i].ex == 0 && p[i].at <= current_time && p[i].remaining_time > 0) { if (p[i].remaining_time < p[shortest_job].remaining_time) { shortest_job = i; } } } if (shortest_job == -1 ) current_time++; else { p[shortest_job].remaining_time--; current_time++; if (p[shortest_job].remaining_time == 0) { p[shortest_job].ct = current_time; p[shortest_job].ex = 1; completed++; } } } for (int i = 0; i < n; i++) { p[i].tat = p[i].ct - p[i].at; p[i].wt = p[i].tat - p[i].bt; } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (p[j].id > p[j + 1].id) swap(&p[j], &p[j + 1]); } } printf("ID AT BT CT TAT WT "); for (int i = 0; i < n; i++) { printf("%d %d %d %d %d %d ", p[i].id, p[i].at, p[i].bt, p[i].ct, p[i].tat, p[i].wt); total_tat += p[i].tat; total_wt += p[i].wt; } printf("AVG TAT TIME : %f ", (total_tat / (float)n)); printf("AVG WT TIME : %f ", (total_wt / (float)n)); return 0; } LRTF #include struct process { int remaining_time; int ex; int id; int at; int bt; int wt; int tat; int ct; } p[50]; void swap(struct process *a, struct process *b) { struct process c = *a; *a = *b; *b = c; } int main() { int current_time = 0; float total_tat = 0, total_wt = 0; int n; printf("Enter the number of processes: "); scanf("%d", &n); for (int i = 0; i < n; i++) { p[i].ex = 0; printf("Enter process id: "); scanf("%d", &p[i].id); printf("Enter the arrival time of process %d: ", p[i].id); scanf("%d", &p[i].at); printf("Enter the burst time of process %d: ", p[i].id); scanf("%d", &p[i].bt); p[i].remaining_time = p[i].bt; } int completed = 0; while (completed < n) { int longest_job = -1; for (int i = 0; i < n; i++) { if(p[i].ex == 0 && p[i].at <= current_time && p[i].remaining_time > 0) { if (longest_job == -1 || p[i].remaining_time > p[longest_job].remaining_time) { longest_job = i; } } } if(longest_job==-1) { current_time++; } else { p[longest_job].remaining_time--; current_time++; if (p[longest_job].remaining_time == 0) { p[longest_job].ct = current_time; p[longest_job].ex = 1; completed++; } } } for (int i = 0; i < n; i++) { p[i].tat = p[i].ct - p[i].at; p[i].wt = p[i].tat - p[i].bt; } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (p[j].id > p[j + 1].id) swap(&p[j], &p[j + 1]); } } printf("ID AT BT CT TAT WT "); for (int i = 0; i < n; i++) { printf("%d %d %d %d %d %d ", p[i].id, p[i].at, p[i].bt, p[i].ct, p[i].tat, p[i].wt); total_tat += p[i].tat; total_wt += p[i].wt; } printf("AVG TAT TIME : %f ", (total_tat / (float)n)); printf("AVG WT TIME : %f ", (total_wt / (float)n)); return 0; } Priority pre-emptive #include struct process { int remaining_time; int ex; int id; int at; int bt; int wt; int tat; int ct; int priority; } p[50]; void swap(struct process *a, struct process *b) { struct process c = *a; *a = *b; *b = c; } int main() { int current_time = 0; float total_tat = 0, total_wt = 0; int n; printf("Enter the number of processes: "); scanf("%d", &n); for (int i = 0; i < n; i++) { p[i].ex = 0; printf("Enter process id: "); scanf("%d", &p[i].id); printf("Enter the arrival time of process %d: ", p[i].id); scanf("%d", &p[i].at); printf("Enter the burst time of process %d: ", p[i].id); scanf("%d", &p[i].bt); printf("Enter the priority of process %d: ", p[i].id); scanf("%d", &p[i].priority); p[i].remaining_time = p[i].bt; } int completed = 0; while (completed < n) { int prior = -1; int highest_priority_process = -1; for (int i = 0; i < n; i++) { if (p[i].ex == 0 && p[i].at <= current_time && p[i].remaining_time > 0) { if ( prior==-1||p[i].priority p[j + 1].id) swap(&p[j], &p[j + 1]); } } printf("ID AT BT Pr CT TAT WT "); for (int i = 0; i < n; i++) { printf("%d %d %d %d %d %d %d ", p[i].id, p[i].at, p[i].bt, p[i].priority, p[i].ct, p[i].tat, p[i].wt); total_tat += p[i].tat; total_wt += p[i].wt; } printf("AVG TAT TIME : %f ", (total_tat / (float)n)); printf("AVG WT TIME : %f ", (total_wt / (float)n)); return 0; } ROUND ROBIN #include struct process { int ex, id, at, bt, wt, tat, ct, rt; }; void swap(struct process *a, struct process *b) { struct process c = *a; *a = *b; *b = c; } int main() { int current_time = 0, tq = 0; float total_tat = 0, total_wt = 0; int n; printf("Enter the number of processes: "); scanf("%d", &n); struct process p[n]; for (int i = 0; i < n; i++) { p[i].ex = 0; printf("Enter process id: "); scanf("%d", &p[i].id); printf("Enter the arrival time of process %d: ", p[i].id); scanf("%d", &p[i].at); printf("Enter the burst time of process %d: ", p[i].id); scanf("%d", &p[i].bt); p[i].rt = p[i].bt; } printf("Enter time quantum: "); scanf("%d", &tq); int rq[n]; int completed = 0; int i = 0; while (completed < n) { if (p[i].ex == 1 || p[i].at > current_time) { i = (i + 1) % n; continue; } if (p[i].rt <= tq) { current_time += p[i].rt; p[i].ct = current_time; p[i].tat = p[i].ct - p[i].at; p[i].wt = p[i].tat - p[i].bt; total_tat += p[i].tat; total_wt += p[i].wt; p[i].rt = 0; p[i].ex = 1; completed++; } else { current_time += tq; p[i].rt -= tq; } i = (i + 1) % n; } printf("ID AT BT CT TAT WT "); for (int i = 0; i < n; i++) { printf("%d %d %d %d %d %d ", p[i].id, p[i].at, p[i].bt, p[i].ct, p[i].tat, p[i].wt); } printf("AVG TAT TIME : %.2f ", (total_tat / n)); printf("AVG WT TIME : %.2f ", (total_wt / n)); return 0; } SCAN #include #include #define n 8 #define disk_size 200 void swap(int *a,int *b) { int c=*a; *a=*b; *b=c; } int main() { int dir,head,seek = 0; int left[n], right[n]; int left_count = 0, right_count = 0; int a[n]; printf("Enter the request arr "); for(int i=0;i head) right[right_count++] = a[i]; } for (int i = 0; i < left_count - 1; i++) { for (int j = 0; j < left_count - i - 1; j++) { if (left[j] >left[j + 1]) swap(&left[j],&left[j+1]); } } for (int i = 0; i < right_count - 1; i++) { for (int j = 0; j < right_count - i - 1; j++) { if (right[j] > right[j + 1]) swap(&right[j],&right[j+1]); } } int run = 2; while (run--) { if (dir==0) { for (int i = left_count-1 ;i >=0; i--) { seek+=abs(left[i]-head); head = left[i]; } dir=1; } else if (dir==1) { for (int i = 0; i < right_count; i++) { seek+=abs(right[i]-head); head = right[i]; } dir=0; } } printf("Total number of seek operations = %d ", seek); } //CSCAN #include #include #define n 8 #define disk_size 200 void swap(int *a,int *b) { int c=*a; *a=*b; *b=c; } int main() { int dir,head,seek = 0; int left[n], right[n]; int left_count = 0, right_count = 0; int a[n]; printf("Enter the request arr "); for(int i=0;i head) right[right_count++] = a[i]; } for (int i = 0; i < left_count - 1; i++) { for (int j = 0; j < left_count - i - 1; j++) { if (left[j] >left[j + 1]) swap(&left[j],&left[j+1]); } } for (int i = 0; i < right_count - 1; i++) { for (int j = 0; j < right_count - i - 1; j++) { if (right[j] > right[j + 1]) swap(&right[j],&right[j+1]); } } if (dir==0) { for (int i = left_count-1 ;i >=0; i--) { seek+=abs(left[i]-head); head = left[i]; } for (int i = right_count-1 ;i >=0; i--) { seek+=abs(right[i]-head); head = right[i]; } } else if (dir==1) { for (int i =0 ;i #include #define n 8 #define disk_size 200 void swap(int *a,int *b) { int c=*a; *a=*b; *b=c; } int main() { int dir,head,seek = 0; int left[n], right[n]; int left_count = 0, right_count = 0; int a[n]; printf("Enter the request arr "); for(int i=0;i head) right[right_count++] = a[i]; } for (int i = 0; i < left_count - 1; i++) { for (int j = 0; j < left_count - i - 1; j++) { if (left[j] >left[j + 1]) swap(&left[j],&left[j+1]); } } for (int i = 0; i < right_count - 1; i++) { for (int j = 0; j < right_count - i - 1; j++) { if (right[j] > right[j + 1]) swap(&right[j],&right[j+1]); } } int run = 2; while (run--) { if (dir==0) { for (int i = left_count-1 ;i >=0; i--) { seek+=abs(left[i]-head); head = left[i]; } dir=1; } else if (dir==1) { for (int i = 0; i < right_count; i++) { seek+=abs(right[i]-head); head = right[i]; } dir=0; } } printf("Total number of seek operations = %d ", seek); } //CLOOK #include #include #define n 7 #define disk_size 200 void swap(int *a,int *b) { int c=*a; *a=*b; *b=c; } int main() { int dir,head,seek = 0; int left[n], right[n]; int left_count = 0, right_count = 0; int a[n]; printf("Enter the request arr "); for(int i=0;i head) right[right_count++] = a[i]; } for (int i = 0; i < left_count - 1; i++) { for (int j = 0; j < left_count - i - 1; j++) { if (left[j] >left[j + 1]) swap(&left[j],&left[j+1]); } } for (int i = 0; i < right_count - 1; i++) { for (int j = 0; j < right_count - i - 1; j++) { if (right[j] > right[j + 1]) swap(&right[j],&right[j+1]); } } if (dir==0) { for (int i = left_count-1 ;i >=0; i--) { seek+=abs(left[i]-head); head = left[i]; } for(int i=right_count-1;i>=0;i--) { seek+=abs(right[i]-head); head = right[i]; } } else if (dir==1) { for (int i = 0 ;i #include #define n 5 #define disk_size 200 int main() { int head,seek = 0; int a[n]; printf("Enter the request arr "); for(int i=0;i int findPage(int page, int frames_arr[], int frames) { for (int i = 0; i < frames; i++) { if (frames_arr[i] == page) { return 1; } } return 0; } int main() { int frames, pages; printf("Enter the number of frames: "); scanf("%d", &frames); printf("Enter the number of pages: "); scanf("%d", &pages); int frames_arr[frames]; int frameindex = 0; int faults = 0; for (int i = 0; i < frames; i++) { frames_arr[i] = -1; } printf("Enter the page reference sequence: "); int rs[pages]; for (int i = 0; i // This function checks if current strea item(key) exists in any of the frames or not int search(int page, int frame_arr[], int occupied) { for (int i = 0; i < occupied; i++) if (frame_arr[i] == page) return 1; return 0; } int optimalindex(int rs[], int frame_arr[], int pages, int index, int occupied) { int result = -1, farthest = index; for (int i = 0; i farthest) { farthest = j; result = i; } break; } } if (j == pages) return i; } return (result == -1) ? 0 : result; } int main() { int rs[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2}; int pages= sizeof(rs) / sizeof(rs[0]); int frames = 4; int frame_arr[frames]; int occupied = 0; int hits = 0; for (int i = 0; i < pages; i++) { if (search(rs[i], frame_arr, occupied)) { hits++; continue; } if (occupied < frames) { frame_arr[occupied] = rs[i]; occupied++; } else { int pos = optimalindex(rs, frame_arr, pages, i + 1, occupied); frame_arr[pos] = rs[i]; } } printf(" Hits: %d ", hits); printf("Misses: %d", pages- hits); return 0; } LRU #include #include // This function checks if the current page exists in any of the frames or not int search(int page, int frame_arr[], int occupied) { for (int i = 0; i < occupied; i++) if (frame_arr[i] == page) return 1; return 0; } // Function to find the index of the page that has not been used for the longest period int lruIndex(int rs[], int frame_arr[], int pages, int index, int occupied) { int result = -1, oldest = INT_MAX; for (int i = 0; i < occupied; i++) { int j; for (j = index - 1; j >= 0; j--) { if (frame_arr[i] == rs[j]) { if (j < oldest) { oldest = j; result = i; } break; } } if (j < 0) return i; } return (result == -1) ? 0 : result; } int main() { int rs[] = {7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1}; int pages = sizeof(rs) / sizeof(rs[0]); int frames = 4; int frame_arr[frames]; int occupied = 0; int hits = 0; for (int i = 0; i < pages; i++) { if (search(rs[i], frame_arr, occupied)) { hits++; continue; } if (occupied < frames) { frame_arr[occupied] = rs[i]; occupied++; } else { int pos = lruIndex(rs, frame_arr, pages, i, occupied); frame_arr[pos] = rs[i]; } } printf(" Hits: %d ", hits); printf("Misses: %d", pages - hits); return 0; }