Advertisement
SIKER_98

lab8-2

Jan 12th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 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.  
  16. static struct sembuf semaphor; // deklaracja semafora
  17.  
  18. void push(int, int); // podnoszenie
  19.  
  20. void pop(int, int); // opuszczanie
  21.  
  22. //--MAIN
  23.  
  24. int main(){
  25. int shmid, semid;
  26. char* buf;
  27.  
  28. //tworzenie tablicy semaforow
  29. semid = semget(34170, 2, IPC_CREAT | 0600);
  30.  
  31. //nadawanie wartosci semaforowi
  32. semctl(semid, 0, SETVAL, (int) 1);
  33. semctl(semid, 1, SETVAL, (int) 0);
  34.  
  35.  
  36. //tworzenie segmentu pamieci wspoldzielonej
  37. shmid = shmget(56392, MAX, IPC_CREAT | 0600);
  38.  
  39. // przylaczanie pamieci wspoldzielonej
  40. buf = shmat(shmid, NULL, 0);
  41.  
  42.  
  43. pid_t p1, p2, p3;
  44.  
  45. // KONSUMENT 1
  46. if ((p1 = fork()) == 0) { // in child process
  47. while (1) {
  48. if (buf[0] != '/') {
  49. pop(semid, 1);
  50. fflush(stdout);
  51. printf("\tp1 PID: %d : %s \n", getpid(), buf);
  52. fflush(stdout);
  53. push(semid, 0);
  54. //push(semid, 1);
  55. }
  56. //exit(1);
  57. }
  58.  
  59. }
  60.  
  61. // KONSUMENT 2
  62. if ((p2 = fork()) == 0) {
  63. while (1) {
  64. if (buf[0] != '/') {
  65. pop(semid, 1);
  66. fflush(stdout);
  67. printf("\tp2 PID: %d : %s \n", getpid(), buf);
  68. fflush(stdout);
  69. push(semid, 0);
  70. }
  71. //exit(1);
  72. }
  73.  
  74. }
  75.  
  76. // KONSUMENT 3
  77. if ((p3 = fork()) == 0) {
  78. while (1) {
  79. if (buf[0] != '/') {
  80. pop(semid, 1);
  81. fflush(stdout);
  82. printf("\tp3 PID: %d : %s \n", getpid(), buf);
  83. fflush(stdout);
  84. push(semid, 0);
  85. }
  86. //exit(1);
  87. }
  88.  
  89. }
  90.  
  91. int i=0;
  92. //-- PRODUCENT
  93. for(;;){
  94. char input[50];
  95.  
  96. if(i==0){
  97. printf("p0 %d produkt:", getpid());
  98. scanf("%s", input);
  99. }
  100. pop(semid, 0);
  101.  
  102. if(i==1){
  103. printf("p0 %d produkt:", getpid());
  104. scanf("%s", input);
  105. }
  106. strcpy(buf, input);
  107. push(semid, 1);
  108. i=1;
  109. }
  110.  
  111. exit(0);
  112. }
  113.  
  114. //--SEMAFORY
  115. void push(int semid, int semnum){
  116. semaphor.sem_flg=0; // flaga
  117. semaphor.sem_num=semnum; // numer
  118. semaphor.sem_op = 1; // operacja
  119. if(semop(semid, &semaphor, 1) == -1){
  120. printf("Semafor podniesiony");
  121. exit(1);
  122. }
  123. //else printf("ERROR push");
  124. }
  125.  
  126. void pop(int semid, int semnum){
  127. semaphor.sem_flg=0; // flaga
  128. semaphor.sem_num=semnum; // numer
  129. semaphor.sem_op = -1; // operacja
  130. if(semop(semid, &semaphor, 1) == -1){
  131. printf("Semafor podniesiony");
  132. exit(1);
  133. }
  134. //else printf("ERROR pop");
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement