Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
ALP4-Tutorials
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
mactavish96
ALP4-Tutorials
Commits
6605b30a
Commit
6605b30a
authored
2 years ago
by
Mactavish
Browse files
Options
Downloads
Patches
Plain Diff
add solution and makefile
parent
8e2f9358
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
exercises/approximate-counter/Makefile
+19
-0
19 additions, 0 deletions
exercises/approximate-counter/Makefile
exercises/approximate-counter/counter.c
+43
-9
43 additions, 9 deletions
exercises/approximate-counter/counter.c
with
62 additions
and
9 deletions
exercises/approximate-counter/Makefile
0 → 100644
+
19
−
0
View file @
6605b30a
PROG
=
main
OBJECTS
=
main.o counter.o
CC
=
gcc
CFLAGS
=
-Wall
-std
=
c99
-pthread
CFLAGS
+=
-I
.
# add the current directory to the include path
.Phone
:
all
all
:
clean $(PROG)
#
build and run the program
./
$(
PROG
)
$(PROG)
:
$(OBJECTS)
#
link the object files into a binary
$(
CC
)
$(
CFLAGS
)
$^
-o
$@
$(OBJECTS)
:
%.o: %.c counter.h
#
compile the source files into object files
$(
CC
)
$(
CFLAGS
)
-c
$<
.PHONY
:
clean
clean
:
#
remove the object files and the binary
rm
-f
$(
OBJECTS
)
$(
PROG
)
This diff is collapsed.
Click to expand it.
exercises/approximate-counter/counter.c
+
43
−
9
View file @
6605b30a
#include
"counter.h"
void
init_counter
(
counter_t
*
c
,
int
threshold
,
int
num_cpu
)
{
// TODO: Implement this function
c
->
num_cpu
=
num_cpu
;
c
->
threshold
=
threshold
;
c
->
global
=
0
;
c
->
local
=
malloc
(
num_cpu
*
sizeof
(
long
));
c
->
llock
=
malloc
(
num_cpu
*
sizeof
(
pthread_mutex_t
));
pthread_mutex_init
(
&
c
->
glock
,
NULL
);
int
i
;
for
(
i
=
0
;
i
<
num_cpu
;
i
++
)
{
c
->
local
[
i
]
=
0
;
pthread_mutex_init
(
&
c
->
llock
[
i
],
NULL
);
}
}
void
update_counter
(
counter_t
*
c
,
int
t_id
,
int
amount
)
{
// TODO: Implement this function
void
update_counter
(
counter_t
*
c
,
int
t_id
,
int
amount
)
{
int
cpu
=
t_id
%
c
->
num_cpu
;
pthread_mutex_lock
(
&
c
->
llock
[
cpu
]);
c
->
local
[
cpu
]
+=
amount
;
if
(
c
->
local
[
cpu
]
>=
c
->
threshold
)
{
pthread_mutex_lock
(
&
c
->
glock
);
c
->
global
+=
c
->
local
[
cpu
];
pthread_mutex_unlock
(
&
c
->
glock
);
c
->
local
[
cpu
]
=
0
;
}
pthread_mutex_unlock
(
&
c
->
llock
[
cpu
]);
}
int
get_counter_val
(
counter_t
*
c
)
{
pthread_mutex_lock
(
&
c
->
glock
);
int
val
=
c
->
global
;
pthread_mutex_unlock
(
&
c
->
glock
);
return
val
;
pthread_mutex_lock
(
&
c
->
glock
);
int
val
=
c
->
global
;
pthread_mutex_unlock
(
&
c
->
glock
);
return
val
;
}
void
sync_counter
(
counter_t
*
c
)
{
// TODO: Implement this function
int
i
;
for
(
i
=
0
;
i
<
c
->
num_cpu
;
i
++
)
{
pthread_mutex_lock
(
&
c
->
llock
[
i
]);
pthread_mutex_lock
(
&
c
->
glock
);
c
->
global
+=
c
->
local
[
i
];
pthread_mutex_unlock
(
&
c
->
glock
);
c
->
local
[
i
]
=
0
;
pthread_mutex_unlock
(
&
c
->
llock
[
i
]);
}
}
void
destroy_counter
(
counter_t
*
c
)
{
// TODO: Implement this function
pthread_mutex_destroy
(
&
c
->
glock
);
int
i
;
for
(
i
=
0
;
i
<
c
->
num_cpu
;
i
++
)
{
pthread_mutex_destroy
(
&
c
->
llock
[
i
]);
}
free
(
c
->
local
);
free
(
c
->
llock
);
}
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