Advertisement
fsoc131y

OS 10-13

Apr 10th, 2023 (edited)
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.55 KB | Source Code | 0 0
  1. OSL 10: ONE WAY COMMUNICATION B/W TWO PROCESSES
  2. - sender:
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <fcntl.h>
  9. #include <sys/stat.h>
  10. #include <sys/types.h>
  11.  
  12. int main()
  13. {
  14.     int fd;
  15.     char * myfifo = "/tmp/myfifo";
  16.     mkfifo(myfifo, 0666); // create the FIFO with read/write permissions
  17.    
  18.     char message[100];
  19.     printf("Enter message: ");
  20.     fgets(message, 100, stdin);
  21.    
  22.     fd = open(myfifo, O_WRONLY); // open the FIFO for writing
  23.     write(fd, message, strlen(message)+1); // write the message to the FIFO
  24.     close(fd);
  25.    
  26.     unlink(myfifo); // remove the FIFO
  27.    
  28.     return 0;
  29. }
  30.        
  31. - receiver:
  32.  
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. #include <fcntl.h>
  38. #include <sys/stat.h>
  39. #include <sys/types.h>
  40.  
  41. int main()
  42. {
  43.     int fd;
  44.     char * myfifo = "/tmp/myfifo";
  45.     mkfifo(myfifo, 0666); // create the FIFO with read/write permissions
  46.    
  47.     char message[100];
  48.    
  49.     fd = open(myfifo, O_RDONLY); // open the FIFO for reading
  50.     read(fd, message, 100); // read the message from the FIFO
  51.     printf("Received message: %s\n", message);
  52.     close(fd);
  53.    
  54.     unlink(myfifo); // remove the FIFO
  55.    
  56.     return 0;
  57. }
  58.  
  59. --------------------------------------------------------------------------------------------------------------------
  60.  
  61. OSL 11: TWO WAY COMMUNICATION B/W TWO PROCESSES
  62. sender.c -
  63. #include <stdio.h>
  64. #include <stdlib.h>
  65. #include <fcntl.h>
  66. #include <sys/stat.h>
  67. #include <sys/types.h>
  68. #include <unistd.h>
  69.  
  70. int main()
  71. {
  72.     int fd;
  73.     char *fifo = "/tmp/myfifo";
  74.     mkfifo(fifo, 0666);
  75.     char arr1[80], arr2[80];
  76.     while (1)
  77.     {
  78.         printf("Waiting for input from User2...\n");
  79.  
  80.         fd = open(fifo, O_WRONLY);
  81.         printf("User1: ");
  82.         fgets(arr2, 80, stdin);
  83.         write(fd, arr2, sizeof(arr2));
  84.         close(fd);
  85.        
  86.         fd = open(fifo, O_RDONLY);
  87.         read(fd, arr1, sizeof(arr1));
  88.         printf("User2: %s\n", arr1);
  89.         close(fd);
  90.     }
  91.     return 0;
  92. }
  93.  
  94. receiver.c -
  95. #include <stdio.h>
  96. #include <stdlib.h>
  97. #include <fcntl.h>
  98. #include <sys/stat.h>
  99. #include <sys/types.h>
  100. #include <unistd.h>
  101.  
  102. int main()
  103. {
  104.     int fd;
  105.     char *fifo = "/tmp/myfifo";
  106.     mkfifo(fifo, 0666);
  107.     char arr1[80], arr2[80];
  108.     while (1)
  109.     {
  110.         printf("Waiting for input from User1...\n");
  111.  
  112.         fd = open(fifo, O_RDONLY);
  113.         read(fd, arr1, sizeof(arr1));
  114.         printf("User1: %s\n", arr1);
  115.         close(fd);
  116.        
  117.         fd = open(fifo, O_WRONLY);
  118.         printf("User2: ");
  119.         fgets(arr2, 80, stdin);
  120.         write(fd, arr2, sizeof(arr2));
  121.         close(fd);
  122.     }
  123.     return 0;
  124. }
  125.  
  126. ------------------------------------------------------------------------------------------------------------------------
  127. OSL 12: DEMONSTRATE WAIT AND SLEEP SYSTEM CALLS
  128. sleep():
  129. #include <stdio.h>
  130. #include <stdlib.h>
  131. #include <unistd.h>
  132.  
  133. int main() {
  134.     printf("Before sleep\n");
  135.     sleep(5); // sleep for 5 seconds
  136.     printf("After sleep\n");
  137.  
  138.     return 0;
  139. }
  140.  
  141. wait():
  142. #include <stdio.h>
  143. #include <stdlib.h>
  144. #include <unistd.h>
  145. #include <sys/wait.h>
  146.  
  147. int main() {
  148.     pid_t pid;
  149.  
  150.     pid = fork();
  151.  
  152.     if (pid == 0) { // child process
  153.         printf("Child process is running\n");
  154.         sleep(5); // child process sleeps for 5 seconds
  155.         exit(0);
  156.     } else if (pid > 0) { // parent process
  157.         printf("Parent process is waiting for child process to terminate\n");
  158.         wait(NULL); // parent process waits for child process to terminate
  159.         printf("Child process terminated\n");
  160.     } else { // error
  161.         fprintf(stderr, "Fork failed\n");
  162.         exit(1);
  163.     }
  164.  
  165.     return 0;
  166. }
  167.  
  168. -----------------------------------------------------------------------------------------------------------------------
  169. OSL 13: BASIC THREAD OPERATIONS
  170. #include <stdio.h>
  171. #include <stdlib.h>
  172. #include <pthread.h>
  173.  
  174. void *print_hello(void *arg) {
  175.   printf("Hello from thread %ld\n", (long)arg);
  176.   pthread_exit(NULL);
  177. }
  178.  
  179. int main() {
  180.   pthread_t thread_id;
  181.   int rc;
  182.  
  183.   // create a new thread
  184.   rc = pthread_create(&thread_id, NULL, print_hello, (void *)1);
  185.   if (rc) {
  186.     printf("ERROR; return code from pthread_create() is %d\n", rc);
  187.     exit(-1);
  188.   }
  189.  
  190.   // wait for the thread to complete
  191.   rc = pthread_join(thread_id, NULL);
  192.   if (rc) {
  193.     printf("ERROR; return code from pthread_join() is %d\n", rc);
  194.     exit(-1);
  195.   }
  196.  
  197.   printf("Thread %ld has completed\n", (long)thread_id);
  198.   pthread_exit(NULL);
  199. }
  200. ------------------------------------------------
  201.  
  202. OPTIONAL THREAD OPS:
  203. #include <stdio.h>
  204. #include <stdlib.h>
  205. #include <pthread.h>
  206.  
  207. pthread_mutex_t mutex;
  208.  
  209. void *print_message(void *ptr);
  210.  
  211. int main() {
  212.     pthread_t thread1, thread2;
  213.     char *message1 = "Thread 1";
  214.     char *message2 = "Thread 2";
  215.     int rc1, rc2;
  216.  
  217.     // initialize mutex
  218.     pthread_mutex_init(&mutex, NULL);
  219.  
  220.     // create thread 1
  221.     printf("Creating thread 1...\n");
  222.     rc1 = pthread_create(&thread1, NULL, print_message, (void*) message1);
  223.     if (rc1) {
  224.         printf("Error: return code from pthread_create() is %d\n", rc1);
  225.         exit(-1);
  226.     }
  227.  
  228.     // detach thread 1
  229.     printf("Detaching thread 1...\n");
  230.     rc1 = pthread_detach(thread1);
  231.     if (rc1) {
  232.         printf("Error: return code from pthread_detach() is %d\n", rc1);
  233.         exit(-1);
  234.     }
  235.  
  236.     // create thread 2
  237.     printf("Creating thread 2...\n");
  238.     rc2 = pthread_create(&thread2, NULL, print_message, (void*) message2);
  239.     if (rc2) {
  240.         printf("Error: return code from pthread_create() is %d\n", rc2);
  241.         exit(-1);
  242.     }
  243.  
  244.     // join thread 2
  245.     printf("Joining thread 2...\n");
  246.     rc2 = pthread_join(thread2, NULL);
  247.     if (rc2) {
  248.         printf("Error: return code from pthread_join() is %d\n", rc2);
  249.         exit(-1);
  250.     }
  251.  
  252.     // lock mutex and print final message
  253.     printf("Locking mutex...\n");
  254.     pthread_mutex_lock(&mutex);
  255.     printf("Main thread and thread 2 are done!\n");
  256.     pthread_mutex_unlock(&mutex);
  257.  
  258.     // destroy mutex
  259.     printf("Destroying mutex...\n");
  260.     pthread_mutex_destroy(&mutex);
  261.  
  262.     printf("Main thread exiting...\n");
  263.     pthread_exit(NULL);
  264. }
  265.  
  266. void *print_message(void *ptr) {
  267.     char *message = (char*) ptr;
  268.  
  269.     // lock mutex
  270.     printf("Locking mutex...\n");
  271.     pthread_mutex_lock(&mutex);
  272.  
  273.     printf("%s\n", message);
  274.  
  275.     // unlock mutex
  276.     printf("Unlocking mutex...\n");
  277.     pthread_mutex_unlock(&mutex);
  278.  
  279.     pthread_exit(NULL);
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement