1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#define NUM_THREADS 8
// Initialisation des constantes
const char cPattern[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
// Initialisation de la structure des arguments de threads
struct threadsArgs {
const char *cOp;
unsigned int iQte;
};
struct threadsArgs genCode_args_array[NUM_THREADS];
void *genCode(void *threadarg)
{
// Declaration de la structure
struct threadsArgs *args;
args = (struct threadsArgs *) threadarg;
// Initialisation des variables
int i = 0;
int iRang = 2;
int iIndex = 0;
char tmpCode[10] = "";
void *ret;
// Initialisation du tableau de la liste des codes
char **codeList;
// Allocation memoire du tableau
codeList = malloc( args->iQte * sizeof(char*));
if ( codeList == NULL )
{
fprintf(stderr, "Allocation impossible");
exit(1);
}
for( i=0; i<args->iQte; i++ )
{
codeList[i] = calloc(10, sizeof(char));
if ( codeList[i] == NULL )
{
fprintf(stderr, "Allocation impossible");
exit(1);
}
}
// On boucle sur la quantite de code desire
for(i=0; i<args->iQte; i++)
{
// On initialise la position dans la chaine
iRang = 2;
// On concatene le code produit en debut de chaine
strcpy(tmpCode, args->cOp);
// On boucle du rang 2 jusqu'au rang 8
while( iRang < 9 )
{
// On genere un nombre aleatoire
iIndex = rand() % (sizeof(cPattern) - 1);
// On recupere le caractere a la position iIndex dans la chaine cPattern
tmpCode[iRang] = cPattern [iIndex];
// On incremente le rang
iRang++;
}
// On copie tmpCode dans le tableau cCode
strcpy(codeList[i], tmpCode);
}
// On affiche tout les codes generes
for(i=0; i<args->iQte; i++)
{
printf("Code %d : %s\n", i, codeList[i]);
}
// Liberation de l'espace mémoire
for( i=0; i<args->iQte; i++ )
{
free(codeList[i]);
codeList[i] = NULL;
}
free(codeList);
codeList = NULL;
return ret;
}
int main(void)
{
pthread_t th1;
void *ret;
// Initialisation du random
srand( time(NULL) );
genCode_args_array[1].cOp = "0B";
genCode_args_array[1].iQte = 10000;
if (pthread_create (&th1, NULL, genCode, &genCode_args_array[1]) < 0)
{
fprintf(stderr, "Impossible de creer le thread.\n");
exit(1);
}
(void)pthread_join (th1, &ret);
exit(0);
} |
Partager