Advertisement
fsoc131y

13

Apr 24th, 2023 (edited)
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.74 KB | None | 0 0
  1. 1. OSL 1:
  2. cd, pwd, mkdir, rmdir, ls, man, touch, cat, rm, cp, mv, head, tail, more, less, history, chmod, cal
  3. -------------------------------------------------------
  4. 2. OSL 2:
  5. chmod, chown, chgrp, adduser, useradd, groupadd, userdel, groupdel, passwd, getent, gpasswd, umask
  6. -------------------------------------------------------
  7. 3. OSL 3:
  8. free, vmstat (-d, -s), top -h, /proc/meminfo, ps, free, df, kill
  9. -------------------------------------------------------
  10. 6. Zombie Process:
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <sys/types.h>
  14. #include <unistd.h>
  15.  
  16. int main() {
  17.     pid_t child_pid = fork();
  18.     if (child_pid > 0) {
  19.         // parent process
  20.         printf("Parent process (PID %d) created child process with PID %d\n", getpid(), child_pid);
  21.         sleep(5);
  22.     } else if (child_pid == 0) {
  23.         // child process
  24.         printf("Child process (PID %d) exiting\n", getpid());
  25.         exit(0);
  26.     } else {
  27.         // fork failed
  28.         printf("Fork failed\n");
  29.         exit(1);
  30.     }
  31.     return 0;
  32. }
  33. ----
  34. Orphan Process:
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <sys/types.h>
  38. #include <unistd.h>
  39.  
  40. int main() {
  41.     pid_t child_pid = fork();
  42.     if (child_pid > 0) {
  43.         // parent process
  44.         printf("Parent process (PID %d) sleeping\n", getpid());
  45.         sleep(10);
  46.     } else if (child_pid == 0) {
  47.         // child process
  48.         printf("Child process (PID %d) created\n", getpid());
  49.         sleep(5);
  50.         printf("Child process (PID %d) exiting\n", getpid());
  51.     } else {
  52.         // fork failed
  53.         printf("Fork failed\n");
  54.         exit(1);
  55.     }
  56.     return 0;
  57. }
  58. ----------
  59. Parent & child process:
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. #include <sys/types.h>
  63. #include <unistd.h>
  64.  
  65. int main() {
  66.     pid_t child_pid = fork();
  67.     if (child_pid > 0) {
  68.         // parent process
  69.         printf("Parent process (PID %d) created child process with PID %d\n", getpid(), child_pid);
  70.         sleep(5);
  71.         printf("Parent process (PID %d) exiting\n", getpid());
  72.     } else if (child_pid == 0) {
  73.         // child process
  74.         printf("Child process (PID %d) created\n", getpid());
  75.         sleep(10);
  76.         printf("Child process (PID %d) exiting\n", getpid());
  77.     } else {
  78.         // fork failed
  79.         printf("Fork failed\n");
  80.         exit(1);
  81.     }
  82.     return 0;
  83. }
  84. ----------------------------------------------------------------
  85. 7. Use of fork, sleep, kill, getpid and getppid:
  86. #include <stdio.h>
  87. #include <stdlib.h>
  88. #include <unistd.h>
  89. #include <sys/wait.h>
  90.  
  91. int main() {
  92.   pid_t pid1, pid2, child_status;
  93.  
  94.   // Create first child process
  95.   pid1 = fork();
  96.   if (pid1 == 0) {
  97.     // Code executed by child process
  98.     printf("Child process 1 created with ID: %d\n", getpid());
  99.     sleep(4);
  100.     printf("Child process 1 parent ID: %d\n", getppid());
  101.   } else {
  102.     // Code executed by parent process
  103.     waitpid(pid1, &child_status, 0);
  104.     printf("Child process 1 with ID %d has terminated\n", pid1);
  105.  
  106.     // Create second child process
  107.     pid2 = fork();
  108.     if (pid2 == 0) {
  109.       // Code executed by child process
  110.       printf("Child process 2 created with ID: %d\n", getpid());
  111.       printf("Child process 2 parent ID: %d\n", getppid());
  112.     } else {
  113.       // Kill second child process
  114.       kill(pid2, SIGKILL);
  115.       waitpid(pid2, &child_status, 0);
  116.       printf("Child process 2 with ID %d has been killed\n", pid2);
  117.     }
  118.   }
  119.  
  120.   return 0;
  121. }
  122. ---------------------------------------------------------------------------
  123. 8. One Way Pipe:
  124. #include <stdio.h>
  125. #include <stdlib.h>
  126. #include <unistd.h>
  127.  
  128. #define MSGSIZE 16
  129.  
  130. int main() {
  131.     char msg[MSGSIZE] = "Hello, World!";
  132.     char buffer[MSGSIZE];
  133.     int p[2];
  134.  
  135.     if (pipe(p) < 0) {
  136.         printf("Pipe creation failed\n");
  137.         exit(1);
  138.     }
  139.  
  140.     pid_t pid = fork();
  141.  
  142.     if (pid < 0) {
  143.         printf("Fork failed\n");
  144.         exit(1);
  145.     }
  146.  
  147.     if (pid > 0) {
  148.         // parent process
  149.         close(p[0]);
  150.         write(p[1], msg, MSGSIZE);
  151.         printf("Parent process sent message: %s\n", msg);
  152.         close(p[1]);
  153.     } else {
  154.         // child process
  155.         close(p[1]);
  156.         read(p[0], buffer, MSGSIZE);
  157.         printf("Child process received message: %s\n", buffer);
  158.         close(p[0]);
  159.     }
  160.  
  161.     return 0;
  162. }
  163. ---------------------------------------------------------------------------------
  164. 9. Two Way PIPE
  165. sender.c:
  166. #include <stdio.h>
  167. #include <stdlib.h>
  168. #include <unistd.h>
  169.  
  170. #define MSGSIZE 16
  171.  
  172. int main() {
  173.     char msg[MSGSIZE];
  174.     int p[2];
  175.  
  176.     if (pipe(p) < 0) {
  177.         printf("Pipe creation failed\n");
  178.         exit(1);
  179.     }
  180.  
  181.     pid_t pid = fork();
  182.  
  183.     if (pid < 0) {
  184.         printf("Fork failed\n");
  185.         exit(1);
  186.     }
  187.  
  188.     if (pid > 0) {
  189.         // parent process (sender)
  190.         close(p[0]);
  191.         printf("Enter a message: ");
  192.         fgets(msg, MSGSIZE, stdin);
  193.         write(p[1], msg, MSGSIZE);
  194.         close(p[1]);
  195.     } else {
  196.         // child process (receiver)
  197.         close(p[1]);
  198.         char buffer[MSGSIZE];
  199.         read(p[0], buffer, MSGSIZE);
  200.         printf("Received message: %s", buffer);
  201.         close(p[0]);
  202.     }
  203.  
  204.     return 0;
  205. }
  206.  
  207. receiver.c:
  208. #include <stdio.h>
  209. #include <stdlib.h>
  210. #include <unistd.h>
  211.  
  212. #define MSGSIZE 16
  213.  
  214. int main() {
  215.     char msg[MSGSIZE];
  216.     int p[2];
  217.  
  218.     if (pipe(p) < 0) {
  219.         printf("Pipe creation failed\n");
  220.         exit(1);
  221.     }
  222.  
  223.     pid_t pid = fork();
  224.  
  225.     if (pid < 0) {
  226.         printf("Fork failed\n");
  227.         exit(1);
  228.     }
  229.  
  230.     if (pid > 0) {
  231.         // parent process (sender)
  232.         close(p[0]);
  233.         printf("Enter a message: ");
  234.         fgets(msg, MSGSIZE, stdin);
  235.         write(p[1], msg, MSGSIZE);
  236.         close(p[1]);
  237.     } else {
  238.         // child process (receiver)
  239.         close(p[1]);
  240.         char buffer[MSGSIZE];
  241.         read(p[0], buffer, MSGSIZE);
  242.         printf("Received message: %s", buffer);
  243.         printf("Enter a response: ");
  244.         fgets(msg, MSGSIZE, stdin);
  245.         write(p[1], msg, MSGSIZE);
  246.         close(p[0]);
  247.     }
  248.  
  249.     return 0;
  250. }
  251. -------------------------------------------------------------------------------
  252. 10. OSL 10: ONE WAY COMMUNICATION B/W TWO PROCESSES USING FIFO
  253. - sender:
  254.  
  255. #include <stdio.h>
  256. #include <stdlib.h>
  257. #include <string.h>
  258. #include <unistd.h>
  259. #include <fcntl.h>
  260. #include <sys/stat.h>
  261. #include <sys/types.h>
  262.  
  263. int main()
  264. {
  265.     int fd;
  266.     char * myfifo = "/tmp/myfifo";
  267.     mkfifo(myfifo, 0666); // create the FIFO with read/write permissions
  268.    
  269.     char message[100];
  270.     printf("Enter message: ");
  271.     fgets(message, 100, stdin);
  272.    
  273.     fd = open(myfifo, O_WRONLY); // open the FIFO for writing
  274.     write(fd, message, strlen(message)+1); // write the message to the FIFO
  275.     close(fd);
  276.    
  277.     unlink(myfifo); // remove the FIFO
  278.    
  279.     return 0;
  280. }
  281.        
  282. - receiver:
  283.  
  284. #include <stdio.h>
  285. #include <stdlib.h>
  286. #include <string.h>
  287. #include <unistd.h>
  288. #include <fcntl.h>
  289. #include <sys/stat.h>
  290. #include <sys/types.h>
  291.  
  292. int main()
  293. {
  294.     int fd;
  295.     char * myfifo = "/tmp/myfifo";
  296.     mkfifo(myfifo, 0666); // create the FIFO with read/write permissions
  297.    
  298.     char message[100];
  299.    
  300.     fd = open(myfifo, O_RDONLY); // open the FIFO for reading
  301.     read(fd, message, 100); // read the message from the FIFO
  302.     printf("Received message: %s\n", message);
  303.     close(fd);
  304.    
  305.     unlink(myfifo); // remove the FIFO
  306.    
  307.     return 0;
  308. }
  309.  
  310. --------------------------------------------------------------------------------------------------------------------
  311.  
  312. OSL 11: TWO WAY COMMUNICATION B/W TWO PROCESSES USING FIFO
  313. sender.c -
  314. #include <stdio.h>
  315. #include <stdlib.h>
  316. #include <fcntl.h>
  317. #include <sys/stat.h>
  318. #include <sys/types.h>
  319. #include <unistd.h>
  320.  
  321. int main()
  322. {
  323.     int fd;
  324.     char *fifo = "/tmp/myfifo";
  325.     mkfifo(fifo, 0666);
  326.     char arr1[80], arr2[80];
  327.     while (1)
  328.     {
  329.         printf("Waiting for input from User2...\n");
  330.  
  331.         fd = open(fifo, O_WRONLY);
  332.         printf("User1: ");
  333.         fgets(arr2, 80, stdin);
  334.         write(fd, arr2, sizeof(arr2));
  335.         close(fd);
  336.        
  337.         fd = open(fifo, O_RDONLY);
  338.         read(fd, arr1, sizeof(arr1));
  339.         printf("User2: %s\n", arr1);
  340.         close(fd);
  341.     }
  342.     return 0;
  343. }
  344.  
  345. receiver.c -
  346. #include <stdio.h>
  347. #include <stdlib.h>
  348. #include <fcntl.h>
  349. #include <sys/stat.h>
  350. #include <sys/types.h>
  351. #include <unistd.h>
  352.  
  353. int main()
  354. {
  355.     int fd;
  356.     char *fifo = "/tmp/myfifo";
  357.     mkfifo(fifo, 0666);
  358.     char arr1[80], arr2[80];
  359.     while (1)
  360.     {
  361.         printf("Waiting for input from User1...\n");
  362.  
  363.         fd = open(fifo, O_RDONLY);
  364.         read(fd, arr1, sizeof(arr1));
  365.         printf("User1: %s\n", arr1);
  366.         close(fd);
  367.        
  368.         fd = open(fifo, O_WRONLY);
  369.         printf("User2: ");
  370.         fgets(arr2, 80, stdin);
  371.         write(fd, arr2, sizeof(arr2));
  372.         close(fd);
  373.     }
  374.     return 0;
  375. }
  376.  
  377. ------------------------------------------------------------------------------------------------------------------------
  378. OSL 12: DEMONSTRATE WAIT AND SLEEP SYSTEM CALLS
  379. sleep():
  380. #include <stdio.h>
  381. #include <stdlib.h>
  382. #include <unistd.h>
  383.  
  384. int main() {
  385.     printf("Before sleep\n");
  386.     sleep(5); // sleep for 5 seconds
  387.     printf("After sleep\n");
  388.  
  389.     return 0;
  390. }
  391.  
  392. wait():
  393. #include <stdio.h>
  394. #include <stdlib.h>
  395. #include <unistd.h>
  396. #include <sys/wait.h>
  397.  
  398. int main() {
  399.     pid_t pid;
  400.  
  401.     pid = fork();
  402.  
  403.     if (pid == 0) { // child process
  404.         printf("Child process is running\n");
  405.         sleep(5); // child process sleeps for 5 seconds
  406.         exit(0);
  407.     } else if (pid > 0) { // parent process
  408.         printf("Parent process is waiting for child process to terminate\n");
  409.         wait(NULL); // parent process waits for child process to terminate
  410.         printf("Child process terminated\n");
  411.     } else { // error
  412.         fprintf(stderr, "Fork failed\n");
  413.         exit(1);
  414.     }
  415.  
  416.     return 0;
  417. }
  418.  
  419. -----------------------------------------------------------------------------------------------------------------------
  420. OSL 13: BASIC THREAD OPERATIONS
  421. #include <stdio.h>
  422. #include <stdlib.h>
  423. #include <pthread.h>
  424.  
  425. void *print_hello(void *arg) {
  426.   printf("Hello from thread %ld\n", (long)arg);
  427.   pthread_exit(NULL);
  428. }
  429.  
  430. int main() {
  431.   pthread_t thread_id;
  432.   int rc;
  433.  
  434.   // create a new thread
  435.   rc = pthread_create(&thread_id, NULL, print_hello, (void *)1);
  436.   if (rc) {
  437.     printf("ERROR; return code from pthread_create() is %d\n", rc);
  438.     exit(-1);
  439.   }
  440.  
  441.   // wait for the thread to complete
  442.   rc = pthread_join(thread_id, NULL);
  443.   if (rc) {
  444.     printf("ERROR; return code from pthread_join() is %d\n", rc);
  445.     exit(-1);
  446.   }
  447.  
  448.   printf("Thread %ld has completed\n", (long)thread_id);
  449.   pthread_exit(NULL);
  450. }
  451. ------------------------------------------------
  452.  
  453. OPTIONAL THREAD OPS:
  454. #include <stdio.h>
  455. #include <stdlib.h>
  456. #include <pthread.h>
  457.  
  458. pthread_mutex_t mutex;
  459.  
  460. void *print_message(void *ptr);
  461.  
  462. int main() {
  463.     pthread_t thread1, thread2;
  464.     char *message1 = "Thread 1";
  465.     char *message2 = "Thread 2";
  466.     int rc1, rc2;
  467.  
  468.     // initialize mutex
  469.     pthread_mutex_init(&mutex, NULL);
  470.  
  471.     // create thread 1
  472.     printf("Creating thread 1...\n");
  473.     rc1 = pthread_create(&thread1, NULL, print_message, (void*) message1);
  474.     if (rc1) {
  475.         printf("Error: return code from pthread_create() is %d\n", rc1);
  476.         exit(-1);
  477.     }
  478.  
  479.     // detach thread 1
  480.     printf("Detaching thread 1...\n");
  481.     rc1 = pthread_detach(thread1);
  482.     if (rc1) {
  483.         printf("Error: return code from pthread_detach() is %d\n", rc1);
  484.         exit(-1);
  485.     }
  486.  
  487.     // create thread 2
  488.     printf("Creating thread 2...\n");
  489.     rc2 = pthread_create(&thread2, NULL, print_message, (void*) message2);
  490.     if (rc2) {
  491.         printf("Error: return code from pthread_create() is %d\n", rc2);
  492.         exit(-1);
  493.     }
  494.  
  495.     // join thread 2
  496.     printf("Joining thread 2...\n");
  497.     rc2 = pthread_join(thread2, NULL);
  498.     if (rc2) {
  499.         printf("Error: return code from pthread_join() is %d\n", rc2);
  500.         exit(-1);
  501.     }
  502.  
  503.     // lock mutex and print final message
  504.     printf("Locking mutex...\n");
  505.     pthread_mutex_lock(&mutex);
  506.     printf("Main thread and thread 2 are done!\n");
  507.     pthread_mutex_unlock(&mutex);
  508.  
  509.     // destroy mutex
  510.     printf("Destroying mutex...\n");
  511.     pthread_mutex_destroy(&mutex);
  512.  
  513.     printf("Main thread exiting...\n");
  514.     pthread_exit(NULL);
  515. }
  516.  
  517. void *print_message(void *ptr) {
  518.     char *message = (char*) ptr;
  519.  
  520.     // lock mutex
  521.     printf("Locking mutex...\n");
  522.     pthread_mutex_lock(&mutex);
  523.  
  524.     printf("%s\n", message);
  525.  
  526.     // unlock mutex
  527.     printf("Unlocking mutex...\n");
  528.     pthread_mutex_unlock(&mutex);
  529.  
  530.     pthread_exit(NULL);
  531. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement