Advertisement
Guest User

semaforki

a guest
Dec 8th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/shm.h>
  4. #include <sys/stat.h>
  5. #include <sys/ipc.h>
  6. #include <sys/sem.h>
  7. #include <sys/time.h>
  8. #include <string.h>
  9. #include <time.h>
  10. #include <math.h>
  11. #include "constants.h"
  12. #include "sem.c"
  13.  
  14. typedef struct node
  15. {
  16.     int priority;
  17.     char *data;
  18.     struct node *next;
  19. } node;
  20.  
  21. int main(int argc, char *argv[])
  22. {
  23.     int segmentSize;
  24.     if (argc != 3)
  25.     {
  26.         printf("Wrong usage");
  27.         exit(1);
  28.     }
  29.     int size = atoi(argv[2]);
  30.     segmentSize = (size + 1) * sizeof(node);
  31.     int queueId, mutexId, fullId, freeId;
  32.     char *name = argv[1];
  33.     node *queueHead, *queueTail;
  34.     queueId = shmget(shmAKey, segmentSize, IPC_CREAT | IPC_EXCL | 0666);
  35.     if (queueId < 0)
  36.     {
  37.         printf("Error: %s", strerror(errno));
  38.         queueId = shmget(shmAKey, segmentSize, 0666);
  39.     }
  40.  
  41.     queueHead = (node *)shmat(queueId, 0, 0);
  42.     mutexId = semget(mutexAKey, 1, IPC_CREAT | IPC_EXCL | 0666);
  43.     if (mutexId == -1)
  44.     {
  45.         mutexId = semget(mutexAKey, 1, 0666);
  46.     }
  47.     if (mutexId == -1)
  48.     {
  49.         printf("Mutex error\n");
  50.     }
  51.     fullId = semget(AFullKey, 1, IPC_CREAT | IPC_EXCL | 0666);
  52.     if (fullId == -1)
  53.     {
  54.         fullId = semget(AFullKey, 1, 0666);
  55.     }
  56.     if (fullId == -1)
  57.     {
  58.         printf("Full error\n");
  59.     }
  60.     freeId = semget(AFreeKey, 1, IPC_CREAT | IPC_EXCL | 0666);
  61.     if (freeId == -1)
  62.     {
  63.         freeId = semget(AFreeKey, 1, 0666);
  64.     }
  65.     if (freeId == -1)
  66.     {
  67.         printf("Free error\n");
  68.     }
  69.     semctl(freeId, 0, SETVAL, size);
  70.  
  71.     for (int i = 0; i < CYCLES; ++i)
  72.     {
  73.         struct sembuf sem = {0, -1, 0};
  74.         printf("%d", semop(freeId, &sem, 0));
  75.     }
  76.     printf("%d", semctl(freeId, 0, GETVAL));
  77.  
  78.     semctl(mutexId, 1, IPC_RMID);
  79.     semctl(fullId, 1, IPC_RMID);
  80.     semctl(freeId, 1, IPC_RMID);
  81.     shmctl(queueId, IPC_RMID, NULL);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement