lonsomehell

4Consumers 2 Producers

Mar 15th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.70 KB | None | 0 0
  1. int in, out1,out2,out3,out4 = 0;
  2. int out[4];
  3. int buf[N];
  4. int read[N];
  5. semaphore n = 0,e=N ,s=1, priority=1;
  6.  
  7. append{
  8.     wait(priority);
  9.     wait(e);
  10.     wait(s);
  11.     buf[in] = value;
  12.     read[in] = 4;// assuer que la donner est acceder par les 4 consomateurs
  13.     in = (in+1) % N;
  14.     signal(s);
  15.     signal(n);
  16.     signal(priority);
  17. }
  18. take(int procressId){
  19.  wait(priority); //attendre qu les producteurs terminent leur traitement
  20.  signal(priority);
  21.  int message;
  22.  wait(n);
  23.  wait(s);
  24.  message = buf[out[processId]];
  25.  read[ out[processId]] --;
  26.  if (read[out[processId]] == 0){
  27.     signal(e); // notify les producteurs
  28.     buf[out[processId]] = Null; // free data
  29.  }
  30.  out[processId] = (out[processId] + 1) % N;
  31.  signal(s);
  32. }
Advertisement
Add Comment
Please, Sign In to add comment