Advertisement
nieduard_01

3

Apr 20th, 2021
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.20 KB | None | 0 0
  1. /* В родителе D(1), далее в цикле write() текст в pipe, A(2), Zero - блок семафора, read();
  2. В ребенке в цикле D(2) - запуск родителя, read(), write(). A(1) */
  3.  
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <sys/ipc.h>
  9. #include <sys/sem.h>
  10. #include <sys/shm.h>
  11.  
  12.  
  13. int main() {
  14.     int fd[2], result, N;
  15.  
  16.     size_t size;
  17.     char resstring[14];
  18.     char path[] = "task03.c";
  19.  
  20.     key_t key;
  21.     struct sembuf mybuf;
  22.     int semid;
  23.  
  24.     printf("Введите N: ");
  25.     scanf("%d", &N);
  26.  
  27.     if (pipe(fd) < 0) {
  28.         printf("Can\'t open pipe\n");
  29.         exit(-1);
  30.     }
  31.  
  32.     if ((key = ftok(path, 0)) < 0) {
  33.         printf("Can\'t generate key");
  34.         exit(-1);
  35.     }
  36.  
  37.     if ((semid = semget(key, 1, 0666 | IPC_CREAT)) < 0) {
  38.         printf("Can\'t create semaphore set\n");
  39.         exit(-1);
  40.     }
  41.  
  42.     //Add_1
  43.     mybuf.sem_num = 0;
  44.     mybuf.sem_op = 1;
  45.     mybuf.sem_flg = 0;
  46.  
  47.     if (semop(semid, &mybuf, 1) < 0) {
  48.         printf("Can\'t set value in semaphore\n");
  49.         exit(-1);
  50.     }
  51.  
  52.     result = fork();
  53.  
  54.     if (result < 0) {
  55.         printf("Can\'t fork child\n");
  56.         exit(-1);
  57.     } else if (result > 0) {
  58.  
  59.         /* Parent process */
  60.  
  61.         //Decrease_1
  62.         mybuf.sem_num = 0;
  63.         mybuf.sem_op = -1;
  64.         mybuf.sem_flg = 0;
  65.  
  66.         if (semop(semid, &mybuf, 1) < 0) {
  67.             printf("Can\'t set value in semaphore\n");
  68.             exit(-1);
  69.         }
  70.  
  71.         for (int i = 0; i < N; ++i) {
  72.  
  73.             size = write(fd[1], "Hello, world!", 14);
  74.  
  75.             if (size != 14) {
  76.                 printf("Can\'t write all string to pipe\n");
  77.                 exit(-1);
  78.             }
  79.  
  80.             //Add_2
  81.             mybuf.sem_num = 0;
  82.             mybuf.sem_op = 2;
  83.             mybuf.sem_flg = 0;
  84.  
  85.             if (semop(semid, &mybuf, 1) < 0) {
  86.                 printf("Can\'t set value in semaphore\n");
  87.                 exit(-1);
  88.             }
  89.             //Zero
  90.             mybuf.sem_num = 0;
  91.             mybuf.sem_op = 0;
  92.             mybuf.sem_flg = 0;
  93.  
  94.             if (semop(semid, &mybuf, 1) < 0) {
  95.                 printf("Can\'t set value in semaphore\n");
  96.                 exit(-1);
  97.             }
  98.  
  99.             //Decrease_1
  100.             mybuf.sem_num = 0;
  101.             mybuf.sem_op = -1;
  102.             mybuf.sem_flg = 0;
  103.  
  104.             if (semop(semid, &mybuf, 1) < 0) {
  105.                 printf("Can\'t set value in semaphore\n");
  106.                 exit(-1);
  107.             }
  108.  
  109.             size = read(fd[0], resstring, 14);
  110.  
  111.             if (size < 0) {
  112.                 printf("Can\'t read all string to pipe\n");
  113.                 exit(-1);
  114.             }
  115.             printf("Parent read from the pipe: %s\n", resstring);
  116.         }
  117.  
  118.         printf("Parent exit\n");
  119.     } else {
  120.  
  121.         /* Child process */
  122.  
  123.         for (int i = 0; i < N; ++i) {
  124.             //Decrease_2
  125.             mybuf.sem_num = 0;
  126.             mybuf.sem_op = -2;
  127.             mybuf.sem_flg = 0;
  128.  
  129.             if (semop(semid, &mybuf, 1) < 0) {
  130.                 printf("Can\'t set value in semaphore\n");
  131.                 exit(-1);
  132.             }
  133.  
  134.             size = read(fd[0], resstring, 14);
  135.  
  136.             if (size < 0) {
  137.                 printf("Can\'t read string from pipe\n");
  138.                 exit(-1);
  139.             }
  140.             printf("Child read from the pipe: %s\n", resstring);
  141.  
  142.  
  143.             size = write(fd[1], "Hello, world!", 14);
  144.  
  145.             if (size != 14) {
  146.                 printf("Can\'t write all string to pipe\n");
  147.                 exit(-1);
  148.             }
  149.  
  150.             //Add_1
  151.             mybuf.sem_num = 0;
  152.             mybuf.sem_op = 1;
  153.             mybuf.sem_flg = 0;
  154.  
  155.             if (semop(semid, &mybuf, 1) < 0) {
  156.                 printf("Can\'t set value in semaphore\n");
  157.                 exit(-1);
  158.             }
  159.         }
  160.  
  161.         if (close(fd[0]) < 0) {
  162.             printf("child: Can\'t close reading side of pipe\n");
  163.             exit(-1);
  164.         }
  165.  
  166.         if (close(fd[1]) < 0) {
  167.             printf("child: Can\'t close writing side of pipe\n");
  168.             exit(-1);
  169.         }
  170.     }
  171.  
  172.     return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement