Compil&Test ok

This commit is contained in:
adrien 2018-11-08 11:47:39 +01:00
commit ba5053af34
5 changed files with 135 additions and 0 deletions

BIN
main Executable file

Binary file not shown.

104
main.c Normal file
View File

@ -0,0 +1,104 @@
#include <pthread.h>
#include <stdio.h>
#include <unistd.h> //usleep
#include <malloc.h>
#include "main.h"
#define RED "\x1B[31m"
#define GRN "\x1B[32m"
#define YEL "\x1B[33m"
#define BLU "\x1B[34m"
#define MAG "\x1B[35m"
#define CYN "\x1B[36m"
#define WHT "\x1B[37m"
#define RESET "\x1B[0m"
#define num_threads 10
int main(void) {
int nbFinThead = 0;//Compteur de thread qui on fini
pthread_t primes[num_threads];
pthread_t printArrayPcps_T;
compute_prime_struct** cps = malloc(sizeof(compute_prime_struct*)*(num_threads+1));
cps[num_threads] = NULL;
int i = 0;
compute_prime_struct *args = malloc(sizeof *args);
args->max_prime = i*i;
args->ith_prime = i;
args->nbFinTheadLoc = nbFinThead;
args->nbFinTheadP = &nbFinThead;
cps[i] = args;
if(i==0 && pthread_create(&printArrayPcps_T, NULL, (void*)printArrayPcps, &cps))
{
for(;i>=0;i--)
free(cps[i]);
free(cps);
return -1;
}
printf(BLU"Thread printArrayPcps creer\n"RESET);
if(pthread_create(&primes[i], NULL, compute_prime, args))
{
for(;i>=0;i--)
free(cps[i]);
free(cps);
return -1;
}
printf(BLU"Thread compute_prime %d creer\n"RESET, i);
usleep(1);
i++;
for (; i < num_threads; ++i)
{
compute_prime_struct *args = malloc(sizeof *args);
args->max_prime = i*i;
args->ith_prime = i;
args->nbFinTheadLoc = nbFinThead;
args->nbFinTheadP = &nbFinThead;
cps[i] = args;
if(pthread_create(&primes[i], NULL, compute_prime, args))
{
for(;i>=0;i--)
free(cps[i]);
free(cps);
return -1;
}
printf(BLU"Thread compute_prime %d creer\n"RESET, i);
usleep(5);
}
pthread_join(printArrayPcps_T, NULL);//On attend que le thread d'impression d'array aie fini
for(int i = 0;i<num_threads+1;i++)
{
if(i<num_threads)
pthread_join(primes[i], NULL);//On vérifie que tout les thread primaire sont fini
free(cps[i]); //On libère la memoire;
}
free(cps);
return 0;
}
void *compute_prime (void *args) { //thread primaire
compute_prime_struct *actual_args = args;
printf(GRN"Enter in compute_prime \t%d\n"RESET,actual_args->ith_prime);
usleep(1000);//args->ith_prime
printf(GRN"LAUNCH compute_prime \t%d\n"RESET,actual_args->ith_prime);
(*(actual_args->nbFinTheadP))++;
((actual_args->nbFinTheadLoc))++;
printf(GRN"FIN compute_prime \t%d\n"RESET, actual_args->ith_prime);
return 0;
}
void* printArrayPcps(void *** cps)
{
printf(CYN"Enter in printArrayPcps\n"RESET);
compute_prime_struct **actual_args;
actual_args = (compute_prime_struct **)*cps;
while (*((*actual_args)->nbFinTheadP) < num_threads)
{
printf(YEL"WAIT"CYN" THREAD %d sur %d\n"RESET, *((*actual_args)->nbFinTheadP), num_threads);
usleep(1);
}
printf(CYN"=====LAUNCH printArrayPcps\n"RESET);
while (*actual_args)
{
printf(CYN"max prime %d\t ith prime %d\t Fin thread loc %d\t Fin thread P %d\n"RESET, ((*actual_args)->max_prime), ((*actual_args)->ith_prime),((*actual_args)->nbFinTheadLoc), *((*actual_args)->nbFinTheadP) );
actual_args++;
}
return 0;
}

22
main.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef MAIN_H
#define MAIN_H
#include <ncurses.h>
void * compute_prime (void *args); //thread primaire
void * printArrayPcps(void*** cps); //Threa secondaire
void * pgm(void **param);
typedef struct{
int mutex;
WINDOW * win;
} paramNcurseThread;
typedef struct { //struct random pour essayer
int max_prime;
int ith_prime;
int nbFinTheadLoc; //Var local pour voir ou en était le nb de thread
int *nbFinTheadP; //Pointeur pour voir l'interation de mem entre thread
paramNcurseThread * pnt;
} compute_prime_struct;
#endif

BIN
main.o Normal file

Binary file not shown.

9
makefile Normal file
View File

@ -0,0 +1,9 @@
CC=gcc
CFLAGS=-Wall -O1 -g
CLIB=-lpthread -lncurses
main: main.o
$(CC) main.o -o main $(CFLAGS) $(CLIB)
main.o: main.c
$(CC) -c main.c $(CFLAGS) $(CLIB)