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

C Discussion :

Getchar() et printf() en parallele dans un thread


Sujet :

C

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Novembre 2008
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2008
    Messages : 2
    Par défaut Getchar() et printf() en parallele dans un thread
    Bonjour,

    Dans le cadre du développement d'une application sous système linux, j'ai besoin de lire une saisie clavier dans un main() tout en étant capable d'écrire des données sur la console depuis un thread initialisé par ce même main().

    Seulement, j'ai beau faire des printf() dans le thread, rien ne s'affiche tant que je n'envoie pas de \n dans un printf().

    Voir fichier attaché printf.c ( gcc printf.c -o printf -lpthread ).
    Si je commente la ligne printf("\n");, "mon texte" ne s'affichera pas.

    Je ne sais pas d'où cela peut venir même si j'imagine que c'est peut être un tampon au niveau de la sortie qui attend un \n.

    Pouvez-vous m'expliquer l'origine de ce problème et idéalement comment le régler ?
    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
     
    #include <stdio.h>
    #include <pthread.h>
    #include <unistd.h>
     
    void* thread(void* arg) {
     
       sleep(1); // attendre une seconde que getchar se lance
     
       printf("mon texte");
     
       printf("\n"); // Obligatoire sinon "mon texte" ne s'affiche pas
     
       return NULL;
    }
     
    int main (int argc, char ** argv) {
     
       pthread_t threadEcoute;
       pthread_attr_t attr;
       pthread_attr_init(&attr);
     
       if (pthread_create(&threadEcoute, &attr, thread, NULL) != 0)
          perror("erreur de thread");
     
       char caractere = getchar();
     
       return 0;
    }

  2. #2
    Membre Expert Avatar de nicolas.sitbon
    Profil pro
    Inscrit en
    Août 2007
    Messages
    2 015
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 2 015
    Par défaut
    7.19.3 Files
    1 A stream is associated with an external file (which may be a physical device) by opening
    a file, which may involve creating a new file. Creating an existing file causes its former
    contents to be discarded, if necessary. If a file can support positioning requests (such as a
    disk file, as opposed to a terminal), then a file position indicator associated with the
    stream is positioned at the start (character number zero) of the file, unless the file is
    opened with append mode in which case it is implementation-defined whether the file
    position indicator is initially positioned at the beginning or the end of the file. The file
    position indicator is maintained by subsequent reads, writes, and positioning requests, to
    facilitate an orderly progression through the file.
    2 Binary files are not truncated, except as defined in 7.19.5.3. Whether a write on a text
    stream causes the associated file to be truncated beyond that point is implementation-
    defined.
    3 When a stream is unbuffered, characters are intended to appear from the source or at the
    destination as soon as possible. Otherwise characters may be accumulated and
    transmitted to or from the host environment as a block. When a stream is fully buffered,
    characters are intended to be transmitted to or from the host environment as a block when
    a buffer is filled. When a stream is line buffered, characters are intended to be
    transmitted to or from the host environment as a block when a new-line character is
    encountered.
    Furthermore, characters are intended to be transmitted as a block to the host
    environment when a buffer is filled, when input is requested on an unbuffered stream, or
    when input is requested on a line buffered stream that requires the transmission of
    characters from the host environment. Support for these characteristics is
    implementation-defined, and may be affected via the setbuf and setvbuf functions.

  3. #3
    Membre Expert Avatar de nicolas.sitbon
    Profil pro
    Inscrit en
    Août 2007
    Messages
    2 015
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 2 015
    Par défaut
    Tu peux forcer l'écriture des données en faisant un

  4. #4
    Membre émérite Avatar de orfix
    Homme Profil pro
    Inscrit en
    Avril 2007
    Messages
    707
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Avril 2007
    Messages : 707
    Par défaut
    Plusieurs solutions s'offre à toi:
    • Écrire sur la sortie d'erreurs qui est non bufferisée
    • Forcer l'affichage des printf incriminé par un fflush(stdout)
    • Changer le mode de bufferisation pour stdout avec un setbuf(stdout,NULL)

  5. #5
    Candidat au Club
    Profil pro
    Inscrit en
    Novembre 2008
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2008
    Messages : 2
    Par défaut
    Merci, ça fonctionne parfaitement

    j'avais déjà essayé fflush mais en le mettant avant mes printf, j'avais compris qu'il empêchait la mise en tampon des données qui arrivaient après son appel.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. impossible d'utiliser un HWND parent dans un thread
    Par sylvain114d dans le forum Windows
    Réponses: 12
    Dernier message: 23/09/2004, 12h21
  2. Synchronize bloquant dans un thread
    Par bencot dans le forum Langage
    Réponses: 3
    Dernier message: 20/08/2004, 16h42
  3. [Process]Execution de process dans un thread
    Par devjava dans le forum Concurrence et multi-thread
    Réponses: 5
    Dernier message: 18/06/2004, 10h34
  4. erreur d'un timer declaré dans un thread
    Par hak5 dans le forum C++Builder
    Réponses: 2
    Dernier message: 03/04/2004, 09h20
  5. Gestion des message windows dans les threads
    Par billyboy dans le forum Windows
    Réponses: 5
    Dernier message: 06/10/2003, 17h25

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