Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. #include <semaphore.h>
  2. #include <sys/types.h>
  3. #include <sys/ipc.h>
  4. #include <sys/shm.h>
  5. #include <sys/sem.h>
  6. #include <sys/stat.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <fcntl.h>
  10. #include <string.h>
  11. #include <err.h>
  12. #include <unistd.h>
  13.  
  14. #define MAX 50
  15. static struct sembuf opr;
  16.  
  17.  
  18. void podnies(int semid, int semnum){
  19. opr.sem_num = semnum;
  20. opr.sem_op = 1;
  21. opr.sem_flg = 0;
  22. if (semop(semid, &opr, 1) == -1){
  23. perror("Podnoszenie semafora");
  24. exit(1);
  25. }
  26. }
  27.  
  28. void opusc(int semid, int semnum){
  29. opr.sem_num = semnum;
  30. opr.sem_op = -1;
  31. opr.sem_flg = 0;
  32. if (semop(semid, &opr, 1) == -1){
  33. perror("Opuszczenie semafora");
  34. exit(1);
  35. }
  36. }
  37.  
  38. void kasuj(char tab[])
  39. {
  40. int i=0;
  41. for(i; i<MAX; i++)
  42. {
  43. tab[i] = ' ';
  44. }
  45.  
  46. }
  47.  
  48. int main(){
  49. int shmid, semid, i;
  50. FILE *file;
  51. char *buf;
  52. char bufor[120];
  53. key_t key1;
  54.  
  55. file = fopen("Puacz_pisarz.c", "r");
  56.  
  57. if ((key1 = ftok(".", 'A')) == -1)
  58. errx(1, "Blad tworzenia klucza!");
  59.  
  60. if (file != NULL) printf("Operacja otwarcia pliku powiodla sie\n");
  61.  
  62. semid = semget(34170, 2, IPC_CREAT|0600);
  63. if (semid == -1){
  64. perror("Blad utworzenia tablicy semaforow");
  65. exit(1);
  66. }
  67.  
  68. if (semctl(semid, 0, SETVAL, (int)1) == -1){
  69. perror("Blad nadania wartosci semaforowi 0");
  70. exit(1);
  71. }
  72. if (semctl(semid, 1, SETVAL, (int)0) == -1){
  73. perror("Blad nadania wartosci semaforowi 1");
  74. exit(1);
  75. }
  76.  
  77.  
  78. shmid = shmget(56392, MAX, IPC_CREAT|0600);
  79.  
  80. if (shmid == -1){
  81. perror("Blad utworzenia segmentu pamieci wspoldzielonej");
  82. exit(1);
  83. }
  84.  
  85. buf = shmat(shmid, NULL, 0);
  86.  
  87. if (buf == NULL){
  88. perror("Blad przylaczenia segmentu pamieci wspoldzielonej");
  89. exit(1);
  90. }
  91.  
  92.  
  93. pid_t pid = fork();
  94. if(pid == 0) { // in child process
  95. while(1){
  96. if (buf[0] != '*') {
  97. //printf("PP1: %d : %d \n", getpid(), getppid());
  98. opusc(semid, 1);
  99. fflush(stdout);
  100. printf("PP1 PID: %d %s \n", getpid(), buf);
  101. fflush(stdout);
  102. podnies(semid, 0);
  103. }
  104. //exit(1);
  105. }
  106.  
  107. }
  108.  
  109. pid_t pidTWO = fork();
  110. if(pidTWO == 0) { // in child process
  111. while(1){
  112. if (buf[0] != '*') {
  113. //printf("PP1: %d : %d \n", getpid(), getppid());
  114. opusc(semid, 1);
  115. fflush(stdout);
  116. printf("PP2 PID: %d %s \n", getpid(), buf);
  117. fflush(stdout);
  118. podnies(semid, 0);
  119. }
  120. //exit(1);
  121. }
  122.  
  123. }
  124.  
  125.  
  126. while(1) {
  127. char pin[50]; // przygotowanie tablicy charowej w celu odebrania znakow
  128. printf("Podaj ciag znakow:\n");
  129. scanf("%s", pin); // odbior znakow
  130. opusc(semid, 0);
  131. strcpy(buf, pin);
  132. podnies(semid, 1);
  133. kasuj(bufor);
  134. }
  135.  
  136.  
  137.  
  138.  
  139. exit(0);
  140.  
  141. return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement