[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [HS] Threads



Le Wed, 13 Apr 2005 01:10:59 +0200
manioul <manioul@manioul.org> a écrit:

> Le mardi 12 avril 2005 à 23:17 +0200, François Boisson a écrit :
> [...]
> > > Que dit `ps fax` ?
> > 
> > 21389 pts/8    Ss     0:00        \_ bash -i
> > 21544 pts/8    S+     0:00              \_ logrecept -y
> > 21546 pts/8    S+     0:00                      \_ logrecept -y
> > 21547 pts/8    S+     0:02                             \_
> > logrecept-y
> > 
> > A priori, il est normal que le dernier soit celui qui bosse, il
> > fabrique les requêtes SQL et les exécute, le maître se contente
> > (comme tous les patrons (!)) de recevoir les paquets et de
> > transmettre...
> Tu as bien vérifié dans ton code si tu es fils ou parent? Il semble
> que tes processus fils fork...


Je confirme bien, un thread crée, 3 processus fils l'un de l'autre:

Le petit fils est le thread (il disparait à la fin du thread)
Le père est le processus maitre
Le fiston semble être un processus qui gère les mutex et autres conds,
j'ai testé ça avec le programme suivant en le modifiant pour geler ou
non l'arrêt.

François Boisson

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <time.h>
#include <pwd.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <signal.h>
#include <pthread.h>
#include <malloc.h>
pthread_mutex_t mutex_file = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_boulot = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_boulot = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex_fini = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_fini = PTHREAD_COND_INITIALIZER;
pthread_t tache;

int STOP=0;


void rajoute()
{
  printf("Hop un coup de tache\n");
  pthread_mutex_lock(& mutex_boulot);
  pthread_cond_signal(& cond_boulot);
  pthread_mutex_unlock(& mutex_boulot);
}

void * tache_de_fond(queud)
     void *queud;
{
  while(STOP==0)
    {
      printf("Salut c'est le thread\n");
      pthread_mutex_lock(& mutex_boulot);
      pthread_cond_wait(& cond_boulot,& mutex_boulot);
      pthread_mutex_unlock(& mutex_boulot);
    }
  printf("Stop Thread\n");
  pthread_mutex_lock(& mutex_fini);
  pthread_cond_signal(& cond_fini);
  pthread_mutex_unlock(& mutex_fini);
  printf("Retour Thread\n");
  pthread_exit(NULL);
}


void arret(inutile)
     int inutile;
{
  int i,j,k,l;
  printf("STOP: ^C arrivé\n");
  pthread_mutex_lock(& mutex_boulot);
  pthread_cond_signal(& cond_boulot);
  pthread_mutex_unlock(& mutex_boulot);
  if (pthread_equal(tache,pthread_self()))
    {
     printf("Hmm, CtrlC sur le Thread\n");
    }
  else
    {
      printf("On arrete le thread!\n");
  pthread_mutex_lock(& mutex_fini);
  printf("Attente du thread\n");
  STOP=1;
  /* attente de la fin du thread */
  pthread_mutex_lock(& mutex_boulot);
  pthread_cond_signal(& cond_boulot);
  pthread_mutex_unlock(& mutex_boulot);
  pthread_cond_wait(& cond_fini,& mutex_fini);
  pthread_mutex_unlock(& mutex_fini);
  printf("Ahah thread fini?\n");
  pthread_join(tache,NULL);
  /* on arrete l'envoi des logs */
  printf("Thread fini!\n");
  exit(0);
    }
}

main(argc,argv)
int argc;
char **argv;
{
  /* programmation dégueulasse, ramassis de variables, tant pis */

  signal(SIGINT,arret);
  /* lancement du thread */
  pthread_create(&tache,NULL,tache_de_fond,NULL);
  while (1==1)
    {
  pthread_mutex_lock(& mutex_boulot);
  pthread_cond_signal(& cond_boulot);
  pthread_mutex_unlock(& mutex_boulot);
    }

  exit(0);
}



Reply to: