Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
uva
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Taddeüs Kroes
uva
Commits
61825fd6
Commit
61825fd6
authored
14 years ago
by
Sander van Veen
Browse files
Options
Downloads
Patches
Plain Diff
Continued assignment 4 of Operating Systems.
parent
834a9bcd
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
os/.gitignore
+1
-0
1 addition, 0 deletions
os/.gitignore
os/ass4/Makefile
+7
-1
7 additions, 1 deletion
os/ass4/Makefile
os/ass4/ass4
+0
-0
0 additions, 0 deletions
os/ass4/ass4
os/ass4/main.c
+33
-15
33 additions, 15 deletions
os/ass4/main.c
paper/paper.tex
+1
-1
1 addition, 1 deletion
paper/paper.tex
with
42 additions
and
17 deletions
os/.gitignore
+
1
−
0
View file @
61825fd6
ass*/ass*
*.o
This diff is collapsed.
Click to expand it.
os/ass4/Makefile
+
7
−
1
View file @
61825fd6
PROG
=
ass4
CC
=
gcc
CFLAGS
=
-std
=
c99
-pedantic
-Wall
-Wextra
#
-O3
-D_POSIX_SOURCE
CFLAGS
=
-std
=
c99
-pedantic
-Wall
-Wextra
# -D_POSIX_SOURCE
LFLAGS
=
-lpthread
OFILES
=
main.o
RM
=
rm
-f
ifdef
DEBUG
CFLAGS
+=
-ggdb
else
CFLAGS
+=
-O3
endif
$(PROG)
:
$(OFILES)
$(
CC
)
$(
CFLAGS
)
$(
LFLAGS
)
-o
$@
$(
OFILES
)
...
...
This diff is collapsed.
Click to expand it.
os/ass4/ass4
deleted
100755 → 0
+
0
−
0
View file @
834a9bcd
File deleted
This diff is collapsed.
Click to expand it.
os/ass4/main.c
+
33
−
15
View file @
61825fd6
...
...
@@ -2,33 +2,46 @@
#include
"stdlib.h"
#include
"pthread.h"
/* temporary include: */
#include
<unistd.h>
int
forks_len
;
pthread_mutex_t
*
forks
;
pthread_cond_t
wait_threshold
;
typedef
struct
diner_stats
{
int
meals
;
}
diner_stats
;
int
count
=
0
;
#define COUNT_LIMIT 12
void
*
philo_start
(
void
*
t_id
)
{
printf
(
"P #%d: hello everybody!
\n
"
,
(
int
)
t_id
);
void
*
philo_start
()
{
int
t_id
=
pthread_self
();
printf
(
"P #%d: hello everybody!
\n
"
,
t_id
);
for
(
int
i
=
0
;
i
<
5
;
i
++
)
{
// Let each philosopher take a random fork.
int
f
=
rand
()
%
forks_len
;
pthread_mutex_lock
(
&
forks
[
f
]);
// Check the value of count and signal waiting thread when condition is
// reached. Note that this occurs while mutex is locked.
pthread_cond_signal
(
&
wait_threshold
);
//int f = rand() % forks_len;
//pthread_mutex_lock(&forks[f]);
pthread_mutex_lock
(
&
forks
[
t_id
]);
pthread_mutex_lock
(
&
forks
[(
t_id
+
1
)
%
forks_len
]);
//while( pthread_mutex_trylock(&forks[(t_id+1) % forks_len]) ) {
// pthread_mutex_unlock(&forks[t_id]);
// // Check the value of count and signal waiting thread when condition is
// // reached. Note that this occurs while mutex is locked.
// //pthread_cond_signal(&wait_threshold);
// sleep(1);
// pthread_mutex_lock(&forks[t_id]);
//}
// Do something useless
printf
(
"P #%d: I'm eating.
\n
"
,
(
int
)
t_id
);
sleep
(
1
);
printf
(
"P #%d: I'm eating.
\n
"
,
t_id
);
pthread_mutex_unlock
(
&
forks
[
f
]);
pthread_mutex_unlock
(
&
forks
[
t_id
]);
pthread_mutex_unlock
(
&
forks
[(
t_id
+
1
)
%
forks_len
]);
}
pthread_exit
(
NULL
);
...
...
@@ -38,10 +51,11 @@ void host_start(int philos) {
pthread_attr_t
attr
;
pthread_t
*
threads
=
malloc
(
philos
*
sizeof
(
pthread_t
));
forks
=
malloc
(
philos
*
sizeof
(
pthread_mutex_t
));
diner_stats
*
stats
=
malloc
(
philos
*
sizeof
(
diner_stats
));
forks_len
=
philos
;
int
rc
;
void
*
status
;
int
rc
;
// POSIX standard specifies that threads are joinable by default, but
// unfortunately, not all pthread implementations set threads as joinable by
...
...
@@ -52,14 +66,18 @@ void host_start(int philos) {
pthread_cond_init
(
&
wait_threshold
,
NULL
);
// Create the cutlery.
for
(
int
i
=
0
;
i
<
philos
;
i
++
)
for
(
int
i
=
0
;
i
<
philos
;
i
++
)
{
pthread_mutex_init
(
&
forks
[
i
],
NULL
);
stats
[
i
].
meals
=
0
;
}
// Invite the philosophers.
for
(
int
i
=
0
;
i
<
philos
;
i
++
)
{
printf
(
"Host: inviting philo #%d
\n
"
,
i
);
rc
=
pthread_create
(
threads
+
i
,
&
attr
,
philo_start
,
(
void
*
)
&
stats
[
i
]);
if
(
(
rc
=
pthread_create
(
&
threads
[
i
],
&
attr
,
philo_start
,
(
void
*
)
i
))
)
{
if
(
rc
)
{
fprintf
(
stderr
,
"pthread_create() returned: %d
\n
"
,
rc
);
exit
(
-
1
);
}
...
...
This diff is collapsed.
Click to expand it.
paper/paper.tex
+
1
−
1
View file @
61825fd6
TT
\documentclass
[twocolumn]
{
article
}
\documentclass
[twocolumn]
{
article
}
\usepackage
{
url
}
\title
{
Web-based digital examination for specific learning areas
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment