First commit
This commit is contained in:
commit
5c269302ed
59 changed files with 5613 additions and 0 deletions
BIN
content/progsys/TDM_5-threads/files/tdm5.pdf
Normal file
BIN
content/progsys/TDM_5-threads/files/tdm5.pdf
Normal file
Binary file not shown.
22
content/progsys/TDM_5-threads/index.md
Normal file
22
content/progsys/TDM_5-threads/index.md
Normal file
|
@ -0,0 +1,22 @@
|
|||
---
|
||||
title : "TDM : Les processus légers"
|
||||
category: Programmation système
|
||||
date: 2018-09-25
|
||||
---
|
||||
[télécharger les questions](./files/tdm5.pdf)
|
||||
|
||||
## Question 1
|
||||
|
||||
Comme vu en TD, les résultats ne sont pas ceux attendu du fait de l'absence de
|
||||
mutex. Je constate aussi qu'ils varient aussi d'une execution sur l'autre.
|
||||
|
||||
### avec un mutex
|
||||
|
||||
J'obtiens la valeur attendue pour blob.
|
||||
|
||||
[Voir mon code dans mutex](./src/td5/q1.c)
|
||||
[Voir mon code avec mutex](./src/td5/q1_mutex.c)
|
||||
|
||||
## Question 2
|
||||
|
||||
[Voir mon code](./src/td5/q2.c)
|
44
content/progsys/TDM_5-threads/src/q1.c
Normal file
44
content/progsys/TDM_5-threads/src/q1.c
Normal file
|
@ -0,0 +1,44 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
|
||||
//define
|
||||
#define THREADS 2
|
||||
#define ITERATIONS 1000000
|
||||
|
||||
static long glob = 0;
|
||||
|
||||
void *tmain( void *arg){
|
||||
long tmp;
|
||||
for (int i=0; i < ITERATIONS; i++){
|
||||
tmp = glob;
|
||||
tmp++;
|
||||
glob = tmp;
|
||||
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
pthread_t *t;
|
||||
void *res;
|
||||
// memory allocation for threads
|
||||
if ( (t=calloc(THREADS, sizeof(pthread_t))) == NULL) {
|
||||
perror("Unable ro allocate memory for pthread");
|
||||
}
|
||||
for (int i=0; i < THREADS; i++){
|
||||
if (pthread_create(&t[i],NULL, tmain, &"") != 0){
|
||||
perror("unable to create thread\n");
|
||||
}
|
||||
}
|
||||
// Need to block parent thread until brother thread are finished
|
||||
for (int j=0;j < THREADS; j++){
|
||||
// bloc main thread exec with pthread_join()
|
||||
if (pthread_join(t[j], &res) != 0){
|
||||
perror ("Unable to join process");
|
||||
}
|
||||
}
|
||||
printf("glob is %ld, should be 2000000\n", glob);
|
||||
free(t);
|
||||
exit (EXIT_SUCCESS);
|
||||
}
|
52
content/progsys/TDM_5-threads/src/q1_mutex.c
Normal file
52
content/progsys/TDM_5-threads/src/q1_mutex.c
Normal file
|
@ -0,0 +1,52 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
|
||||
//define
|
||||
#define THREADS 2
|
||||
#define ITERATIONS 1000000
|
||||
|
||||
static long glob = 0;
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
void *tmain( void *arg){
|
||||
long tmp;
|
||||
for (int i=0; i < ITERATIONS; i++){
|
||||
if (pthread_mutex_lock(&mutex) != 0) {
|
||||
perror("Mutex lock failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
tmp = glob;
|
||||
tmp++;
|
||||
glob = tmp;
|
||||
if (pthread_mutex_unlock(&mutex) != 0) {
|
||||
perror("Mutex lock failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
pthread_t *t;
|
||||
void *res;
|
||||
// memory allocation for threads
|
||||
if ( (t=calloc(THREADS, sizeof(pthread_t))) == NULL) {
|
||||
perror("Unable ro allocate memory for pthread");
|
||||
}
|
||||
for (int i=0; i < THREADS; i++){
|
||||
if (pthread_create(&t[i],NULL, tmain, &"") != 0){
|
||||
perror("unable to create thread\n");
|
||||
}
|
||||
}
|
||||
// Need to block parent thread until brother thread are finished
|
||||
for (int j=0;j < THREADS; j++){
|
||||
// bloc main thread exec with pthread_join()
|
||||
if (pthread_join(t[j], &res) != 0){
|
||||
perror ("Unable to join process");
|
||||
}
|
||||
}
|
||||
printf("glob is %ld, should be 2000000\n", glob);
|
||||
free(t);
|
||||
exit (EXIT_SUCCESS);
|
||||
}
|
40
content/progsys/TDM_5-threads/src/q2.c
Normal file
40
content/progsys/TDM_5-threads/src/q2.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
|
||||
//define
|
||||
#define THREADS 5
|
||||
#define ITERATIONS 5
|
||||
#define SLEEP 2
|
||||
|
||||
void *tmain( void *arg){
|
||||
for (int i=0; i < ITERATIONS; i++){
|
||||
sleep(SLEEP);
|
||||
printf("I'm alive : %i\n",(int) pthread_self());
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
pthread_t *t;
|
||||
void *res;
|
||||
// memory allocation for threads
|
||||
if ( (t=calloc(THREADS, sizeof(pthread_t))) == NULL) {
|
||||
perror("Unable ro allocate memory for pthread");
|
||||
}
|
||||
for (int i=0; i < THREADS; i++){
|
||||
if (pthread_create(&t[i],NULL, tmain, &"") != 0){
|
||||
perror("unable to create thread\n");
|
||||
}
|
||||
}
|
||||
// Need to block parent thread until brother thread are finished
|
||||
for (int j=0;j < THREADS; j++){
|
||||
// bloc main thread exec with pthread_join()
|
||||
if (pthread_join(t[j], &res) != 0){
|
||||
perror ("Unable to join process");
|
||||
}
|
||||
}
|
||||
free(t);
|
||||
exit (EXIT_SUCCESS);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue