Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/shm.h>
  4. #include <sys/sem.h>
  5. #include "settings.h"
  6. #include "semaphore.h"
  7.  
  8. int main(int argc, char* argv[]) {
  9.  
  10.     /* Create semaphores for each buffer */
  11.     int empty, full, conditions; //id's of buffers
  12.     empty = initialize_sem_array((int)EMPTY, 3, 0); //init with key, number of sems, init value
  13.     full = initialize_sem_array((int)FULL, 3, 9);
  14.     conditions = initialize_sem_array((int)CONDITIONS, 2, 0); //production limitations as specified
  15.  
  16.     /* Create shared memory for buffers
  17.      *
  18.      * bufX[MAX] holds the index of the 1st element in the queue
  19.      */
  20.     int idA, idB, idC;
  21.     int *bufA, *bufB, *bufC;
  22.     idA = shmget(BUFA, (MAX + 1) * sizeof(int), IPC_CREAT | 0600);
  23.     idB = shmget(BUFB, (MAX + 1) * sizeof(int), IPC_CREAT | 0600);
  24.     idC = shmget(BUFC, (MAX + 1) * sizeof(int), IPC_CREAT | 0600);
  25.     bufA = (int*)shmat(idA, NULL, 0);
  26.     bufB = (int*)shmat(idB, NULL, 0);
  27.     bufC = (int*)shmat(idC, NULL, 0);
  28.     if (bufA == NULL || bufB == NULL || bufC == NULL) {
  29.         perror("main: Creating shared memory for buffers failed");
  30.     }
  31.    
  32.     bufA[0] = 7;
  33.  
  34.     //printf("A: %i\n", bufA[0]);  
  35.    
  36.  
  37.  
  38.     return 0;
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement