Advertisement
arczi316

SO_Lab10_Y5

Dec 15th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <sys/ipc.h>
  8. #include <sys/shm.h>
  9. #include <stdlib.h>
  10. #include <err.h>
  11. #include <sys/sem.h>
  12. #include <err.h>
  13. #include <string.h>
  14. #include <signal.h>
  15.  
  16.  
  17. #define SHM_SIZE sizeof(char)
  18.  
  19.  
  20. union semun
  21. {
  22.     int val;
  23.     struct semid_ds *buf;
  24.     unsigned short int *array;
  25.     struct seminfo *__buf;
  26. };
  27.  
  28. int semlock(int semid)
  29. {
  30.    
  31.     struct sembuf opr; 
  32.     opr.sem_num =  0;
  33.     opr.sem_op  = -1;
  34.     opr.sem_flg =  0;
  35.            
  36.     if (semop(semid, &opr, 1) == -1){
  37.         warn("Blad blokowania semafora!");
  38.         return 0;
  39.     }else{
  40.         return 1;
  41.     }
  42. }
  43.  
  44. int semunlock(int semid)
  45. {
  46.     struct sembuf opr; 
  47.    
  48.     opr.sem_num = 0;
  49.     opr.sem_op  = 1;
  50.     opr.sem_flg = 0;
  51.            
  52.     if (semop(semid, &opr, 1) == -1)
  53.     {
  54.         warn("Blad odblokowania semafora!");
  55.         return 0;
  56.     }
  57.     else
  58.     {
  59.         return 1;
  60.     }
  61. }
  62.  
  63. void *przylaczSegment(key_t key)
  64. {
  65.     int shmid;
  66.     char *shm;
  67.     if ((shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666)) < 0)
  68.         errx(2, "Blad tworzenia segmentu pamieci dzielonej!");
  69.     if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
  70.         errx(3, "Blad przylaczania pamieci dzielonej!");
  71.     return (void*)shm;
  72. }
  73.  
  74. void usunSegment(key_t key)
  75. {
  76.     int shmid;
  77.     char *shm;
  78.     if ((shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666)) < 0)
  79.         errx(2, "Blad tworzenia segmentu pamieci dzielonej!");
  80.     if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
  81.         errx(3, "Blad przylaczania pamieci dzielonej!");
  82.     shmdt(shm);
  83.     shmctl(shmid, IPC_RMID, NULL);
  84. }
  85.  
  86. int m, p1, p2;
  87.  
  88. int main(int argc, char *argv[])
  89. {
  90.     m = getpid();
  91.     if (argc != 2)
  92.     {
  93.         printf("Zla ilosc argumentow!\n");
  94.         return 1;
  95.     }
  96.     FILE*input = fopen(argv[1], "r");
  97.     if(input == NULL) {
  98.         printf("Blad przy odczytywaniu pliku!\n");
  99.         return 2;
  100.     }
  101.     else if(access ( argv[1], F_OK ) == -1)
  102.     {
  103.         printf("Brak dostepu!\n");
  104.         return 3;
  105.     }
  106.     FILE* output = fopen("out.txt", "w");
  107.     if(output == NULL) {
  108.         printf("Blad przy otwieraniu pliku do zapisu!\n");
  109.         return 4;
  110.     }
  111.    
  112.    
  113.     key_t key1, key2;           // klucz dla semaforow
  114.     int   semid1, semid2;   // ID semaforow
  115.     union semun ctl;            // unia do kontroli semafora   
  116.    
  117.  
  118.     if ((key1 = ftok(".", 'A')) == -1)
  119.         errx(1, "Blad tworzenia klucza!");
  120.    
  121.     if ((semid1 = semget(key1, 1, IPC_CREAT | 0600)) == -1)
  122.         errx(2, "Blad tworzenia semafora!");
  123.    
  124.     ctl.val = 1;
  125.     if (semctl(semid1, 0, SETVAL, ctl) == -1)
  126.         errx(3, "Blad ustawiania semafora!");
  127.    
  128.     if ((key2 = ftok(".", 'B')) == -1)
  129.         errx(1, "Blad tworzenia klucza!");
  130.    
  131.     if ((semid2 = semget(key2, 1, IPC_CREAT | 0600)) == -1)
  132.         errx(2, "Blad tworzenia semafora!");
  133.    
  134.     ctl.val = 1;
  135.     if (semctl(semid2, 0, SETVAL, ctl) == -1)
  136.         errx(3, "Blad ustawiania semafora!");
  137.    
  138.     semlock(semid2);   
  139.    
  140.     int datakey;
  141.     if ((datakey = ftok(".", 'A')) == -1)
  142.     errx(1, "Blad tworzenia klucza!");
  143.     przylaczSegment(datakey);
  144.    
  145.     if(fork())
  146.     {
  147.             p1 = getpid();
  148.             int c;
  149.             do
  150.             {
  151.                 c = fgetc(input);
  152.                 semlock(semid2);
  153.                 char *znak = (char *)przylaczSegment(datakey);
  154.                 *znak = c;
  155.                 semunlock(semid1);
  156.             } while (c != -1);
  157.             fclose(input);
  158.             return 0;
  159.         }
  160.         else
  161.     {
  162.             p2 = getpid();
  163.             while (1)
  164.             {
  165.                 semlock(semid1);
  166.                 char *znak = (char *)przylaczSegment(datakey);
  167.                 char x = *znak;
  168.  
  169.                 if (x == -123)
  170.                         x = 'a';
  171.                 else if (x == -121)
  172.                         x = 'c';
  173.                 else if (x == -103)
  174.                         x = 'e';
  175.                 else if (x == -124)
  176.                         x = 'n';
  177.                 else if (x == -126)
  178.                         x = 'l';
  179.                 else if (x == -77)
  180.                         x = 'o';
  181.                 else if (x == -101)
  182.                         x = 's';
  183.                 else if (x == -70)
  184.                         x = 'z';
  185.                 else if (x == -68)
  186.                         x = 'z';
  187.                 if (!(x == -59 || x == -60 || x == -61 || x == -1 || x == 0))
  188.                         fprintf(output, "%c", x);
  189.  
  190.                 semunlock(semid2);
  191.                 if (x == -1) {
  192.                     fclose(output);
  193.                     usunSegment(datakey);
  194.                     kill(p1, SIGKILL);
  195.                     kill(p2, SIGKILL);
  196.                     kill(m, SIGKILL);
  197.                     break;
  198.                 }
  199.             }
  200.     }
  201.  
  202.         for (;;)
  203.             pause();
  204.         return 0;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement