fork download
  1. // By Zachary Lilley, Username: lill0017 - Intro to Operating Systems 9/22/24 --Modified 10/25/24 For Programming Assignment #2
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/ipc.h>
  5. #include <sys/shm.h>
  6. #include <sys/wait.h>
  7. #include <unistd.h>
  8. #include <semaphore.h> // Added Semaphore Library --10/25/24
  9. #include <fcntl.h> // O_CREAT & O_EXCL --10/25
  10.  
  11.  
  12. // Declaring Semaphore Globally
  13. sem_t *sem;
  14.  
  15. void inc(int *total, int target, int process_id) {
  16. for (int i = 0; i < target; i++) {
  17. sem_wait(sem); // Enter critical section
  18. (*total)++;
  19. sem_post(sem); // Exit critical section
  20. }
  21. printf("From Process %d: counter = %d.\n", process_id, *total);
  22. exit(0); // Exit after incrementing
  23. }
  24.  
  25. int main() {
  26. int shmid;
  27. int *total;
  28. key_t key = IPC_PRIVATE; // Only grants access to the
  29. // shared memory to parent proccess' children
  30.  
  31. sem = sem_open("/zachs_sem", O_CREAT, 0666, 1); //Semaphore initialized with a value of 1
  32. if (sem == SEM_FAILED) {
  33. perror("semaphore failed");
  34. exit(1);
  35. }
  36.  
  37. // Shared memory for total count
  38. shmid = shmget(key, sizeof(int), IPC_CREAT | 0666); //IPC_CREAT makes the memory segment if it doesn't exist already 0666 gives read, write and execution permissions
  39. if (shmid < 0) { // Validate allocation
  40. perror("shmget failed");
  41. exit(1);
  42. }
  43.  
  44. // Attach total to the shared memory
  45. total = (int *)shmat(shmid, NULL, 0); //cast to int
  46. if (total == (int *) -1) {
  47. perror("shmat failed");
  48. exit(1);
  49. }
  50.  
  51. *total = 0; // Initialize the shared variable once it's allocated
  52.  
  53. // Fork 4 times
  54. for (int i = 1; i <= 4; i = i + 1) {
  55.  
  56. int pid = fork();
  57.  
  58. if (pid == -1) { //Handle fork error and close out semaphore --10/25/24
  59. perror("fork failed");
  60. sem_close(sem);
  61. sem_unlink("/zachs_sem");
  62. exit(1);
  63. }
  64.  
  65. // For each fork conduct an increment
  66. if (pid == 0) {
  67. // Set target values for each child process
  68. int target = 0;
  69. if (i == 1) target = 100000;
  70. if (i == 2) target = 200000;
  71. if (i == 3) target = 300000;
  72. if (i == 4) target = 500000;
  73. inc(total, target, getpid()); // Pass current PID to inc function
  74. }
  75. }
  76.  
  77. // Parent processes wait for children
  78. for (int i = 1; i <= 4; i++) {
  79. int pid = wait(NULL);
  80. printf("Child ID: %d exited.\n", pid);
  81. }
  82.  
  83. // Deallocate and release memory and the semaphore --10/25/24
  84. shmdt(total);
  85. shmctl(shmid, IPC_RMID, NULL);
  86. sem_close(sem);
  87. sem_unlink("/zachs_sem");
  88.  
  89. printf("End of Simulation.\n");
  90. return 0;
  91. }
  92.  
  93.  
Success #stdin #stdout 0.05s 5280KB
stdin
Standard input is empty
stdout
From Process 1139444: counter = 704046.
From Process 1139443: counter = 770431.
From Process 1139445: counter = 1029487.
From Process 1139446: counter = 1100000.
Child ID: 1139444 exited.
Child ID: 1139443 exited.
Child ID: 1139445 exited.
Child ID: 1139446 exited.
End of Simulation.