elementary.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include <mpi.h>
  2. #include <signal.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <unistd.h>
  7. #define CHECKPOINT_FILE "checkpoint.txt"
  8. char buffer[100];
  9. char checkpointfile[100];
  10. void elapsedTimeToString(char *output, size_t size) {
  11. static clock_t start_time = 0;
  12. if (start_time == 0) {
  13. // Enregistrez le temps de démarrage lors de la première invocation
  14. start_time = clock();
  15. }
  16. // Calculez le temps écoulé depuis le démarrage du programme
  17. clock_t current_time = clock();
  18. double elapsed_time = ((double)(current_time - start_time)) / CLOCKS_PER_SEC;
  19. // Écrivez le temps écoulé dans la chaîne de texte
  20. snprintf(output, size, "Temps écoulé : %.2f secondes", elapsed_time);
  21. }
  22. // Fonction pour sauvegarder l'état (checkpoint)
  23. void save_checkpoint(int x, int y) {
  24. FILE *fp = fopen(checkpointfile, "w");
  25. if (fp == NULL) {
  26. perror("Erreur à l'ouverture du fichier pour sauvegarder le checkpoint");
  27. exit(EXIT_FAILURE);
  28. }
  29. fprintf(fp, "%d %d", x, y);
  30. fclose(fp);
  31. printf("Checkpoint sauvegardé dans %s\n", checkpointfile);
  32. }
  33. // Fonction pour restaurer l'état à partir du checkpoint
  34. void restore_checkpoint(int *x, int *y) {
  35. FILE *fp = fopen(checkpointfile, "r");
  36. if (fp == NULL) {
  37. perror("Erreur à l'ouverture du fichier pour restaurer le checkpoint");
  38. exit(EXIT_FAILURE);
  39. }
  40. if (fscanf(fp, "%d %d", x, y) != 2) {
  41. fprintf(stderr, "Erreur de lecture dans le fichier de checkpoint\n");
  42. exit(EXIT_FAILURE);
  43. }
  44. fclose(fp);
  45. printf("Checkpoint restauré : x = %d, y = %d\n", *x, *y);
  46. }
  47. int writeToFile(const char *text) {
  48. FILE *file;
  49. // Ouverture du fichier en mode écriture (efface le contenu existant)
  50. file = fopen("/tmp/output.txt", "a");
  51. // Vérifier si l'ouverture du fichier a réussi
  52. if (file == NULL) {
  53. perror("Erreur lors de l'ouverture du fichier");
  54. return 1;
  55. }
  56. // Écriture dans le fichier
  57. fprintf(file, "%s", text);
  58. // Fermeture du fichier
  59. fclose(file);
  60. printf("Données écrites dans le fichier avec succès.\n");
  61. return 0;
  62. }
  63. void handle_sigterm(int signum) {
  64. printf("Received SIGTERM (%d).\n", signum);
  65. snprintf(buffer, sizeof(buffer), "Received SIGTERM (%d).\n", signum);
  66. writeToFile(buffer);
  67. elapsedTimeToString(buffer, sizeof(buffer));
  68. writeToFile(buffer);
  69. // Ajoutez ici le code de nettoyage ou de gestion du signal SIGKILL
  70. // Exemple : Fermez les fichiers, libérez la mémoire, etc.
  71. // ...
  72. // Terminez proprement le programme
  73. exit(EXIT_SUCCESS);
  74. }
  75. void handle_sigint(int signum) {
  76. printf("Received SIGINT (%d).\n", signum);
  77. snprintf(buffer, sizeof(buffer), "Received SIGINT (%d).\n", signum);
  78. writeToFile(buffer);
  79. elapsedTimeToString(buffer, sizeof(buffer));
  80. writeToFile(buffer);
  81. exit(EXIT_SUCCESS);
  82. }
  83. int main() {
  84. // Installez le gestionnaire de signal pour SIGKILL
  85. elapsedTimeToString(buffer, sizeof(buffer));
  86. if (signal(SIGTERM, handle_sigterm) == SIG_ERR) {
  87. perror("Erreur lors de l'installation du gestionnaire de signal");
  88. return EXIT_FAILURE;
  89. }
  90. if (signal(SIGINT, handle_sigint) == SIG_ERR) {
  91. perror(
  92. "Erreur lors de l'installation du gestionnaire de signal pour SIGINT");
  93. return EXIT_FAILURE;
  94. }
  95. MPI_Init(NULL, NULL);
  96. // Get the number of processes
  97. int world_size;
  98. MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  99. // Get the rank of the process
  100. int world_rank;
  101. MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  102. sprintf(checkpointfile, "output.txt.%i", world_rank);
  103. // Get the name of the processor
  104. char processor_name[MPI_MAX_PROCESSOR_NAME];
  105. int name_len;
  106. MPI_Get_processor_name(processor_name, &name_len);
  107. // Print off a hello world message
  108. printf("Hello world from processor %s, rank %d out of %d processors\n",
  109. processor_name, world_rank, world_size);
  110. printf("Le programme est en cours d'exécution. PID: %d\n", getpid());
  111. // Simulation d'une tâche en cours d'exécution
  112. while (1) {
  113. // Votre code ici...
  114. // Ajoutez une pause pour éviter une utilisation intensive du processeur
  115. sleep(1);
  116. }
  117. MPI_Finalize();
  118. return EXIT_SUCCESS;
  119. }