#include<stdio.
h>
#include<pthread.h>
#include<string.h>
//creer un thread principale th1 afficher un entier
//pthread_create(TID,attribut:NULL,Fct,&arguments) attribut : caractéristique de
thread , Fct: function a éxecuter
//fct : type de retour void * et parametre void * : void* fct(void * param)
//void * retourner quelque soit le type : int,float,char
//la fct pthread retourne 0 si le thread est creer un autre numéro si le thread
n'est pas creer
void * display(void * arg){
int * t =(int*) arg;
printf("\n Ici thread , arg = %d\n",*t);
pthread_exit(NULL); //exit
}
int main(){
int x=5;
pthread_t th1; //nom du thread et lidentifiant c'est l& dans le memoire
unique
if (pthread_create(&th1,NULL,display,&x) == 0){
printf("\nThread created !!!\n");
pthread_join(th1,NULL); //wait
return 0;
}
else{
printf("\nThread not created !!!\n");
return 1;
}
}
#include <stdio.h>
#include <pthread.h>
// Créer un thread principal th1 pour afficher un entier
void* display(void* arg) {
int* t = (int*)arg;
printf("\nIci thread 1, arg = %d\n", *t);
pthread_exit(NULL); // Terminer le thread
}
void* add(void* arg1) {
int* x = (int*)arg1;
int y = 12;
int s=0;
s= *x + y;
printf("\nIci thread 2, x + y = %d + %d = %d\n", *x,y,s);
pthread_exit(NULL); // Terminer le thread
}
int main() {
int x = 5;
pthread_t th1; // Nom du thread et son identifiant, c'est l'adresse mémoire
unique
pthread_t th2;
pthread_create(&th1, NULL, display, &x);
pthread_create(&th2, NULL, add, &x);
pthread_join(th1, NULL); // Attendre la fin du thread
pthread_join(th2, NULL); // Attendre la fin du thread
return 0;
}
#include<stdio.h>
#include<pthread.h>
#include<string.h>
//creer un thread th1 et creer th2
void * display(void * arg){
int * t =(int*) arg;
for(int i=0;i<=500000;i++){
printf("\n %d",*t);
}
pthread_exit(NULL); //exit
}
int main(){
int x=1;
int y=2;
pthread_t th1;
pthread_t th2;
if (pthread_create(&th1,NULL,display,&x) == 0 &&
pthread_create(&th2,NULL,display,&y) == 0){
printf("\nThreads created !!!\n");
pthread_join(th1,NULL); //wait
pthread_join(th2,NULL); //wait
return 0;
}
else{
printf("\nThreads not created !!!\n");
return 1;
}
}
#include <stdio.h>
#include <pthread.h>
//probleme acces concorenciel a un variable partager
int arg = 0;
int x= 5000000;
pthread_mutex_t serrure; // Déclaration du verrou mutex
void* INC() {
for (int i = 0; i <= x; i++) {
pthread_mutex_lock(&serrure); // Verrouillage du mutex
arg++;
//printf("\n INC, arg = %d", arg);
pthread_mutex_unlock(&serrure); // Déverrouillage du mutex
}
pthread_exit(NULL); // Terminer le thread
}
void* DEC() {
for (int i = 0; i <= x; i++) {
pthread_mutex_lock(&serrure); // Verrouillage du mutex
arg--;
//printf("\n DEC, arg = %d", arg);
pthread_mutex_unlock(&serrure); // Déverrouillage du mutex
}
pthread_exit(NULL); // Terminer le thread
}
int main() {
pthread_mutex_init(&serrure, NULL); // Initialisation du mutex ouverte
pthread_t th1;
pthread_t th2;
pthread_create(&th1, NULL, INC,NULL);
pthread_create(&th2, NULL, DEC, NULL);
pthread_join(th1, NULL); // Attendre la fin du thread th1
pthread_join(th2, NULL); // Attendre la fin du thread th2
printf("La valeur finale de arg = %d\n", arg);
pthread_mutex_destroy(&serrure); // Destruction du mutex
return 0;
}
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <assert.h>
// je recupere le n thread dans linvite de commande et chaque thread affiche
bonjour et le TID
void* Afficher(){
printf("Bonjour TID = %d\n",pthread_self());
pthread_exit(NULL);
}
int main(int argc,char* argv[]) {
assert(argc==2);
int N=atoi(argv[1]);
pthread_t thread[N];// ou bien creer allocation dynamic pthread_t * threads;
threads = malloc(N * sizeof(pthread_t));
for(int i=0;i<N;i++){
pthread_create(&thread[i],NULL,Afficher,NULL);
}
for(int i=0;i<N;i++){
pthread_join(thread[i],NULL);
}
printf("\n threads terminé!!!!!!!!!!!!!!!!!!!!!\n");
return 0;
}
#include <stdio.h>
#include <string.h>
#include <pthread.h>
//je calcul la somme ------>moyenne------->affichage
int tab[5] = {2,3,4,5,6};
int s = 0;
int m;
void* Somme() {
for (int i = 0; i < 5; i++) {
s += tab[i];
}
pthread_exit(NULL); // Terminer le thread
}
void* Moyenne() {
m = (float)s / 5;
pthread_exit(NULL); // Terminer le thread
}
void* Afficher() {
printf("La somme = %d, la moyenne = %d \n", s, m);
pthread_exit(NULL); // Terminer le thread
}
int main() {
pthread_t th1;
pthread_t th2;
pthread_t th3;
pthread_create(&th1, NULL, Somme,NULL);
pthread_join(th1, NULL); // Attendre la fin du thread th1 //la somme est
calculer
pthread_create(&th2, NULL, Moyenne,NULL);
pthread_join(th2, NULL); // Attendre la fin du thread th2 //le moyenne est
calculer
pthread_create(&th3, NULL, Afficher,NULL);
pthread_join(th3, NULL); // Attendre la fin du thread th3
return 0;
}
#include <stdio.h>
#include <string.h>
#include <pthread.h>
//somme et moyenne execute simultanné il faut le moyenne calcul la somme aussi
int tab[5] = {2,3,4,5,6};
int s = 0;
int m;
void* Somme() {
for (int i = 0; i < 5; i++) {
s += tab[i];
}
pthread_exit(NULL); // Terminer le thread
}
void* Moyenne() {
int somme=0;
for (int i = 0; i < 5; i++) {
somme += tab[i];
}
m = (float)somme / 5;
pthread_exit(NULL); // Terminer le thread
}
void* Afficher() {
printf("La somme = %d, la moyenne = %d \n", s, m);
pthread_exit(NULL); // Terminer le thread
}
int main() {
pthread_t th1;
pthread_t th2;
pthread_t th3;
pthread_create(&th1, NULL, Somme,NULL);
pthread_create(&th2, NULL, Moyenne,NULL);
pthread_join(th1, NULL); // Attendre la fin du thread th1
pthread_join(th2, NULL); // Attendre la fin du thread th2
pthread_create(&th3, NULL, Afficher,NULL);
pthread_join(th3, NULL); // Attendre la fin du thread th3
return 0;
}