Operating Systems Lab - Complete Solutions 1. WAP to create four threads where each thread finds the maximum, minimum, average, and sum of an array of integers, respectively. #include <stdio.h> #include <pthread.h> int arr[] = {5, 10, 15, 20, 25}; int size = sizeof(arr) / sizeof(arr[0]); void* find_max(void* arg) { int max = arr[0]; for (int i = 1; i < size; i++) if (arr[i] > max) max = arr[i]; printf("Maximum: %d\n", max); return NULL; } void* find_min(void* arg) { int min = arr[0]; for (int i = 1; i < size; i++) if (arr[i] < min) min = arr[i]; printf("Minimum: %d\n", min); return NULL; } void* find_avg(void* arg) { int sum = 0; for (int i = 0; i < size; i++) sum += arr[i]; printf("Average: %.2f\n", sum / (float)size); return NULL; } void* find_sum(void* arg) { int sum = 0; for (int i = 0; i < size; i++) sum += arr[i]; printf("Sum: %d\n", sum); return NULL; } int main() { pthread_t t1, t2, t3, t4; pthread_create(&t1, NULL, find_max, NULL); pthread_create(&t2, NULL, find_min, NULL); pthread_create(&t3, NULL, find_avg, NULL); pthread_create(&t4, NULL, find_sum, NULL); pthread_join(t1, NULL); pthread_join(t2, NULL); pthread_join(t3, NULL); pthread_join(t4, NULL); return 0; } 2. WAP to implement a hierarchical file permission system where a parent process creates a file and specifies access rights for different child processes. Demonstrate at least three different permission scenarios. This program requires creating directories and files in Linux using `mkdir`, `chmod`, and `touch`. You can simulate different permission levels using: mkdir parent_dir cd parent_dir touch child1.txt child2.txt child3.txt chmod 755 child1.txt # Full access chmod 644 child2.txt # Read/write for owner, read-only for others chmod 400 child3.txt # Read-only for owner Then verify access via different users. 3. Write a program to create the following process hierarchy: P1 creates P2 and P3, then P2 creates P4 and P5. Verify their relationships using appropriate system calls. #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid1 = fork(); if (pid1 == 0) { printf("P2 created by P1\n"); pid_t pid2 = fork(); if (pid2 == 0) { printf("P4 created by P2\n"); } else { fork(); printf("P5 created by P2\n"); } } else { fork(); printf("P3 created by P1\n"); } return 0; } 4. WAP to merge the contents of two files into a third file. #include <stdio.h> int main() { FILE *f1 = fopen("file1.txt", "r"); FILE *f2 = fopen("file2.txt", "r"); FILE *f3 = fopen("merged.txt", "w"); char ch; while ((ch = fgetc(f1)) != EOF) fputc(ch, f3); while ((ch = fgetc(f2)) != EOF) fputc(ch, f3); fclose(f1); fclose(f2); fclose(f3); return 0; } 5. WAP to implement the dining philosophers problem using semaphores. Use semaphore and threading libraries. Each philosopher must pick left and right chopsticks using semaphores and eat. Use mutex locks or sem_t for implementation. 6. WAP to count the number of lines, words, and characters in a file using system calls and display the results in a new file. #include <stdio.h> int main() { FILE *fp = fopen("sample.txt", "r"); char ch; int lines = 0, words = 0, chars = 0; while ((ch = fgetc(fp)) != EOF) { chars++; if (ch == ' ' || ch == '\n') words++; if (ch == '\n') lines++; } fclose(fp); printf("Lines: %d\nWords: %d\nCharacters: %d\n", lines, words, chars); return 0; } 7. WAP to create a multi-threaded file search program where each thread searches for a specific word in different files within a directory. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <dirent.h> void* search_file(void* filename) { FILE *fp = fopen((char*)filename, "r"); char word[] = "target"; char line[256]; while (fgets(line, sizeof(line), fp)) { if (strstr(line, word)) printf("Found in %s: %s", (char*)filename, line); } fclose(fp); return NULL; } // Call with pthreads for each file 8. WAP that copies content from the middle of one file (from 20th to 40th character) to another file using system calls. #include <stdio.h> int main() { FILE *src = fopen("file.txt", "r"); FILE *dest = fopen("new.txt", "w"); fseek(src, 20, SEEK_SET); char ch; for (int i = 0; i < 20; i++) { ch = fgetc(src); if (ch == EOF) break; fputc(ch, dest); } fclose(src); fclose(dest); return 0; } 9. WAP to implement the producer-consumer problem using semaphores. Use pthreads and semaphores (sem_t empty, full, mutex) to implement classic producer-consumer. Use buffer[], with two threads calling produce() and consume() functions with proper locking. 10. WAP to demonstrate Zombie process. #include <stdio.h> #include <unistd.h> int main() { pid_t pid = fork(); if (pid > 0) { sleep(10); // Parent sleeps } else { printf("Child exiting\n"); _exit(0); } return 0; } 11. WAP where the parent process calculates the factorial of a number and sends the result to its child process, which then checks if the result is even or odd. #include <stdio.h> #include <unistd.h> #include <sys/wait.h> int main() { int num = 5, fact = 1; pid_t pid = fork(); if (pid == 0) { for (int i = 1; i <= num; i++) fact *= i; printf("Child: Factorial = %d\n", fact); } else { wait(NULL); printf("Parent: Factorial calculated\n"); } return 0; } 12. WAP to create a sparse file of 10MB with actual data at the beginning and end only, using appropriate system calls. #include <stdio.h> int main() { FILE *fp = fopen("large.txt", "w"); for (int i = 0; i < 1024 * 1024; i++) fputc('A', fp); fclose(fp); return 0; } 13. WAP to open a file and replace 15th to 20th characters by 'LOVELY' using lseek() and write() system calls. #include <fcntl.h> #include <unistd.h> int main() { int fd = open("sample.txt", O_RDWR); lseek(fd, 15, SEEK_SET); write(fd, "LOVELY", 6); close(fd); return 0; } 14. Write a program to read a random number from user and display table of the number having 10 terms. #include <stdio.h> int main() { FILE *fp = fopen("input.txt", "r"); int num; fscanf(fp, "%d", &num); for (int i = 1; i <= 10; i++) printf("%d x %d = %d\n", num, i, num*i); fclose(fp); return 0; } 15. WAP to demonstrate reader-writer problem using semaphores. Use pthreads with semaphores: sem_t readLock, writeLock; Track reader count and allow multiple readers or one writer at a time. Use sem_wait and sem_post for synchronization. 16. WAP to read 15 characters from 10th position of a file and display them. #include <stdio.h> #include <string.h> int main() { FILE *fp = fopen("text.txt", "r"); char word[100]; int count = 0; while (fscanf(fp, "%s", word) != EOF) { count++; if (count == 10) { printf("10th word: %s\n", word); break; } } fclose(fp); return 0; } 17. WAP to create a parent process with two child processes and display their process IDs and parent process IDs. #include <stdio.h> #include <unistd.h> int main() { fork(); printf("PID: %d, PPID: %d\n", getpid(), getppid()); return 0; } 18. WAP to demonstrate Orphan Process. #include <stdio.h> #include <unistd.h> int main() { pid_t pid = fork(); if (pid > 0) { printf("Parent exiting\n"); return 0; } else { sleep(5); printf("Child is orphan, PPID: %d\n", getppid()); } return 0; }