Advertisement
vasilev5

Untitled

Mar 23rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.48 KB | None | 0 0
  1. #include <sys/ipc.h>
  2. #include <sys/sem.h>
  3. #include <sys/shm.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include "se207_sems.h"
  8. #include <sys/wait.h>
  9. /* Remember to try reversing the timings...*/
  10.  
  11.  
  12. int bufferlength=8; //Length of buffer.
  13.  
  14. int main(int argc, char argv[]){
  15.  
  16. pid_t pid;
  17.     int status = 0;
  18.     int i;
  19.   //Create shared memory segment
  20.   int shm_id=shmget(ftok("prodcon_example2.c",2),bufferlength,
  21.             0666|IPC_CREAT);
  22.  
  23.   //Use our source file as the "key"
  24.   int id=se207_semget("prodcon_example2.c",0);
  25.  
  26.   char* data; //For our pointer to shared memory...
  27.  
  28.   pid=fork();  
  29.   if(pid){
  30.     //P1 - CONSUMER
  31.     shm_id=shmget(ftok("prodcon_example2.c",2),0,006);
  32.  
  33.     //Attach the shared buffer
  34.     data = shmat(shm_id, (void *)0, 0);
  35.     int consumed=0;
  36.     while(consumed<bufferlength){
  37.       se207_wait(id);
  38.       printf("Consuming item number %d...\n",consumed);
  39.       sleep(1);
  40.       char item=data[consumed];
  41.      
  42.       printf("Consumed item number %d.  Item value was %d\n",
  43.          consumed,item);
  44.       consumed++; //Each time the while loop runs the consumer counter is incremented.
  45.  
  46.       if(consumed == 7) //If statement that checks to make sure that value of consumed is not greater than the value of the buffer length and sets it to 0 if the value is 7 effectively creating an infinite loop.
  47.       {
  48.          consumed = 0;
  49.       }
  50.     }
  51.  
  52.     //Detatch
  53.     shmdt(data);
  54.     printf("All done consuming.\n");
  55.  
  56.     wait(&status); //For child process so that we can
  57.  
  58.     //Delete the shared memory
  59.     printf("Child ended, removing shm\n");
  60.     shmctl(shm_id, IPC_RMID, NULL);    
  61.   }else{
  62.     //P2 - PRODUCER
  63.     shm_id=shmget(ftok("prodcon_example2.c",2),0,006);                  
  64.     //Attach the shared buffer
  65.     data = shmat(shm_id, (void *)0, 0);
  66.  
  67.     int produced=0;
  68.     while(produced<bufferlength){
  69.       printf("Producing item number %d...\n",produced);
  70.       sleep(2);
  71.       data[produced]=produced*2; //Simple data, easy to check.
  72.       printf("Produced item number %d.  Value is %d\n",
  73.          produced,data[produced]);
  74.       se207_signal(id);
  75.       produced++;
  76.  
  77.       if(produced == 7) //If statement that checks to make sure that the value of produced is not greater than the value of the buffer length and sets it to 0 if the value is 7 effectively creating an infinite loop.
  78.         {
  79.            produced = 0;
  80.         }
  81.     }
  82.     //Detatch
  83.     shmdt(data);
  84.     printf("Producer finished.");
  85.   }  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement