"debut TD0"
This commit is contained in:
57
2019-2020/S3T_TP_07_THREADS/Demonstrations/demo_mutex.c
Normal file
57
2019-2020/S3T_TP_07_THREADS/Demonstrations/demo_mutex.c
Normal file
@@ -0,0 +1,57 @@
|
||||
///********************************************************************/
|
||||
///* IUT NICE-COTE D'AZUR - Departement INFORMATIQUE - R. CHIGNOLI */
|
||||
///* Module DUT M311 Theme PTHREADS */
|
||||
///********************************************************************/
|
||||
/// demo_mutex.c : Demonstration simple d'utilisation des
|
||||
/// pthreads-mutex rapides.
|
||||
/// Utiliser par exemple les s<>quences LU0 LL0 LULU0 LUUL0
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include <assert.h>
|
||||
|
||||
pthread_t filsA;
|
||||
pthread_mutex_t fastmutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
char *sequence;
|
||||
|
||||
void* action (void* name) {
|
||||
int encore = 0;
|
||||
while (sequence [encore] != '0') {
|
||||
switch (sequence [encore]) {
|
||||
case 'L': printf("L ... ");
|
||||
if (pthread_mutex_lock (&fastmutex) != 0)
|
||||
printf("... L PB.\n"); else printf("... L OK.\n"); break;
|
||||
case 'U': printf("U ... ");
|
||||
if (pthread_mutex_unlock (&fastmutex) != 0)
|
||||
printf("... U PB.\n"); else printf("... U OK.\n"); break;
|
||||
case 'X': printf("X ... ");
|
||||
if (pthread_mutex_destroy (&fastmutex) != 0)
|
||||
printf("... X PB.\n"); else printf("... X OK.\n"); break;
|
||||
default: printf("... Commande incorrecte\n");
|
||||
}
|
||||
encore ++;
|
||||
}
|
||||
printf("... Bye\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
pthread_t filsA;
|
||||
assert (argc ==2);
|
||||
sequence = argv[1];
|
||||
printf ("\nEssayer avec les sequences : LU0 LL0 LULU0 LUUL0\n\n");
|
||||
|
||||
if (pthread_create(&filsA, NULL, action, "filsA")) {x
|
||||
perror("pthread_create");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
printf("\nPERE (Thread principal) : boucle infinie\n\n") ;
|
||||
for(;;);
|
||||
return (0);
|
||||
}
|
||||
|
Reference in New Issue
Block a user