Skip to content
Commits on Source (2)
......@@ -3,10 +3,30 @@
# Makefile
TARGETS: runsim testsim
binaries=runsim testsim
binaries=runsim testsim runsim.o testsim.o liblicense.o liblicense.a
%: %.c
gcc -o $@ $<
all: runsim testsim liblicense.a testsim.o runsim.o
#%: %.c
# gcc -o $@ $<
runsim: testsim runsim.o liblicense.a
gcc -o runsim -pthread runsim.o -L. -llicense
testsim: testsim.o liblicense.a
gcc -o testsim testsim.o -L. -llicense
testsim.o: testsim.c
gcc -c testsim.c -o testsim.o
runsim.o: runsim.c
gcc -c runsim.c -pthread -o runsim.o
liblicense.a: liblicense.o
ar rcs liblicense.a liblicense.o
liblicense.o: liblicense.c
gcc -c liblicense.c -pthread -o liblicense.o
.PHONY: clean
......
......@@ -11,4 +11,6 @@
// program expires after this amount of seconds
#define TIME_LIMIT 100
#define SEM_NAME "urschler"
#endif
// Tony Urschler
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <semaphore.h>
#include <sys/stat.h>
#include "config.h"
#include "license.h"
#define FLAGS (O_CREAT | O_EXCL)
#define PERMS (mode_t)(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
// waits for a license to become available
int getlicense(sem_t* sem, int* nlicenses) {
sem_wait(sem);
if (errno != EINTR) {
perror("License: failed to lock sem\n");
return -1;
}
*nlicenses -= 1;
return 0;
}
// returns a license
int returnlicense(sem_t* sem, int* nlicenses) {
if (sem_post(sem) == -1) {
perror("License: Failed to unlock sem\n");
return -1;
}
*nlicenses += 1;
return 0;
}
// initializes semaphore with appropriate value
// if semaphore already exists, gives access
int initlicense(sem_t **sem, int val) {
while (((*sem = sem_open(SEM_NAME, FLAGS, PERMS, val)) == SEM_FAILED) &&
(errno == EINTR));
if (*sem != SEM_FAILED)
return 0;
if (errno != EEXIST)
return -1;
while (((*sem = sem_open(SEM_NAME, 0)) == SEM_FAILED) && (errno == EINTR));
if (*sem != SEM_FAILED)
return 0;
return -1;
}
// adds n amount of licenses
int addtolicenses(int n, sem_t* sem, int* nlicenses) {
for (int i = 0; i < n; i++) {
if (sem_post(sem) == -1) {
perror("License: Failed to unlock sem\n");
return -1;
}
*nlicenses += 1;
}
return 0;
}
// removes n amount of licenses
int removelicenses(int n, sem_t* sem, int* nlicenses) {
for (int i = 0; i < n; i++) {
sem_wait(sem);
if (errno != EINTR) {
perror("License: failed to lock sem\n");
return -1;
}
*nlicenses -= 1;
}
return 0;
}
// logs msg to file
int logmsg(const char* msg) {
return 0;
}
// Tony Urschler
#ifndef LICENSE_H
#define LICENSE_H
int getlicense(sem_t* sem, int* nlicenses);
int returnlicense(sem_t* sem, int* nlicenses);
int initlicense(sem_t **sem, int val);
int addtolicenses(int n, sem_t* sem, int* nlicenses);
int removelicenses(int n, sem_t* sem, int* nlicenses);
int logmsg(const char* msg);
#endif
// Tony Urschler
#include <stdio.h>
int getlicense(void) {
return 0;
}
int returnlicense(void) {
return 0;
}
int initlicense(void) {
return 0;
}
int addtolicenses(int n) {
return 0;
}
int removelicenses(int n) {
return 0;
}
int logmsg(const char* msg) {
return 0;
}
......@@ -3,11 +3,11 @@
#ifndef LICENSE_H
#define LICENSE_H
int getlicense(void);
int returnlicense(void);
int initlicense(void);
int addtolicenses(int n);
int removelicenses(int n);
int getlicense(sem_t* sem, int* nlicenses);
int returnlicense(sem_t* sem, int* nlicenses);
int initlicense(sem_t **sem, int val);
int addtolicenses(int n, sem_t* sem, int* nlicenses);
int removelicenses(int n, sem_t* sem, int* nlicenses);
int logmsg(const char* msg);
#endif
// Tony Urschler
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
......@@ -11,6 +13,7 @@
#include <sys/wait.h>
#include <unistd.h>
#include "config.h"
#include "liblicense.h"
int segment_id;
int* nlicenses;
......@@ -74,6 +77,48 @@ int main(int argc, char* argv[]) {
*nlicenses = n;
printf("Value of memory: %d\n", *nlicenses);
// forking child
pid_t pid;
pid = fork();
// setup semaphore
sem_t* sem;
if (initlicense(&sem, n) == -1) {
perror("License initiation failed.");
exit(EXIT_FAILURE);
}
for (int i = 0; i < 5; i++) {
while(sem_wait(sem) == -1)
if (errno != EINTR) {
perror("Failed to lock semlock");
exit(EXIT_FAILURE);
}
if (pid == 0) {
sleep(.5);
printf("c\n");
}
else {
sleep(1);
printf("p\n");
}
if (sem_post(sem) == -1) {
perror("Failed to unlock semlock");
exit(EXIT_FAILURE);
}
if (pid == 0)
sleep(.5);
else
sleep(.5);
}
// close and unlink semaphore
int error;
if (sem_close(sem) == -1)
error = errno;
if ((sem_unlink(SEM_NAME) != -1) && !error);
if (error)
errno = error;
// deallocating shared memory
shmdt(nlicenses);
......
// Tony Urschler
// September 26, 2021
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
......