Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
unix
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
tu-wien
unix
Commits
2c4471a7
Commit
2c4471a7
authored
Jan 12, 2019
by
Ivaylo Ivanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add semaphore functions implementations
parent
833c5606
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
126 additions
and
23 deletions
+126
-23
fb_arc_set/Makefile
fb_arc_set/Makefile
+4
-4
fb_arc_set/generator.c
fb_arc_set/generator.c
+18
-8
fb_arc_set/shared/sem.c
fb_arc_set/shared/sem.c
+41
-0
fb_arc_set/shared/shm.c
fb_arc_set/shared/shm.c
+17
-7
fb_arc_set/shared/structs.c
fb_arc_set/shared/structs.c
+23
-0
fb_arc_set/supervisor.c
fb_arc_set/supervisor.c
+23
-4
No files found.
fb_arc_set/Makefile
View file @
2c4471a7
...
...
@@ -5,16 +5,16 @@ TARGET_2 = supervisor
all
:
$(TARGET_1).c $(TARGET_2).c
$(CC)
$(CFLAGS)
$(TARGET_1)
.c
$(TARGET_2)
.c
$(CC)
$(TARGET_1)
.o
-o
$(TARGET_1)
-lrt
$(CC)
$(TARGET_2)
.o
-o
$(TARGET_2)
-lrt
$(CC)
$(TARGET_1)
.o
-o
$(TARGET_1)
-lrt
-lpthread
$(CC)
$(TARGET_2)
.o
-o
$(TARGET_2)
-lrt
-lpthread
generator
:
$(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
$(CC)
$(CFLAGS)
$(TARGET_2)
.c
$(CC)
$(TARGET_2)
.o
-o
$(TARGET_2)
-lrt
$(CC)
$(TARGET_2)
.o
-o
$(TARGET_2)
-lrt
-lpthread
install
:
cp
$(TARGET_1)
/usr/local/bin/graph-
$(TARGET_1)
...
...
fb_arc_set/generator.c
View file @
2c4471a7
...
...
@@ -27,6 +27,8 @@
*
**/
#include "shared/shm.c"
#include "shared/sem.c"
#include "shared/structs.c"
#include <regex.h>
int
main
(
int
argc
,
char
*
argv
[])
{
...
...
@@ -36,25 +38,33 @@ int main(int argc, char *argv[]) {
exit
(
EXIT_FAILURE
);
}
/// Create a regex to check the input against
regex_t
regex
;
if
(
regcomp
(
&
regex
,
"^[0-9]*-[0-9]*$"
,
REG_EXTENDED
|
REG_NOSUB
))
{
fprintf
(
stderr
,
"ERROR: Could not compile regex
\n
"
);
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
)
{
fprintf
(
stderr
,
"ERROR: Incorrect input found
\n
"
);
puts
(
"Usage:
\n
generator EDGE1 EDGE2 ..."
);
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
);
write_to_shm
(
terminate_shm
,
"1"
,
TERMINATE_SHM_SIZE
);
puts
(
terminate_shm
);
close_shm
(
terminate_shm
,
TERMINATE_SHM_SIZE
);
destroy_shm
(
TERMINATE_SHM
);
exit
(
EXIT_SUCCESS
);
}
fb_arc_set/shared/sem.c
0 → 100644
View file @
2c4471a7
#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
);
}
}
fb_arc_set/shared/shm.c
View file @
2c4471a7
#ifndef LIBRARIES
#define LIBRARIES
#define _GNU_SOURCE
#include <fcntl.h>
#include <fcntl.h>
///< For O_* constants *
#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#endif
#include <string.h>
#include <sys/mman.h>
#include <sys/types.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_siz
e
)
{
int
create_shmfd
(
char
*
shm_nam
e
)
{
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
);
}
}
fb_arc_set/shared/structs.c
0 → 100644
View file @
2c4471a7
#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
;
fb_arc_set/supervisor.c
View file @
2c4471a7
...
...
@@ -22,6 +22,8 @@
*
**/
#include "shared/shm.c"
#include "shared/sem.c"
#include "shared/structs.c"
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
>
1
)
{
...
...
@@ -29,9 +31,26 @@ int main(int argc, char *argv[]) {
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
);
close_shm
(
terminate_shm
,
TERMINATE_SHM_SIZE
);
destroy_shm
(
TERMINATE_SHM
);
/// Open semaphores
free_sem
=
open_sem
(
FREE_SEM_NAME
,
BUFF_SIZE
);
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
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment