IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

POSIX C Discussion :

Thread et structure


Sujet :

POSIX C

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2011
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2011
    Messages : 20
    Points : 9
    Points
    9
    Par défaut Thread et structure
    Bonjour,

    j'ai un gros probleme de segmentation...
    probleme je ne vois pas ou.

    Voila je dois créer 2 threads. un programme principale rempli un tableau aléatoirement.
    un thread cherche dans la première moitié, l'autre thread l'autre moitié. et ils retransmettent le resultat de la position de l'occurrence recherchée.

    merci en tout cas.





    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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);
    }

  2. #2
    Membre éprouvé
    Homme Profil pro
    R&D imagerie 3D / prog embarquée
    Inscrit en
    Mars 2007
    Messages
    417
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : R&D imagerie 3D / prog embarquée
    Secteur : Santé

    Informations forums :
    Inscription : Mars 2007
    Messages : 417
    Points : 1 247
    Points
    1 247
    Par défaut
    Salut,

    Ton problème vient de l’absence de synchronisation de tes threads et plus particulièrement de la ligne
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ((struct structure *)data)->position=i+1;
    Quand plusieurs threads accèdent seulement en lecture à la même variable, il n'y a pas de pb. Par contre, dès qu'au moins 1 thread écrit dans une variable, tu dois ajouter de la synchro pour garantir qu'un thread n'écrit pas dans la variable pendant qu'un autre y accède en lecture ou en écriture. Sinon, le comportement est indéterminé.

    Tu peux lire ce tuto pour comprendre les enjeux de la synchronisation inter-threads, et celui-là pour comprendre comment utiliser les objets de synchro de pthread.

    a+

  3. #3
    Membre éprouvé
    Homme Profil pro
    R&D imagerie 3D / prog embarquée
    Inscrit en
    Mars 2007
    Messages
    417
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : R&D imagerie 3D / prog embarquée
    Secteur : Santé

    Informations forums :
    Inscription : Mars 2007
    Messages : 417
    Points : 1 247
    Points
    1 247
    Par défaut
    Le lien que je t'ai donné pour les enjeux de la synchro n'est pas très détaillé.
    Je te conseille donc plutot de lire celui-ci, car même s'il parle de Java, les principes qu'il présente sont applicables en C/C++.

    A moins que quelqu'un ait un meilleur lien que moi ?!

  4. #4
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2011
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2011
    Messages : 20
    Points : 9
    Points
    9
    Par défaut
    merci
    je vais lire les tutos.

    j'ai mis une boucle for pour mes 2 threads comme ceci
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
            t = calloc (2,sizeof(struct structure));
     
            t->position = -1;
            t->tab=ptd;
            t->nbRech=nbUti;
            t->taille = x/2;
     
            for (i=0;i<2;++i){
                  if (pthread_create(p+i,&attr,fonction,(void *)(t+i)) != 0) {perror ("Rate:");exit (1);}
            }
    la il cherche, il trouve dans la première partie mais pas dans la seconde

  5. #5
    Membre éprouvé
    Homme Profil pro
    R&D imagerie 3D / prog embarquée
    Inscrit en
    Mars 2007
    Messages
    417
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : R&D imagerie 3D / prog embarquée
    Secteur : Santé

    Informations forums :
    Inscription : Mars 2007
    Messages : 417
    Points : 1 247
    Points
    1 247
    Par défaut
    Ok, dit moi si je comprend bien. Tu veux faire un tableau à 2 index de "structure" et passé chaque index à un thread différent ?

    Si c'est la cas, tu n'as effectivement pas besoin de synchro puisque chaque thread accèdera à un espace mémoire (la structure) différent.

    Par contre, t+i c'est l'adresse du premier index de ton tableau + i bytes. Ce n'est pas le 2ieme index de ton tableau (note que le pb est identique pour le tableau "p").
    Tu peux écrire:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    for (i=0;i<2;++i){
                  if (pthread_create(&p[i],&attr,fonction,(void *)(&t[i])) != 0) {perror ("Rate:");exit (1);}
            }
    Ou

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    for (i=0;i<2;++i){
                  if (pthread_create(p+i*sizeof(pthread_t),&attr,fonction,(void *)(t+i*sizeof(structure))) != 0) {perror ("Rate:");exit (1);}
            }
    Mais attention de bien initialiser les structures avant de les passer à tes threads. Là, tu n'initialise que le premier index de ton tableau :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
            t->position = -1;
            t->tab=ptd;
            t->nbRech=nbUti;
            t->taille = x/2;
    Que tu devrais plutôt écrire:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
            t[0].position = -1;
            t[0].tab=ptd;
            t[0].nbRech=nbUti;
            t[0].taille = x/2;

  6. #6
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2011
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2011
    Messages : 20
    Points : 9
    Points
    9
    Par défaut
    ca marche avec ce que tu m'as dit. enfin après j'ai juste initialisé mes tailles. car avant il cherchait 0 fois le 1er thread, ensuite il cherchait bien 5 fois. Maintenant avec le code entier que je vais poster il chercher bien deux fois à 5 reprises(vue que tab[10]). mais il trouve dans la première partie, mais si c dans la seconde, il me dit qu'il n'est pas dans le tableau. merci de ton aide, j'avance. je débute en thread, c'est mon premier essai compliqué
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    #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));
     
            t->position = -1;
            t->tab=ptd;
            t->nbRech=nbUti;
            t[0].taille = x/2;
            t[1].taille = x-(x/2);
     
     
     
            for (i=0;i<2;++i){
     
                if (pthread_create(p+i,&attr,fonction,(void *)(t+i)) != 0) {perror ("Rate:");exit (1);}
     
            }
            struct structure * * ret;
            ret = calloc (2,sizeof(struct structure *));
     
                for(i=0;i<2;++i){
                  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);
    }

  7. #7
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2011
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2011
    Messages : 20
    Points : 9
    Points
    9
    Par défaut
    la première phrase est ca marche pas avec t+i(sizeof(structure)

  8. #8
    Membre éprouvé
    Homme Profil pro
    R&D imagerie 3D / prog embarquée
    Inscrit en
    Mars 2007
    Messages
    417
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : R&D imagerie 3D / prog embarquée
    Secteur : Santé

    Informations forums :
    Inscription : Mars 2007
    Messages : 417
    Points : 1 247
    Points
    1 247
    Par défaut
    Ca devrait il me semble. Mais laisse tomber, c'est de l’arithmétique de pointer c'est jamais bon... Et surtout souvent illisible...
    Préfères &t[i], soit un pointer sur l'index i du tableau t. Ca c'est clair et tout le monde comprend

  9. #9
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2011
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2011
    Messages : 20
    Points : 9
    Points
    9
    Par défaut
    c est un travail à rendre donc je peux pas laisser tomber mais c de l'arithmétique de pointeur, c vrai

  10. #10
    Membre éprouvé
    Homme Profil pro
    R&D imagerie 3D / prog embarquée
    Inscrit en
    Mars 2007
    Messages
    417
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : R&D imagerie 3D / prog embarquée
    Secteur : Santé

    Informations forums :
    Inscription : Mars 2007
    Messages : 417
    Points : 1 247
    Points
    1 247
    Par défaut
    Si ton prof ne t'as pas demandé de faire d'arithmétique de pointer, n'en fais pas. Tu vas perdre des points plutôt qu'en gagner.

    Si mon t+i*sizeof(structure) ne fonctionne pas, c'est peut être parce que ton système à ré-aligné ton espace mémoire (ou que j'ai fait une grossière erreur que je vois pas).
    Par exemple, certain système aligne les double sur des adresses modulo 4 parce que l'opérateur point flotant est beaucoup plus efficace si tes données commencent à une telle adresse.
    Ainsi :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    struct toto
    {
      char c;
      double d;
      int i;
    }
    Pourrait avoir une taille supérieure à sizeof(char)+sizeof(double)+sizeof(int) et/ou new structure[10] pourrait avoir une taille supérieure à 10*sizeof(toto).

    Si tu as absolument besoin d'arithmétique de pointer, poste ton code actuel, je vais le relire au complet.

  11. #11
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2011
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2011
    Messages : 20
    Points : 9
    Points
    9
    Par défaut
    pour l'arithmétique des pointeurs, je pense qu'il préfère car la partie des threads, c'est lui qui me la corrigé comme ca. merci en tout cas voilà mon code entier

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    #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;
        int indice;
     
    };
     
    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));
     
            t->position = -1;
            t->tab=ptd;
            t->nbRech=nbUti;
            t[0].taille = x/2;
            t[1].taille = x-(x/2);
     
     
     
     
            for (i=0;i<2;++i){
     
                if (pthread_create(p+i,&attr,fonction,(void *)(&t[i])) != 0) {perror ("Rate:");exit (1);}
     
            }
            struct structure * * ret;
            ret = calloc (2,sizeof(struct structure *));
     
                for(i=0;i<2;++i){
                  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);
    }
    il compile, il trouve dans la premiere partie mais pas dans la seconde

  12. #12
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2011
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2011
    Messages : 20
    Points : 9
    Points
    9
    Par défaut
    j'ai deux taille différentes car normalement c l'utilisateur qui entre la taille du tableau mais pour le moment je mets 10.
    j'avais le meme exo sur les fork et la aucun problème.

  13. #13
    Membre éprouvé
    Homme Profil pro
    R&D imagerie 3D / prog embarquée
    Inscrit en
    Mars 2007
    Messages
    417
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : R&D imagerie 3D / prog embarquée
    Secteur : Santé

    Informations forums :
    Inscription : Mars 2007
    Messages : 417
    Points : 1 247
    Points
    1 247
    Par défaut
    Salut,

    Je suis entrain de regarder ça.
    Note que j'avais pas vu que tu faisais des calloc et pas des new, donc laisse tomber l'histoire d'alignement mémoire.

  14. #14
    Membre éprouvé
    Homme Profil pro
    R&D imagerie 3D / prog embarquée
    Inscrit en
    Mars 2007
    Messages
    417
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : R&D imagerie 3D / prog embarquée
    Secteur : Santé

    Informations forums :
    Inscription : Mars 2007
    Messages : 417
    Points : 1 247
    Points
    1 247
    Par défaut
    Voici une correction rapide. Dis moi si ça marche, car je n'ai pas pu compilé (je suis sous windows et sans pthread).
    Si le problème persiste, j’essaierais ça ce soir sous Linux.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #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;
      int indice;
     
    };
     
    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(structure));
     
      // Attention ici, modif
      t[0].position = -1;
      t[0].tab=ptd;
      t[0].nbRech=nbUti;
      t[1] = t[0]; // Ou => memcpy(&t[1], &t[0], sizeof(structure));
     
      t[0].taille = x/2;
      t[1].taille = x-(x/2);
     
      for (i=0;i<2;++i){
        // Attention ici, modif
        if (pthread_create(&p[i] /*ou p+i*sizeof(pthread_t)*/,&attr,fonction,(void *)(&t[i] /*ou t+i*sizeof(structure), ca devrait fonctionner*/)) != 0) 
        {
          perror("Rate:");
          exit(1);
        }
     
      }
     
      // Attention ici, modif
      //struct structure * * ret;
      //ret = calloc (2,sizeof(struct structure *));
     
      for(i=0;i<2;++i){
        // Attention ici, modif
        void *ret;
        pthread_join (p[i], &ret); // Attention ici, tu devrais vérifier la valeur de retour de ton join
     
        if(((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", ((structure*)ret)->position);
      }
     
      /* p++;
      ret++;
     
      pthread_join (*(p),(void**)(ret));*/
     
     
      // Attention ici, modif
      free(p);
      free(t);
     
      return(EXIT_SUCCESS);
    }

  15. #15
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2011
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2011
    Messages : 20
    Points : 9
    Points
    9
    Par défaut
    ca compile pas, mon profs nous a dit que pour le retour il fallait un double pointeur voilà son code d'exemple; il marche celui la.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    /*
     ============================================================================
     Name        : labo1_2.c
     Author      : Adrien PEIFFER
     Version     : Corrigée par fred  version avec structure
     Copyright   : Your copyright notice
     Description : Hello World in C, Ansi-style
     ============================================================================
     */
    #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;
     
    };
     
    void *fonction (void * data)
    {
     
        printf("thread %d cree\n",((struct structure *)data)->entier);
     
        ((struct structure *)data)->entier = ((struct structure *)data)->entier + ((struct structure *)data)->entier;
        pthread_exit(data);
    }
     
    int main()
    {
     
    	pthread_attr_t attr;
    	pthread_attr_init(&attr);
     
            int i;
     
            pthread_t *  p;
     
            p = (pthread_t *  )calloc ( 5 , sizeof(pthread_t));
     
            struct structure *  t;
     
            t = calloc (5,sizeof(struct structure));
     
            for (i=0;i<5;++i){
     
                 (t+i)->entier = i;
                if ( pthread_create(p+i,&attr,fonction,(void *)(t+i)) != 0) {perror ("Rate:");exit (1);}
            }
     
            struct structure * * ret;
            ret = calloc (5,sizeof(struct structure *));
     
            for (i=0;i<5;++i){
                  pthread_join (*(p+i),(void**)(ret+i));
                  printf ("valeur de retour de %d = %d et valeur de t = %d \n",i,((struct structure*) *(ret+i))->entier,(t+i)->entier );
            }
     
     
            free(ret);
            free(p);
            free(t);
     
        return(EXIT_SUCCESS);
    }

  16. #16
    Membre éprouvé
    Homme Profil pro
    R&D imagerie 3D / prog embarquée
    Inscrit en
    Mars 2007
    Messages
    417
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : R&D imagerie 3D / prog embarquée
    Secteur : Santé

    Informations forums :
    Inscription : Mars 2007
    Messages : 417
    Points : 1 247
    Points
    1 247
    Par défaut
    Note que tu pourrais faire
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    pthread_join (p[i], NULL);
    Et aller lire ton résultat directement depuis le tableau t

    p.e.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    printf("le nombre entr� se trouve en position : %d\n", t[i].position);

  17. #17
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2011
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2011
    Messages : 20
    Points : 9
    Points
    9
    Par défaut
    merci en tout cas je vais le remettre comme ca tant pis, c'est pour demain. je vais quand meme passé encore un peu de temps maintenant
    mais merci de ton aide

  18. #18
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2011
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2011
    Messages : 20
    Points : 9
    Points
    9
    Par défaut
    il plante en premier lieu au memcpy

  19. #19
    Membre éprouvé
    Homme Profil pro
    R&D imagerie 3D / prog embarquée
    Inscrit en
    Mars 2007
    Messages
    417
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : R&D imagerie 3D / prog embarquée
    Secteur : Santé

    Informations forums :
    Inscription : Mars 2007
    Messages : 417
    Points : 1 247
    Points
    1 247
    Par défaut
    Il plante ou il compile pas ?

    S'il ne compile pas, donne moi l'output du compilateur.

  20. #20
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2011
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2011
    Messages : 20
    Points : 9
    Points
    9
    Par défaut
    ca yest j'ai reussi à compiler ton programme, mais il cherche que dans le premier tableau tout les deux. il fait de 0 a 4 ensuite il continue pas.

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. [Win] Thread passage Structure pointeur Vector + string
    Par jerem3000 dans le forum Threads & Processus
    Réponses: 2
    Dernier message: 31/10/2012, 15h24
  2. thread et structure
    Par jiganos dans le forum C
    Réponses: 9
    Dernier message: 12/05/2011, 14h04
  3. [Thread] Qt et multithread, structure ou conception
    Par alpha_one_x86 dans le forum Multithreading
    Réponses: 3
    Dernier message: 13/04/2008, 12h42
  4. Envoie de structure à un thread.
    Par mohdaef dans le forum Threads & Processus
    Réponses: 2
    Dernier message: 24/03/2008, 17h23
  5. Thread Synchronisation avec structure FIFO ??
    Par vincedom dans le forum MFC
    Réponses: 5
    Dernier message: 30/03/2006, 06h00

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo