Example #1
0
void* musician_management(void* arg)
{
	//Déclarations des variables
	DataSpec * data = (DataSpec *) arg; //On caste le pointeur passé en argument pour retrouver un DataSpec (au lieu d'un void*)
  	int arret = FALSE, nblus; //On initialise le booléen qui arrêtera la boucle
  	char texte[LIGNE_MAX]; 

  	//Gestion des musiciens
  	printf("worker %d: waiting for channel.\n", data->tid);
    while(TRUE)
    {
    	if (sem_wait(&data->sem) != 0) //Les semaphores associés aux workers sont initialisés à 0, donc bloqués par la fonction sem_wait, en l'attente d'un passage à 1 (c-à-d de l'affectation du thread) 
	    {
	      erreur_pthread_IO("sem_wait");
	    }
	    data->libre = FALSE; //Une fois l'attente terminée, on marque le thread comme occupé
	    printf("worker %d: reading channel %d.\n", data->tid, data->canal); 

	    arret = FALSE;

	    while (arret == FALSE) //Boucle d'exécution du thread
	    {
	    	nblus = lireLigne (data->canal, texte); //On lit les commandes de l'utilisateur
	      	if (nblus <= 0 || nblus == LIGNE_MAX) 
	      	{
				erreur("lireLigne\n");
		    }

			if (strcmp(texte, "/end") == 0) 
			{
				printf("worker %d: ask for disconnection.\n", data->tid);
				arret = TRUE;
				continue;//On reboucle, et arret = TRUE donc on ne repasse pas par le while, on passe à la suite
		    }
		}
		if (close(data->canal) == -1) //On ferme le canal
		{
	     	erreur_IO("close");
	    }
	    data->canal = -1; //On réinitialise les paramètres
	    data->libre = TRUE; //On indique que le thread est dispo
	    if (sem_post(&sem_libres) != 0) //On marque le sémaphore
	    {
	      	erreur_pthread_IO("sem_post");
	    }
    }//En rebouclant, on attend une nouvelle connexion
    
  	pthread_exit(NULL); //On libère le thread
}
Example #2
0
File: ish.c Project: Depado/ish
int main (int N, char *P[])
{
int n;
   signal(SIGINT,SIG_IGN);
   signal(SIGTERM,interrupt);
   while (RUN) {
     printf("ish> ");  /* affichage du prompt */
     if ((n = lireLigne(buf, LBUF)) == -1) {
        fprintf(stderr,"La ligne est trop grande !!\n");
        continue;
     } 
     if (n) execute(buf);
   }
   printf("Au revoir !\n");
   return 0;
}
Example #3
0
int main(int N, char *P[])
{
int n,Ctx;
char *dirW = ".nife";
    if (N > 2) {
       fprintf(stderr,"nife [nif-file]\n");
       return(1);
    }
    if ((sizeof(void*) != sizeof(long)) ||
       (sizeof(double) != sizeof(long long))) {
       fprintf(stderr,"Nife open-source don't runs on these machine !\n");
       return(2);
    }
    signal(SIGQUIT,SIG_IGN);
    signal(SIGABRT,SIG_IGN);
    signal(SIGUSR1,SIG_IGN);
    signal(SIGCONT,SIG_IGN);
    signal(SIGSTOP,SIG_IGN);
    signal(SIGTSTP,SIG_IGN);
    signal(SIGINT,Interrupt);
    signal(SIGTERM,Interrupt);
    signal(SIGPIPE,Interrupt);
    signal(SIGCHLD,Interrupt);
    signal(SIGQUIT,Interrupt);
    signal(SIGSEGV,Interrupt);
    signal(SIGFPE,Interrupt);
    signal(SIGALRM,Interrupt);
    /* work in ./.nife for facilities of debugging !! */
    if (chdir(dirW) != 0) {
       if (mkdir(dirW, 0755) == -1) {
          perror("mkdir"); return 1;
       }
       if (chdir(dirW) != 0) {
          perror("chdir"); return 1;
       }
    }
    termInit(); /* may stop if no term found */
    TH_init();
    initLib();
    D_Reset();
    if (N==2) {
       IF_Load();
       lectFic(P[1]);
    } else {
      printf("Welcome to Nife : Just stack it !\n");
      IF_helpS();
    }
    while (RUN) {
       if ((FD_IN+iTERM) == 0) {
          printf("> ");
          fflush(stdout);
          Ctx=0;
       } else Ctx=1;
       razErr();
       if ((n=lireLigne(FD_IN,bufP,bufP2,LBUF)) == -1)
                 printf("Line too long!\n");
       else
          if (n>0) traiteLigne(bufP,0);
    }
    IF_delAllGP();
    IF_netStopS();
    IF_netOff();
    D_Close();
    termReset();
    printf("Bye !\n");
    return 0;
}
Example #4
0
int main(int argc, char *argv[]) {
  int fifo, log, arret = FAUX, nblus, mode;
  char texte[LIGNE_MAX];
  
  fifo = open("fifo", O_RDONLY);
  if (fifo == -1) {
    erreur_IO("open fifo");
  }
 
  mode = O_WRONLY|O_APPEND|O_CREAT;
  log = open("journal.log", mode, 0660);
  if (log == -1) {
    erreur_IO("open log");
  }

  while (arret == FAUX) {
    mode |= O_TRUNC;
    nblus = lireLigne (fifo, texte);
    if (nblus == -1) {
      erreur_IO("lireLigne");
    }
    else if (nblus == LIGNE_MAX) {
      erreur("ligne trop longue\n");
    }
    else if (nblus == 0) {
      continue;
    }
    else {
      if (strcmp(texte, "fin") == 0) {
	printf("Serveur. arret demande.\n");
	arret = VRAI;
	continue;
      }
      else if (strcmp(texte, "init") == 0) {
	printf("Serveur. remise a zero du journal demandee.\n");
	if (close(log) == -1) {
	  erreur_IO("close log");
	}
	log = open("journal.log", mode, 0660);
	if (log == -1) {
	  erreur_IO("open trunc log");
	}
      }
      else {
	if (ecrireLigne(log, texte) == -1) {
	  erreur_IO("ecrireLigne");
	}
	printf("Serveur. ligne de %d octets ecrite dans le journal.\n", nblus);
      }
    }
  }

  if (close(log) == -1) {
    erreur_IO("close log");
  }

  if (close(fifo) == -1) {
    erreur_IO("close fifo");
  }
  
  exit(EXIT_SUCCESS);
}