RTAI的用户空间编程(六)——管道通讯
来源:互联网 发布:张海迪开车 知乎 编辑:程序博客网 时间:2024/06/04 20:32
包含头文件#include “rtai_sched.h”
1. 发送函数
int rt_mbx_send (MBX* mbx, void* msg, int msg_size);//无条件发送消息int rt_mbx_send_wp (MBX* mbx, void* msg, int msg_size); //在不阻塞当期调用的任务的情况下,尽可能多的发送数据。消息长度是msg_size。int rt_mbx_send_if (MBX* mbx, void* msg, int msg_size); //发送一个消息, 要求在不阻塞当前调用任务的情况下,整个消息可以完整传递。int rt_mbx_send_until (MBX* mbx, void* msg, int msg_size, RTIME time);int rt_mbx_send_timed (MBX* mbx, void* msg, int msg_size, RTIME delay);//rt_mbx_send_until和rt_mbx_send_timed发送消息msg_size大小的msg到邮箱mbx。调用者将被阻塞,直到所有字节的入消息队列、超时过期或发生错误。
函数返回
成功,未寄出的字节的数量被返回;失败,返回负值,负值定义如下:EINVAL mbx指向无效的mailbox(mbx points to not a valid mailbox).
2. 接收函数
int rt_mbx_receive (MBX* mbx, void* msg, int msg_size);//无条件接受消息int rt_mbx_receive_wp (MBX* mbx, void* msg, int msg_size);//在不阻塞当期调用的任务的情况下,尽可能多的接收数据。int rt_mbx_receive_if (MBX* mbx, void* msg, int msg_size);//整个msg_size大小的消息能立刻获取到时,接收消息。int rt_mbx_receive_until (MBX* mbx, void* msg, int msg_size, RTIME time);int rt_mbx_receive_timed (MBX* mbx, void* msg, int msg_size, RTIME delay);//调用者将被阻塞,直到所有字节的入消息队列、超时过期或发生错误。time是绝对值,Delay是相对于当前时间的相对值.
函数返回
成功,返回收到的字节数量。
失败,返回一个负值,定义如下:EINVAL mbx指向无效的mailbox(mbx points to not a valid mailbox).
3. 示例代码
/* messqueue.c*//* ------------ headers ------------------------------------------- */#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <sys/types.h>#include <sys/mman.h>#include <sys/stat.h>#include <fcntl.h>#include <sched.h>#include <linux/errno.h> /* error codes */#include <signal.h>#include <pthread.h>#include <rtai_lxrt.h>#include <rtai_mbx.h>#include <rtai_msg.h>/* ------------ globals ------------------------------------------- *//* turn on(1) or off(0) debugging */const int DEBUG=1;// fix include warning//#include <asm/system.h>//#include <rtai_posix.h>int pthread_cancel_rt(pthread_t thread);// linux threads id'sstatic int thread1;static int thread2;// realtime task structuresstatic RT_TASK *t1;static RT_TASK *t2;// message queuestatic MBX *mesgQueueId;#define MAX_MESSAGES 100#define MAX_MESSAGE_LENGTH 50/* ------------ functions ------------------------------------------- */void taskOne(void *arg){ int retval; char message[] = "Received message from taskOne"; /* make this thread LXRT soft realtime */ t1 = rt_task_init_schmod(nam2num("TASK1"), 0, 0, 0, SCHED_FIFO, 0xF); mlockall(MCL_CURRENT | MCL_FUTURE); // makes task hard real time (only when not developing/debugging) if ( !DEBUG ) rt_make_hard_real_time(); rt_printk("Started taskOne\n"); /* send message */ retval = rt_mbx_send(mesgQueueId, message, sizeof(message)); if (0 != retval) { if (-EINVAL == retval) { rt_printk("mailbox is invalid\n"); } else { /* unknown error */ rt_printk("Unknown mailbox error\n"); } }}void taskTwo(void *arg){ int retval; char msgBuf[MAX_MESSAGE_LENGTH]; /* make this thread LXRT soft realtime */ t2 = rt_task_init_schmod(nam2num("TASK2"), 0, 0, 0, SCHED_FIFO, 0xF); mlockall(MCL_CURRENT | MCL_FUTURE); // makes task hard real time (only when not developing/debugging) if ( !DEBUG ) rt_make_hard_real_time(); rt_printk("Started taskTwo\n"); /* receive message */ retval = rt_mbx_receive_wp(mesgQueueId, msgBuf, 50); if (-EINVAL == retval) { rt_printk("mailbox is invalid\n"); } else { rt_printk("message length=50-%d\n",retval); rt_printk("message : %s\n",msgBuf); }}void cleanup(void){ /* delete message queue */ rt_mbx_delete(mesgQueueId); // task end themselves -> not necesssary to delete them return;}/* ------------ main ------------------------------------------- */int main(void){ printf("Start of main\n"); /* make this thread LXRT soft realtime */ rt_task_init_schmod(nam2num("TASK0"), 0, 0, 0, SCHED_FIFO, 0xF); mlockall(MCL_CURRENT | MCL_FUTURE); // set realtime timer to run in oneshot mode rt_set_oneshot_mode(); // start realtime timer and scheduler start_rt_timer(1); /* create message queue */ mesgQueueId = rt_typed_mbx_init (nam2num("MSGQUEUE"), MAX_MESSAGES, FIFO_Q); if (mesgQueueId == 0) { printf("Error creating message queue\n"); return 1; } // create the linux threads thread1 = rt_thread_create(taskOne, NULL, 10000); thread2 = rt_thread_create(taskTwo, NULL, 10000); // wait for end of program printf("TYPE <ENTER> TO TERMINATE\n"); getchar(); // cleanup cleanup(); printf("Finished\n"); return 0;}
0 0
- RTAI的用户空间编程(六)——管道通讯
- RTAI的用户空间编程(五)——注意事项
- RTAI的用户空间编程(八)——中断
- RTAI用户空间编程(七)——共享内存通讯
- RTAI的用户空间编程(一)——任务创建
- RTAI的用户空间编程(二)——实时任务定时器设置
- RTAI的用户空间编程(三)——定期运行一个任务
- RTAI的用户空间编程(四)——代码基础框架
- RTAI用户空间编程
- 编写LXRT(RTAI-LXRT)用户空间程序详解
- 进程间通讯——管道(有名管道)
- 无名管道通讯编程
- 有名管道通讯编程
- 进程间通讯—管道
- linux RTAI模块编程遇到的问题
- 管道的故事(六)
- 无名管道与有名管道通讯编程
- linux——进程间通讯(管道)
- VR系列——Oculus Publishing文档:五、应用签名
- 【Ado.Net】SQLHelper+配置文件+参数查询
- 离散化介绍
- 查看主板型号
- hdoj1049
- RTAI的用户空间编程(六)——管道通讯
- Windows核心编程笔记(二十一) 硬件输入模型和局部输入状态
- 【ACM】Solution of UVA 227-Puzzle
- 【Unity】窗口失去焦点后继续游戏处理
- C#98课的主要内容
- C++标准、开发工具即相关书籍
- 多重背包—bLue祝你元宵节快乐!
- ifcongig命令
- 从零开始,搭建博客系统MVC5+EF6搭建框架(2),测试添加数据、集成Autofac依赖注入