Advertisement
Cosmin3105

so lab 2-5

Nov 10th, 2022
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.94 KB | None | 0 0
  1. // lab2
  2. // hello.c
  3.  
  4. #include <unistd.h>
  5.  
  6. int main() {
  7.     write(1, "Hello world\n", 13);
  8. }
  9.  
  10. // mycp.c
  11.  
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <unistd.h>
  15. #include <fcntl.h>
  16.  
  17. int main(int argc, char **argv) {
  18.     // struct stat *buff;
  19.     // stat(argv[1], buff);
  20.     // long int size = buff->st_size;
  21.    
  22.     int sursa = open(argv[1], O_RDONLY);
  23.     char continut;
  24.    
  25.     int dest = open(argv[2], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
  26.    
  27.     while (read(sursa, &continut, 1)) {
  28.         write(dest, &continut, 1);
  29.     }
  30.    
  31.     close(sursa);  
  32.     close(dest);
  33. }
  34.  
  35. // lab3
  36.  
  37. // forkls.c
  38.  
  39. #include <stdio.h>
  40. #include <sys/types.h>
  41. #include <unistd.h>
  42. #include <errno.h>
  43. #include <sys/wait.h>
  44.  
  45. int main() {
  46.     pid_t pid = fork();
  47.     if (pid < 0)
  48.         return errno;
  49.     else if (pid == 0) {
  50.         printf("My PID=%d, child PID=%d\n", getppid(), getpid());
  51.  
  52.         char *argv[] = {"ls", NULL};
  53.         execve("/usr/bin/ls", argv, NULL);
  54.         perror(NULL);
  55.     }
  56.     else {
  57.         pid_t childPid = wait(NULL);
  58.         printf("Child %d finished\n", childPid);
  59.     }
  60. }
  61.  
  62. // collatz.c
  63.  
  64. #include <stdio.h>
  65. #include <stdlib.h>
  66. #include <sys/types.h>
  67. #include <unistd.h>
  68. #include <errno.h>
  69. #include <sys/wait.h>
  70.  
  71. int main(int argc, char *argv[]) {
  72.     pid_t pid = fork();
  73.     if (pid < 0)
  74.         return errno;
  75.     else if (pid == 0) {
  76.         int n = atoi(argv[1]);
  77.         printf("%d: %d ", n, n);
  78.  
  79.         while (n != 1) {
  80.             if (n % 2 == 0)
  81.                 n = n / 2;
  82.             else
  83.                 n = 3 * n + 1;
  84.  
  85.             printf("%d ", n);
  86.         }
  87.  
  88.     }
  89.     else {
  90.         pid_t childPid = wait(NULL);
  91.         printf("\nChild %d finished\n", childPid);
  92.     }
  93. }
  94.  
  95. // ncollatz.c
  96.  
  97. #include <stdio.h>
  98. #include <stdlib.h>
  99. #include <sys/types.h>
  100. #include <unistd.h>
  101. #include <errno.h>
  102. #include <sys/wait.h>
  103.  
  104. int main(int argc, char *argv[]) {
  105.     pid_t parent = getpid();
  106.     printf("Starting parent %d\n", parent);
  107.  
  108.     for (int i = 1; i < argc; i++) {
  109.         pid_t child = fork();
  110.        
  111.         if (child < 0)
  112.             return errno;
  113.         else if (child == 0) {
  114.             int n = atoi(argv[i]);
  115.             printf("%d: %d ", n, n);
  116.  
  117.             while (n != 1) {
  118.                 if (n % 2 == 0)
  119.                     n = n / 2;
  120.                 else
  121.                     n = 3 * n + 1;
  122.  
  123.                 printf("%d ", n);
  124.             }
  125.             printf("\n");
  126.             return 0;
  127.         }
  128.     }
  129.  
  130.     for (int i = 1; i < argc; i++) {
  131.         pid_t childf = wait(0);
  132.         printf("Done Parent %d, Me %d\n", getpid(), childf);
  133.     }
  134.     printf("Done Parent %d, Me %d\n", getppid(), getpid());
  135. }
  136.  
  137.  
  138. // lab5
  139. // shmcollatz.c
  140.  
  141. #include <stdio.h>
  142. #include <stdlib.h>
  143. #include <sys/types.h>
  144. #include <unistd.h>
  145. #include <errno.h>
  146. #include <sys/wait.h>
  147. #include <sys/mman.h>
  148. #include <sys/stat.h>        
  149. #include <fcntl.h>
  150.  
  151. int main(int argc, char *argv[]) {
  152.     pid_t parent = getpid();
  153.     printf("Starting parent %d\n", parent);
  154.  
  155.     char shm_name[] = "myshm";
  156.     int shm_fd = shm_open(shm_name, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
  157.     if (shm_fd < 0) {
  158.         perror(NULL);
  159.         return errno;
  160.     }
  161.  
  162.     int child_size = getpagesize();
  163.     size_t shm_size = child_size * (argc - 1);
  164.  
  165.     if (ftruncate(shm_fd, shm_size) == -1) {
  166.         perror(NULL);
  167.         shm_unlink(shm_name);
  168.         return errno;
  169.     }
  170.  
  171.     for (int i = 1; i < argc; i++) {
  172.         pid_t child = fork();
  173.  
  174.         int *shm_ptr = mmap(0, child_size, PROT_WRITE, MAP_SHARED, shm_fd, child_size * (i - 1));
  175.         if (shm_ptr == MAP_FAILED) {
  176.             perror(NULL);
  177.             shm_unlink(shm_name);
  178.             return errno;
  179.         }
  180.  
  181.         if (child < 0)
  182.             return errno;
  183.         else if (child == 0) {
  184.             int n = atoi(argv[i]);
  185.             // printf("%d: %d ", n, n);
  186.            
  187.             int j = 0;
  188.             while (n != 1) {
  189.                 if (n % 2 == 0)
  190.                     n = n / 2;
  191.                 else
  192.                     n = 3 * n + 1;
  193.  
  194.                 // printf("%d ", n);
  195.                 *(shm_ptr + j) = n;
  196.                 j++;
  197.                 // i += sizeof(int);
  198.             }
  199.             // printf("\n");
  200.             return 0;
  201.         }
  202.     }
  203.  
  204.     for (int i = 1; i < argc; i++) {
  205.         pid_t childf = wait(0);
  206.         printf("Done Parent %d, Me %d\n", getpid(), childf);
  207.     }
  208.  
  209.     // int *shm_ptr = mmap(0, shm_size, PROT_WRITE, MAP_SHARED, shm_fd, 0);
  210.    
  211.     for (int i = 0; i < argc - 1; i++) {
  212.         int *shm_ptr = mmap(0, child_size, PROT_WRITE, MAP_SHARED, shm_fd, child_size * i);
  213.  
  214.         int n = atoi(argv[i + 1]);
  215.         printf("%d: ", n);
  216.         int j = 0;
  217.         while (shm_ptr[j] != 1) {
  218.             printf("%d ", shm_ptr[j]);
  219.             j++;
  220.         }  
  221.         printf("\n");
  222.     }
  223.    
  224.    
  225.     printf("Done Parent %d, Me %d\n", getppid(), getpid());
  226. }
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement