Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <stdlib.h>
  4. #include <sys/shm.h>
  5. #include <time.h>
  6. #include <string.h>
  7. #include <sys/ipc.h>
  8. #include <sys/sem.h> /* Pour semget, semctl, semop */
  9. #include <semaphore.h>
  10.  
  11. #include <unistd.h>
  12. #define NBR_VOIT 1
  13. #define KEY 123
  14.  
  15. typedef struct{
  16. int voitID;
  17. float tempsSecteur [4];
  18. //int bestTemps [4];
  19. float tempsTotal;
  20. } voiture;
  21.  
  22. int temps(int min , int max) {//Cette fonction est utilisée pour generer un nb compris entre deux intervalles
  23. int r = (rand()%(max-min)+min);
  24. return r;
  25. }
  26.  
  27. float generer_secteur() {//Cette fonction est utilisée afin de generer un temps qui contiendra des secondes et des millisec (35.659)
  28. // VARIABLE SECONDE ENTRE 0 ET 40
  29. int seconde = 0;
  30. seconde = temps(0,40);
  31. //printf("Seconde: %d \n", seconde);
  32.  
  33. // VARIABLE POUR LES MILLISECONDES
  34. int milliSeconde = 0;
  35. milliSeconde = temps(0,999);
  36. //printf("milliSeconde: %d \n", milliSeconde);
  37.  
  38. // METTRE LE INT MILLISECONDE EN FLOAT
  39. float tampon = 0.0;
  40. tampon = milliSeconde;
  41. //printf ("tampon: %f\n", tampon);
  42.  
  43. // DIVISER LE TAMPON POUR AVOIR LES MILLISECONDE INFERIEURE A 0 (exemple: 0.458)
  44. float milliSecFloat = 0.0;
  45. milliSecFloat = tampon/1000;
  46. //printf("milliSec: %f\n",milliSecFloat);
  47.  
  48. // ADDITIONNER LES SECONDES ET LES MILLISEC POUR AVOIR UN TEMPS SECTEUR (exemple: 35 + 0.458)
  49. float secteur = 0.0;
  50. secteur = seconde + milliSecFloat;
  51.  
  52. return secteur;
  53. }
  54.  
  55. int main()
  56. {
  57. int numerosDesVoitures[20] = {44, 77, 3, 33, 5, 7, 11, 31, 19, 18, 14, 2, 10, 55, 8, 20, 27, 30, 9, 94};
  58.  
  59. //Creation et initialisation des semaphores
  60. sem_t semaphores[NBR_VOIT];
  61. int a;
  62. for(a = 0; a < NBR_VOIT;a++) {
  63. sem_init(&semaphores[a], 0, 1);
  64. }
  65. int shm_ID;
  66. voiture* mesVoitures;
  67.  
  68. //Allouage de la zone de mémoire partagée
  69. voiture maVoiture;
  70. shm_ID = shmget(KEY, 24*sizeof(maVoiture), 0666 | IPC_CREAT);
  71. if(shm_ID < 0) {
  72. perror("shmget");
  73. exit(1);
  74. }
  75. if((mesVoitures = shmat(shm_ID, NULL, 0)) == (void*) -1) {
  76. perror("shmat");
  77. exit(1);
  78. }
  79.  
  80. //Initialisation des voitures a 0
  81. int n;
  82. for(n=0; n<NBR_VOIT; n++) {
  83. mesVoitures[n].tempsSecteur [0] = 0.0;
  84. mesVoitures[n].tempsSecteur [1] = 0.0;
  85. mesVoitures[n].tempsSecteur [2] = 0.0;
  86. mesVoitures[n].tempsSecteur [3] = 0.0;
  87. }
  88.  
  89. int k;
  90. for(k = 0; k < NBR_VOIT; k++) {
  91. sleep(1);
  92. pid_t pid = fork();
  93.  
  94. if(pid < 0) { //erreur
  95. printf("fork() failed");
  96. }
  97. else if (pid == 0) { //dans le fils
  98. voiture voituresSimulateur;
  99. voituresSimulateur.voitID = numerosDesVoitures[k];
  100.  
  101. srand(time(NULL));
  102. voituresSimulateur.tempsTotal = 0;
  103. int j;
  104. for(j=0; j<3; j++) {
  105. sleep(1);
  106. voituresSimulateur.tempsSecteur[j] = generer_secteur();
  107.  
  108. //Acces a la mem partagee pour ecriture du temps de secteur
  109. sem_wait(&semaphores[k]);
  110. mesVoitures[k].tempsSecteur[j] = voituresSimulateur.tempsSecteur[j];
  111. sem_post(&semaphores[k]);
  112. }
  113. voituresSimulateur.tempsTotal = voituresSimulateur.tempsSecteur[0]+voituresSimulateur.tempsSecteur[1]+voituresSimulateur.tempsSecteur[2];
  114. /*printf("\n\n\ntestID: %d \n\n\n", voituresSimulateur.voitID );
  115. 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);*/
  116.  
  117.  
  118. exit(1);
  119. }
  120. }
  121.  
  122. voiture voituresAfficheur[NBR_VOIT];
  123. int b;
  124. //Acces a la mem partagee pour recupere les donnees des voitures
  125. for(b=0; b<NBR_VOIT;b++) {
  126. sem_wait(&semaphores[b]);
  127. voituresAfficheur[b] = mesVoitures[b];
  128. sem_post(&semaphores[b]);
  129. }
  130.  
  131. printf("Id Sec 1 Sec 2 Sec 3 Total\n"); //Ceci est l'en-tête du tableau
  132. int i;
  133. for (i = 0; i < NBR_VOIT; i++) {
  134. printf("| N°: %3d", voituresAfficheur[i].voitID);
  135. int j;
  136. for (j = 0; j < 3; j++) {
  137. printf("|%6.3f sec ", voituresAfficheur[i].tempsSecteur[j]);
  138. }
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement