Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //MONITOR
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <unistd.h>
- #define LOW_PRIORITY 10 // Prioridad del proceso con baja prioridad
- #define HIGH_PRIORITY 5 // Prioridad del proceso con alta prioridad
- #define AVGSIZE 3 // Cantidad de promedios a calcular
- #define WRITE 1
- #define READ 0
- int tempPipe[2];
- void *thread_alta_prioridad_func(void *arg)
- {
- float temp;
- while(read(STDIN_FILENO, &temp, sizeof(float)) > 0)
- {
- if(temp > 90.0)
- {
- fprintf(stderr, "Mayor a 90C: %f\n", temp);
- }
- else
- {
- write(tempPipe[WRITE], &temp, sizeof(float));
- }
- }
- fprintf(stderr, "Fin thread alta prioridad\n");
- close(tempPipe[WRITE]);
- return NULL;
- }
- void *thread_baja_prioridad_func(void *arg)
- {
- float temp, last_temps[AVGSIZE] = { 0 };
- while(read(tempPipe[READ], &temp, sizeof(float)) > 0)
- {
- for(int i = 0; i < AVGSIZE - 1; i++)
- {
- last_temps[i] = last_temps[i + 1];
- }
- last_temps[AVGSIZE - 1] = temp;
- }
- float avg;
- for(int i = 0; i < AVGSIZE; i++)
- {
- avg += last_temps[i];
- }
- avg = avg / AVGSIZE;
- fprintf(stderr, "El promedio de los últimos %d valores: %f\n", AVGSIZE, avg);
- close(tempPipe[READ]);
- return NULL;
- }
- int main()
- {
- pthread_t thread_alta_prioridad, thread_baja_prioridad;
- struct sched_param param_ap, param_bp;
- // Intentar crear pipe de temperaturas
- if(pipe(tempPipe))
- {
- exit(EXIT_FAILURE);
- }
- // Crear threads
- if(pthread_create(&thread_alta_prioridad, NULL, thread_alta_prioridad_func, NULL))
- {
- exit(EXIT_FAILURE);
- }
- if(pthread_create(&thread_baja_prioridad, NULL, thread_baja_prioridad_func, NULL))
- {
- exit(EXIT_FAILURE);
- }
- // Asignar prioridades
- param_ap.sched_priority = HIGH_PRIORITY;
- pthread_setschedparam(thread_alta_prioridad, SCHED_RR, ¶m_ap);
- param_bp.sched_priority = LOW_PRIORITY;
- pthread_setschedparam(thread_baja_prioridad, SCHED_RR, ¶m_bp);
- // Hacer join y salir
- pthread_join(thread_alta_prioridad, NULL);
- pthread_join(thread_baja_prioridad, NULL);
- return 0;
- }
- //sss
- #define _GNU_SOURCE
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <time.h>
- #include <stdint.h>
- int main(int argc, char **argv)
- {
- char *line = NULL;
- size_t len = 0;
- // Abrir archivo
- FILE * file = fopen("datos.txt", "r");
- if(file == NULL)
- {
- exit(EXIT_FAILURE);
- }
- float last_time_in_s = 0;
- // Leer lineas mientras haya
- while(getline(&line, &len, file) != -1)
- {
- float temp;
- float time_in_s;
- long time_in_ns;
- // Leer datos
- sscanf(line, "%f\t%f", &time_in_s, &temp);
- // Convertir diferencia a nanosegundos
- time_in_ns = (time_in_s - last_time_in_s) * 1000000000L;
- last_time_in_s = time_in_s;
- // Esperar time_in_ns
- struct timespec sleep_params;
- sleep_params.tv_sec = time_in_ns / 1000000000L;
- sleep_params.tv_nsec = time_in_ns % 1000000000L;
- nanosleep(&sleep_params, NULL);
- // Imprimir la temperatura
- write(STDOUT_FILENO, &temp, sizeof temp);
- }
- fclose(file);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement