Advertisement
SIKER_98

lab8-3

Jan 12th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 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. }
  55. //exit(1);
  56. }
  57.  
  58. }
  59.  
  60. // KONSUMENT 2
  61. if ((p2 = fork()) == 0) {
  62. while (1) {
  63. if (buf[0] != '/') {
  64. pop(semid, 1);
  65. fflush(stdout);
  66. printf("\tp2 PID: %d : %s \n", getpid(), buf);
  67. fflush(stdout);
  68. push(semid, 1);
  69. }
  70. //exit(1);
  71. }
  72.  
  73. }
  74.  
  75. // KONSUMENT 3
  76. if ((p3 = fork()) == 0) {
  77. while (1) {
  78. if (buf[0] != '/') {
  79. pop(semid, 1);
  80. fflush(stdout);
  81. printf("\tp3 PID: %d : %s \n", getpid(), buf);
  82. fflush(stdout);
  83. push(semid, 1);
  84. }
  85. //exit(1);
  86. }
  87.  
  88. }
  89.  
  90. int i=0;
  91. //-- PRODUCENT
  92. for(;;){
  93. char input[50];
  94.  
  95. if(i==0){
  96. printf("p0 %d produkt:", getpid());
  97. scanf("%s", input);
  98. }
  99. pop(semid, 0);
  100.  
  101. if(i==1){
  102. printf("p0 %d produkt:", getpid());
  103. scanf("%s", input);
  104. }
  105. strcpy(buf, input);
  106. push(semid, 1);
  107. i=1;
  108. }
  109.  
  110. exit(0);
  111. }
  112.  
  113. //--SEMAFORY
  114. void push(int semid, int semnum){
  115. semaphor.sem_flg=0; // flaga
  116. semaphor.sem_num=semnum; // numer
  117. semaphor.sem_op = 1; // operacja
  118. if(semop(semid, &semaphor, 1) == -1){
  119. printf("Semafor podniesiony");
  120. exit(1);
  121. }
  122. //else printf("ERROR push");
  123. }
  124.  
  125. void pop(int semid, int semnum){
  126. semaphor.sem_flg=0; // flaga
  127. semaphor.sem_num=semnum; // numer
  128. semaphor.sem_op = -1; // operacja
  129. if(semop(semid, &semaphor, 1) == -1){
  130. printf("Semafor podniesiony");
  131. exit(1);
  132. }
  133. //else printf("ERROR pop");
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement