Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. //MONITOR
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5. #include <unistd.h>
  6.  
  7. #define LOW_PRIORITY 10 // Prioridad del proceso con baja prioridad
  8. #define HIGH_PRIORITY 5 // Prioridad del proceso con alta prioridad
  9. #define AVGSIZE 3 // Cantidad de promedios a calcular
  10. #define WRITE 1
  11. #define READ 0
  12. int tempPipe[2];
  13.  
  14. void *thread_alta_prioridad_func(void *arg)
  15. {
  16. float temp;
  17. while(read(STDIN_FILENO, &temp, sizeof(float)) > 0)
  18. {
  19. if(temp > 90.0)
  20. {
  21. fprintf(stderr, "Mayor a 90C: %f\n", temp);
  22. }
  23. else
  24. {
  25. write(tempPipe[WRITE], &temp, sizeof(float));
  26. }
  27. }
  28.  
  29. fprintf(stderr, "Fin thread alta prioridad\n");
  30.  
  31. close(tempPipe[WRITE]);
  32. return NULL;
  33. }
  34.  
  35. void *thread_baja_prioridad_func(void *arg)
  36. {
  37. float temp, last_temps[AVGSIZE] = { 0 };
  38.  
  39. while(read(tempPipe[READ], &temp, sizeof(float)) > 0)
  40. {
  41. for(int i = 0; i < AVGSIZE - 1; i++)
  42. {
  43. last_temps[i] = last_temps[i + 1];
  44. }
  45.  
  46. last_temps[AVGSIZE - 1] = temp;
  47. }
  48.  
  49. float avg;
  50. for(int i = 0; i < AVGSIZE; i++)
  51. {
  52. avg += last_temps[i];
  53. }
  54. avg = avg / AVGSIZE;
  55.  
  56. fprintf(stderr, "El promedio de los últimos %d valores: %f\n", AVGSIZE, avg);
  57. close(tempPipe[READ]);
  58.  
  59. return NULL;
  60. }
  61.  
  62. int main()
  63. {
  64. pthread_t thread_alta_prioridad, thread_baja_prioridad;
  65. struct sched_param param_ap, param_bp;
  66.  
  67. // Intentar crear pipe de temperaturas
  68. if(pipe(tempPipe))
  69. {
  70. exit(EXIT_FAILURE);
  71. }
  72.  
  73. // Crear threads
  74. if(pthread_create(&thread_alta_prioridad, NULL, thread_alta_prioridad_func, NULL))
  75. {
  76. exit(EXIT_FAILURE);
  77. }
  78.  
  79. if(pthread_create(&thread_baja_prioridad, NULL, thread_baja_prioridad_func, NULL))
  80. {
  81. exit(EXIT_FAILURE);
  82. }
  83.  
  84. // Asignar prioridades
  85. param_ap.sched_priority = HIGH_PRIORITY;
  86. pthread_setschedparam(thread_alta_prioridad, SCHED_RR, &param_ap);
  87.  
  88. param_bp.sched_priority = LOW_PRIORITY;
  89. pthread_setschedparam(thread_baja_prioridad, SCHED_RR, &param_bp);
  90.  
  91. // Hacer join y salir
  92. pthread_join(thread_alta_prioridad, NULL);
  93. pthread_join(thread_baja_prioridad, NULL);
  94.  
  95. return 0;
  96. }
  97.  
  98. //sss
  99. #define _GNU_SOURCE
  100. #include <stdio.h>
  101. #include <stdlib.h>
  102. #include <unistd.h>
  103. #include <time.h>
  104. #include <stdint.h>
  105.  
  106. int main(int argc, char **argv)
  107. {
  108. char *line = NULL;
  109. size_t len = 0;
  110.  
  111. // Abrir archivo
  112. FILE * file = fopen("datos.txt", "r");
  113. if(file == NULL)
  114. {
  115. exit(EXIT_FAILURE);
  116. }
  117.  
  118. float last_time_in_s = 0;
  119.  
  120. // Leer lineas mientras haya
  121. while(getline(&line, &len, file) != -1)
  122. {
  123. float temp;
  124. float time_in_s;
  125. long time_in_ns;
  126.  
  127. // Leer datos
  128. sscanf(line, "%f\t%f", &time_in_s, &temp);
  129.  
  130. // Convertir diferencia a nanosegundos
  131. time_in_ns = (time_in_s - last_time_in_s) * 1000000000L;
  132.  
  133. last_time_in_s = time_in_s;
  134.  
  135. // Esperar time_in_ns
  136. struct timespec sleep_params;
  137. sleep_params.tv_sec = time_in_ns / 1000000000L;
  138. sleep_params.tv_nsec = time_in_ns % 1000000000L;
  139. nanosleep(&sleep_params, NULL);
  140.  
  141. // Imprimir la temperatura
  142. write(STDOUT_FILENO, &temp, sizeof temp);
  143. }
  144.  
  145. fclose(file);
  146. return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement