Real-time Xenomai 3 example 1
Xenomai 3.** have the following contexts
1 Creating a task & Starting a task
The exercises are based onXenomai 3using theAlchemy API.The example program below creates and starts a task, see also the task management services of the Alchemy API. The codes are
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <alchemy/task.h>
RT_TASK hello_task;
// function to be executed by task
void helloWorld(void *arg)
{
RT_TASK_INFO curtaskinfo;
printf("Hello World!n");
// inquire current task
rt_task_inquire(NULL,&curtaskinfo);
// print task name
printf("Task name : %s n", curtaskinfo.name);
}
int main(int argc, char* argv[])
{
char str[10];
printf("start taskn");
sprintf(str,"hello");
/* Create task
* Arguments: &task,
* name,
* stack size (0=default),
* priority,
* mode (FPU, start suspended, ...)
*/
rt_task_create(&hello_task, str, 0, 50, 0);
/* Start task
* Arguments: &task,
* task function,
* function argument
*/
rt_task_start(&hello_task, &helloWorld, 0);
}
To compile the program ex01.c we use the following Makefile
XENO_CONFIG := /usr/xenomai/bin/xeno-config
CFLAGS := $(shell $(XENO_CONFIG) --posix --alchemy --cflags)
LDFLAGS := $(shell $(XENO_CONFIG) --posix --alchemy --ldflags)
CC := gcc
EXECUTABLE := ex01a
all: $(EXECUTABLE)
%: %.c
$(CC) -o $@ $< $(CFLAGS) $(LDFLAGS)
clean:
rm -f $(EXECUTABLE)
Note that by adding the “posix” flag in the Makefile,printf
is mapped tort_printf
which puts text to be printed in a buffer. The actual printing is done by a non-real-time periodic thread.
2 Changing a global variable by two tasks
In the example below, two tasks (taskOne and taskTwo), are competing to update the value of a global variable, called "global." The objective of the program is to toggle the value of the global variable (1s and 0s). TaskOne increments the value of "global" and taskTwo decrements the value.
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <alchemy/task.h>
#include <alchemy/timer.h>
#include <alchemy/sem.h>
#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++) {
printf("I am taskOne and global = %d................n", ++global);
}
}
void taskTwo(void *arg)
{
int i;
for (i=0; i < ITER; i++) {
printf("I am taskTwo and global = %d----------------n", --global);
}
}
int main(int argc, char* argv[]) {
rt_task_create(&t1, "task1", 0, 1, 0);
rt_task_create(&t2, "task2", 0, 1, 0);
rt_task_start(&t1, &taskOne, 0);
rt_task_start(&t2, &taskTwo, 0);
return 0;
}
3 Priority-Based Scheduling
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <alchemy/task.h>
#include <alchemy/sem.h>
#include <alchemy/timer.h>
#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 EXECTIME 2e8 // execution time in ns
#define SPINTIME 1e7 // spin time in ns
void demo(void *arg)
{
RTIME starttime, runtime;
RT_TASK_INFO taskinfo;
int num=*(int *)arg;
printf("Task : %dn",num);
rt_sem_p(&mysync,TM_INFINITE);
runtime = 0;
while(runtime < EXECTIME) {
rt_timer_spin(SPINTIME); // spin cpu doing nothing
runtime = runtime + SPINTIME;
printf("Running Task : %d at ms : %dn",num, runtime/1000000);
}
printf("End Task : %dn",num);
}
//startup code
void startup()
{
int i;
char str[20] ;
// semaphore to sync task startup on
rt_sem_create(&mysync,"MySemaphore",0,S_FIFO);
for(i=0; i < NTASKS; i++) {
printf("start task : %dn",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
rt_task_set_priority(&demo_task[0],LOW);
rt_task_set_priority(&demo_task[1],MID);
rt_task_set_priority(&demo_task[2],HIGH);
printf("wake up all tasksn");
rt_sem_broadcast(&mysync);
}
int main(int argc, char* argv[])
{
startup();
printf("nType CTRL-C to end this programnn" );
pause();
}
4 Round-robin Scheduling
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <alchemy/task.h>
#include <alchemy/sem.h>
#include <alchemy/timer.h>
#define NTASKS 3
RT_TASK demo_task[NTASKS];
RT_SEM mysync;
#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;
printf("Task : %dn",num);
rt_sem_p(&mysync,TM_INFINITE);
// let the task run RUNTIME ns in steps of SPINTIME ns
runtime = 0;
while(runtime < EXECTIME) {
rt_timer_spin(SPINTIME); // spin cpu doing nothing
runtime = runtime + SPINTIME;
printf("Running Task : %d at time : %dn",num,runtime);
}
printf("End Task : %dn",num);
}
//startup code
void startup() {
int i;
char str[20] ;
// semaphore to sync task startup on
rt_sem_create(&mysync,"MySemaphore",0,S_FIFO);
for(i=0; i < NTASKS; i++) {
printf("start task : %dn",i);
sprintf(str,"task%d",i);
rt_task_create(&demo_task[i], str, 0, 50, 0);
rt_task_start(&demo_task[i], &demo, &i);
}
printf("wake up all tasksn");
rt_sem_broadcast(&mysync);
}
int main(int argc, char* argv[])
{
startup();
printf("nType CTRL-C to end this programnn" );
pause();
}
- 用R语言照葫芦画瓢撸了一个简易代理~
- 左手用R右手Python——CSS网页解析实战
- R语言数据抓取实战——RCurl+XML组合与XPath解析
- Python网络数据抓取实战——Xpath解析豆瓣书评
- 左手用R右手Python系列17——CSS表达式与网页解析
- 左手用R右手Python系列16——XPath与网页解析库
- 扒一扒rvest的前世今生!
- RCurl中这么多get函数,是不是一直傻傻分不清!!!
- 机器学习(二)深度学习实战-使用Kera预测人物年龄问题描述引入所需要模块加载数据集创建模型编译模型优化optimize1 使用卷积神经网络optimize2 增加神经网络的层数输出结果结果
- 异步加载的基本逻辑与浏览器抓包一般流程
- 左手用R右手Python系列之——表格数据抓取之道
- XML/HTML/JSON——数据抓取过程中不得不知的几个概念
- R语言网络数据抓取的又一个难题,终于攻破了!
- R语言数据清洗实战——高效list解析方案
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法