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
|
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <pthread.h>
struct structure {
//int entier;
int taille;
int position;
int nbRech;
int *tab;
};
void *fonction (void * data)
{
printf("thread cree\n");
int fin = ((struct structure *)data)->taille;
printf("%d\n", ((struct structure *)data)->taille);
int i;
for(i=0; i<fin && ((struct structure *)data)->position==-1;i++){
if(((struct structure *)data)->tab[i]==((struct structure *)data)->nbRech)((struct structure *)data)->position=i+1;
}
pthread_exit(data);
}
int main()
{
int i;
int x=10;
int nbUti;
int *pti, *ptd;
pti=(int*)malloc(sizeof(int)*x);
if(pti!=NULL){
ptd = pti;
for(i=0;i<x;i++){
*pti = random() % (100-1) +1;
printf("%d\n", *pti);
pti++;
}
}
printf("Entrez un nombre a rechercher\n");
scanf("%d", &nbUti);
printf("%d\n", nbUti);
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_t * p;
p = (pthread_t * )calloc ( 2 , sizeof(pthread_t));
struct structure * t;
t = calloc (2,sizeof(struct structure));
//for (i=0;i<2;++i){
t->taille = x/2;
t->position = -1;
t->tab=ptd;
t->nbRech=nbUti;
if ( pthread_create(p,&attr,fonction,(void *)(t)) != 0) {perror ("Rate:");exit (1);}
p++;
t++;
t->taille =x-(x/2);
if (t->position==-1){
if ( pthread_create(p,&attr,fonction,(void *)(t)) != 0) {perror ("Rate:");exit (1);}
}
struct structure * * ret;
ret = calloc (2,sizeof(struct structure *));
pthread_join (*(p),(void**)(ret));
p++;
ret++;
pthread_join (*(p),(void**)(ret));
if(((struct structure*) *(ret))->position==-1)printf("le nombre entr� n'est pas dans le tableau\n");
else printf("le nombre entr� se trouve en position : %d\n", ((struct structure*) *(ret))->position);
free(ret);
free(p);
free(t);
return(EXIT_SUCCESS);
} |
Partager