Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1553 connectés 

  FORUM HardWare.fr
  Programmation
  C

  [c] Problème producteur-consommateur

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[c] Problème producteur-consommateur

n°1380580
kerrighan
Carpe diem, seize the day...
Posté le 03-06-2006 à 21:06:40  profilanswer
 

Bonjour,
Je suis entrain d'essayer de faire un petit code mettant en oeuvre le schéma producteur / consommateur.
Le fonctionnement du programme est simple:
Le producteur incrémente une variable entière et le producteur affiche cette variable.
Le soucis c'est que j'ai une segmentation fault au moment de l'affichage et je n'arrive pas à trouver de solution :(
 

Code :
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <pthread.h>
  4. #include <semaphore.h>
  5. // initialisation du mutex
  6. pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
  7. sem_t scons;
  8. sem_t sprod;
  9. int somme=0;
  10. int * p_somme;
  11. int temp;
  12. int * p_temp;
  13. void affichage(int * p_somme) {
  14. printf("%s\n",*p_somme);
  15. }
  16. void calcul(int * p_somme) {
  17. *p_somme = *p_somme + 1;
  18. p_temp = &somme;
  19. sleep(1);
  20. }
  21. //**********************************
  22. //Le consommateur
  23. //**********************************
  24. void mutex_cons (int somme)
  25. {
  26. for (;;)
  27. {
  28.  sem_wait(&scons);
  29.  pthread_mutex_lock(&m);
  30.  affichage(p_somme);
  31.  pthread_mutex_unlock(&m);
  32.  sem_post(&sprod);
  33. }
  34. }
  35. //**********************************
  36. //Le producteur
  37. //**********************************
  38. char mutex_prod (int somme)
  39. {
  40. for (;;)
  41. {
  42.  calcul(p_somme);
  43.  sem_wait(&sprod);
  44.  pthread_mutex_lock(&m);
  45.  p_somme = p_temp;
  46.  sem_post(&scons);
  47.  pthread_mutex_unlock(&m);
  48. }
  49. }
  50. //**********************************
  51. //Le main du programme
  52. //**********************************
  53. int main(int argc, char * argv[])
  54. {
  55. p_somme = &somme;
  56. // Initialise les semaphores
  57. sem_init(&scons, 0, 0);
  58. sem_init(&sprod, 0, 1);
  59. // Cree les threads
  60. pthread_t tid_cons;
  61. pthread_t tid_prod;
  62.  
  63. pthread_create(&tid_prod, NULL, mutex_prod,p_somme);
  64. pthread_create(&tid_cons, NULL, mutex_cons,p_somme);
  65.  
  66. // termine les threads
  67. int ret;
  68. pthread_join(tid_cons, &ret);
  69. printf("Retour thread conso n%d = %d \n", tid_cons, ret);
  70. pthread_join(tid_prod, &ret);
  71. printf("Retour thread prod n%d = %d \n", tid_prod, ret);
  72. return 0;
  73. }
  74. //fin main

mood
Publicité
Posté le 03-06-2006 à 21:06:40  profilanswer
 

n°1380597
Taz
bisounours-codeur
Posté le 03-06-2006 à 21:47:43  profilanswer
 

printf("%s\n",*p_somme)
 
nan mais franchement ...

n°1380601
kerrighan
Carpe diem, seize the day...
Posté le 03-06-2006 à 21:51:54  profilanswer
 

kel naze je fais :D désolé d'avoir posté un bourde pareille :jap:

n°1380605
Emmanuel D​elahaye
C is a sharp tool
Posté le 03-06-2006 à 22:01:20  profilanswer
 

kerrighan a écrit :

Bonjour,
Je suis entrain d'essayer de faire un petit code mettant en oeuvre le schéma producteur / consommateur.
Le fonctionnement du programme est simple:
Le producteur incrémente une variable entière et le producteur affiche cette variable.
Le soucis c'est que j'ai une segmentation fault au moment de l'affichage et je n'arrive pas à trouver de solution :(
 


Ca fout la trouille...


Project   : OP code
Compiler  : GNU GCC Compiler (called directly)
Directory : C:\dev\forums\OP\
--------------------------------------------------------------------------------
Switching to target: default
Compiling: main.c
In file included from main.c:3:
main.c:20: warning: declaration of 'p_somme' shadows a global declaration
main.c:14: warning: shadowed declaration is here
main.c:20: warning: no previous prototype for 'affichage'
main.c: In function `affichage':
main.c:21: warning: format argument is not a pointer (arg 2)
main.c: At top level:
main.c:24: warning: declaration of 'p_somme' shadows a global declaration
main.c:14: warning: shadowed declaration is here
main.c:24: warning: no previous prototype for 'calcul'
main.c:33: warning: declaration of 'somme' shadows a global declaration
main.c:13: warning: shadowed declaration is here
main.c:34: warning: no previous prototype for 'mutex_cons'
main.c:48: warning: declaration of 'somme' shadows a global declaration
main.c:13: warning: shadowed declaration is here
main.c:49: warning: no previous prototype for 'mutex_prod'
main.c: In function `main_':
main.c:77: warning: passing arg 3 of `pthread_create' from incompatible pointer type
main.c:78: warning: passing arg 3 of `pthread_create' from incompatible pointer type
main.c:82: warning: passing arg 2 of `pthread_join' from incompatible pointer type
main.c:83: warning: int format, pthread_t arg (arg 2)
main.c:84: warning: passing arg 2 of `pthread_join' from incompatible pointer type
main.c:85: warning: int format, pthread_t arg (arg 2)
main.c: In function `mutex_cons':
main.c:44: warning: function might be possible candidate for attribute `noreturn'
main.c: In function `mutex_prod':
main.c:60: warning: function might be possible candidate for attribute `noreturn'
Linking console executable: C:\dev\forums\OP\01.exe
Process terminated with status 0 (0 minutes, 1 seconds)
0 errors, 21 warnings


http://mapage.noos.fr/emdel/pthreads.htm
http://mapage.noos.fr/emdel/goret.htm


Message édité par Emmanuel Delahaye le 03-06-2006 à 22:03:20

---------------
Des infos sur la programmation et le langage C: http://www.bien-programmer.fr Pas de Wi-Fi à la maison : http://www.cpl-france.org/

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C

  [c] Problème producteur-consommateur

 

Sujets relatifs
probleme avec mon serpentProblème EasyPHP $_GET[param] sans guillemets (urgent svp)
[Résolu] Problème MySQL 1&1 : plus de protection de chaine !Problème excel... [RESOLU]
Gros problème de liens dans mon site..Probleme sur DataGridView sous VB DotNet
[jsp] problème sur un useBeanProbleme validation xhtml
Problème en AccessLes iframes: problème pour le référencement!
Plus de sujets relatifs à : [c] Problème producteur-consommateur


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR