Advertisement
SIKER_98

lab8-4

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