Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/shm.h>
- #include <sys/stat.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #include <sys/time.h>
- #include <string.h>
- #include <time.h>
- #include <math.h>
- #include "constants.h"
- #include "sem.c"
- typedef struct node
- {
- int priority;
- char *data;
- struct node *next;
- } node;
- int main(int argc, char *argv[])
- {
- int segmentSize;
- if (argc != 3)
- {
- printf("Wrong usage");
- exit(1);
- }
- int size = atoi(argv[2]);
- segmentSize = (size + 1) * sizeof(node);
- int queueId, mutexId, fullId, freeId;
- char *name = argv[1];
- node *queueHead, *queueTail;
- queueId = shmget(shmAKey, segmentSize, IPC_CREAT | IPC_EXCL | 0666);
- if (queueId < 0)
- {
- printf("Error: %s", strerror(errno));
- queueId = shmget(shmAKey, segmentSize, 0666);
- }
- queueHead = (node *)shmat(queueId, 0, 0);
- mutexId = semget(mutexAKey, 1, IPC_CREAT | IPC_EXCL | 0666);
- if (mutexId == -1)
- {
- mutexId = semget(mutexAKey, 1, 0666);
- }
- if (mutexId == -1)
- {
- printf("Mutex error\n");
- }
- fullId = semget(AFullKey, 1, IPC_CREAT | IPC_EXCL | 0666);
- if (fullId == -1)
- {
- fullId = semget(AFullKey, 1, 0666);
- }
- if (fullId == -1)
- {
- printf("Full error\n");
- }
- freeId = semget(AFreeKey, 1, IPC_CREAT | IPC_EXCL | 0666);
- if (freeId == -1)
- {
- freeId = semget(AFreeKey, 1, 0666);
- }
- if (freeId == -1)
- {
- printf("Free error\n");
- }
- semctl(freeId, 0, SETVAL, size);
- for (int i = 0; i < CYCLES; ++i)
- {
- struct sembuf sem = {0, -1, 0};
- printf("%d", semop(freeId, &sem, 0));
- }
- printf("%d", semctl(freeId, 0, GETVAL));
- semctl(mutexId, 1, IPC_RMID);
- semctl(fullId, 1, IPC_RMID);
- semctl(freeId, 1, IPC_RMID);
- shmctl(queueId, IPC_RMID, NULL);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement