Shared memory and semaphore setup
This commit is contained in:
parent
9ac6d9fbfe
commit
e3095f8ce0
58
supervisor.c
58
supervisor.c
@ -9,6 +9,8 @@
|
|||||||
|
|
||||||
static const char *pname;
|
static const char *pname;
|
||||||
|
|
||||||
|
struct circ_buf *shared;
|
||||||
|
|
||||||
struct sigaction sa;
|
struct sigaction sa;
|
||||||
|
|
||||||
static volatile sig_atomic_t quit = 0;
|
static volatile sig_atomic_t quit = 0;
|
||||||
@ -25,6 +27,40 @@ void usage(void)
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void initCircBuf()
|
||||||
|
{
|
||||||
|
shared->validItems = 0;
|
||||||
|
shared->head = 0;
|
||||||
|
shared->tail = 0;
|
||||||
|
shared->quit = 0;
|
||||||
|
for (int i = 0; i < MAX_ITEMS; i++) {
|
||||||
|
shared->data[i] = 0;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int isEmpty()
|
||||||
|
{
|
||||||
|
if (shared->validItems == 0)
|
||||||
|
return 1;
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getItem()
|
||||||
|
{
|
||||||
|
if (isEmpty())
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
int value;
|
||||||
|
|
||||||
|
value = shared->data[shared->head];
|
||||||
|
shared->head = (shared->head + 1) % MAX_ITEMS;
|
||||||
|
shared->validItems--;
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if (argc != 1)
|
if (argc != 1)
|
||||||
@ -37,7 +73,29 @@ int main(int argc, char *argv[])
|
|||||||
sigaction(SIGINT, &sa, NULL);
|
sigaction(SIGINT, &sa, NULL);
|
||||||
sigaction(SIGTERM, &sa, NULL);
|
sigaction(SIGTERM, &sa, NULL);
|
||||||
|
|
||||||
|
int shmfd = shm_open(SHM_NAME, O_RDWR | O_CREAT, PERMISSION);
|
||||||
|
if (shmfd == -1) {
|
||||||
|
perror(pname);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
if (ftruncate(shmfd, sizeof(*shared)) == -1) {
|
||||||
|
perror(pname);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
shared = mmap(NULL, sizeof(*shared), PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0);
|
||||||
|
if (shared == MAP_FAILED) {
|
||||||
|
perror(pname);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
sem_t *sFreeSpace = sem_open(SEM_FREE_SPACE, O_CREAT | O_EXCL, PERMISSION, MAX_ITEMS);
|
||||||
|
sem_t *sUsedSpace = sem_open(SEM_USED_SPACE, O_CREAT | O_EXCL, PERMISSION, 0);
|
||||||
|
|
||||||
|
initCircBuf();
|
||||||
|
|
||||||
while (quit == 0) {
|
while (quit == 0) {
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user