Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include <sys/shm.h>
- #include <time.h>
- #include <string.h>
- #include <sys/ipc.h>
- #include <sys/sem.h> /* Pour semget, semctl, semop */
- #include <semaphore.h>
- #include <unistd.h>
- #define NBR_VOIT 1
- #define KEY 123
- typedef struct{
- int voitID;
- float tempsSecteur [4];
- //int bestTemps [4];
- float tempsTotal;
- } voiture;
- int temps(int min , int max) {//Cette fonction est utilisée pour generer un nb compris entre deux intervalles
- int r = (rand()%(max-min)+min);
- return r;
- }
- float generer_secteur() {//Cette fonction est utilisée afin de generer un temps qui contiendra des secondes et des millisec (35.659)
- // VARIABLE SECONDE ENTRE 0 ET 40
- int seconde = 0;
- seconde = temps(0,40);
- //printf("Seconde: %d \n", seconde);
- // VARIABLE POUR LES MILLISECONDES
- int milliSeconde = 0;
- milliSeconde = temps(0,999);
- //printf("milliSeconde: %d \n", milliSeconde);
- // METTRE LE INT MILLISECONDE EN FLOAT
- float tampon = 0.0;
- tampon = milliSeconde;
- //printf ("tampon: %f\n", tampon);
- // DIVISER LE TAMPON POUR AVOIR LES MILLISECONDE INFERIEURE A 0 (exemple: 0.458)
- float milliSecFloat = 0.0;
- milliSecFloat = tampon/1000;
- //printf("milliSec: %f\n",milliSecFloat);
- // ADDITIONNER LES SECONDES ET LES MILLISEC POUR AVOIR UN TEMPS SECTEUR (exemple: 35 + 0.458)
- float secteur = 0.0;
- secteur = seconde + milliSecFloat;
- return secteur;
- }
- int main()
- {
- int numerosDesVoitures[20] = {44, 77, 3, 33, 5, 7, 11, 31, 19, 18, 14, 2, 10, 55, 8, 20, 27, 30, 9, 94};
- //Creation et initialisation des semaphores
- sem_t semaphores[NBR_VOIT];
- int a;
- for(a = 0; a < NBR_VOIT;a++) {
- sem_init(&semaphores[a], 0, 1);
- }
- int shm_ID;
- voiture* mesVoitures;
- //Allouage de la zone de mémoire partagée
- voiture maVoiture;
- shm_ID = shmget(KEY, 24*sizeof(maVoiture), 0666 | IPC_CREAT);
- if(shm_ID < 0) {
- perror("shmget");
- exit(1);
- }
- if((mesVoitures = shmat(shm_ID, NULL, 0)) == (void*) -1) {
- perror("shmat");
- exit(1);
- }
- //Initialisation des voitures a 0
- int n;
- for(n=0; n<NBR_VOIT; n++) {
- mesVoitures[n].tempsSecteur [0] = 0.0;
- mesVoitures[n].tempsSecteur [1] = 0.0;
- mesVoitures[n].tempsSecteur [2] = 0.0;
- mesVoitures[n].tempsSecteur [3] = 0.0;
- }
- int k;
- for(k = 0; k < NBR_VOIT; k++) {
- sleep(1);
- pid_t pid = fork();
- if(pid < 0) { //erreur
- printf("fork() failed");
- }
- else if (pid == 0) { //dans le fils
- voiture voituresSimulateur;
- voituresSimulateur.voitID = numerosDesVoitures[k];
- srand(time(NULL));
- voituresSimulateur.tempsTotal = 0;
- int j;
- for(j=0; j<3; j++) {
- sleep(1);
- voituresSimulateur.tempsSecteur[j] = generer_secteur();
- //Acces a la mem partagee pour ecriture du temps de secteur
- sem_wait(&semaphores[k]);
- mesVoitures[k].tempsSecteur[j] = voituresSimulateur.tempsSecteur[j];
- sem_post(&semaphores[k]);
- }
- voituresSimulateur.tempsTotal = voituresSimulateur.tempsSecteur[0]+voituresSimulateur.tempsSecteur[1]+voituresSimulateur.tempsSecteur[2];
- /*printf("\n\n\ntestID: %d \n\n\n", voituresSimulateur.voitID );
- printf("\n\n\ntest: %f \n\n\ntest2: %f \n\n\ntest3: %f \n\n\ntotal: %f\n\n\n", voituresSimulateur.tempsSecteur[0], voituresSimulateur.tempsSecteur[1], voituresSimulateur.tempsSecteur[2], voituresSimulateur.tempsTotal);*/
- exit(1);
- }
- }
- voiture voituresAfficheur[NBR_VOIT];
- int b;
- //Acces a la mem partagee pour recupere les donnees des voitures
- for(b=0; b<NBR_VOIT;b++) {
- sem_wait(&semaphores[b]);
- voituresAfficheur[b] = mesVoitures[b];
- sem_post(&semaphores[b]);
- }
- printf("Id Sec 1 Sec 2 Sec 3 Total\n"); //Ceci est l'en-tête du tableau
- int i;
- for (i = 0; i < NBR_VOIT; i++) {
- printf("| N°: %3d", voituresAfficheur[i].voitID);
- int j;
- for (j = 0; j < 3; j++) {
- printf("|%6.3f sec ", voituresAfficheur[i].tempsSecteur[j]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement