Advertisement
AleksandarH

POS - Semaphore 2

Dec 21st, 2022 (edited)
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. // P1.c
  2.  
  3. #include <stdio.h>
  4. #include <shared.h>
  5.  
  6. int main() {
  7.         sem_t sem;
  8.         sem_t empty;
  9.         sem_t full;
  10.  
  11.         sem = sem_init(91011);
  12.         empty = sem_init(91012);
  13.         full = sem_init(91013);
  14.  
  15.         char *buff;
  16.         char b;
  17.         int i;
  18.         buff = (char *)getmem(9101);
  19.  
  20.         for (char k = 'a'; k <= 'z'; ++k)
  21.         {
  22.                 PS(full);
  23.                 PS(sem);
  24.                 printf("%c\n", *buff);
  25.                 VS(empty);
  26.                 VS(sem);
  27.                 for (i = 0; i < 1000000; i++);
  28.         }
  29.  
  30.         sem_remove(sem);
  31.         sem_remove(empty);
  32.         sem_remove(full);
  33.  
  34.         printf("\nConsumer process ended\n");
  35. }
  36.  
  37. // P2.c
  38.  
  39. #include <stdio.h>
  40. #include <shared.h>
  41.  
  42. int main() {
  43.         sem_t sem;
  44.         sem_t empty;
  45.         sem_t full;
  46.  
  47.         sem = sem_init(91011);
  48.         empty = sem_init(91012);
  49.         full = sem_init(91013);
  50.  
  51.         sem_set(sem, 1);
  52.         sem_set(empty, 1);
  53.         sem_set(full, 0);
  54.  
  55.         char *buff;
  56.         char b;
  57.         int i;
  58.         buff = (char *)getmem(9101);
  59.  
  60.         for (b = 'a'; b <= 'z'; ++b) {
  61.                 PS(empty);
  62.                 PS(sem);
  63.                 *buff = b;
  64.                 VS(sem);
  65.                 VS(full);
  66.                 for (i = 0; i < 1000000; i++);
  67.         }
  68.  
  69.         printf("Producer process ended\n");
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement