Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <sys/ipc.h>
  6. #include <sys/sem.h>
  7. #include <sys/shm.h>
  8. #include <errno.h>
  9.  
  10. int semafor;
  11. key_t klucz;
  12.  
  13. /**************** TWORZENIE SEMAFORA ********************/
  14.  
  15. static void tworzenie_semafora()
  16. {
  17. semafor = semget(klucz, 2, 0600 | IPC_CREAT | IPC_EXCL); //Minimalne prawa 'rw'
  18.  
  19. if(semafor == -1)
  20. {
  21. printf("Blad tworzenia semafora (producent)\m");
  22. exit(-1);
  23. }
  24. else
  25. printf("Semafor zostal utworzony, id = %d z kluczem = %d\n", semafor, klucz);
  26. }
  27. /**************** UZYSKANIE DOSTEPU DO SEMAFORA ********************/
  28.  
  29. static uzyskaj_dostep()
  30. {
  31. semafor = semget(klucz, 2, 0600 | IPC_CREAT);
  32.  
  33. if(semafor == -1)
  34. {
  35. printf("Blad uzyskania dostepu do semafora (producent)\n");
  36. exit(-1);
  37. }
  38. else
  39. printf("Proces %d uzyskal dostep do semafora (producent)\n", getpic());
  40.  
  41. }
  42.  
  43. /**************** USTAWIENIE SEMAFORA ********************/
  44.  
  45. static void ustaw_semafor(int sem, int na)
  46. {
  47. int ustaw_semafor;
  48.  
  49. ustaw_semafor = semctl(semafor, sem, SETVAL, na);
  50.  
  51. if(ustaw_semafor == -1)
  52. {
  53. printf("Nie mozna ustawic semafora (producent)\n");
  54. exit(-1);
  55. }
  56. else
  57. printf("Semafor zostal ustawiony\n");
  58.  
  59. }
  60. /**************** OPUSC SEMAFOR ********************/
  61.  
  62. static void opusc_semafor(int nr_sem)
  63. {
  64. int status;
  65. struct sembuf bufor_semafora;
  66.  
  67. bufor_semafora.sem_num = nr_sem;
  68. bufor_semafora.sem_op = -1;
  69. bufor_semafora.sem_flg = SEM_UNDO;
  70.  
  71. while(1)
  72. {
  73. status = semop(semafor, &bufor_semafora, 1);
  74.  
  75. if(status == 0 | errno != 4)
  76. break;
  77. }
  78.  
  79. if (status == -1 && errno != 4)
  80. {
  81. printf("Blad opuszczenia semafora (producent)\n");
  82. exit(-1);
  83. }
  84. else
  85. printf("Opuszczenie semafora\n");
  86.  
  87. }
  88.  
  89. /**************** PODNIES SEMAFOR ********************/
  90.  
  91. static void podnies_semafor(int nr_sem)
  92. {
  93. int status;
  94. struct sembuf bufor_semafora;
  95.  
  96. bufor_semafora.sem_num = nr_sem;
  97. bufor_semafora.sem_op = 1;
  98. bufor_semafora.sem_flg = SEM_UNDO;
  99.  
  100. while(1)
  101. {
  102. status = semop(semafor, &bufor_semafora, 1);
  103.  
  104. if(status == 0 | errno != 4)
  105. break;
  106. }
  107.  
  108. if (status == -1 && errno != 4)
  109. {
  110. printf("Blad podniesienia semafora (producent)\n");
  111. exit(-1);
  112. }
  113. else
  114. printf("Podniesienie semafora\n");
  115.  
  116. }
  117.  
  118. /**************** USUNIECIE SEMAFORA ********************/
  119.  
  120. static void usun_semafor()
  121. {
  122. int status;
  123.  
  124. status = semctl(semafor, 0, IPC_RMID);
  125.  
  126. if(status == -1)
  127. {
  128. printf("Nie mozna usunac semafora (producent)\n");
  129. ecit(-1);
  130. }
  131. else
  132. printf("Semafor zostal usuniety: %d\n", semafor);
  133.  
  134. }
  135.  
  136.  
  137. int main()
  138. {
  139. /**************** SEGMENT PAMIECI DZIELONEJ ********************/
  140. char *adres_pamieci;
  141. int segment;
  142. key_t klucz_segmentu;
  143.  
  144. klucz_segmentu = ftok(".", 'Z');
  145.  
  146. segment = shmget(klucz_segmentu, sizeof(char), IPC_CREAT | IPC_EXCL | 0600);
  147.  
  148. if(segment == -1)
  149. {
  150. printf("Nie mozna utworzyc segmentu pamieci (producent)\n");
  151. exit(-1);
  152. }
  153. else
  154. printf("Utworzono segment pamieci (producent)\n");
  155.  
  156. adres_pamieci = (char *) shmat(segment, 0, 0);
  157.  
  158. if(adres_pamieci == -1)
  159. {
  160. printf("Nie mozna uzyskac adresu segmentu pamieci (producent)\n");
  161. exit(-1);
  162. }
  163. else
  164. printf("Uzyskano adres segmentu pamieci (producent)\n");
  165.  
  166. /**************** TWORZENIE KLUCZA ********************/
  167.  
  168. klucz = ftok(".",'A');
  169.  
  170. if(klucz == -1)
  171. {
  172. printf("Blad tworzenia klucza (producent)\n");
  173. exit(-1);
  174. }
  175. else
  176. printf("Zoastal utworzony klucz: %d (producent)\n", klucz);
  177.  
  178. /**************** TWORZENIE SEMAFORA ************/
  179.  
  180. tworzenie_semafora();
  181.  
  182. /**************** USTAWIENIE SEMAFORA ************/
  183.  
  184. ustaw_semafor(0, 1);
  185. ustaw_semafor(1, 0);
  186.  
  187. /**************** PLIK WEJSCIE ************/
  188. char znak;
  189. FILE *wejscie;
  190.  
  191. wejscie = fopen("wejscie", "r");
  192.  
  193. if(wejscie == NULL)
  194. {
  195. printf("Nie mozna otworzyc pliku wejscie dla odczytu (producent)\n");
  196. exit(-1);
  197. }
  198. else
  199. {
  200. while(!feof(wejscie)
  201. {
  202. znak = fgetc(wejscie);
  203.  
  204. if(znak != EOF)
  205. {
  206. opusc_semafor(0);
  207.  
  208. *adres_pamieci = znak;
  209. printf("Znak = %c, adres_pamieci = %c (producent)\n", znak, *adres_pamieci);
  210.  
  211. podnies_semafor(1);
  212. }
  213. }
  214.  
  215. opusc_semafor(0);
  216. *adres_pamieci = EOF;
  217. podnies_semafor(1)
  218.  
  219.  
  220. }
  221.  
  222. fclose(wejscie);
  223.  
  224. /**************** OD£¥CZENIE PAMIECI DZIELONEJ ************/
  225.  
  226. shmdt(adres_pamieci);
  227.  
  228. printf("Producent zakonczyl dzialanie\n");
  229.  
  230. return 0;
  231.  
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement