OS Lab Programs

001. evenorodd.sh

1 2 3 4 5 6 7 8 9 10 11 #!/bin/bash echo "Enter a number" read number if((number % 2 == 0)); then echo "The $number is even" else echo "The number $number is odd" fi

002. leapyear.sh

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #!/bin/bash echo "Enter a year" read year if((year%400 == 0)); then echo "$year is a leap year" elif((year%100 == 0)); then echo "$year is a leap year" elif((year%4 == 0)); then echo "$year is a leap year" else echo "$year is not a leap year" fi

003. factorial.sh

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #!bin/bash echo "Enter a number" read num factorial=1 if((num<0)); then echo "Error: Factorial is not defined for negative numbers" elif((num == 0)); then echo "Factorial of 0 is 1" else for((i=1;i<=num;i++)); do factorial=$((factorial*i)) done echo "Factorial of $num is $factorial" fi

004. swaptwointegers.sh

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #!/bin/bash echo "Enter the first integer" read a echo "Enter the second integer" read b echo "Before swapping a = $a, b = $b" temp=$a a=$b b=$temp echo "After swapping a = $a, b = $b"

005. createprocess.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include <sys.types.h> #include <stdio.h> #include <unistd.h> int main(){ pid_t pid, child_id, parent_id; pid = getpid(); printf("Current process id is : %d\n",pid); pid = fork(); if(pid<0){ // Error fprintf(stderr,"fork failed"); return 1; } else if(pid == 0){ // Execute code for child process child_id = getpid(); parent_id=getppid(); printf("Child created from parent and id is %d\n",child_id); printf("Parent id is: %d\n",parent_id); } else{ // Execute code for parent process wait(NULL); printf("Child complete \n"); } return 0; }

006. systemcallslinux.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/stat.h> #include <dirent.h> int main(){ pid_t child_pid; int status; child_pid = fork(); // Create a child process by forking the current process if(child_pid == -1){ // If the fork failed, print an error message and exit perror("Fork failed"); exit(EXIT_FAILURE); } else if(child_pid == 0){ // Child process char *args[] = {"ls","-l",NULL}; // Arguments for the "ls" command execvp("ls",args); // Execute the "ls" command with the given arguments perror("Exec failed"); // execvp only returns if an error occurs, so print an error message and exit exit(EXIT_FAILURE); } else{ // Parent process printf("Parent process: PID = %d, Child PID = %d \n",getpid(),child_pid); // Print the process IDs of the parent and child processes wait(&status); // Wait for the child process to exit and store its status if(WIFEXITED(status)){ // Check if the child process exited normally printf("Child process exited with status %d\n",WEXITSTATUS(status)); // Print the exit status of the child process } } int fd = close(STDOUT_FILENO); // Close the standard output file descriptor if(fd == -1){ // If closing the file descriptor failed, print an error message and exit perror("Close failed"); exit(EXIT_FAILURE); } struct stat st; if(stat("example.txt",&st) == 0){ // Check if "example.txt" file exists // File exists DIR *dirp; dirp = opendir("."); // Open the current directory struct dirent *dptr; while((dptr = readdir(dirp))!=NULL){ // Read directory entries one by one printf("%s\n",dptr->d_name); // Print the name of each entry } } return 0; }

007. iosystemcalls.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> int main(){ int fd, numBytes; char buffer[100]; // Open file in read only mode fd = open ("input.txt",O_RDONLY); if(fd==-1){ perror("Failed to open the file"); exit(EXIT_FAILURE); } //Read data from the file numBytes = read(fd,buffer,sizeof(buffer)); if(numBytes == -1){ perror("Failed to read from the file"); exit(EXIT_FAILURE); } // Write the read data to the console if(write(STDOUT_FILENO,buffer,numBytes) == -1){ perror("Failed to write to console"); exit(EXIT_FAILURE); } // Close the file if(close(fd) == -1){ perror("Failed to close the file"); exit(EXIT_FAILURE); } return 0; }

008. fcfscpuscheduling.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 // Formula to study // TAT = CT - AT // WT = TAT - BT #include <stdio.h> void main() { // Declare variables int i = 0, j = 0, burst[20], arrival[20], completion[20], process[20], waitingtime[20], tat[20], n = 0, m; float avgw = 0, avgt = 0; // Prompt user for the number of processes printf("Enter the number of processes: "); scanf("%d", &n); // Input process details for (i = 0; i < n; i++) { printf("Process ID: "); scanf("%d", &process[i]); printf("Arrival time: "); scanf("%d", &arrival[i]); printf("Burst time: "); scanf("%d", &burst[i]); } // Sort processes based on arrival time using bubble sort int temp = 0; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1; j++) { if (arrival[j] > arrival[j + 1]) { // Swap arrival time temp = arrival[j]; arrival[j] = arrival[j + 1]; arrival[j + 1] = temp; // Swap burst time temp = burst[j]; burst[j] = burst[j + 1]; burst[j + 1] = temp; // Swap process ID temp = process[j]; process[j] = process[j + 1]; process[j + 1] = temp; } } } // Calculate completion time for each process completion[0] = 0; for (i = 0; i < n; i++) { completion[i + 1] = completion[i] + burst[i]; } // Calculate turnaround time and waiting time for each process for (i = 0; i < n; i++) { tat[i] = completion[i + 1] - arrival[i]; waitingtime[i] = tat[i] - burst[i]; avgw += waitingtime[i]; avgt += tat[i]; } // Calculate average waiting time and average turnaround time avgw = avgw / n; avgt = avgt / n; // Print process details and performance metrics printf("PID\tAT\tBT\tCT\tWT\tTAT\n"); for (i = 0; i < n; i++) { printf("%d\t%d\t%d\t%d\t%d\t%d\n", process[i], arrival[i], burst[i], completion[i + 1], waitingtime[i], tat[i]); } printf("Average waiting time: %f\n", avgw); printf("Average turnaround time: %f\n", avgt); }

008. fcfscpuscheduling.md

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 The formulas to know are Turn Around Time = Completion Time - Arrival Time Waiting Time = Turnaround time - Burst Time TAT = CT - AT WT = TAT - BT - First we need to start - Then inside the structure we need to declare some variables - So, since we need processes, first we ask how many processes is needed, and the value is stored in n - After entering the number of processes, we enter the details of the each process, the loop count is the number of processes we entered earlier - The details are - Process id - Arrival time - Burst time - After that we sort the procesess based on the arrival time - Now visualize a table, with all our values, - AT BT CT TAT WT - What values are missing? - CT TAT and WT - So we can calculate them one by one - First we will calculate CT - For calculating CT, we will be iterating over the processes one by one - CT value of i will be equal to CT value of the previous process + Burst Time of current process - Next we will be calculating the turnaround time and waiting time - We use the formulas for each and calculate and assign to that process - Along side also calculate the total waiting time and turnaround time, so we can calculate the averages later. - Calculate the Avg TAT and WT, and display it - FCFS CPU scheduling is now done!

009. sjfcpuscheduling.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 #include<stdio.h> void main(){ // Variable declarations int i=0,j=0,k=1,btime=0,burst[20],arrival[20],completion[20],process[20],waitingtime[20],tat[20],n=0,m,min=0; float avgw = 0, avgt = 0; // Input number of processes printf("Enter the number of processes"); scanf("%d",&n); // Input process details for(i=0;i<n;i++){ printf("Process ID\t"); scanf("%d",&process[i]); printf("Arrival time\t"); scanf("%d",&arrival[i]); printf("Burst time\t"); scanf("%d",&burst[i]); } int temp = 0; // Sort processes based on arrival time using bubble sort for(i=0;i<n-1;i++){ for(j=0;j<n-1;j++){ if(arrival[j]>arrival[j+1]){ // Swap arrival time temp = arrival[j]; arrival[j] = arrival[j+1]; arrival[j+1] = temp; // Swap burst time temp = burst[j]; burst[j] = burst[j+1]; burst[j+1] = temp; // Swap process ID temp = process[j]; process[j] = process[j+1]; process[j+1] = temp; } } } // Schedule the processes for(i=0;i<n;i++){ btime = btime+burst[i]; // Updating burst time min = burst[k]; // Initializing the min variable // It compares the burst time of each process // with the current minimum burst time (min) // to find the process with the shortest remaining burst time. for(j=k;j<n;j++){ if(btime>=arrival[j] && burst[j]<min){ // Swap arrival time temp=arrival[j]; arrival[j]=arrival[j-1]; arrival[j-1]=temp; // Swap burst time temp=burst[j]; burst[j]=burst[j-1]; burst[j-1]=temp; // Swap process ID temp=process[j]; process[j]=process[j-1]; process[j-1]=temp; } } k++; } // Calculate completion time, waiting time, and turnaround time completion[0] = arrival[0]; for(i=0;i<n;i++){ completion[i+1] = completion[i] + burst[i]; if(completion[i]<arrival[i]) completion[i] = arrival[i]; } for(i=0;i<n;i++){ tat[i]=completion[i+1]-arrival[i]; waitingtime[i]=tat[i]-burst[i]; avgw+=waitingtime[i]; avgt+=tat[i]; } avgw=avgw/n; avgt=avgt/n; // Print the results printf("pid\tAT\tBT\tCT \tWT \t TAT\n "); for(i=0;i<n;i++){ printf("%d\t%d\t%d\t%d\t%d\t%d\n",process[i],arrival[i],burst[i],completion[i+1],waitingtime[i],tat[i]); } printf("average waiting time = %f \n",avgw); printf("average turn around time = %f \n",avgt); }

009. sjfcpuscheduling.md

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 First we need to start - Then inside the structure we need to declare some variables - So, since we need processes, first we ask how many processes is needed, and the value is stored in n - After entering the number of processes, we enter the details of the each process, the loop count is the number of processes we entered earlier - The details are - Process id - Arrival time - Burst time - After that we sort the procesess based on the arrival time - Now we need to schedule the processes - We go through the processes one by one - We update the btime variable, by adding the the burst time of the current process - We set a miniumum burst time variable variable as the burst time in the kth process - the kth process is what we initialtize as the process with the minimum burst time - Now since we have all these variables we will be comparing the minimum burst time with the current burst time and it gets updated if the current process burst time is less than the specified burst time - The swapping is done if it satisfies the following conditions - the arrival time is reached - the burst time is less than the minimum - The swapping will swap out the process id, at and bt - At the end we update the k variable by one - Now since the scheduling is done - We will be calculating the remaining values like CT, TAT, WT - First we will calculate CT - For calculating CT, we will be iterating over the processes one by one - CT value of i will be equal to CT value of the previous process + Burst Time of current process - Next we will be calculating the turnaround time and waiting time - We use the formulas for each and calculate and assign to that process - Along side also calculate the total waiting time and turnaround time, so we can calculate the averages later. - Calculate the Avg TAT and WT, and display it - SJF CPU scheduling is now done!

010. prioritycpuscheduling.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 #include <stdio.h> int main(){ int burst_time[20], process[20], waiting_time[20], turnaround_time[20],priority[20]; int i,j, limit, sum = 0, position, temp; float avg_wait_time, avg_turnaround_time; // Input the number of processes printf("Enter the number of processes"); scanf("%d",&limit); // Input burst time and priority for each process printf("Enter burst time and priority for %d processes\n",limit); for(i=0; i<limit;i++){ printf("\n Process [%d]\n",i+1); printf("Process burst time"); scanf("%d",&burst_time[i]); printf("Process priority"); scanf("%d",&priority[i]); process[i] = i+1; } // Sort the processes based on priority using bubble sort for (i = 0; i < limit - 1; i++) { for (j = 0; j < limit - i - 1; j++) { if (priority[j] > priority[j + 1]) { // Swap if current element has higher priority // Swap priority temp = priority[j]; priority[j] = priority[j + 1]; priority[j + 1] = temp; // Swap burst time temp = burst_time[j]; burst_time[j] = burst_time[j + 1]; burst_time[j + 1] = temp; // Swap process ID temp = process[j]; process[j] = process[j + 1]; process[j + 1] = temp; } } } // Calculate waiting time for each process waiting_time[0] = 0; for(i=1;i<limit;i++){ waiting_time[i] = 0; for(j=0;j<i;j++){ waiting_time[i] += burst_time[j]; } sum+= waiting_time[i]; } // Calculate average waiting time avg_wait_time = (float)sum/limit; sum=0; // Print the process ID, burst time, waiting time, and turnaround time for each process printf("Process ID\t Burst time\t Waiting Time\t Turnaround Time\n"); for(i=0;i<limit;i++){ turnaround_time[i] = burst_time[i] + waiting_time[i]; sum+=turnaround_time[i]; printf("Process[%d]\t%d\t%d\t%d\n",process[i],burst_time[i],waiting_time[i],turnaround_time[i]); } // Calculate average turnaround time avg_turnaround_time = (float)sum/limit; // Print the average waiting time and average turnaround time printf("Average waiting time %2f\n",avg_wait_time); printf("Average Turnaround Time %2f \n",avg_turnaround_time); return 0; }

010.priorityscheduling.md

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 WT = TAT - BT TAT = WT + BT turnaround_time[i] = burst_time[i] + waiting_time[i]; First we need to start - Then inside the structure we need to declare some variables - So, since we need processes, first we ask how many processes is needed, and the value is stored in n - After entering the number of processes, we enter the details of the each process, the loop count is the number of processes we entered earlier - The details are - Burst time - Priority - Next we will be sorting the processes based on priority - first we will setting the position to the ith position - We assume that this position has the minimum priority - In the inner loop, each priority[j] is compared with priority[position] to find the index (position) of the process with the highest priority. If a process with higher priority is found, the 'position' variable is updated. - After the inner loop finishes, the process with the highest priority among the remaining processes (starting from i) is found. The 'position' variable contains the index of that process. - The values of priority[i], burst_time[i], and process[i] are swapped with the process of higher priority - After executing this code, the 'priority', 'burst_time', and 'process' arrays will be sorted in ascending order based on the priority of the processes. - Now we calculate the remaining - Waiting time - For waiting time we add waitingtime + burst time for each process - We also add the waiting time sum to calculate the average waiting time later - Then we calculate turnaround time - We use the formula TAT = WT + BT - Also add the sum for average calculation later -

011. roundrobincpuscheduling.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 #include <stdio.h> int main() { int i, limit, total = 0, procnum, counter = 0, time_quantum; int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], remainingTime[10]; float avg_wait_time, avg_turnaround_time; printf("\nEnter the total number of processes\n"); scanf("%d", &limit); procnum = limit; for (i = 0; i < limit; i++) { // Entering process details printf("\nEnter details of process[%d]", i + 1); printf("Arrival time:\t"); scanf("%d", &arrival_time[i]); printf("Burst time:\t"); scanf("%d", &burst_time[i]); remainingTime[i] = burst_time[i]; } printf("Enter Time Quantum\t"); scanf("%d", &time_quantum); printf("\nProcess ID\tBurst time\tTurnaround time\tWaiting time"); for (i = 0; procnum != 0;) { if (remainingTime[i] <= time_quantum && remainingTime[i] > 0) { total = total + remainingTime[i]; remainingTime[i] = 0; counter = 1; } else if (remainingTime[i] > 0) { remainingTime[i] = remainingTime[i] - time_quantum; total = total + time_quantum; } if (remainingTime[i] == 0 && counter == 1) { procnum--; printf("\nProcess[%d]\t\t%d\t\t%d\t\t%d\n", i + 1, burst_time[i], total - arrival_time[i], total - arrival_time[i] - burst_time[i]); wait_time = wait_time + total - arrival_time[i] - burst_time[i]; turnaround_time = turnaround_time + total - arrival_time[i]; counter = 0; } if (i == limit - 1) { i = 0; } else if (arrival_time[i + 1] <= total) { i++; } else { i = 0; } } avg_wait_time = wait_time * 1.0 / limit; avg_turnaround_time = turnaround_time * 1.0 / limit; printf("Average waiting time:\t%f", avg_wait_time); printf("Average turnaround time:\t%f", avg_turnaround_time); return 0; }

011. roundrobincpuscheduling.md

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 - First we need to start - Then inside the structure we need to declare some variables - So, since we need processes, first we ask how many processes is needed, and the value is stored in n - Assign a variable procnum to the number of processes, this will be used later in the program - After entering the number of processes, we enter the details of the each process, the loop count is the number of processes we entered earlier - The details are - Arrival time - Burst time We will also be assigning remainingtime[i] to the burst time, for calculating the remaining time - Since its a round robin program, we will input the time quantum - After that we will be starting a loop which goes over each processes until all processes are complete - The loop will run until all the processes have been executed - There will be a series of conditions we will be checking - Condition 1.1: If the process remaining burst time can finish within the time quantum - If the condition is true - the total execution time (`total`) is increased by `temp[i]` - `temp[i]` is set to 0 to indicate that the process has completed(Burst time is completely exausted). - The `counter` variable is set to 1 to mark that a process has finished. - Condition 1.2 Process does not complete within the time quantum and remaining burst greater than 0 - If this is true, the quantum amount will be consumed from the process, leaving behind burst - quantum left - And the total is updated with the quantum, since that much amount is consumed - Condition 2.1 This will check if the process is completed - Procnum will be decremented - The relevant info will be printed - Condition 3 1. The next `if` statement checks if `i` has reached the last process. - If true, it means that the current process is the last process in the list. In this case, `i` is set to 0 to start processing from the beginning. 2. The last `else` statement handles the case when the next process arrival time is less than or equal to the current total execution time (`total`). - If true, it means that the next process has arrived and should be processed. `i` is incremented to move to the next process. 3. If none of the above conditions are satisfied, it means that no process is available for processing at the moment. - In this case, `i` is set to 0 to start processing from the beginning.

012. fifopagereplacement.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 #include <stdio.h> #define FRAME_SIZE 3 // Function to print the frames void printFrames(int frames[], int size) { for (int i = 0; i < size; i++) { if (frames[i] == -1) { printf("- "); // Display "-" for empty frame } else { printf("%d ", frames[i]); // Display the page number in the frame } } printf("\n"); } // Function to check if a page fault occurs int isPageFault(int frames[], int size, int page) { for (int i = 0; i < size; i++) { if (frames[i] == page) { return 0; // Page found in frame, no page fault } } return 1; // Page not found in frame, page fault occurs } // Function to perform FIFO page replacement algorithm void fifoPageReplacement(int pages[], int numPages) { int frames[FRAME_SIZE]; int front = 0; // Initialize all frames as empty (-1) for (int i = 0; i < FRAME_SIZE; i++) { frames[i] = -1; } int pageFaults = 0; printf("Page Faults: \n"); // Iterate over the pages for (int i = 0; i < numPages; i++) { printf("Page %d: ", pages[i]); // Check if page fault occurs if (isPageFault(frames, FRAME_SIZE, pages[i])) { frames[front] = pages[i]; // Replace the oldest page in the frame front = (front + 1) % FRAME_SIZE; // Update the front pointer pageFaults++; printFrames(frames, FRAME_SIZE); // Print the frames after page replacement } else { printf("No page fault occurred.\n"); } } printf("\nTotal Page Faults: %d\n", pageFaults); } int main() { int pages[] = {1, 2, 3, 4, 5, 3, 1, 6, 7, 8, 7, 8, 9, 3, 9, 5}; int numPages = sizeof(pages) / sizeof(pages[0]); // Perform FIFO page replacement algorithm fifoPageReplacement(pages, numPages); return 0; }

012.fifopagereplacement.md

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 **isPageFault(frames, size, page)** - Here we iterate through the pages, if any one of the frame values equals the given page, then theres no page fault - Else theres a page fault **printFrames(frames, size)** We iterate through the frames, and we will print them, leave a hyphen if the frame value is -1 - First we define the set of pages - Then we also store the number of pages - Then we execute the function - fifopagereplacement, which takes pages and number of pages - Here first we initialize an array, called frame, and it has the size defined(SIZE = framesize) - Since the entire array doesnt have any values, we will fill it up with -1s - Now since we have got the array ready, - We will iterate over the pages mentioned in the function - We execute a function called isPageFault, which checks whether its a page fault - If its a page fault - We will place this new page into the frame - The position in the frame is determined by front variable - After assigning, we will update this front variable by front = (front+1)%FRAME_SIZE - And we also increment the pagefaults variable to keep a count of that - Once thats done we execute a function called printframes(frames, FRAME_SIZE) which prints out the new frame

013. lrupagereplacement.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 #include <stdio.h> #define FRAME_SIZE 3 // Function to print the frames void printFrames(int frames[], int size){ for(int i=0;i<size;i++){ if(frames[i] == -1){ printf("-"); } else{ printf("%d",frames[i]); } } printf("\n"); } // Function to check if a page is a page fault int isPageFault(int frames[], int size, int page){ for(int i=0;i<size;i++){ if(frames[i] == page){ return 0; // Page found in the frames, no page fault } } return 1; // Page not found in the frames, page fault } // Function to get the least recently used page from the frames int getLRUPage(int frames[], int pageIndices[], int size){ //finds the frame with the minimum page index in the pageIndices array //and returns the corresponding page value from the frames array.(This is the LRU PAGE) int lruIndex = 0; for(int i=1;i<size;i++){ if(pageIndices[i] < pageIndices[lruIndex]){ lruIndex = i; // Update the index of the least recently used page } } return frames[lruIndex]; // Return the value of the least recently used page } // Function to update the page indices after a page is accessed // Example, Suppose we access the page index 1 // Before: pageIndices: [2, 3, 1] // After: pageIndices: [3, 0, 2] // 0 Signifies the page index 1 is the most recently used void updatePageIndices(int pageIndices[], int size, int pageIndex){ for(int i=0;i<size;i++){ if(pageIndices[i] != -1){ // Checks if its an empty frame which has page index of -1 pageIndices[i]++; // Increment the page index if it is not -1, This signifies the page has been updated recently } } pageIndices[pageIndex] = 0; // Set the page index of the recently accessed page to 0 } // Function to perform LRU page replacement void lruPageReplacement(int pages[], int numPages){ int frames[FRAME_SIZE]; // Array to store the frames int pageIndices[FRAME_SIZE]; // Array to store the page indices int pageFaults = 0; // Counter for page faults // Initialize frames and page indices with -1 for(int i=0;i<FRAME_SIZE;i++){ frames[i] = -1; pageIndices[i] = -1; } printf("Page Faults\n"); for(int i=0; i<numPages;i++){ printf("Page %d:", pages[i]); if(isPageFault(frames, FRAME_SIZE, pages[i])){ // Check if current page is a page fault int lruPage = getLRUPage(frames, pageIndices, FRAME_SIZE); // If its a page fault, least recently used page is searched int pageIndex = -1; // Find the index of the least recently used page in the frames for(int j=0; j<FRAME_SIZE; j++){ if(frames[j] == lruPage){ pageIndex = j; // Store the index of the least recently used page break; } } frames[pageIndex] = pages[i]; // Replace the least recently used page with the current page updatePageIndices(pageIndices, FRAME_SIZE, pageIndex); // Update the page indices pageFaults++; // Increment the page fault count printFrames(frames, FRAME_SIZE); // Print the current state of the frames } else{ updatePageIndices(pageIndices, FRAME_SIZE, i); // Update the page indices printf("No page fault occurred\n"); } } printf("Total page faults: %d\n", pageFaults); } int main(){ int pages[] = {1, 2, 3, 4, 5, 3, 1, 6, 7, 8, 7, 8, 9, 3, 9, 5}; int numPages = sizeof(pages) / sizeof(pages[0]); lruPageReplacement(pages, numPages); return 0; }

013.lrupagereplacement.md

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 ### LRU PAGE REPLACEMENT **isPageFault(frames, size, page)** - Here we iterate through the frames, if any one of the frame values equals the given page, then theres no page fault - Else theres a page fault **getLRUPage(int frames[], int pageIndices[], int size)** - We set a variable named lruindex to 0 - Then we go through each element of page indices - We start from index 1 since lruindex is already set to 0 - Inside the loop we check if the page index of the current element is less that the lruindex - This indicates that the current index is less recently used, hence we update the lruindex to this value - By the end of the loop we will obtain the LRU index - Now we return the value corresponding to the LRU index value, which is the least recently used frame First we define the set of pages - Then we also store the number of pages - Then we execute the function - lrupagereplacement, this takes the pages and amount of pages - First we declare the frames using an array - Then we also declare page index array - We fill both these arrays with -1s - Then we iterate through the pages - **if page fault occurred (using function isPageFault)** - We will obtain the least recently used page using function(**getLRUPage**) where we provide the frames, indices, and frame size - We initialize pageIndex as -1 - We iterate through the frame to find the index of the LRU - if it matches we update the pageindex with the value - After getting the page index - We will put this new page number into the page index position in the frame - Then we will be updating the page indices after this new change by providing the page index - Increment the page fault count - Print the frames - If no page fault occured - update the page indices, with the ith position - Print out the total page faults

014. lfupagereplacement.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 #include <stdio.h> #include <limits.h> #define FRAME_SIZE 3 // Function to print the contents of the frames void printFrames(int frames[], int size) { for (int i = 0; i < size; i++) { if (frames[i] == -1) { printf("- "); } else { printf("%d ", frames[i]); } } printf("\n"); } // Function to check if a page fault occurred int isPageFault(int frames[], int size, int page) { for (int i = 0; i < size; i++) { if (frames[i] == page) { return 0; // Page found in frames, no page fault } } return 1; // Page not found in frames, page fault occurred } // Function to get the least frequently used page from the frames // Example // Frames: [1, 2, 3] // Counts: [2, 3, 1] // 1 is the least count here, so the LFU is its corresponding frame which is 3 int getLFUPage(int frames[], int counts[], int size) { int lfuIndex = 0; // Setting least frequently used index to 0 int minCount = INT_MAX; // Setting the minimum count found so far to the for (int i = 0; i < size; i++) { if (counts[i] < minCount) { minCount = counts[i]; // Sets the least count lfuIndex = i; } } return frames[lfuIndex]; // Return the least frequently used page } // Function to update the count of a page in the frames void updatePageCount(int pages[], int counts[], int size, int page) { for (int i = 0; i < size; i++) { if (pages[i] == page) { counts[i]++; // Increment the count for the page break; } } } // Function to perform LFU page replacement void lfuPageReplacement(int pages[], int numPages) { int frames[FRAME_SIZE]; // Frames to hold the pages int counts[FRAME_SIZE]; // Count of each page in the frames int pageFaults = 0; // Counter for page faults // Initialize frames and counts with default values for (int i = 0; i < FRAME_SIZE; i++) { frames[i] = -1; // -1 represents an empty frame counts[i] = 0; // Initialize count to 0 for each frame } printf("Page Faults: \n"); for (int i = 0; i < numPages; i++) { printf("Page %d: ", pages[i]); if (isPageFault(frames, FRAME_SIZE, pages[i])) { int lfuPage = getLFUPage(frames, counts, FRAME_SIZE); // Find the least frequently used page int pageIndex = -1; // Find the index of the least frequently used page in frames for (int j = 0; j < FRAME_SIZE; j++) { if (frames[j] == lfuPage) { pageIndex = j; break; } } frames[pageIndex] = pages[i]; // Replace the least frequently used page with the new page counts[pageIndex] = 1; // Reset the count for the new page pageFaults++; // Increment the page fault count printFrames(frames, FRAME_SIZE); // Print the updated frames } else { updatePageCount(frames, counts, FRAME_SIZE, pages[i]); // Update the count for the existing page printf("No page fault occurred.\n"); } } printf("\nTotal Page Faults: %d\n", pageFaults); } int main() { int pages[] = {1, 2, 3, 4, 5, 3, 1, 6, 7, 8, 7, 8, 9, 3, 9, 5}; int numPages = sizeof(pages) / sizeof(pages[0]); lfuPageReplacement(pages, numPages); // Perform LFU page replacement return 0; }

014.lfupagereplacement.md

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 ### LFU PAGE REPLACEMENT **printFrames()** Iterate through each frame and print value, if its -1 then put as hyphen **isPageFault(frames. page)** Iterate through each frame, check page = frame, then no page fault **getLFUPage** - We set the lfuindex as 0 - and mincount to highest value possible - We iterate through count and compare with mincount - and set the mincount accordingly if count < mincount - also update the lfuindex - By the end of the loop we get the lfuindex - We will return the value of frame at lfuindex **updatePageCount()** - We iterate through the pages, if the pages are matching, we update the count by 1 and break First we define the set of pages - Then we also store the number of pages - Then we execute the function - lfupagereplacement, this takes the pages and amount of pages - First we will declare the frames - Then we have another array called count, which has a count for each page in the frame - We set all the frame values to -1 - We also set all the count values to 0 - Now we will iterate over the pages - If its a page fault - We will get the LFU value - We will set the page index to -1 - We iterate through each frame - If the frame equals the LFU - the page index is noted - page index location value is then replaced with the new value - the count value is reset to 1 - page faults are incremented - The frames are printed - If its not a page fault - We update the page count - Print total page faults

015. bankersalgorithm.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 #include <stdio.h> struct Process { int allocated[10], max_required[10], remaining_need[10]; int is_executing; }; int i, j, num_processes, num_resources, new_resource_amount, process_id, process_index, sequence_index = 0; int is_safe = 0, available_resources_count = 0, execution_count = 0, waiting = 0, max_usage_exceeded = 0; struct Process processes[10]; int available_resources[10], execution_sequence[10]; void determineSafeState() { while (execution_count != num_processes) { is_safe = 0; for (i = 0; i < num_processes; i++) { if (processes[i].is_executing) { int can_execute = 1; for (j = 0; j < num_resources; j++) { if (processes[i].remaining_need[j] > available_resources[j]) { can_execute = 0; break; } } if (can_execute) { for (j = 0; j < num_resources; j++) { available_resources[j] += processes[i].allocated[j]; } processes[i].is_executing = 0; execution_sequence[sequence_index++] = i; is_safe = 1; execution_count++; } } } if (!is_safe) { printf("System is in an Unsafe State\n"); break; } } if (is_safe) { printf("\n\nSystem is in a safe state\n"); printf("Safe State Sequence:\n"); for (i = 0; i < sequence_index; i++) { printf("P[%d] ", execution_sequence[i]); } printf("\n\n"); } } void requestResource() { printf("\nRequest for new Resources"); printf("\nProcess id? "); scanf("%d", &process_id); process_index = process_id; printf("Enter new Request details "); for (i = 0; i < num_resources; i++) { scanf("%d", &new_resource_amount); if (new_resource_amount <= processes[process_index].remaining_need[i]) { if (new_resource_amount <= available_resources[i]) { available_resources[i] -= new_resource_amount; processes[process_index].allocated[i] += new_resource_amount; processes[process_index].remaining_need[i] -= new_resource_amount; } else { waiting = 1; } } else { max_usage_exceeded = 1; } } if (!max_usage_exceeded && !waiting) { determineSafeState(); } else if (max_usage_exceeded) { printf("\nProcess has exceeded its maximum usage\n"); } else { printf("\nProcess needs to wait\n"); } } int main() { printf("Enter the number of processes: "); scanf("%d", &num_processes); printf("Enter the number of resources: "); scanf("%d", &num_resources); printf("Enter the available resources of each type "); for (i = 0; i < num_resources; i++) { scanf("%d", &available_resources[i]); } printf("\n\n---Resource Details---"); for (i = 0; i < num_processes; i++) { printf("\nResources for process %d\n", i); printf("\nAllocation Matrix\n"); for (j = 0; j < num_resources; j++) { scanf("%d", &processes[i].allocated[j]); } printf("Maximum Resource Request \n"); for (j = 0; j < num_resources; j++) { scanf("%d", &processes[i].max_required[j]); } processes[i].is_executing = 1; } // Calculate remaining need for (i = 0; i < num_processes; i++) { for (j = 0; j < num_resources; j++) { processes[i].remaining_need[j] = processes[i].max_required[j] - processes[i].allocated[j]; } } // Print current details printf("\nProcess Details\n"); printf("Pid\t\tAllocation\t\tMax\t\tNeed\n"); for (i = 0; i < num_processes; i++) { printf("%d\t\t", i); for (j = 0; j < num_resources; j++) { printf("%d ", processes[i].allocated[j]); } printf("\t\t"); for (j = 0; j < num_resources; j++) { printf("%d ", processes[i].max_required[j]); } printf("\t\t"); for (j = 0; j < num_resources; j++) { printf("%d ", processes[i].remaining_need[j]); } printf("\n"); } // Determine current state in safe state determineSafeState(); int choice = 1; do { printf("Request new resource? [0/1]: "); scanf("%d", &choice); if (choice) { requestResource(); } } while (choice != 0); return 0; }

015. bankersalgorithm.md

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 ### checksafestate() - First we will check if execution count!= number of processes - We initialize is_safe variable to 0 - Then we iterate through the process - if process.is_executing - we set canexecute = 1 - Now we check if the remaining need is greater than avaiable - If so, then we set canexecute to 0 and break - After iterating then we check the process is able to execute or not - if its able to execute - We increment the avaialable resources with the allocated resources - And we set the process is_executing to 0 - Its set to safe state - Execution sequence is appended with the new process id - update the execution count - If system is not safe, then print the corresponding message - If its safe, print out the sequence ### requestResource() - First we request the process id where the request for resource is done - Then we iterate over the number of resources - We enter the new resource amount - It checks if the new resource amount is less than or equal to the remaining need of the process - if that's true - it will check if the new resource amount is less than or equal to the available resource, since we need to check if its compatible - Now we will consume the new_resources amount from available resources - Since this is consumed, the allocated value of the process is incremented with the new resources - Since some of the need is satisfied, we will be subtracting the new resource from the need - else - waiting is set to 1 - if not true - It means the new resource amount is greater than remaining need - So max_usage_exceeded is set to 1 - If we max usage is not exceeded and the process is not waiting, then we need to determine the safe state The equation used need = MAX - Allocation - First we enter the number of processes for bankers algorithm - Then we enter the number of resource types like (A B C D) - Then we enter the data for the available resoures - After that we will enter the resource details - First we enter allocation matrix - Then we enter max resource request - Then we set the process as is_executing - After entering the resource details we will be calculating the need of each process - We know that need = MAX - Allocation - After that we will print the whole table with pid, allocation, max and need - After printing we will check the safestate - After checking that we will have a prompt to check for resource request - The user can Request new resources by entering process id - it will execute requestResources() -

016. fcfsdiskscheduling.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <stdio.h> #include <stdlib.h> void fcfsDiskScheduling(int requests[], int numRequests, int head) { int totalSeekTime = 0; int current = head; printf("Sequence: "); printf("%d", current); for (int i = 0; i < numRequests; i++) { int distance = abs(current - requests[i]); totalSeekTime += distance; current = requests[i]; printf(" -> %d", current); } printf("\nTotal Seek Time: %d\n", totalSeekTime); } int main() { int requests[] = {98, 183, 37, 122, 14, 124, 65, 67}; int numRequests = sizeof(requests) / sizeof(requests[0]); int head = 53; fcfsDiskScheduling(requests, numRequests, head); return 0; }

016.fcfsdiskscheduling.md

1 2 3 4 5 6 7 8 9 10 11 12 13 14 - First we have the requests - Then we calculate the number of requests - We set the head as a number - We execute function fcfsdiskscheduling - We set the totalseektime as 0 - Then we set the current to head - We print out the head as it starts there - We iterate through each request, - We calculate the distance by finding the difference between current and the current request - Then we add this difference to the total seek time - Then we update and print the current variable - After that we print the total seek time

017. scandiskscheduling.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 #include <stdio.h> #include <stdlib.h> int main() { int ioq[20], i, n, j, ihead, temp, scan, tot; float seek = 0, avgs; // Prompt user for the number of requests printf("Enter the number of requests: "); scanf("%d", &n); // Prompt user for the initial head position printf("Enter the initial head position: "); scanf("%d", &ihead); ioq[0] = ihead; ioq[1] = 0; n += 2; // Prompt user for I/O queue requests printf("Enter the I/O queue requests:\n"); for (i = 2; i < n; i++) { scanf("%d", &ioq[i]); } // Sort the I/O queue requests for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (ioq[j] > ioq[j + 1]) { temp = ioq[j]; ioq[j] = ioq[j + 1]; ioq[j + 1] = temp; } } } ioq[n] = ioq[n - 1]; // Find the initial head position in the sorted I/O queue for (i = 0; i < n; i++) { if (ihead == ioq[i]) { scan = i; break; } } printf("\nOrder of request served:\n\n"); tot = 0; // Serve requests in the lower direction for (i = scan; i >= 0; i--) { if (i == 0) // For last element we will calculate the distance from 0 to scan +1 tot = abs(ioq[i] - ioq[scan + 1]); else tot = abs(ioq[i] - ioq[i - 1]); // if (tot < 0) // tot = -tot; printf("%d\t%d\n", ioq[i], tot); } // Serve requests in the upper direction for (i = scan + 1; i < n; i++) { tot = abs(ioq[i + 1] - ioq[i]); // if (tot < 0) // tot = -tot; printf("%d\t%d\n", ioq[i], tot); } // Calculate seek time and average seek time seek = ihead + ioq[n - 1]; avgs = seek / (n - 2); printf("\n\nTotal seek time: %.2f\n", seek); printf("Average seek time: %.2f\n\n", avgs); return 0; }

017. scandiskscheduling.md

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Moves in a single direction (either towards the innermost or outermost track) and services requests in that direction - First we calculate the amount of requests we need - Then we ask for the intitial head position - We declare a variable called ioq, for the queue of this schedule - The first element is the head, 2nd is 0, since its a scan schedule, we go to the very left - Then we enter the i/o requests one by one upto the specified number of request - After that we sort out the queue requests in ascending order - Then we find the head position among the sorted queue - And set that to the scan variable - The first `for` loop serves the requests in the lower direction of the queue. It iterates from `scan` down to 0 (inclusive). The variable `i` represents the current index being processed. - The second `for` loop serves the requests in the upper direction of the queue. It starts from `scan + 1and iterates up to `n - 1` - Then we calculate seek = ihead + ioq[n-1] - avgs = seek/(n-2)

018. cscandiskscheduling.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 #include<stdio.h> void main() { int ioq[20], i, n, j, ihead, itail, temp, scan, tot = 0; float seek = 0, avgs; // Prompt user for the number of requests printf("Enter the number of requests: "); scanf("%d", &n); ioq[0] = 0; // Prompt user for the initial head position printf("Enter the initial head position: "); scanf("%d", &ihead); ioq[1] = ihead; // Prompt user for the maximum track limit printf("Enter the maximum track limit: "); scanf("%d", &itail); ioq[2] = itail; n += 3; // Prompt user for I/O queue requests printf("Enter the I/O queue requests:\n"); for (i = 3; i < n; i++) { scanf("%d", &ioq[i]); } // Sort the I/O queue requests for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1; j++) { if (ioq[j] > ioq[j + 1]) { temp = ioq[j]; ioq[j] = ioq[j + 1]; ioq[j + 1] = temp; } } } // Find the initial head position in the sorted I/O queue for (i = 0; i < n; i++) { if (ihead == ioq[i]) { scan = i; break; } } i = scan; temp = n; printf("\nOrder of requests served:\n\n"); printf("\n"); while (i != temp) { if (i < temp - 1) { tot = abs(ioq[i + 1] - ioq[i]); seek += tot; } printf("%d-->", ioq[i]); i++; if (i == n) // Checks if the end is reached, is so, then the seek goes to the beginning { i = 0; temp = scan; seek += itail; // The distance between start and end is added since, it goes back } } avgs = seek / (n - 3); printf("\n\nTotal seek time: %.2f", seek); printf("\nAverage seek time: %.2f\n\n", avgs); }

018.cscandiskscheduling.md

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Moves in a single direction, but when it reaches the end of the disk, it quickly jumps to the other end without servicing any requests in between. - First we enter the number of requests - And we set the first element of the queue as 0 - We enter the initial head position - And put it as the ioq[1] position - Then we enter the track limit - We put this track limit as the 3rd element in the queu - Then we enter the actual i/o requests - Then we sort these requests - Then we find the position of head - Then we do a while loop, setting i as scan - We calculate the distance between i and i+1 - And add this distance to total seek - A check is made if i is in the end - If it reaches end, then the i position is set to 0 - And the upper limit temp is set to scan - Seek time is incremented with the value of the tracking limit - Average is calculated at the end dividing by the number of requests -3 - Total seek and average seek time is displayed

019. producerconsumerproblem.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 #include <stdio.h> #include <stdlib.h> int mutex = 1, full = 0, empty = 3, x = 0; int wait(int s) { return (--s); } int signal(int s) { return (++s); } void producer() { mutex = wait(mutex); full = signal(full); empty = wait(empty); x++; printf("\nProducer produces the item %d", x); mutex = signal(mutex); } void consumer() { mutex = wait(mutex); full = wait(full); empty = signal(empty); printf("\nConsumer consumes item %d", x); x--; mutex = signal(mutex); } int main() { int n; printf("\n1. PRODUCER\n2. CONSUMER\n3. EXIT\n"); while (1) { printf("\nENTER YOUR CHOICE\n"); 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; } } return 0; }

019.producerconsumerproblem.md

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 - First we have a set of options - Producer, consumer, exit - If producer is chosen - A check for mutex condition and whether empty spaces are !=0 is taken - Insider producer() function - First we decrement the mutex value to make it unavailable and aquire the lock - This is done using wait() signal - After that, since production is being done, we will increment the full variable - This is done using signal() semaphore - Since production consumes empty space we will decrement empty - This is done by using the wait() signal - Now we actually do the production by incrementing x variable - Since the operation is complete, we will now disable the mutex lock by incrementing the mutex value to 1 - If the check fails, then we know that either the lock is aquired or the memory is being completely used - We will print that, the buffer is full - If consumer is chosen - We will check for mutex lock(mutex == 1) and full variable is not equal to 0 (To ensure that there will be something to consume) - Inside consumer() function - First we aquire the mutex lock by doing wait() on mutex to make it 0 - Then, since the full value is being consumed, we will be decrementing the full variable, so we will be doing a wait() operation - Since the amount of empty space will increase since consumption is happening, we will be doing signal() function to decrement the operation - We will now consume by decrementing x variable - Now we remove the mutex lock by doing signal() operation and making the mutex value 1 - Else - We have to display that the buffer is empty

020.Firstfit.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include<stdio.h> void main() { int bsize[10],psize[10],bno,pno,flags[10],allocation[10],i,j; for(i=0;i<10;i++) { flags[i]=0; allocation[i]=-1; } printf("enter no. of blocks "); scanf("%d",&bno); printf("Enter size of each block "); for(i=0;i<bno;i++) { scanf("%d",&bsize[i]); } printf("Enter no of processes"); scanf("%d",&pno); printf("Enter size of each processes"); for(i=0;i<pno;i++) scanf("%d",&psize[i]); for(i=0;i<pno;i++) { for(j=0;j<bno;j++) { if(bsize[j]>=psize[i]) { allocation[i]=j; //allocates block j to p[i] process bsize[j]-=psize[i]; //reduce available mem in this block break; } } } printf("\nProcess No.\tProcess Size\tBlock no\n"); for(i=0;i<pno;i++) { printf("%d\t\t%d\t\t",i+1,psize[i]); if(allocation[i]!=-1) printf("%d\n",allocation[i]+1); else printf("Not Allocated\n"); } }

020.Firstfit.md

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 - First we iterate 10 times - We intialize the flags to 0 - We intialize the allocation to -1 - Then we enter the number of blocks that we need - After that we enter the size of each block - We iterate bno number of times and read bsize for each block - Then we enter the number of processes - Then we enter the size of each processes - Then we do a set of for loops - First we iterate through the processes - Then we iterate through each block of the process - If the block size is greater than or equal to process size - We will allocate j block to ith process - Since this block is allocated, now we minus the process size from the block size - Then we print out the Process number , size, and block no - Print only if the allocation is not -1 - Print the number, allocation[i]

021.Bestfit.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include <stdio.h> #include <limits.h> int main() { int blockSizes[20], processSizes[20], i, j, numBlocks, numProcesses, temp, lowest = INT_MAX; static int allocatedBlocks[20], allocatedProcesses[20]; printf("Enter number of blocks: "); scanf("%d", &numBlocks); printf("Enter number of processes: "); scanf("%d", &numProcesses); printf("Enter size of each block:\n"); for (i = 0; i < numBlocks; i++) { printf("Block %d: ", i + 1); scanf("%d", &blockSizes[i]); } printf("Enter the size of the processes:\n"); for (i = 0; i < numProcesses; i++) { printf("Process %d: ", i + 1); scanf("%d", &processSizes[i]); } for (i = 0; i < numProcesses; i++) { for (j = 0; j < numBlocks; j++) { if (allocatedBlocks[j] == 0) { temp = blockSizes[j] - processSizes[i]; if (temp >= 0 && lowest > temp) { allocatedProcesses[i] = j; lowest = temp; } } } allocatedBlocks[allocatedProcesses[i]] = 1; lowest = INT_MAX; } printf("\nProcess no\tProcess size\tBlock no\tBlock size\t"); for (i = 0; i < numProcesses; i++) printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i + 1, processSizes[i], allocatedProcesses[i] + 1, blockSizes[allocatedProcesses[i]]); return 0; }

021.Bestfit.md

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 - First we enter the number of blocks - Then we enter the number of processes - Then we iterate through the blocks to enter the size of each block - Then we enter the size of each processes - We declare allocatedblocks array as an array of 0s to mark allocation of block - Then we start a series of for loops - First we iterate through the processes - Then we iterate through the blocks of each process - If the block is unnoccupied (barray is 0) - Remaining space in the block is calculated - It checks if the difference is greater than 0 and if the difference is less than the least value - This check will ensure the difference is the minimum which ensures best fit - After evaluating all blocks for the current process, the program marks the block that has been selected as the best fit as "allocated" by setting the corresponding index in the `allocatedBlocks` array to 1. - Print the process number, process size, block no, block size

022.Worstfit.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 #include <stdio.h> int main() { int blockSizes[20], processSizes[20]; int blockAllocation[20], numBlocks, numProcesses, i, j; printf("Enter the number of Blocks: "); scanf("%d", &numBlocks); printf("Enter size of each block: "); for (i = 0; i < numBlocks; i++) scanf("%d", &blockSizes[i]); printf("Enter number of processes: "); scanf("%d", &numProcesses); printf("Enter size of each process: "); for (i = 0; i < numProcesses; i++) { scanf("%d", &processSizes[i]); blockAllocation[i] = -1; } for (i = 0; i < numProcesses; i++) { int worstBlockIndex = -1; for (j = 0; j < numBlocks; j++) { if (blockSizes[j] >= processSizes[i]) { if (worstBlockIndex == -1) worstBlockIndex = j; else if (blockSizes[worstBlockIndex] < blockSizes[j]) worstBlockIndex = j; } } if (worstBlockIndex != -1) { blockAllocation[i] = worstBlockIndex; blockSizes[worstBlockIndex] -= processSizes[i]; } } printf("\nProcess No.\tProcess Size\tBlock no.\n"); for (i = 0; i < numProcesses; i++) { printf("%d\t\t%d\t\t", i + 1, processSizes[i]); if (blockAllocation[i] != -1) printf("%d\n", blockAllocation[i] + 1); else printf("Not Allocated\n"); } return 0; }

022.Worstfit.md

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 - Enter the number of blocks - Enter size of each blocks - Enter the number of processes - Enter size of each processes - Also assign block allocation to -1 indicate that the block is not being allocated - Then we iterate through each process - We set the index of the worst block as -1 - We iterate through each block in a process - We do a check if the given process is greater than or equal to the process size - If its true, then we will check if the worst block index is -1 - if its true, then the current block will become the worst block - Else we compare the worst block to the current block - if the current block is larger, it will be set as the worst block - We get the worst block in the end - the worst block will be allocated to allocation matrix - Block size of the worst index will be minused with the process size - We will print process number, process size, Block number

023.IPCShared1.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include <sys/types.h> // for key_t #include <sys/ipc.h> // For IPC #include <sys/shm.h> //Creating attaching shared memory #include <stdio.h> #include <stdlib.h> // Exit and memory allocation functions #include <unistd.h> // Include this header for the sleep function #define SHMSZ 27 int main() { char c; int shmid; key_t key; char *shm, *s; key = 5678; // Key used to identify the shared memory segment // Create a new shared memory segment or attach to an existing one if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) { // 0666 permission mode perror("shmget"); exit(1); } // Attach the shared memory segment to the process's address space if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } s = shm; // Set 's' pointer to point to the beginning of the shared memory // Write the letters from 'a' to 'z' into the shared memory segment for (c = 'a'; c <= 'z'; c++) *s++ = c; *s = '\0'; // Add a null terminator to mark the end of the string // Loop until the character in the shared memory becomes '*' while (*shm != '*') sleep(1); // Sleep for 1 second before checking again // Detach from the shared memory segment and exit the program shmdt(shm); exit(0); }

024.IPCShared2.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> #include <stdlib.h> #define SHMSZ 27 // Shared memory size int main() { int shmid; key_t key; char *shm, *s; key = 5678; // Key used to identify the shared memory segment // Get the identifier of an existing shared memory segment if ((shmid = shmget(key, SHMSZ, 0666)) < 0) { perror("shmget"); exit(1); } // Attach the shared memory segment to the process's address space if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } // Loop through the shared memory and print its contents for (s = shm; *s != '\0'; s++) putchar(*s); putchar('\n'); // Print a newline character to separate output // Change the first character in shared memory to '*' *shm = '*'; // Detach from the shared memory segment and exit the program shmdt(shm); exit(0); }

025.IPCMessageSend copy.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <stdio.h> #include <stdlib.h> #define MSGSZ 128 typedef struct msgbuf{ long mtype; char mtext[MSGSZ]; } message_buf; int main(){ int msqid; int msgflg = IPC_CREAT | 0666; key_t key; message_buf send_buffer; size_t msg_length; key = 1234; if((msqid = msgget(key,msgflg))<0){ perror("msgget"); exit(1); } // Setting up the message send_buffer.mtype = 1; strcpy(send_buffer.mtext,"Did you get this?"); msg_length = strlen(send_buffer.mtext) +1; if(msgsnd(msqid,&send_buffer,msg_length,IPC_NOWAIT)<0){ perror("msgsend"); } exit(0) }

025.IPCMessageSend.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <stdio.h> #include <string.h> #define MSGSZ 128 // Define a structure for the message typedef struct msgbuf { long mtype; // Message type char mtext[MSGSZ]; // Message text } message_buf; int main() { int msqid; // Message queue ID int msgflg = IPC_CREAT | 0666; // Flags for message queue creation key_t key; // Key for the message queue message_buf send_buffer; // Message buffer for sending size_t msg_length; // Length of the message key = 1234; // Key for the message queue // Create or access an existing message queue if ((msqid = msgget(key, msgflg)) < 0) { perror("msgget"); exit(1); } send_buffer.mtype = 1; // Set the message type strcpy(send_buffer.mtext, "Did you get this?"); // Set the message text msg_length = strlen(send_buffer.mtext) + 1; // Calculate the message length // Send the message to the message queue if (msgsnd(msqid, &send_buffer, msg_length, IPC_NOWAIT) < 0) { printf("%d, %ld, %s, %ld\n", msqid, send_buffer.mtype, send_buffer.mtext, msg_length); perror("msgsnd"); exit(1); } printf("Message: \"%s\" Sent\n", send_buffer.mtext); // Confirm the message sent exit(0); }

026.IPCMessageRev copy.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <stdio.h> #include <stdlib.h> #define MSGSZ 128 typedef struct msg_buffer{ long mtype; char mtext[MSGSZ]; } message_buf; int main(){ int msqid; key_t key; message_buf receive_buffer; key = 1234; msqid = msgget(key, 0666); msgrcv(msqid,&receive_buffer, MSGSZ,1,0); }

026.IPCMessageRev.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <stdio.h> #include <stdlib.h> #define MSGSZ 128 // Define a structure for the message typedef struct msgbuf { long mtype; // Message type char mtext[MSGSZ]; // Message text } message_buf; int main() { int msqid; // Message queue ID key_t key; // Key for the message queue message_buf receive_buffer; // Message buffer for receiving key = 1234; // Key for the message queue // Access the existing message queue if ((msqid = msgget(key, 0666)) < 0) { perror("msgget"); exit(1); } // Receive a message from the queue if (msgrcv(msqid, &receive_buffer, MSGSZ, 1, 0) < 0) { perror("msgrcv"); exit(1); } printf("%s\n", receive_buffer.mtext); // Print the received message exit(0); }

027.IPCPipes copy.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <stdio.h> int main(){ int fd[2]; pipe(fd); int child; child = fork(); char input_string[10]; printf("Enter the input string"); scanf("%s",&input_string); if(!child){ close(fd[0]); write(fd[1],input_string,5); wait(0); } else{ // Read close(fd[1]); read(fd[0], input_string, 5); printf("The string receive from the pipe is %s",input_string); } }

027.IPCPipes.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include <stdio.h> int main() { int fd[2]; // File descriptors for the pipe // fd[0] is the read end and fd[1] is the write end of the pipe int child; // Child process indicator char input_string[10]; // Input string to be sent through the pipe printf("\nEnter the string to be sent through the pipe: "); scanf("%s", input_string); // Create a pipe pipe(fd); // Fork a child process child = fork(); if (!child) { // Child process close(fd[0]); // Close the read end of the pipe write(fd[1], input_string, 5); // Write the input string to the pipe, 5 is the number of bytes wait(0); // Wait for the child process to complete } else { // Parent process close(fd[1]); // Close the write end of the pipe read(fd[0], input_string, 5); // Read data from the pipe printf("\nThe string retrieved from the pipe is: %s\n", input_string); } return 0; }

a.out

1 2 3 4 5 6 ELF>`@8@8 @@@@��00]] LLx-x=x=���-==��88800hhhDDS�td88800P�tdl l l 44Q�tdR�tdx-x=x=��/lib64/ld-linux-x86-64.so.2 GNU���GNU�q��Inl���E��b�I�Z&2GNU��e�m� Qb-3� 8� LG"__cxa_finalizereadwrite__libc_start_mainclosepipe__isoc99_scanfforkwait__stack_chk_failprintflibc.so.6GLIBC_2.7GLIBC_2.4GLIBC_2.2.5GLIBC_2.34_ITM_deregisterTMCloneTable__gmon_start___ITM_registerTMCloneTableiii sii }ui �����x=@�=@@�?�?�? �? �?�?�?�?�?�?�?�? �? �? ��H��H��/H��t��H����5Z/��%[/��h���������h���������h���������h��������h��������h��������h��������h��q������h��a��������%-/D����%.D����%.D����%.D����%.D����%.D����%.D����%.D����%}.D����%u.D��1�I��^H��H���PTE1�1�H�=��S.�f.�H�=y.H�r.H9�tH�6.H��t �����H�=I.H�5B.H)�H��H��?H��H�H��tH�.H��t��fD�����=.u+UH�=-H��t H�=-�����d�����-]������w�����UH��H�� dH�%(H�E�1�H�� H�Ǹ�x���H�E�H��H�� H�Ǹ����H�E�H�Ǹ�l��������E��}�u;�E�Ǹ�?����E�H�M�H�ΉǸ��������U����E�E�Ǹ�����E�H�M�H�ΉǸ� ���H�E�H��H� H�Ǹ�����H�U�dH+%(t��������H��H��� Enter the string to be sent through the pipe: %s The string retrieved from the pipe is: %s ;0����dT����d��������L�����zRx �����&D$4H����FJ w�?:*3$"\����t���������E�C � @i  Px=�=���o�@� � x?�X�� ���o���oH���o���o$���o�=0@P`p����@GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0�� � ��� �3I@U�=|@�x=������H!����=�l �x?� � @/A@HPNi|���@� �@� �@�`&�@�I�@# =N"i oScrt1.o__abi_tagcrtstuff.cderegister_tm_clones__do_global_dtors_auxcompleted.0__do_global_dtors_aux_fini_array_entryframe_dummy__frame_dummy_init_array_entry027.IPCPipes.c__FRAME_END___DYNAMIC__GNU_EH_FRAME_HDR_GLOBAL_OFFSET_TABLE___libc_start_main@GLIBC_2.34_ITM_deregisterTMCloneTablewrite@GLIBC_2.2.5_edata_fini__stack_chk_fail@GLIBC_2.4printf@GLIBC_2.2.5close@GLIBC_2.2.5pipe@GLIBC_2.2.5read@GLIBC_2.2.5__data_start__gmon_start____dso_handle_IO_stdin_used_end__bss_startmain__isoc99_scanf@GLIBC_2.7__TMC_END___ITM_registerTMCloneTablewait@GLIBC_2.2.5__cxa_finalize@GLIBC_2.2.5_initfork@GLIBC_2.2.5.symtab.strtab.shstrtab.interp.note.gnu.property.note.gnu.build-id.note.ABI-tag.gnu.hash.dynsym.dynstr.gnu.version.gnu.version_r.rela.dyn.rela.plt.init.plt.got.plt.sec.text.fini.rodata.eh_frame_hdr.eh_frame.init_array.fini_array.dynamic.data.bss.comment#8806hh$I�� W���o��$a ��hi@@�q���o$$~���oHHP�����BXX���  ���������``��PP � l�l l 4�� � ��x=x-��=-��=-��x?x/�@0 @000+@0  `4��6