"depot M311"
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -1,57 +0,0 @@
|
||||
///********************************************************************/
|
||||
///* 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);
|
||||
}
|
||||
|
Binary file not shown.
@@ -1,38 +0,0 @@
|
||||
///********************************************************************/
|
||||
///* IUT NICE-COTE D'AZUR - Departement INFORMATIQUE - R. CHIGNOLI */
|
||||
///* Module DUT M311 Theme PTHREADS */
|
||||
///********************************************************************/
|
||||
/// demo_pthread.c : Demonstration d'utilisation des threads
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
|
||||
void* action (void* name) {
|
||||
printf ("\n... ACTION par thread %s (pid : %d) ... Puis MORT\n\n",
|
||||
(char*) name, getpid());
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main (void) {
|
||||
pthread_t filsA, filsB;
|
||||
|
||||
printf ("\nDebut du pere ( pid : %d ; ppid : %d ) )\n\n",
|
||||
getpid(), getppid());
|
||||
|
||||
if (pthread_create(&filsA, NULL, action, "filsA")) {
|
||||
perror("pthread_create");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (pthread_create(&filsB, NULL, action, "filsB")) {
|
||||
perror("pthread_create");
|
||||
exit(-1);
|
||||
}
|
||||
printf("\nFin du pere\n\n") ;
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
Binary file not shown.
@@ -1,22 +0,0 @@
|
||||
///********************************************************************/
|
||||
///* IUT NICE-COTE D'AZUR - Departement INFORMATIQUE - R. CHIGNOLI */
|
||||
///* Module DUT M311 Theme PTHREADS */
|
||||
///********************************************************************/
|
||||
/// demo_system : Demonstration d'utilisation de "system"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include <pthread.h>
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
int i;
|
||||
assert (argc == 2);
|
||||
for (i=1; i<=3; i++) { /// 3 : par exemple ...
|
||||
system (argv[1]);
|
||||
printf (" ... Au suivant ...\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
@@ -1,34 +0,0 @@
|
||||
|
||||
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
|
||||
int GlobV = 0;
|
||||
|
||||
void* action (void* increment) {
|
||||
for (i=1 ; i<= NOMBRE; i++) globV = globV + *increment;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main (int argc; char* argv[]) {
|
||||
pthread_t unThread;
|
||||
int valeur;
|
||||
assert (argc == 2);
|
||||
valeur = atoi(argv[1];
|
||||
|
||||
if (pthread_create(&unThread, NULL, action, &valeur) {
|
||||
perror("pthread_create");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
}
|
||||
printf("\nmain : globV = %d \n\n", globV) ;
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
@@ -1,13 +0,0 @@
|
||||
CFLAGS = -Wall -g -std=gnu99 -lpthread
|
||||
CC = gcc
|
||||
|
||||
EXECUTABLES = demo_pthread \
|
||||
demo_system \
|
||||
pthread10 \
|
||||
demo_mutex
|
||||
|
||||
all : ${EXECUTABLES}
|
||||
|
||||
clean :
|
||||
@rm -f core *.o *.out *~
|
||||
@rm -f ${EXECUTABLES}
|
Binary file not shown.
@@ -1,38 +0,0 @@
|
||||
///********************************************************************/
|
||||
///* IUT NICE-COTE D'AZUR - Departement INFORMATIQUE - R. CHIGNOLI */
|
||||
///* Module DUT M311 Theme PTHREADS */
|
||||
///********************************************************************/
|
||||
/// pthread10.c : squelette pour creation de trois threads
|
||||
/// Cf enonce ...
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
typedef struct { int nombre; char car;} struct_p;
|
||||
|
||||
void imprimer (struct_p* param){
|
||||
int i;
|
||||
for (i=0 ; i < param->nombre; i++) printf ("%c", param->car); }
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
int i, valeur;
|
||||
struct_p x;
|
||||
char tab[3] = {'/', '*', '='};
|
||||
|
||||
assert (argc == 2);
|
||||
valeur = atoi (argv[1]);
|
||||
for (i=1; i<=3; i++) {
|
||||
printf ("\nMAIN : Tour de boucle nO %d\n", i);
|
||||
/// A VOUS DE JOUER
|
||||
/// Creation des trois threads d'affichage ...
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Binary file not shown.
@@ -1,41 +0,0 @@
|
||||
///********************************************************************/
|
||||
///* IUT NICE-COTE D'AZUR - Departement INFORMATIQUE - R. CHIGNOLI */
|
||||
///* Module DUT M311 Theme PTHREADS */
|
||||
///********************************************************************/
|
||||
/// demo_pthread.c : Demonstration d'utilisation des threads
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
|
||||
void* action (void* name) {
|
||||
printf ("\n... ACTION par thread %s (pid : %d) ... Puis MORT\n\n",
|
||||
(char*) name, getpid());
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main (void) {
|
||||
pthread_t filsA, filsB;
|
||||
|
||||
printf ("\nDebut du pere ( pid : %d ; ppid : %d ) )\n\n",
|
||||
getpid(), getppid());
|
||||
|
||||
if (pthread_create(&filsA, NULL, action, "filsA")) {
|
||||
perror("pthread_create");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (pthread_create(&filsB, NULL, action, "filsB")) {
|
||||
perror("pthread_create");
|
||||
exit(-1);
|
||||
}
|
||||
printf("\nFin du pere\n\n") ;
|
||||
|
||||
for(;;) {}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
@@ -1,131 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/// Processus legers
|
||||
|
||||
#define TAILLE 8 /// Multiple de 4 !!!!
|
||||
|
||||
int SOMME; /// Variable GLOBALE en mutex (somme totale)
|
||||
int NOMBRE; /// Variable GLOBALE en mutex (nbre de reponses)
|
||||
int sommeFinale[4] = {-1}; /// Variable GLOBALE pour les sous-tableau
|
||||
|
||||
pthread_mutex_t mutexSomme = PTHREAD_MUTEX_INITIALIZER; /// mutex
|
||||
|
||||
typedef struct TF
|
||||
{
|
||||
int numero;
|
||||
int *tabAdresse;
|
||||
int tabNombre;
|
||||
} tabParams;
|
||||
|
||||
void initTabAleat (int *leTableau, int leNombre);
|
||||
/// Initialise <leTableau> de <leNombre> elements
|
||||
/// de fa<66>on aleatoire avec des nombres tous differents.
|
||||
|
||||
int sommeTab (int *leTableau, int leNombre);
|
||||
/// retourne la somme des <leNombre> elements de <leTableau>.
|
||||
|
||||
void afficheTableau (int leTableau [], int leNombre);
|
||||
/// Affiche <leTableau> de <leNombre> elements sur la sortie standard.
|
||||
|
||||
void initTabAleat (int leTableau [], int leNombre)
|
||||
{
|
||||
int i;
|
||||
int min = 0;
|
||||
srand (time (NULL));
|
||||
|
||||
for (i=0; i<leNombre; i++)
|
||||
{
|
||||
leTableau [i] = (rand() % (leNombre - min + 1));
|
||||
}
|
||||
}
|
||||
|
||||
int sommeTab (int leTableau [], int leNombre)
|
||||
{
|
||||
int i, somme = 0;
|
||||
for(i=0 ; i < leNombre ; i++)
|
||||
{
|
||||
somme = somme + leTableau[i];
|
||||
}
|
||||
return somme;
|
||||
}
|
||||
|
||||
void afficheTableau (int leTableau [], int leNombre)
|
||||
{
|
||||
int i;
|
||||
printf ("\n==> tableau : ");
|
||||
for (i=0; i <leNombre; i++)
|
||||
printf ("%d ", leTableau[i]);
|
||||
printf ("\n\n");
|
||||
}
|
||||
|
||||
void ajouterGlob (int unNombre)
|
||||
{
|
||||
pthread_mutex_lock (&mutexSomme);
|
||||
SOMME = SOMME + unNombre;
|
||||
NOMBRE++;
|
||||
pthread_mutex_unlock (&mutexSomme);
|
||||
}
|
||||
|
||||
int threadFonction (tabParams *param)
|
||||
{
|
||||
int local;
|
||||
printf ("Lancement threadFonction ...\n");
|
||||
local = sommeTab (param->tabAdresse, param->tabNombre);
|
||||
|
||||
sommeFinale [param->numero] = local;
|
||||
ajouterGlob (local);
|
||||
|
||||
return local;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
int * grandTableau;
|
||||
pthread_t tache[4];
|
||||
tabParams *localParam;
|
||||
|
||||
SOMME = 0;
|
||||
NOMBRE = 0;
|
||||
|
||||
grandTableau = malloc (sizeof(int) * TAILLE);
|
||||
|
||||
initTabAleat (grandTableau, TAILLE);
|
||||
afficheTableau(grandTableau, TAILLE);
|
||||
|
||||
for (i=0; i<4; i++)
|
||||
{
|
||||
localParam = malloc (sizeof (tabParams));
|
||||
localParam->numero = i;
|
||||
localParam->tabAdresse = grandTableau + (i * TAILLE / 4);
|
||||
localParam->tabNombre = TAILLE / 4;
|
||||
pthread_create(&tache[i], NULL, (void*(*)(void*)) threadFonction,
|
||||
localParam);
|
||||
}
|
||||
/// Version 1 : avec pthread_join
|
||||
pthread_join (tache[0], NULL); pthread_join (tache[1], NULL);
|
||||
pthread_join (tache[2], NULL); pthread_join (tache[3], NULL);
|
||||
printf ("\nVERSION 1 : OK !\n");
|
||||
|
||||
/// Version 2 : attente du remplissage de sommeFinale []
|
||||
while ( sommeFinale [0] == 0 || sommeFinale [1] == 0 ||
|
||||
sommeFinale [2] == 0 || sommeFinale [3] == 0 );
|
||||
printf ("VERSION 2 : OK !\n");
|
||||
|
||||
/// Version 3 : attente de NOMBRE == 4
|
||||
while ( NOMBRE != 4);
|
||||
printf ("VERSION 3 : OK !\n");
|
||||
|
||||
/// Affichage toutes versions ...
|
||||
SOMME = sommeFinale [0] + sommeFinale [1] +
|
||||
sommeFinale [2] + sommeFinale [3] ;
|
||||
|
||||
printf ( "\nSOMME : %d = %d + %d + %d + %d\n", SOMME, sommeFinale [0],
|
||||
sommeFinale [1],sommeFinale [2], sommeFinale [3]);
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user