Exemple #1
0
static void sigChldHandler(int noSig, siginfo_t *siginfo, void *context)
{
	std::map<pid_t, Voiture>::iterator voiturierIt = mapVoiturier.find(siginfo->si_pid);
	if(voiturierIt == mapVoiturier.end())
    {
    	return; //si le signal n'a pas ete envoye par un voiturier
    }
    else
    {
        //affiche sur l'IHM
        AfficherPlace(siginfo->si_status,voiturierIt->second.typeUsager,voiturierIt->second.immatriculation,
                      voiturierIt->second.heureArrivee);
        //ajoute la voiture au parking
    	semP(SEMELM_MP_PARKING);
    	mpParking[siginfo->si_status] = voiturierIt->second;
    	semV(SEMELM_MP_PARKING);
    	mapVoiturier.erase(voiturierIt);
    }
}
static
void
makeprocs(bool dowait)
{
    int i, status, failcount;
    struct usem s1, s2;
    pid_t pids[NJOBS];

    if (dowait) {
        semcreate("1", &s1);
        semcreate("2", &s2);
    }

    tprintf("Job size approximately %lu bytes\n", (unsigned long) JOBSIZE);
    tprintf("Forking %d jobs; total load %luk\n", NJOBS,
            (unsigned long) (NJOBS * JOBSIZE)/1024);

    for (i=0; i<NJOBS; i++) {
        pids[i] = fork();
        if (pids[i]<0) {
            warn("fork");
        }
        if (pids[i]==0) {
            /* child */
            if (dowait) {
                //tsay("Process %d forked\n", i);
                semopen(&s1);
                semopen(&s2);
                semV(&s1, 1);
                semP(&s2, 1);
                semclose(&s1);
                semclose(&s2);
            }
            go(i);
        }
    }

    if (dowait) {
        semopen(&s1);
        semopen(&s2);
        //tsay("Waiting for fork...\n");
        semP(&s1, NJOBS);
        //tsay("Starting computation.\n");
        semV(&s2, NJOBS);
    }

    failcount=0;
    for (i=0; i<NJOBS; i++) {
        if (pids[i]<0) {
            failcount++;
        }
        else {
            if (waitpid(pids[i], &status, 0)<0) {
                err(1, "waitpid");
            }
            if (status_is_failure(status)) {
                failcount++;
            }
        }
    }

    if (failcount>0) {
        printf("%d subprocesses failed\n", failcount);
        exit(1);
    }
    nprintf("\n");
    tprintf("Test complete\n");
    success(TEST161_SUCCESS, SECRET, "/testbin/parallelvm");

    semclose(&s1);
    semclose(&s2);
    semdestroy(&s1);
    semdestroy(&s2);
}
Exemple #3
0
void Entree(TypeBarriere typeBarriere)
{
	//definition des id d'acces au boites aux lettres et semaphores
	long msgBufEntreeId;
	long msgbufRequeteId;
	unsigned short semElementSyncEntree;
    TypeZone zoneEcranRequete;
	switch (typeBarriere)
	{
		case PROF_BLAISE_PASCAL :
			//deja initialises aux bonnes valeurs;
			msgBufEntreeId = MSGBUF_ID_ENTREE_P;
			msgbufRequeteId = MSGBUF_ID_REQUETE_P;
	 		semElementSyncEntree = SEMELM_SINC_ENTREE_P;
            zoneEcranRequete = REQUETE_R1;
			break;
			
		case AUTRE_BLAISE_PASCAL :
		
			msgBufEntreeId = MSGBUF_ID_ENTREE_A;
			msgbufRequeteId = MSGBUF_ID_REQUETE_A;
			semElementSyncEntree = SEMELM_SINC_ENTREE_A;
            zoneEcranRequete = REQUETE_R2;
			break;

		case ENTREE_GASTON_BERGER :
		
			msgBufEntreeId = MSGBUF_ID_ENTREE_GB;
			msgbufRequeteId = MSGBUF_ID_REQUETE_GB;
			semElementSyncEntree = SEMELM_SINC_ENTREE_GB;
            zoneEcranRequete = REQUETE_R3;
			break;

		default :

			std::cerr << "not an entry process error" << std::endl;
            return;
			break;
	}
    //initialisation
    init();

    //phase moteur
    pid_t pidCurr;
    Voiture voiture;
    for(;;)
    {
        //appel bloquant, attente d'une demande d'entree
        if(msgrcv(msgbuffId, &voiture, sizeof(Voiture)-sizeof(long), msgBufEntreeId, 0) == -1)
            continue; // sarestart (sur les erreurs aussi)
		DessinerVoitureBarriere(typeBarriere, voiture.typeUsager);
		
		if(semVal(SEMELM_PLACEDISPO) > 0)
		{
			if((pidCurr = GarerVoiture(typeBarriere)) != -1)
			{
				semP(SEMELM_PLACEDISPO);
				mapVoiturier.insert(std::pair<pid_t, Voiture>(pidCurr, voiture));
			}
		}
		else
		{
            //affiche requete
            AfficherRequete(typeBarriere,voiture.typeUsager,voiture.heureArrivee);
            Requete re;
            re.type = msgbufRequeteId;
            re.heureArrivee = voiture.heureArrivee;
            re.typeUsager = voiture.typeUsager;
            if(msgsnd(msgbuffId, &re, sizeof(Requete)-sizeof(long), 0 )==-1);
			semP(semElementSyncEntree);
			if((pidCurr = GarerVoiture(typeBarriere)) != -1)
			{
                //met à jour l'heure d'entree dans le parking
                voiture.heureArrivee = time(NULL);
				mapVoiturier.insert(std::pair<pid_t, Voiture>(pidCurr, voiture));
			}
            Effacer(zoneEcranRequete);
		}
    }
}