Skip to content
GitLab
Explore
Sign in
teudmk
urschler.3
Compare revisions
8c02c8fc3763472f244a3abc8083823b189d3de0 to bab21eb5c2b1a595db32b4306d59fafa12d9097e
Commits on Source (2)
Added all functionality to license.c except logmsg
· 0f4aedb2
teudmk
authored
Oct 02, 2021
0f4aedb2
Runsim now can for a child and use semaphore to protect a critical section
· bab21eb5
teudmk
authored
Oct 06, 2021
bab21eb5
Hide whitespace changes
Inline
Side-by-side
Makefile
View file @
bab21eb5
...
...
@@ -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
...
...
config.h
View file @
bab21eb5
...
...
@@ -11,4 +11,6 @@
// program expires after this amount of seconds
#define TIME_LIMIT 100
#define SEM_NAME "urschler"
#endif
liblicense.c
0 → 100644
View file @
bab21eb5
// 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
;
}
liblicense.h
0 → 100644
View file @
bab21eb5
// 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
license.c
deleted
100644 → 0
View file @
8c02c8fc
// 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
;
}
license.h
View file @
bab21eb5
...
...
@@ -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
runsim
deleted
100755 → 0
View file @
8c02c8fc
File deleted
runsim.c
View file @
bab21eb5
// 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
);
...
...
testsim
deleted
100755 → 0
View file @
8c02c8fc
File deleted
testsim.c
View file @
bab21eb5
// Tony Urschler
// September 26, 2021
#include
<semaphore.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
...
...