RTAI的用户空间编程(六)——管道通讯
2017-02-13 20:38
218 查看
包含头文件#include “rtai_sched.h”
1. 发送函数
函数返回
成功,未寄出的字节的数量被返回;失败,返回负值,负值定义如下:EINVAL mbx指向无效的mailbox(mbx points to not a valid mailbox).
2. 接收函数
函数返回
成功,返回收到的字节数量。
失败,返回一个负值,定义如下:EINVAL mbx指向无效的mailbox(mbx points to not a valid mailbox).
3. 示例代码
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's static int thread1; static int thread2; // realtime task structures static RT_TASK *t1; static RT_TASK *t2; // message queue static 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; }
相关文章推荐
- RTAI用户空间编程
- RTAI的用户空间编程(三)——定期运行一个任务
- RTAI的用户空间编程(五)——注意事项
- RTAI的用户空间编程(八)——中断
- RTAI的用户空间编程(四)——代码基础框架
- RTAI的用户空间编程(二)—— 实时任务定时器设置
- 用户空间和内核空间通讯之【Netlink 下】
- 用户空间和内核空间通讯之【Netlink 上】
- 用户空间与内核空间通讯接口之sysctl
- 了解一个通讯组里面成员的一个用户当前的邮箱空间达到80%配额的用户!
- 用户空间和内核空间通讯之【proc文件系统】
- 用户空间和内核空间通讯之【Netlink 下】
- 用户空间和内核空间通讯之【Netlink 下】
- ALSA用户空间编程
- alsa-lib、tinyalsa以及基于alsa的用户空间编程
- 用户空间和内核空间通讯之【Netlink 上、中、下】
- 内核模块编程对用户空间文件的访问
- 【网络编程学习笔记03】进程间通讯-命名管道
- 使用netlink机制实现内核空间和用户空间的双向消息通讯