58 lines
1.9 KiB
C
58 lines
1.9 KiB
C
///********************************************************************/
|
|
///* 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);
|
|
}
|
|
|