From 6112e32082df65a2852fe3cc11b87d7bc0816fe2 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Wed, 16 Sep 2015 11:41:26 +0200 Subject: [PATCH] added exercises for week 2 --- mart/ex03/ex03a.c | 56 +++++++++++++++++++++++++++ mart/ex06/ex06a.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++ mart/ex07/ex07a.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 254 insertions(+) create mode 100644 mart/ex03/ex03a.c create mode 100644 mart/ex06/ex06a.c create mode 100644 mart/ex07/ex07a.c diff --git a/mart/ex03/ex03a.c b/mart/ex03/ex03a.c new file mode 100644 index 0000000..195a64f --- /dev/null +++ b/mart/ex03/ex03a.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include + +#include +#include +#include + +#include + +#define ITER 10 + +static RT_TASK t1; +static RT_TASK t2; + +int global = 0; + +void taskOne(void *arg) +{ + int i; + for (i=0; i < ITER; i++) + { + rt_printf("I am taskOne and global = %d................\n", ++global); + } +} + +void taskTwo(void *arg) +{ + int i; + for (i=0; i < ITER; i++) + { + rt_printf("I am taskTwo and global = %d----------------\n", --global); + } +} + +int main(int argc, char* argv[]) +{ + /* Perform auto-init of rt_print buffers if the task doesn't do so */ + rt_print_auto_init(1); + + /* Avoids memory swapping for this program */ + mlockall(MCL_CURRENT|MCL_FUTURE); + + /* create the two tasks */ + rt_task_create(&t1, "task1", 0, 1, 0); + rt_task_create(&t2, "task2", 0, 1, 0); + + /* start the two tasks */ + rt_task_start(&t1, &taskOne, 0); + rt_task_start(&t2, &taskTwo, 0); + + return 0; +} + + diff --git a/mart/ex06/ex06a.c b/mart/ex06/ex06a.c new file mode 100644 index 0000000..cdefd1b --- /dev/null +++ b/mart/ex06/ex06a.c @@ -0,0 +1,99 @@ +/* ex06a.c */ + +#include +#include +#include +#include + +#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 + +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); +} + +//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); +} + +void init_xenomai() { + /* 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); +} + +int main(int argc, char* argv[]) +{ + printf("\nType CTRL-C to end this program\n\n" ); + + // code to set things to run xenomai + init_xenomai(); + + //startup code + startup(); + + // wait for CTRL-c is typed to end the program + pause(); +} diff --git a/mart/ex07/ex07a.c b/mart/ex07/ex07a.c new file mode 100644 index 0000000..11eac9b --- /dev/null +++ b/mart/ex07/ex07a.c @@ -0,0 +1,99 @@ +/* ex07a.c */ + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +#define NTASKS 3 + +RT_TASK demo_task[NTASKS]; +RT_SEM mysync; + +// set new baseperiod for the clock : +// - clock only increments counter when a base period is passed +// - the unit of the clock counter is called a jiffie, which in fact +// represents that a baseperiod is passed +// e.g. If 10 baseperiods are passed, the clock gives an increase +// in 10 jiffies +#define BASEPERIOD 1e6 // baseperiod in ns. + +// when base period is set, all times in the api are expressed in jiffies +#define EXECTIME 200 // execution time in jiffies +#define SPINTIME 10 // spin time in jiffies + +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); + + // let the task run RUNTIME(=200) jiffies in steps of SPINTIME(=20) jiffies + runtime = 0; + while(runtime < EXECTIME) { + rt_timer_spin(SPINTIME*BASEPERIOD); // spin cpu doing nothing + // note: rt_timer_spin function does not accept jiffies only nanoseconds + // deviates from timing conventions throughout the Xenomai API + + runtime = runtime + SPINTIME; + + rt_printf("Running Task : %d at time : %d\n",num,runtime); + } + 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); + + // change to period mode because round robin does not work + // in one shot mode + rt_timer_set_mode(BASEPERIOD);// set tick period + 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); + } + 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); + + /* 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" ); + + // code to set things to run xenomai + init_xenomai(); + + //startup code + startup(); + + // wait for CTRL-c is typed to end the program + pause(); +} -- 2.20.1