#include #include #include #include #include #include #include "deferred_server.h" #define PRIO_ALTA 10 #define PRIO_MEDIA 8 #define PRIO_BAJA 6 /* * Show a message with the elapsed time */ void report (char * message, int id) { struct timespec now; clock_gettime(CLOCK_MONOTONIC,&now); printf("%3.3f - %s - %d\n",(double) now.tv_sec+ now.tv_nsec/1.0e9,message,id); } /* * Threads de la "aplicación" */ // argumento para los threads de la "aplicación" typedef struct { struct timespec period; int id; } periodic_data_t; // cuerpo de los threads de la "aplicación" void * thread_app (void *arg) { periodic_data_t my_data; struct timespec next_time; int i; my_data = * (periodic_data_t *)arg; clock_gettime(CLOCK_MONOTONIC, &next_time); while (1) { for(i=0; i<5; i++) { report ("Run app thread ",my_data.id); eat(0.1); } report ("End app thread ",my_data.id); // wait for next period incr_timespec(&next_time,&my_data.period); CHK( clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next_time, NULL) ); } } /* * round robin threads */ void * thread_rr_body (void *arg) { int id = (int)arg; while (1) { report ("Run rr thread ", id); eat(0.1); } } /* * Crea los threads RR * TODO: les añade al grupo */ void crea_threads_rr(marte_thread_set_t *set) { pthread_attr_t attr; struct sched_param sch_param; pthread_t th1, th2, th3; // attr: RR, prio_normal CHK( pthread_attr_init(&attr) ); CHK( pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) ); CHK( pthread_attr_setschedpolicy(&attr, SCHED_FIFO) ); sch_param.sched_priority = PRIO_MEDIA; CHK( pthread_attr_setschedparam(&attr, &sch_param) ); // create three RR threads CHK( pthread_create(&th1, &attr, thread_rr_body, (void *)1) ); CHK( pthread_create(&th2, &attr, thread_rr_body, (void *)2) ); CHK( pthread_create(&th3, &attr, thread_rr_body, (void *)3) ); // TODO: crea el grupo y añade los threads } /* * main */ int main() { pthread_t th_id; periodic_data_t per_params1, per_params2; marte_thread_set_t set; // se pone a la prio max CHK( pthread_setschedprio(pthread_self(), PRIO_ALTA+2) ); // crea pthreads de la "aplicación" per_params1.period.tv_sec = 2; per_params1.period.tv_nsec = 0; per_params1.id=1; CHK( pthread_create(&th_id, NULL, thread_app, &per_params1) ); CHK( pthread_setschedprio(th_id, PRIO_ALTA) ); per_params2.period.tv_sec = 5; per_params2.period.tv_nsec = 0; per_params2.id=2; CHK( pthread_create(&th_id, NULL, thread_app, &per_params2) ); CHK( pthread_setschedprio(th_id, PRIO_BAJA) ); // crea los threads round robin y los mete en el grupo crea_threads_rr(&set); // crea el deferred server struct timespec period = {3, 300000000}; struct timespec capacity = {0, 500000000}; deferred_server_create(PRIO_ALTA+1, PRIO_MEDIA, period, capacity, set); // espera para siempre (hasta que acabe uno de los threads) CHK( pthread_join(th_id, NULL) ); return 0; }