Add semaphore functions implementations

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

View File

@ -5,16 +5,16 @@ TARGET_2 = supervisor
all: $(TARGET_1).c $(TARGET_2).c all: $(TARGET_1).c $(TARGET_2).c
$(CC) $(CFLAGS) $(TARGET_1).c $(TARGET_2).c $(CC) $(CFLAGS) $(TARGET_1).c $(TARGET_2).c
$(CC) $(TARGET_1).o -o $(TARGET_1) -lrt $(CC) $(TARGET_1).o -o $(TARGET_1) -lrt -lpthread
$(CC) $(TARGET_2).o -o $(TARGET_2) -lrt $(CC) $(TARGET_2).o -o $(TARGET_2) -lrt -lpthread
generator: $(TARGET_1).c generator: $(TARGET_1).c
$(CC) $(CFLAGS) $(TARGET_1).c $(CC) $(CFLAGS) $(TARGET_1).c
$(CC) $(TARGET_1).o -o $(TARGET_1) -lrt $(CC) $(TARGET_1).o -o $(TARGET_1) -lrt -lpthread
supervisor: $(TARGET_2).c supervisor: $(TARGET_2).c
$(CC) $(CFLAGS) $(TARGET_2).c $(CC) $(CFLAGS) $(TARGET_2).c
$(CC) $(TARGET_2).o -o $(TARGET_2) -lrt $(CC) $(TARGET_2).o -o $(TARGET_2) -lrt -lpthread
install: install:
cp $(TARGET_1) /usr/local/bin/graph-$(TARGET_1) cp $(TARGET_1) /usr/local/bin/graph-$(TARGET_1)

View File

@ -27,6 +27,8 @@
* *
**/ **/
#include "shared/shm.c" #include "shared/shm.c"
#include "shared/sem.c"
#include "shared/structs.c"
#include <regex.h> #include <regex.h>
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
@ -36,25 +38,33 @@ int main(int argc, char *argv[]) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/// Create a regex to check the input against
regex_t regex; regex_t regex;
if(regcomp(&regex, "^[0-9]*-[0-9]*$", REG_EXTENDED|REG_NOSUB)) { if(regcomp(&regex, "^[0-9]*-[0-9]*$", REG_EXTENDED|REG_NOSUB)) {
fprintf(stderr, "ERROR: Could not compile regex\n"); fprintf(stderr, "ERROR: Could not compile regex\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
for(int i=1; i <= argc - 1; i++) { edge_t edges[argc - 2]; ///< Array to hold the edges
char edge[128]; ///< Variable to hold a single edge later on
edge[127] = 0;
for(int i=1; i < argc; i++) {
/// Check the input and terminate if incorrect
if(regexec(&regex, argv[i], 0, NULL, 0) == REG_NOMATCH) { if(regexec(&regex, argv[i], 0, NULL, 0) == REG_NOMATCH) {
fprintf(stderr, "ERROR: Incorrect input found\n"); fprintf(stderr, "ERROR: Incorrect input found\n");
puts("Usage: \ngenerator EDGE1 EDGE2 ..."); puts("Usage: \ngenerator EDGE1 EDGE2 ...");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} else {
/// If input is correct, split and add to the edges array
strcpy(edge, argv[i]);
char *u = strtok(edge, "-");
char *v = strtok(NULL, "");
edges[i-1].u = strtol(u, NULL, 10);
edges[i-1].v = strtol(v, NULL, 10);
} }
} }
void* terminate_shm = open_shm(TERMINATE_SHM, TERMINATE_SHM_SIZE); exit(EXIT_SUCCESS);
write_to_shm(terminate_shm, "1", TERMINATE_SHM_SIZE);
puts(terminate_shm);
close_shm(terminate_shm, TERMINATE_SHM_SIZE);
destroy_shm(TERMINATE_SHM);
} }

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 #define _GNU_SOURCE
#include <fcntl.h> #include <fcntl.h> ///< For O_* constants *
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#endif
#include <string.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h>
#include <string.h>
#define TERMINATE_SHM "/terminate" #define BUFFER_SHM_NAME "/11777707_buff"
#define TERMINATE_SHM_SIZE 8
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); int shmfd = shm_open(shm_name, O_RDWR | O_CREAT, 0600);
if(shmfd == -1) { if(shmfd == -1) {
fprintf(stderr, "ERROR: Failed creating shared memory object\n"); fprintf(stderr, "ERROR: Failed creating shared memory object\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return shmfd;
}
void* open_shm(int shmfd, size_t shm_size) {
if(ftruncate(shmfd, shm_size) < 0) { if(ftruncate(shmfd, shm_size) < 0) {
fprintf(stderr, "ERROR: Failed truncating shared memory object\n"); fprintf(stderr, "ERROR: Failed truncating shared memory object\n");
exit(EXIT_FAILURE); 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) { if (shm_unlink(shm_name) == -1) {
fprintf(stderr, "ERROR: Failed unlinking shared memory object\n"); fprintf(stderr, "ERROR: Failed unlinking shared memory object\n");
exit(EXIT_FAILURE); 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;

View File

@ -22,6 +22,8 @@
* *
**/ **/
#include "shared/shm.c" #include "shared/shm.c"
#include "shared/sem.c"
#include "shared/structs.c"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if(argc > 1) { if(argc > 1) {
@ -29,9 +31,26 @@ int main(int argc, char *argv[]) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
void* terminate_shm = open_shm(TERMINATE_SHM, TERMINATE_SHM_SIZE); /// Open shared memory objects
int buffer_shmfd = create_shmfd(BUFFER_SHM_NAME);
circ_buffer = (buffer_t *)open_shm(buffer_shmfd, sizeof(buffer_t));
write_to_shm(terminate_shm, "1", TERMINATE_SHM_SIZE); /// Open semaphores
close_shm(terminate_shm, TERMINATE_SHM_SIZE); free_sem = open_sem(FREE_SEM_NAME, BUFF_SIZE);
destroy_shm(TERMINATE_SHM); used_sem = open_sem(USED_SEM_NAME, 0);
mutex = open_sem(MUTEX_NAME, 1);
/// Close and destory semaphores
close_sem(free_sem);
close_sem(used_sem);
close_sem(mutex);
destroy_sem(FREE_SEM_NAME);
destroy_sem(USED_SEM_NAME);
destroy_sem(MUTEX_NAME);
/// Close and destroy shared memory objects
close_shm(circ_buffer, sizeof(buffer_t));
destroy_shm(BUFFER_SHM_NAME, buffer_shmfd);
} }