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
004ee52a
Commit
004ee52a
authored
Jan 13, 2019
by
Ivaylo Ivanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add shuffler and paramaterize the open_sem function additionally
parent
8c423aaa
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
81 additions
and
6 deletions
+81
-6
fb_arc_set/generator.c
fb_arc_set/generator.c
+66
-1
fb_arc_set/shared/sem.c
fb_arc_set/shared/sem.c
+12
-3
fb_arc_set/supervisor.c
fb_arc_set/supervisor.c
+3
-2
No files found.
fb_arc_set/generator.c
View file @
004ee52a
...
...
@@ -30,6 +30,10 @@
#include "shared/sem.c"
#include "shared/structs.c"
#include <regex.h>
#include <time.h>
void
swap
(
int
*
a
,
int
*
b
);
void
shuffle
(
int
arr
[],
size_t
size
);
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
<
2
)
{
...
...
@@ -76,13 +80,74 @@ int main(int argc, char *argv[]) {
}
num_nodes
++
;
///< Our first node is labeled 0, so we need to add 1 more
printf
(
"%d"
,
num_nodes
);
/// Declare arrays for unique nodes and for the chosen edges
int
unique_nodes
[
num_nodes
];
bool
chosen_edges
[
num_edges
];
/// Filling the node array with the nodes
for
(
int
i
=
0
;
i
<
num_nodes
;
i
++
)
{
unique_nodes
[
i
]
=
i
;
}
/// Marking all edges as not chosen
for
(
int
i
=
0
;
i
<
num_edges
;
i
++
)
{
chosen_edges
[
i
]
=
false
;
}
/// Allocate resources for circular buffer
int
wr_pos
=
0
;
buffer_shmfd
=
create_shmfd
(
BUFF_SHM_NAME
);
circ_buffer
=
(
buffer_t
*
)
open_shm
(
buffer_shmfd
,
sizeof
(
buffer_t
));
/// Open semaphores
free_sem
=
open_sem
(
FREE_SEM_NAME
,
BUFF_SIZE
,
1
);
use_sem
=
open_sem
(
USE_SEM_NAME
,
0
,
1
);
mutex
=
open_sem
(
MUTEX_NAME
,
1
,
1
);
int
best_solution
=
num_edges
-
1
;
///< Set a basic best solution
/// Declaring buffer arrays
int
buf_nodes
[
num_nodes
];
int
buf_chosen_edges
[
num_edges
];
while
(
terminate
!=
true
&&
circ_buffer
->
finished
!=
true
)
{
///< Loop until told to stop or until an acyclic solution is discovered
/// Copy the original into a buffer array and shuffle it
memcpy
(
buf_nodes
,
unique_nodes
,
sizeof
(
unique_nodes
));
shuffle
(
buf_nodes
,
num_nodes
);
/// Reset the chosen status of the array
memcpy
(
buf_chosen_edges
,
chosen_edges
,
sizeof
(
chosen_edges
));
}
exit
(
EXIT_SUCCESS
);
}
/**
* @brief Function that swaps a two variables
* @details The function takes pointers to two variables and swaps their values with a buffer variable
* @param *a, *b
* @return none
**/
void
swap
(
int
*
a
,
int
*
b
)
{
int
temp
=
*
a
;
*
a
=
*
b
;
*
b
=
temp
;
}
/**
* @brief Function that shuffles a given array
* @details The function takes an array and shuffles it randomly using the Fisher-Yates algorithm:
* https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
* @param arr, size
* @return res
**/
void
shuffle
(
int
arr
[],
size_t
size
)
{
srand
(
time
(
NULL
));
///< Set a random seed
int
j
=
0
;
///< Index that will be randomized later on
for
(
int
i
=
size
-
1
;
i
>
0
;
i
--
)
{
j
=
rand
()
%
(
i
+
1
);
///< Pick a random index
swap
(
&
arr
[
i
],
&
arr
[
j
]);
///< Swap the values
}
}
fb_arc_set/shared/sem.c
View file @
004ee52a
...
...
@@ -22,12 +22,21 @@ static sem_t *mutex;
* @brief Function that executes sem_open(3) and does error handling.
* @details The function executes sem_open(3), giving it a semaphor name and size as parameters,
* and handles the errors if any. The same behaviour as sem_open(3) should be expected.
* The type parameter is used to determine how the semaphore should be opened:
* - as a supervisor: 0
* - as a generator: 1
* Returns a new semaphore
* @param sem_name, sem_size
* @param sem_name, sem_size
, type
* @return res
**/
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
);
sem_t
*
open_sem
(
char
*
sem_name
,
size_t
sem_size
,
short
type
)
{
sem_t
*
res
;
if
(
type
==
0
)
res
=
sem_open
(
sem_name
,
O_CREAT
|
O_EXCL
,
0600
,
sem_size
);
else
res
=
sem_open
(
sem_name
,
sem_size
);
if
(
res
==
SEM_FAILED
)
{
fprintf
(
stderr
,
"ERROR: Failed opening semaphore
\n
"
);
exit
(
EXIT_FAILURE
);
...
...
fb_arc_set/supervisor.c
View file @
004ee52a
...
...
@@ -46,8 +46,9 @@ int main(int argc, char *argv[]) {
circ_buffer
=
(
buffer_t
*
)
open_shm
(
buffer_shmfd
,
sizeof
(
buffer_t
));
/// Open semaphores
free_sem
=
open_sem
(
FREE_SEM_NAME
,
BUFF_SIZE
);
use_sem
=
open_sem
(
USE_SEM_NAME
,
0
);
free_sem
=
open_sem
(
FREE_SEM_NAME
,
BUFF_SIZE
,
0
);
use_sem
=
open_sem
(
USE_SEM_NAME
,
0
,
0
);
mutex
=
open_sem
(
MUTEX_NAME
,
1
,
0
);
int
free_space
=
0
;
int
use_space
=
0
;
...
...
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