Add semaphore functions implementations

This commit is contained in:
2019-01-12 12:39:01 +01:00
parent 833c560621
commit 2c4471a7b5
6 changed files with 126 additions and 23 deletions

41
fb_arc_set/shared/sem.c Normal file
View File

@@ -0,0 +1,41 @@
#ifndef LIBRARIES
#define LIBRARIES
#define _GNU_SOURCE
#include <fcntl.h> ///< For O_* constants *
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#endif
#include <semaphore.h> ///< For semaphores
#define FREE_SEM_NAME "/11777707_free"
#define USED_SEM_NAME "/11777707_used"
#define MUTEX_NAME "/11777707_mutex"
static sem_t *free_sem;
static sem_t *used_sem;
static sem_t *mutex;
sem_t * open_sem(char *sem_name, size_t sem_size) {
sem_t *res = sem_open(sem_name, O_CREAT | O_EXCL, 0600, sem_size);
if(res == SEM_FAILED) {
fprintf(stderr, "ERROR: Failed opening semaphore\n");
exit(EXIT_FAILURE);
}
return res;
}
void close_sem(sem_t *sem) {
if(sem_close(sem) < 0) {
fprintf(stderr, "ERROR: Failed closing semaphore\n");
exit(EXIT_FAILURE);
}
}
void destroy_sem(char *sem_name) {
if(sem_unlink(sem_name) < 0) {
fprintf(stderr, "ERROR: Failed closing semaphore\n");
exit(EXIT_FAILURE);
}
}

View File

@@ -1,22 +1,28 @@
#ifndef LIBRARIES
#define LIBRARIES
#define _GNU_SOURCE
#include <fcntl.h>
#include <fcntl.h> ///< For O_* constants *
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#endif
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#define TERMINATE_SHM "/terminate"
#define TERMINATE_SHM_SIZE 8
#define BUFFER_SHM_NAME "/11777707_buff"
void* open_shm(char *shm_name, size_t shm_size) {
int create_shmfd(char *shm_name) {
int shmfd = shm_open(shm_name, O_RDWR | O_CREAT, 0600);
if(shmfd == -1) {
fprintf(stderr, "ERROR: Failed creating shared memory object\n");
exit(EXIT_FAILURE);
}
return shmfd;
}
void* open_shm(int shmfd, size_t shm_size) {
if(ftruncate(shmfd, shm_size) < 0) {
fprintf(stderr, "ERROR: Failed truncating shared memory object\n");
exit(EXIT_FAILURE);
@@ -44,9 +50,13 @@ void close_shm(void* shm, size_t shm_size) {
}
}
void destroy_shm(char *shm_name) {
void destroy_shm(char *shm_name, int shmfd) {
if (shm_unlink(shm_name) == -1) {
fprintf(stderr, "ERROR: Failed unlinking shared memory object\n");
exit(EXIT_FAILURE);
}
if(close(shmfd) < 0) {
fprintf(stderr, "ERROR: Failed closing file descriptor\n");
exit(EXIT_FAILURE);
}
}

View File

@@ -0,0 +1,23 @@
#include <stdbool.h> ///< For boolean constants
#define BUFF_SIZE 8
#define SET_MAX_SIZE 8
/// Struct for the edge
typedef struct edge {
int u, v;
} edge_t;
/// Struct for the feedback arc set
typedef struct fb_set {
bool valid;
edge_t edges[SET_MAX_SIZE];
} fb_set_t;
/// Struct for the circular buffer
typedef struct buffer {
bool terminate;
fb_set_t sets[BUFF_SIZE];
} buffer_t;
static buffer_t *circ_buffer;