#include #include #include #include #include #include #include "msg.h" #include "periodic_tasks.h" static struct port *p; static int th_cnt = 1; static void *thread1(void *v) { struct msg m; struct periodic_task *p_d; int cnt = th_cnt++; cnt *= 1000; p_d = start_periodic_timer(2000000, 50 * cnt); while (1) { wait_next_activation(p_d); m.value = cnt++; printf("Sending %d\n", m.value); msg_send(p, &m); } return NULL; } static void *thread2(void *v) { struct msg m; while(1) { msg_receive(p, &m); printf("Received %d\n", m.value); } return NULL; } int main(int argc, char *argv[]) { int err; pthread_t id_producer1, id_producer2, id_consumer; pthread_attr_t attrs; struct sched_param sp; int pmin; p = port_init(); pthread_attr_init(&attrs); pthread_attr_setinheritsched(&attrs, PTHREAD_EXPLICIT_SCHED); pthread_attr_setschedpolicy(&attrs, SCHED_FIFO); pmin = sched_get_priority_min(SCHED_FIFO); sp.sched_priority = pmin + 3; err = pthread_attr_setschedparam(&attrs, &sp); err = pthread_create(&id_consumer, &attrs, thread2, NULL); if (err) { perror("PThread Create"); } sp.sched_priority = pmin + 2; err = pthread_attr_setschedparam(&attrs, &sp); err = pthread_create(&id_producer1, &attrs, thread1, NULL); if (err) { perror("PThread Create"); } sp.sched_priority = pmin + 1; err = pthread_attr_setschedparam(&attrs, &sp); err = pthread_create(&id_producer2, &attrs, thread1, NULL); if (err) { perror("PThread Create"); } pthread_join(id_consumer, NULL); pthread_join(id_producer1, NULL); pthread_join(id_producer2, NULL); return 0; }