update
This commit is contained in:
BIN
SCR/SCR2/TP06/ex1
Executable file
BIN
SCR/SCR2/TP06/ex1
Executable file
Binary file not shown.
30
SCR/SCR2/TP06/ex1.c
Normal file
30
SCR/SCR2/TP06/ex1.c
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
#include <assert.h>
|
||||
|
||||
#define NUM_THREADS 16
|
||||
|
||||
void *thread(void *thread_id) {
|
||||
int id = *((int *) thread_id);
|
||||
printf("Hello from thread %d\n", id);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main() {
|
||||
pthread_t threads[NUM_THREADS];
|
||||
int tabid[NUM_THREADS];
|
||||
for (int i = 0; i < NUM_THREADS; i++){
|
||||
tabid[i] = i+1;
|
||||
}
|
||||
for (int i = 0; i < NUM_THREADS; i++){
|
||||
assert( pthread_create(&threads[i], NULL, thread, &tabid[i]) == 0);
|
||||
}
|
||||
|
||||
for (int i = 0; i < NUM_THREADS; i++){
|
||||
assert( pthread_join(threads[i], NULL) == 0);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
105
SCR/SCR2/TP06/ex2.c
Normal file
105
SCR/SCR2/TP06/ex2.c
Normal file
@@ -0,0 +1,105 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
#include <assert.h>
|
||||
#include <time.h>
|
||||
|
||||
#define ELIMINE -1
|
||||
|
||||
typedef struct duel{
|
||||
int* liste;
|
||||
int debut;
|
||||
int tailleGroupe;
|
||||
} duel;
|
||||
|
||||
void affichage(int* liste, int capacite){
|
||||
int i;
|
||||
for(i=0; i<capacite; i++){
|
||||
if (liste[i] != ELIMINE){
|
||||
printf("%d ",liste[i]);
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int* creerListe(int taille){
|
||||
int* liste = (int*) malloc(taille*sizeof(int));
|
||||
int i;
|
||||
for (i=0; i<taille; i++){
|
||||
liste[i] = (rand()%100);
|
||||
}
|
||||
return liste;
|
||||
}
|
||||
|
||||
int plusPetit(int* liste, int debut, int nb){
|
||||
int plusGrand = -1;
|
||||
int i;
|
||||
int participant1 =- 1;
|
||||
int indiceParticipant1;
|
||||
for (i=0; i<nb; i++){
|
||||
if (liste[debut+i] != ELIMINE){
|
||||
if (participant1 == -1){
|
||||
participant1 = liste[debut+i];
|
||||
indiceParticipant1 = i;
|
||||
}
|
||||
else{
|
||||
if (liste[debut+i] > participant1){
|
||||
return debut + indiceParticipant1;
|
||||
}
|
||||
else{
|
||||
return debut + i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void *tournois(void *argument) {
|
||||
duel duo = *((duel*) argument);
|
||||
int i;
|
||||
int perdant = plusPetit(duo.liste, duo.debut, duo.tailleGroupe);
|
||||
duo.liste[perdant] = -1;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int puissance2(int n){
|
||||
int p;
|
||||
for (p=1; p<n; p*=2){}
|
||||
if (p==n){
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv[]) {
|
||||
int nbParticipant = argc-1;
|
||||
int nbDuel = nbParticipant/2;
|
||||
int* listeParticipant;
|
||||
|
||||
assert(puissance2(nbParticipant)==1);
|
||||
srand(time(NULL));
|
||||
listeParticipant = creerListe(nbParticipant);
|
||||
affichage(listeParticipant, nbParticipant);
|
||||
|
||||
while (nbDuel>=1){
|
||||
pthread_t threads[nbDuel];
|
||||
duel argument[nbDuel];
|
||||
int i;
|
||||
for (i=0; i<nbDuel; i++){
|
||||
argument[i].liste = listeParticipant;
|
||||
argument[i].debut = i*(nbParticipant/nbDuel);
|
||||
argument[i].tailleGroupe = nbParticipant/nbDuel;
|
||||
}
|
||||
for (i=0; i<nbDuel; i++){
|
||||
assert( pthread_create(&threads[i], NULL, tournois, &argument[i]) == 0);
|
||||
}
|
||||
for (i=0; i<nbDuel; i++){
|
||||
assert( pthread_join(threads[i], NULL) == 0);
|
||||
}
|
||||
affichage(listeParticipant, nbParticipant);
|
||||
nbDuel /= 2;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
60
SCR/SCR2/TP06/ex3.c
Normal file
60
SCR/SCR2/TP06/ex3.c
Normal file
@@ -0,0 +1,60 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
#include <assert.h>
|
||||
#include <time.h>
|
||||
|
||||
typedef struct structure{
|
||||
int debut;
|
||||
int fin;
|
||||
int saut;
|
||||
int* resultat;
|
||||
} structure;
|
||||
|
||||
static inline double tstamp(void)
|
||||
{
|
||||
struct timespec tv;
|
||||
clock_gettime(CLOCK_REALTIME, &tv);
|
||||
return tv.tv_sec + tv.tv_nsec * 1.0e-9;
|
||||
}
|
||||
|
||||
|
||||
void *somme(void *argument) {
|
||||
structure data = *((structure *) argument);
|
||||
for (; data.debut<=data.fin; data.debut += data.saut){
|
||||
*data.resultat += data.debut;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
assert(argc-1 == 2);
|
||||
int limite = (int) strtol(argv[1], NULL, 0);
|
||||
int nbThread = (int) strtol(argv[2], NULL, 0);
|
||||
int sommeFinal = 0;
|
||||
double startTime, endTime;
|
||||
int resultatThread[nbThread];
|
||||
pthread_t threads[nbThread];
|
||||
structure argument[nbThread];
|
||||
|
||||
for (int i = 0; i < nbThread; i++){
|
||||
argument[i].debut = i;
|
||||
argument[i].fin = limite;
|
||||
argument[i].saut = nbThread;
|
||||
resultatThread[i] = 0;
|
||||
argument[i].resultat = &resultatThread[i];
|
||||
}
|
||||
startTime = tstamp();
|
||||
for (int i = 0; i < nbThread; i++){
|
||||
assert( pthread_create(&threads[i], NULL, somme, &argument[i]) == 0);
|
||||
}
|
||||
|
||||
for (int i = 0; i < nbThread; i++){
|
||||
assert( pthread_join(threads[i], NULL) == 0);
|
||||
sommeFinal += *argument[i].resultat;
|
||||
}
|
||||
endTime = tstamp();
|
||||
printf("somme : %d\ntemps : %lf\n", sommeFinal, endTime-startTime);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
BIN
SCR/SCR2/TP06/exe
Executable file
BIN
SCR/SCR2/TP06/exe
Executable file
Binary file not shown.
Reference in New Issue
Block a user