/* * Implement 3 periodic threads, with periods 50ms, 100ms, and 150ms. * The job bodies are functions named task1(), task2() and task3() * (defined in cyclic_test.c) */ #include #include #include #include #include #include #include "periodic_tasks.h" #include "cyclic_test.h" void *thread1(void *param) { struct periodic_task *p_d; p_d = start_periodic_timer(2000000, 50000); while (1) { wait_next_activation(p_d); task1(); } } void *thread2(void *param) { struct periodic_task *p_d; p_d = start_periodic_timer(2000000, 100000); while (1) { wait_next_activation(p_d); task2(); } } void *thread3(void *param) { struct periodic_task *p_d; p_d = start_periodic_timer(2000000, 150000); while (1) { wait_next_activation(p_d); task3(); } } int main(int argc, char *argv[]) { int err; void *returnvalue; pthread_t second_id, third_id, fourth_id; int pmin; pthread_attr_t attrs; struct sched_param sp; pthread_attr_init(&attrs); err = pthread_attr_setinheritsched(&attrs, PTHREAD_EXPLICIT_SCHED); if (err != 0) { perror("setineheritsched"); } err = pthread_attr_setschedpolicy(&attrs, SCHED_FIFO); if (err != 0) { perror("setschedpolicy"); } pmin = sched_get_priority_min(SCHED_FIFO); /* creation and activation of the new thread */ sp.sched_priority = pmin + 3; err = pthread_attr_setschedparam(&attrs, &sp); if (err != 0) { perror("setschedparam"); } err = pthread_create(&second_id, &attrs, thread1, (void *)NULL); if (err != 0) { fprintf(stderr, "Cannot create pthread 1"); } sp.sched_priority = pmin + 2; err = pthread_attr_setschedparam(&attrs, &sp); if (err != 0) { perror("setschedparam"); } err = pthread_create(&third_id, &attrs, thread2, (void *)NULL); if (err != 0) { fprintf(stderr, "Cannot create pthread 2"); } sp.sched_priority = pmin + 1; err = pthread_attr_setschedparam(&attrs, &sp); if (err != 0) { perror("setschedparam"); } err = pthread_create(&fourth_id, &attrs, thread3, (void *)NULL); if (err != 0) { fprintf(stderr, "Cannot create pthread 3"); } /* We wait the end of the threads we just created. */ pthread_join(second_id, &returnvalue); pthread_join(third_id, &returnvalue); pthread_join(fourth_id, &returnvalue); printf("main: returnvalue is %d\n", (int)returnvalue); return 0; }