Advertisement
AbhijitPaul

producer consumer

Aug 19th, 2022
809
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.22 KB | None | 0 0
  1. // C program to implement Peterson’s Algorithm
  2. // for producer-consumer problem.
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <time.h>
  7. #include <sys/types.h>
  8. #include<sys/wait.h>
  9. #include <sys/ipc.h>
  10. #include <sys/shm.h>
  11. #include <stdbool.h>
  12. #define _BSD_SOURCE
  13. #include <sys/time.h>
  14. #include <stdio.h>
  15.  
  16. #define BSIZE 8 // Buffer size
  17. #define PWT 2 // Producer wait time limit
  18. #define CWT 10 // Consumer wait time limit
  19. #define RT 10 // Program run-time in seconds
  20.  
  21. int shmid1, shmid2, shmid3, shmid4;
  22. key_t k1 = 5491, k2 = 5812, k3 = 4327, k4 = 3213;
  23. bool* SHM1;
  24. int* SHM2;
  25. int* SHM3;
  26.  
  27. int myrand(int n) // Returns a random number between 1 and n
  28. {
  29.     time_t t;
  30.     srand((unsigned)time(&t));
  31.     return (rand() % n + 1);
  32. }
  33.  
  34. int main()
  35. {
  36.     shmid1 = shmget(k1, sizeof(bool) * 2, IPC_CREAT | 0660); // flag
  37.     shmid2 = shmget(k2, sizeof(int) * 1, IPC_CREAT | 0660); // turn
  38.     shmid3 = shmget(k3, sizeof(int) * BSIZE, IPC_CREAT | 0660); // buffer
  39.     shmid4 = shmget(k4, sizeof(int) * 1, IPC_CREAT | 0660); // time stamp
  40.  
  41.     if (shmid1 < 0 || shmid2 < 0 || shmid3 < 0 || shmid4 < 0) {
  42.         perror("Main shmget error: ");
  43.         exit(1);
  44.     }
  45.     SHM3 = (int*)shmat(shmid3, NULL, 0);
  46.     int ix = 0;
  47.     while (ix < BSIZE) // Initializing buffer
  48.         SHM3[ix++] = 0;
  49.  
  50.     struct timeval t;
  51.     time_t t1, t2;
  52.     gettimeofday(&t, NULL);
  53.     t1 = t.tv_sec;
  54.  
  55.     int* state = (int*)shmat(shmid4, NULL, 0);
  56.     *state = 1;
  57.     int wait_time;
  58.  
  59.     int i = 0; // Consumer
  60.     int j = 1; // Producer
  61.  
  62.     if (fork() == 0) // Producer code
  63.     {
  64.         SHM1 = (bool*)shmat(shmid1, NULL, 0);
  65.         SHM2 = (int*)shmat(shmid2, NULL, 0);
  66.         SHM3 = (int*)shmat(shmid3, NULL, 0);
  67.         if (SHM1 == (bool*)-1 || SHM2 == (int*)-1 || SHM3 == (int*)-1) {
  68.             perror("Producer shmat error: ");
  69.             exit(1);
  70.         }
  71.  
  72.         bool* flag = SHM1;
  73.         int* turn = SHM2;
  74.         int* buf = SHM3;
  75.         int index = 0;
  76.  
  77.         while (*state == 1) {
  78.             flag[j] = true;
  79.             printf("Producer is ready now.\n\n");
  80.             *turn = i;
  81.             while (flag[i] == true && *turn == i)
  82.                 ;
  83.  
  84.             // Critical Section Begin
  85.             index = 0;
  86.             while (index < BSIZE) {
  87.                 if (buf[index] == 0) {
  88.                     int tempo = myrand(BSIZE * 3);
  89.                     printf("Job %d has been produced\n", tempo);
  90.                     buf[index] = tempo;
  91.                     break;
  92.                 }
  93.                 index++;
  94.             }
  95.             if (index == BSIZE)
  96.                 printf("Buffer is full, nothing can be produced!!!\n");
  97.             printf("Buffer: ");
  98.             index = 0;
  99.             while (index < BSIZE)
  100.                 printf("%d ", buf[index++]);
  101.             printf("\n");
  102.             // Critical Section End
  103.  
  104.             flag[j] = false;
  105.             if (*state == 0)
  106.                 break;
  107.             wait_time = myrand(PWT);
  108.             printf("Producer will wait for %d seconds\n\n", wait_time);
  109.             sleep(wait_time);
  110.         }
  111.         exit(0);
  112.     }
  113.  
  114.     if (fork() == 0) // Consumer code
  115.     {
  116.         SHM1 = (bool*)shmat(shmid1, NULL, 0);
  117.         SHM2 = (int*)shmat(shmid2, NULL, 0);
  118.         SHM3 = (int*)shmat(shmid3, NULL, 0);
  119.         if (SHM1 == (bool*)-1 || SHM2 == (int*)-1 || SHM3 == (int*)-1) {
  120.             perror("Consumer shmat error:");
  121.             exit(1);
  122.         }
  123.  
  124.         bool* flag = SHM1;
  125.         int* turn = SHM2;
  126.         int* buf = SHM3;
  127.         int index = 0;
  128.         flag[i] = false;
  129.         sleep(5);
  130.         while (*state == 1) {
  131.             flag[i] = true;
  132.             printf("Consumer is ready now.\n\n");
  133.             *turn = j;
  134.             while (flag[j] == true && *turn == j)
  135.                 ;
  136.  
  137.             // Critical Section Begin
  138.             if (buf[0] != 0) {
  139.                 printf("Job %d has been consumed\n", buf[0]);
  140.                 buf[0] = 0;
  141.                 index = 1;
  142.                 while (index < BSIZE) // Shifting remaining jobs forward
  143.                 {
  144.                     buf[index - 1] = buf[index];
  145.                     index++;
  146.                 }
  147.                 buf[index - 1] = 0;
  148.             } else
  149.                 printf("Buffer is empty, nothing can be consumed!!!\n");
  150.             printf("Buffer: ");
  151.             index = 0;
  152.             while (index < BSIZE)
  153.                 printf("%d ", buf[index++]);
  154.             printf("\n");
  155.             // Critical Section End
  156.  
  157.             flag[i] = false;
  158.             if (*state == 0)
  159.                 break;
  160.             wait_time = myrand(CWT);
  161.             printf("Consumer will sleep for %d seconds\n\n", wait_time);
  162.             sleep(wait_time);
  163.         }
  164.         exit(0);
  165.     }
  166.     // Parent process will now for RT seconds before causing child to terminate
  167.     while (1) {
  168.         gettimeofday(&t, NULL);
  169.         t2 = t.tv_sec;
  170.         if (t2 - t1 > RT) // Program will exit after RT seconds
  171.         {
  172.             *state = 0;
  173.             break;
  174.         }
  175.     }
  176.     // Waiting for both processes to exit
  177.     wait(NULL);
  178.     wait(NULL);
  179.     printf("The clock ran out.\n");
  180.     return 0;
  181. }
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement