Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include <stdlib.h>
  5. #include <sys/stat.h>
  6. #include <sys/ipc.h>
  7. #include <sys/sem.h>
  8. #include <sys/shm.h>
  9. #include <sys/types.h>
  10. #include <err.h>
  11. #include <stdbool.h>
  12. #include <pthread.h>
  13. #include <time.h>
  14. #include <semaphore.h>
  15. #include <string.h>
  16.  
  17. #define BUF_SIZE 256
  18. #define SEM_NAME_czyt "/semafor_nazwany_czytanie"
  19. #define SEM_NAME_pis "/semafor_nazwany_pisanie"
  20. #define SEM_NAME "/semafor_nazwany"
  21. #define INIT_VAL 1
  22.  
  23. typedef struct mutex_funkcja{
  24. pthread_mutex_t mutex_write;
  25. pthread_mutex_t mutex_read;
  26. } mutex;
  27.  
  28. static mutex *data = NULL;
  29.  
  30. void initialise_shared() {
  31. pthread_mutexattr_t attr_read;
  32. pthread_mutexattr_t attr_write;
  33. pthread_mutexattr_init(&attr_read);
  34. pthread_mutexattr_init(&attr_write);
  35. pthread_mutexattr_setpshared(&attr_read, PTHREAD_PROCESS_SHARED);
  36. pthread_mutexattr_setpshared(&attr_write, PTHREAD_PROCESS_SHARED);
  37. pthread_mutex_init(&data->mutex_read, &attr_read);
  38. pthread_mutex_init(&data->mutex_write, &attr_write);
  39. pthread_mutex_lock(&data->mutex_read);
  40. }
  41.  
  42. int main(int argc, char ** argv)
  43. {
  44. /////////////////////// DEKLARACJE /////////////////////////
  45.  
  46. pid_t pidm, pid1, pid2, pid3;
  47. int i;
  48.  
  49. char *sciezka;
  50. char bufor1[BUF_SIZE];
  51. char bufor2[BUF_SIZE];
  52. char bufor3[BUF_SIZE];
  53.  
  54. /* mutex */
  55. FILE *plik2;
  56.  
  57. /* shared memory */
  58. int shmid;
  59. key_t key;
  60. char *shm, *s;
  61.  
  62. /* semafory */
  63. sem_t *semafor_czyt;
  64. sem_t *semafor_pis;
  65.  
  66. semafor_czyt = sem_open(SEM_NAME_czyt, O_CREAT | O_EXCL, 0644, INIT_VAL);
  67. if (semafor_czyt == SEM_FAILED) {
  68. perror("sem_czyt_open error");
  69. exit(EXIT_FAILURE);
  70. }
  71.  
  72. semafor_pis = sem_open(SEM_NAME_pis, O_CREAT | O_EXCL, 0644, INIT_VAL);
  73. if (semafor_pis == SEM_FAILED) {
  74. perror("sem_pis_open error");
  75. exit(EXIT_FAILURE);
  76. }
  77.  
  78. /////////////////////// PROCES MACIERZYSTY //////////////////////////
  79.  
  80. /* printf("Flaga -p [sciezka do pliku] -> pobranie z pliku.\nFlaga -k -> pobranie z klawiatury");
  81. printf("\nargc = %d\n", argc);
  82. printf("\nargv[0] = %d\n", argv[0]);
  83. printf("\nargv[1] = %d\n", argv[1]);
  84. if(argc == 2 && argv[1] == "-k")
  85. scanf("%s", bufor1);
  86. else if(argc == 3 && argv[1] == "-p")
  87. strcpy(sciezka, argv[2]);
  88. else
  89. {
  90. errx(-1, "Blad parametrow!");
  91. exit(EXIT_FAILURE);
  92. }*/
  93. pidm = getpid();
  94. printf("PID Procesu Macierzystego : %d\n", pidm);
  95.  
  96. /////////////////////////////////////////////////////////////////////
  97.  
  98. if(!fork())
  99. {
  100. // PID procesu 1
  101. pid1 = getpid();
  102. printf("PID Procesu 1: %d\n", pid1);
  103. FILE *plik = fopen("dane.txt", "r");
  104. bool fd1 = true;
  105.  
  106. sem_wait(semafor_pis); // zablokowanie pisania
  107. sem_wait(semafor_czyt); // zablokowanie czytania
  108.  
  109. /* Tworzymy klucz dla pamieci dzielonej - dla takich samych argumentow wynik zawsze bedzie taki sam. */
  110. if ((key = ftok(".", 'A')) == -1)
  111. errx(1, "Blad tworzenia klucza!");
  112.  
  113. /* Tworzymy segment pamieci dzielonej */
  114. if ((shmid = shmget(key, BUF_SIZE, IPC_CREAT | 0666)) < 0)
  115. errx(2, "Blad tworzenia segmentu pamieci dzielonej!");
  116.  
  117. /* Dolaczamy pamiec dzielona do naszej przestrzeni danych. */
  118. if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
  119. errx(3, "Blad przylaczania pamieci dzielonej!");
  120.  
  121. if(plik == NULL){
  122. puts("Blad przy otwieraniu pliku z danymi. Koncze proces.");
  123. return -1;
  124. }
  125. s = shm;
  126. //bufor1[0] = fgetc(plik);
  127. i=0;
  128. while(true)
  129. {
  130. while(bufor1[i]!=EOF)
  131. {
  132. bufor1[i] = fgetc(plik);
  133. *s++ = bufor1[i];
  134. i++;
  135. }
  136. break;
  137. }
  138. fclose(plik);
  139.  
  140. sem_post(semafor_czyt); // odblokowanie czytania
  141. sem_post(semafor_pis); // odblokowanie pisania
  142.  
  143. /* Odlaczamy sie od pamieci dzielonej. */
  144. shmdt(shm);
  145.  
  146. /* Kasujemy segment pamieci dzielonej. */
  147. if(semafor_czyt == 1 && semafor_pis == 1)
  148. shmctl(shmid, IPC_RMID, NULL);
  149. sem_unlink(semafor_czyt);
  150. sem_unlink(semafor_pis);
  151. }
  152. else
  153. {
  154. if(!fork())
  155. {
  156. // PID procesu 2
  157. pid2 = getpid();
  158. printf("PID Procesu 2: %d\n", pid2);
  159. int bajty = 0;
  160. bool fd2 = true;
  161.  
  162. /////////////////////////// WCZYTYWANIE DANYCH Z PROCESU 1 ////////////////////////////////
  163. sem_wait(semafor_czyt); // zablokowanie czytania
  164.  
  165. if ((key = ftok(".", 'A')) == -1)
  166. errx(1, "Blad tworzenia klucza!");
  167.  
  168. /* Tworzymy (podłączamy się ) do pamieci dzielonej. */
  169. if ((shmid = shmget(key, BUF_SIZE, IPC_CREAT | 0666)) < 0)
  170. errx(2, "Blad tworzenia segmentu pamieci dzielonej!");
  171.  
  172. /* Dolaczamy pamiec dzielona do naszej przestrzeni danych - funkcja shmat zwraca wskaźnik na adres podpietego segmentu pamieci dzielonej. */
  173. if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
  174. errx(3, "Blad przylaczania pamieci dzielonej!");
  175.  
  176. plik2 = fopen("mutex.txt", "w");
  177. if (plik2 == NULL) {
  178. fprintf(stderr, "Blad otwarcia pliku mutex!\n");
  179. return -1;
  180. }
  181.  
  182. pthread_mutex_lock(&data->mutex_write);
  183. s = shm;
  184. while(true)
  185. {
  186. for(i = 0; i < BUF_SIZE; i++)
  187. {
  188. // pobranie danych z pamieci wspoldzielonej do bufora - bufor2[i] = fgetc(); - chyba
  189. bufor2[i] = *s;
  190. fputc(bufor2[i], plik2);
  191. if(bufor2[i] == EOF)
  192. {
  193. fd2 = false;
  194. break;
  195. }
  196. s++;
  197. bajty++;
  198. }
  199. if(fd2 == false)
  200. break;
  201. }
  202. pthread_mutex_unlock(&data->mutex_read);
  203. fprintf(stderr, "Proces Drugi Odebral %d bajtow.\n", bajty);
  204. sem_post(semafor_czyt); // odblokowanie czytania
  205. /* Odlaczamy sie od pamieci dzielonej. */
  206. shmdt(shm);
  207. }
  208. else
  209. {
  210. if(!fork())
  211. {
  212. // PID procesu 3
  213. bool fd3 = true;
  214. pid3 = getpid();
  215. printf("PID Procesu 3: %d\n", pid3);
  216.  
  217. plik2 = fopen("mutex.txt", "r");
  218. if (plik2 == NULL) {
  219. fprintf(stderr, "Blad otwarcia pliku mutex!\n");
  220. return -1;
  221. }
  222.  
  223. pthread_mutex_lock(&data->mutex_read);
  224. while(true)
  225. {
  226. for(i = 0; i < BUF_SIZE; i++){
  227. bufor3[i] = fgetc(plik2);
  228. printf("%c", bufor3[i]);
  229. if(bufor3[i] == EOF){
  230. fd3 = false;
  231. break;
  232. }
  233. }
  234. if(fd3 == false)
  235. break;
  236. }
  237. pthread_mutex_unlock(&data->mutex_write);
  238. }
  239. }
  240. }
  241.  
  242. ///////////// UTRZYMANIE MACIERZYSTEGO //////////////
  243.  
  244. while(true)
  245. sleep(1);
  246.  
  247. /////////////////////////////////////////////////////
  248.  
  249. pthread_mutex_destroy(&data->mutex_write);
  250. pthread_mutex_destroy(&data->mutex_read);
  251. fclose(plik2);
  252. return 0;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement