Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
exercises
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
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
ISC2
pco
exercises
Commits
a9d7ef5c
Commit
a9d7ef5c
authored
1 year ago
by
iliya
Browse files
Options
Downloads
Patches
Plain Diff
todo, prev exercises
parent
08e82543
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
serie4/ex1/ex1.md
+34
-0
34 additions, 0 deletions
serie4/ex1/ex1.md
serie4/ex2/.gitignore
+2
-0
2 additions, 0 deletions
serie4/ex2/.gitignore
serie4/ex2/Makefile
+26
-0
26 additions, 0 deletions
serie4/ex2/Makefile
serie4/ex2/prog.c
+53
-0
53 additions, 0 deletions
serie4/ex2/prog.c
with
115 additions
and
0 deletions
serie4/ex1/ex1.md
0 → 100644
+
34
−
0
View file @
a9d7ef5c
# Exercice 1
## Question
Est-ce que l'algorithme ci-dessous garanti l'exclusion mutuelle sur un monoprocesseur (exclusion
mutuelle, progression, attente bornée) entre les threads T0 et T1 ? Si ce n’est pas la cas, justifiez
votre réponse en décrivant une séquence possible d’opérations qui prouve le contraire.
```
c
bool
inside
[
2
]
=
{
false
,
false
};
// id is the thread number (0 or 1)
void
prelude
(
int
id
)
{
while
(
inside
[
1
-
id
])
{}
inside
[
id
]
=
true
;
}
void
postlude
(
int
id
)
{
inside
[
id
]
=
false
;
}
```
## Réponse
Vu que nous sommes sur une architecture monoprocesseur, cela signifie que le
code ne pourra pas s'exécuter de manière parallèle, il n'y aura que des
changements de contexte.
Si l'on part du principe que T0 rentre dans la fonction
`prelude`
, exécute la
ligne correspondante à la boucle
`while (false)`
qu'à la suite de cela, il y'a
un changement de contexte dans lequel T1 exécute les deux lignes de la fonction
`prelude`
et que finalement T0 reprend l'exécution, à la fin on aboutira avec
les deux variables du tableau
`inside`
à
`true`
ce qui implique le fait que cet
algorithme ne garanti pas l'exclusion mutuelle car les deux variables sont à
`true`
.
This diff is collapsed.
Click to expand it.
serie4/ex2/.gitignore
0 → 100644
+
2
−
0
View file @
a9d7ef5c
*.o
prog
This diff is collapsed.
Click to expand it.
serie4/ex2/Makefile
0 → 100644
+
26
−
0
View file @
a9d7ef5c
CC
:=
clang
CFLAGS
:=
-g
-pedantic
-Wall
-Wextra
-std
=
c11
LDFLAGS
:=
-fsanitize
=
address
-fsanitize
=
leak
-fsanitize
=
undefined
-lm
-lpthread
TARGET
:=
prog
all
:
$(TARGET)
$(TARGET)
:
prog.o
@
printf
"=================== Building executable ===================
\n
"
$(
CC
)
$(
CFLAGS
)
$^
-o
$@
$(
LDFLAGS
)
@
printf
"
\n
"
%.o
:
%.c
@
printf
"================== Building object files ==================
\n
"
$(
CC
)
$(
CFLAGS
)
-c
$<
@
printf
"
\n
"
.PHONY
:
clean
clean
:
rm
-f
*
.o
$(
TARGET
)
.PHONY
:
rebuild
rebuild
:
clean all
This diff is collapsed.
Click to expand it.
serie4/ex2/prog.c
0 → 100644
+
53
−
0
View file @
a9d7ef5c
#include
<pthread.h>
#include
<stdbool.h>
#include
<stdio.h>
#include
<stdlib.h>
#define NB_THREADS 2
#define MAX_INCREMENT 10000
int
lock
;
int
counter
;
void
init_lock
(
int
*
lock_ptr
)
{
__sync_val_compare_and_swap
(
lock_ptr
,
0
,
0
);
}
void
acquire_lock
(
int
*
lock_ptr
)
{
__sync_val_compare_and_swap
(
lock_ptr
,
*
lock_ptr
,
1
);
}
void
release_lock
(
int
*
lock_ptr
)
{
__sync_val_compare_and_swap
(
lock_ptr
,
*
lock_ptr
,
0
);
}
void
*
routine
(
void
*
arg
)
{
int
*
counter
=
(
int
*
)
arg
;
acquire_lock
(
&
lock
);
for
(
int
i
=
0
;
i
<
MAX_INCREMENT
;
i
++
)
{
(
*
counter
)
++
;
}
release_lock
(
&
lock
);
return
NULL
;
}
int
main
(
void
)
{
pthread_t
threads
[
NB_THREADS
];
init_lock
(
&
lock
);
for
(
int
i
=
0
;
i
<
NB_THREADS
;
i
++
)
{
if
(
pthread_create
(
&
threads
[
i
],
NULL
,
routine
,
&
counter
)
==
-
1
)
{
perror
(
"pthread_create"
);
}
}
for
(
int
i
=
0
;
i
<
NB_THREADS
;
i
++
)
{
if
(
pthread_join
(
threads
[
i
],
NULL
))
{
perror
(
"pthread_join"
);
}
}
fprintf
(
stdout
,
"Value of counter = %d
\n
"
,
counter
);
return
EXIT_SUCCESS
;
}
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