#include #include #include #include #include #include #include #include #include #include "rt_time_ops.h" #include "rt_events.h" static int error_status=-1; struct periodic_data { struct timespec period; struct timespec wcet; struct timespec deadline; int id; }; // Body of periodic thread void * periodic (void *arg) { struct periodic_data my_data; char s[20]; int errorcode; int deadline_was_missed; int budget_was_overrun; rt_event_manager evntmanager; // initialization my_data = * (struct periodic_data*)arg; if ((errorcode=rt_init_periodic_event_manager (&evntmanager,my_data.period,my_data.wcet, my_data.deadline,my_data.id))!=OK) { error_status=errorcode; pthread_exit((void *) &error_status); } // periodic loop while (1) { if ((errorcode=rt_schedule_periodic_job (&evntmanager,&deadline_was_missed,&budget_was_overrun))!=OK) { error_status=errorcode; pthread_exit((void *) &error_status); } // simulate the execution of useful work rt_now_to_string(s,20); printf("%s: Thread with Id=%d starts\n",s,my_data.id); eat(((float)my_data.wcet.tv_sec+ ((float)my_data.wcet.tv_nsec)/1.0e9)*0.99); // multiplico por 0.99 para evitar que salten los timers de WCET rt_now_to_string(s,20); printf("%s: Thread with Id=%d finishes\n",s,my_data.id); } } // Main program, that creates two periodic threads int main () { pthread_t t1,t2; sigset_t set; struct periodic_data per_params1,per_params2; struct sched_param sch_param; pthread_attr_t attr; int errorcode; // se pone a una prio mayor que la de las tareas CHK( pthread_setschedprio(pthread_self(), 10) ); adjust(); sigemptyset(&set); sigaddset(&set,SIGRTMIN); if (pthread_sigmask(SIG_BLOCK, &set, NULL) !=0) { printf ("Error while setting the signal mask\n"); exit (1); } // inicializa el event manager if ((errorcode=rt_init_event_managers())!=OK) { printf ("Error code %d in init event managers\n",errorcode); exit (1); } // Create the thread attributes object CHK( pthread_attr_init (&attr) ); // Set each of the scheduling attributes CHK( pthread_attr_setinheritsched (&attr,PTHREAD_EXPLICIT_SCHED) ); CHK( pthread_attr_setdetachstate (&attr,PTHREAD_CREATE_DETACHED) ); CHK( pthread_attr_setschedpolicy (&attr, SCHED_FIFO) ); // create thread 1 per_params1.period.tv_sec=2; per_params1.period.tv_nsec=0; per_params1.wcet.tv_sec=1; per_params1.wcet.tv_nsec=0; per_params1.deadline=per_params1.period; per_params1.id=1; sch_param.sched_priority = (sched_get_priority_min(SCHED_FIFO)+5); CHK( pthread_attr_setschedparam (&attr, &sch_param) ); CHK( pthread_create (&t1,&attr,periodic,&per_params1) ); // create thread 2 per_params2.period.tv_sec=7; per_params2.period.tv_nsec=0; per_params2.wcet.tv_sec=3; per_params2.wcet.tv_nsec=0; per_params2.deadline=per_params2.period; per_params2.id=2; sch_param.sched_priority = (sched_get_priority_min(SCHED_FIFO)+5); CHK( pthread_attr_setschedparam (&attr, &sch_param) ); CHK( pthread_create (&t2,&attr,periodic,&per_params2) ); sleep(30); exit(0); }