int add_word(Cellule* tab, long size_of_tab, unsigned char* word, unsigned short word_len, unsigned long sentence_pos, Liste* alphabetical_word_list) { int hash = hache((char*) word); Liste l = &tab[hash % size_of_tab]; /* we get the address of the row we have to write to */ /* we have to add it at the end of the cell list. */ do { if(l->valeur != NULL && strcmp((const char*)l->valeur->mot, (const char*) word) == 0) { celmot_add_position(l->valeur, sentence_pos); return 1; } if(l->suivant == NULL) break; l = l->suivant; } while(l->suivant != NULL); Liste to_add; if(l->valeur == NULL) to_add = l; else to_add = liste_new(); char* word_to_add = malloc(sizeof(char) * (word_len)); strcpy(word_to_add, word); Celmot* elem = celmot_new(word_to_add); to_add->valeur = elem; celmot_add_position(elem, sentence_pos); if(l != to_add) liste_add(&l, to_add); liste_add_alphabetical(alphabetical_word_list, elem); return 1; }
int trt_connexion (Traitement * t) { frame *f = NULL ; entete_frame * ef = NULL ; void * data = NULL ; Comm * c = NULL ; f = TRAITEMET_FRAME(t); ef = ENTETE_FRAME(f); data = DATA_FRAME(f) ; c = TRAITEMET_AUTRE(t); switch (SOUS_TYPE_FRAME(ef)) { case ST_GC_DEMANDE_CONNEXION : if (LOG_ENTETE_FRAME(f , 0 , 40 , LOG_CONNEXION , "Demande connexion park %d %d %d %d") <0) return -1 ; SET_ENTETE_FRAME(ef , TYPE_GESTION_CONNEXION , ST_GC_CONNEXION_ACCEPTER , 0 , 0); if (ecrit_frame_liste (f , COMM_LISTE_ECR(c) , 0) < 0) return -1 ; sem_post(&COMM_SEM_ECR(c)); free(t) ; break ; case ST_GC_DEMANDE_DECONNEXION : if (LOG_ENTETE_FRAME( f , 0 , 42 , LOG_CONNEXION , "Demande deconnexion park %d %d %d %d") <0) return -1 ; SET_ENTETE_FRAME(ef , TYPE_GESTION_CONNEXION , ST_GC_DECONNEXION_ACCEPTER , 0 , 0); if (ecrit_frame_liste (f , COMM_LISTE_ECR(c) , 0) < 0) return -1 ; sem_post(&COMM_SEM_ECR(c)); sem_wait(&LISTE_SEMAPHORE(liste_deconnexion)); liste_add(liste_deconnexion , LISTE_QUEUE(liste_deconnexion) , (void *) c); sem_post(&LISTE_SEMAPHORE(liste_deconnexion)); sem_post(&(sem_deconnexion)); free(t) ; break ; } return 0 ; }
int log_mesg (int id , int n , enum type_log t , char * mesg , ...) { Log * lg = NULL ; if ((lg = malloc (sizeof(Log))) == NULL) return -1 ; LOG_ID(lg) = id ; LOG_TYPE(lg) = t ; LOG_TIME(lg) = time(NULL) ; if (n) { if ((LOG_MESG(lg) = malloc (n+1)) == NULL) { free(lg); return -1 ; } va_list ap ; va_start (ap , mesg) ; vsnprintf (LOG_MESG(lg) , n , mesg , ap); LOG_MESG(lg)[n] = '\0' ; } else { if ((LOG_MESG(lg) = strdup(mesg)) == NULL) { free(lg); return -1 ; } } sem_wait(&(LISTE_SEMAPHORE(file_logs))); liste_add(file_logs , LISTE_QUEUE(file_logs) , lg); sem_post(&(LISTE_SEMAPHORE(file_logs))); sem_post(&(sem_log)) ; return 0 ; }
void* TGV_thread_fn(void* arg) { train *TGV = (train*) arg; list *l; switch (TGV->direction) { case OE: l = liste_new(dictionary_char_2d_get(TGV->postes, "Gare", "Voie C")); liste_add(l, dictionary_char_2d_get(TGV->postes, "P0", "Aiguillage 2")); liste_add(l, dictionary_char_2d_get(TGV->postes, "P1", "Ligne TGV")); printf("%p - %s - OE - locking : Voie C, Aiguillage 2, Ligne TGV\n", (void*) (pthread_self()), TGV->nom); banquier_lock(TGV->banquier, l); printf("%p - %s - OE - locked : Voie C, Aiguillage 2, Ligne TGV\n", (void*) (pthread_self()), TGV->nom); liste_del(l); l = liste_new(dictionary_char_2d_get(TGV->postes, "Gare", "Voie C")); liste_add(l, dictionary_char_2d_get(TGV->postes, "P0", "Aiguillage 2")); printf("%p - %s - OE - releasing : Voie C, Aiguillage 2\n", (void*) (pthread_self()), TGV->nom); banquier_unlock(TGV->banquier, l); printf("%p - %s - OE - released : Voie C, Aiguillage 2\n", (void*) (pthread_self()), TGV->nom); liste_del(l); l = liste_new(dictionary_char_2d_get(TGV->postes, "P2", "Tunnel")); liste_add(l, dictionary_char_2d_get(TGV->postes, "P3", "Ligne")); printf("%p - %s - OE - locking : Tunnel, Ligne\n", (void*) (pthread_self()), TGV->nom); banquier_lock(TGV->banquier, l); printf("%p - %s - OE - locked : Tunnel, Ligne\n", (void*) (pthread_self()), TGV->nom); liste_del(l); l = liste_new(dictionary_char_2d_get(TGV->postes, "P1", "Ligne TGV")); liste_add(l, dictionary_char_2d_get(TGV->postes, "P2", "Tunnel")); liste_add(l, dictionary_char_2d_get(TGV->postes, "P3", "Ligne")); printf("%p - %s - OE - releasing : Ligne TGV, Tunnel, Ligne\n", (void*) (pthread_self()), TGV->nom); banquier_unlock(TGV->banquier, l); printf("%p - %s - OE - released : Ligne TGV, Tunnel, Ligne\n", (void*) (pthread_self()), TGV->nom); liste_del(l); break; case EO: l = liste_new(dictionary_char_2d_get(TGV->postes, "P3", "Ligne")); liste_add(l, dictionary_char_2d_get(TGV->postes, "P3", "Tunnel")); liste_add(l, dictionary_char_2d_get(TGV->postes, "P2", "Ligne TGV")); printf("%p - %s - EO - locking : Ligne, Tunnel, Ligne TGV\n", (void*) (pthread_self()), TGV->nom); banquier_lock(TGV->banquier, l); printf("%p - %s - EO - locked : Ligne, Tunnel, Ligne TGV\n", (void*) (pthread_self()), TGV->nom); liste_del(l); l = liste_new(dictionary_char_2d_get(TGV->postes, "P3", "Ligne")); liste_add(l, dictionary_char_2d_get(TGV->postes, "P3", "Tunnel")); printf("%p - %s - EO - releasing : Ligne, Tunnel\n", (void*) (pthread_self()), TGV->nom); banquier_unlock(TGV->banquier, l); printf("%p - %s - EO - released : Ligne, Tunnel\n", (void*) (pthread_self()), TGV->nom); liste_del(l); l = liste_new(dictionary_char_2d_get(TGV->postes, "P1", "Aiguillage 2")); liste_add(l, dictionary_char_2d_get(TGV->postes, "P0", "Voie D")); printf("%p - %s - EO - locking : Aiguillage 2, Voie D\n", (void*) (pthread_self()), TGV->nom); banquier_lock(TGV->banquier, l); printf("%p - %s - EO - locked : Aiguillage 2, Voie D\n", (void*) (pthread_self()), TGV->nom); liste_del(l); l = liste_new(dictionary_char_2d_get(TGV->postes, "P2", "Ligne TGV")); liste_add(l, dictionary_char_2d_get(TGV->postes, "P1", "Aiguillage 2")); liste_add(l, dictionary_char_2d_get(TGV->postes, "P0", "Voie D")); printf("%p - %s - EO - releasing : Ligne TGV, Aiguillage 2, Voie D\n", (void*) (pthread_self()), TGV->nom); banquier_unlock(TGV->banquier, l); printf("%p - %s - EO - released : Ligne TGV, Aiguillage 2, Voie D\n", (void*) (pthread_self()), TGV->nom); liste_del(l); break; default: perror("Erreur : TGV - direction inconnu"); pthread_exit(NULL); } printf("%p - %s - %s - ARRIVE ------------------------------------------\n", (void*) (pthread_self()), TGV->nom, TGV->direction == OE ? "OE" : "EO"); return NULL; }
void* M_thread_fn(void* arg) { train *M = (train*) arg; list* l; switch (M->direction) { case OE: l = liste_new(dictionary_char_2d_get(M->postes, "P1", "Voie A")); liste_add(l, dictionary_char_2d_get(M->postes, "P1", "Aiguillage 1")); liste_add(l, dictionary_char_2d_get(M->postes, "P1", "Ligne M - OE")); printf("%p - %s - OE - locking : Voie A, Aiguillage 1, Ligne M - OE\n", (void*) (pthread_self()), M->nom); banquier_lock(M->banquier, l); printf("%p - %s - OE - locked : Voie A, Aiguillage 1, Ligne M - OE\n", (void*) (pthread_self()), M->nom); liste_del(l); l = liste_new(dictionary_char_2d_get(M->postes, "P1", "Voie A")); liste_add(l, dictionary_char_2d_get(M->postes, "P1", "Aiguillage 1")); printf("%p - %s - OE - releasing : Voie A, Aiguillage 1\n", (void*) (pthread_self()), M->nom); banquier_unlock(M->banquier, l); printf("%p - %s - OE - released : Voie A, Aiguillage 1\n", (void*) (pthread_self()), M->nom); liste_del(l); l = liste_new(dictionary_char_2d_get(M->postes, "P2", "Tunnel")); liste_add(l, dictionary_char_2d_get(M->postes, "P3", "Ligne")); printf("%p - %s - OE - locking : Tunnel, Ligne\n", (void*) (pthread_self()), M->nom); banquier_lock(M->banquier, l); printf("%p - %s - OE - locked : Tunnel, Ligne\n", (void*) (pthread_self()), M->nom); liste_del(l); l = liste_new(dictionary_char_2d_get(M->postes, "P1", "Ligne M - OE")); liste_add(l, dictionary_char_2d_get(M->postes, "P2", "Tunnel")); liste_add(l, dictionary_char_2d_get(M->postes, "P3", "Ligne")); printf("%p - %s - OE - releasing : Ligne M - OE, Tunnel, Ligne\n", (void*) (pthread_self()), M->nom); banquier_unlock(M->banquier, l); printf("%p - %s - OE - released : Ligne M - OE, Tunnel, Ligne\n", (void*) (pthread_self()), M->nom); liste_del(l); break; case EO: l = liste_new(dictionary_char_2d_get(M->postes, "P3", "Ligne")); liste_add(l, dictionary_char_2d_get(M->postes, "P3", "Tunnel")); liste_add(l, dictionary_char_2d_get(M->postes, "P2", "Ligne M - EO")); printf("%p - %s - EO - locking : Ligne, Tunnel, Ligne M - EO\n", (void*) (pthread_self()), M->nom); banquier_lock(M->banquier, l); printf("%p - %s - EO - locked : Ligne, Tunnel, Ligne M - EO\n", (void*) (pthread_self()), M->nom); liste_del(l); l = liste_new(dictionary_char_2d_get(M->postes, "P3", "Ligne")); liste_add(l, dictionary_char_2d_get(M->postes, "P3", "Tunnel")); printf("%p - %s - EO - releasing : Ligne, Tunnel\n", (void*) (pthread_self()), M->nom); banquier_unlock(M->banquier, l); printf("%p - %s - EO - released : Ligne, Tunnel\n", (void*) (pthread_self()), M->nom); liste_del(l); l = liste_new(dictionary_char_2d_get(M->postes, "P1", "Aiguillage 1")); liste_add(l, dictionary_char_2d_get(M->postes, "P1", "Voie B")); printf("%p - %s - EO - locking : Aiguillage 1, Voie B\n", (void*) (pthread_self()), M->nom); banquier_lock(M->banquier, l); printf("%p - %s - EO - locked : Aiguillage 1, Voie B\n", (void*) (pthread_self()), M->nom); liste_del(l); l = liste_new(dictionary_char_2d_get(M->postes, "P2", "Ligne M - EO")); liste_add(l, dictionary_char_2d_get(M->postes, "P1", "Aiguillage 1")); liste_add(l, dictionary_char_2d_get(M->postes, "P1", "Voie B")); printf("%p - %s - EO - releasing : Ligne M - EO, Aiguillage 1, Voie B\n", (void*) (pthread_self()), M->nom); banquier_unlock(M->banquier, l); printf("%p - %s - EO - released : Ligne M - EO, Aiguillage 1, Voie B\n", (void*) (pthread_self()), M->nom); liste_del(l); break; default: perror("Erreur : M - direction inconnu"); pthread_exit(NULL); } printf("%p - %s - %s - ARRIVE ------------------------------------------\n", (void*) (pthread_self()), M->nom, M->direction == OE ? "OE" : "EO"); return NULL; }