X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=mart%2Fex06%2Fex06a.c;h=4fb986c3a47c6c409eec0fe60ad43f5d1901e417;hb=cb6632a064a7688274a0a89def28dbe5bcce5693;hp=cdefd1b9fab2fbd8eed1be582608c675a1f87a7e;hpb=6112e32082df65a2852fe3cc11b87d7bc0816fe2;p=des2015.git diff --git a/mart/ex06/ex06a.c b/mart/ex06/ex06a.c index cdefd1b..4fb986c 100644 --- a/mart/ex06/ex06a.c +++ b/mart/ex06/ex06a.c @@ -1,5 +1,3 @@ -/* ex06a.c */ - #include #include #include @@ -9,91 +7,81 @@ #include #include -#include +#include #include #define NTASKS 3 -#define HIGH 52 /* high priority */ -#define MID 51 /* medium priority */ -#define LOW 50 /* low priority */ - -RT_TASK demo_task[NTASKS]; -RT_SEM mysync; - #define BASEPERIOD 0 // baseperiod 0 to get ns #define EXECTIME 2e8 // execution time in ns #define SPINTIME 1e7 // spin time in ns +RT_TASK demo_task[NTASKS]; +RT_SEM mysync; + void demo(void *arg) { - RTIME starttime, runtime; - int num=*(int *)arg; - RT_TASK *curtask; - RT_TASK_INFO curtaskinfo; - - rt_printf("Task : %d\n",num); - - rt_sem_p(&mysync,TM_INFINITE); - - runtime = 0; - while(runtime < EXECTIME) { - rt_timer_spin(SPINTIME); // spin cpu doing nothing - - runtime = runtime + SPINTIME; - - rt_printf("Running Task : %d at ms : %d\n",num,runtime/1000000); - } - rt_printf("End Task : %d\n",num); + RTIME starttime; + RTIME runtime; + int num = *(int *)arg; + RT_TASK *curtask; + RT_TASK_INFO curtaskinfo; + + rt_printf("Task: %d\n", num); + + rt_sem_p(&mysync, TM_INFINITE); + + runtime = 0; + while(runtime < EXECTIME) { + rt_timer_spin(SPINTIME); // spin cpu doing nothing + runtime = runtime + SPINTIME; + rt_printf("Running Task: %d at ms: %d\n", num, runtime/1000000); + } + rt_printf("End Task: %d\n", num); } //startup code void startup() { - int i; - char str[10] ; - - // semaphore to sync task startup on - rt_sem_create(&mysync,"MySemaphore",0,S_FIFO); - - // set timing to ns - rt_timer_set_mode(BASEPERIOD); - - for(i=0; i < NTASKS; i++) { - rt_printf("start task : %d\n",i); - sprintf(str,"task%d",i); - rt_task_create(&demo_task[i], str, 0, 50, 0); - rt_task_start(&demo_task[i], &demo, &i); - } - // assign priorities to tasks - // (or in creation use 50+i) - rt_task_set_priority(&demo_task[0],LOW); - rt_task_set_priority(&demo_task[1],MID); - rt_task_set_priority(&demo_task[2],HIGH); - - rt_printf("wake up all tasks\n"); - rt_sem_broadcast(&mysync); + int i; + char str[10] ; + + // semaphore to sync task startup on + rt_sem_create(&mysync, "MySemaphore", 0, S_FIFO); + + // set timing to ns + rt_timer_set_mode(BASEPERIOD); + + for(i = 0; i < NTASKS; i++) { + rt_printf("start task: %d\n",i); + sprintf(str,"task%d",i); + rt_task_create(&demo_task[i], str, 0, 50+i, 0); + rt_task_start(&demo_task[i], &demo, &i); + } + + rt_printf("wake up all tasks\n"); + rt_sem_broadcast(&mysync); } void init_xenomai() { - /* Avoids memory swapping for this program */ - mlockall(MCL_CURRENT|MCL_FUTURE); + /* Avoids memory swapping for this program */ + mlockall(MCL_CURRENT|MCL_FUTURE); - /* Perform auto-init of rt_print buffers if the task doesn't do so */ - rt_print_auto_init(1); + /* Perform auto-init of rt_print buffers if the task doesn't do so */ + rt_print_auto_init(1); } int main(int argc, char* argv[]) { - printf("\nType CTRL-C to end this program\n\n" ); + printf("\nType CTRL-C to end this program\n\n" ); - // code to set things to run xenomai - init_xenomai(); + // code to set things to run xenomai + init_xenomai(); - //startup code - startup(); + //startup code + startup(); - // wait for CTRL-c is typed to end the program - pause(); + // wait for CTRL-c is typed to end the program + pause(); }