/*管道
* :任务A写入管道,任务B读取
* */
#include<stdio.h>
#include<ioLib.h>
#include<taskLib.h>
#include<wdLib.h>
#include<unistd.h>
#include<pipeDrv.h>
#define TASK_PRIORITY_A 130
#define TASK_PRIORITY_B 130
#define STACK_SIZE 225
int taskId_A, taskId_B;
/*发送的消息*/
#define MSG "Hello"
#define PIPE_NAME "/tmp/pipe"
/*定义消息队列*/
int pipeId;
/*定义两个Task*/
void taskA(void);
void taskB(void);
int taskDemo() {
/*创建任务:taskSpawn创建并激活任务*/
taskId_A=taskSpawn("taskA", TASK_PRIORITY_A, VX_FP_TASK, STACK_SIZE,
(FUNCPTR)taskA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
taskId_B=taskSpawn("taskB", TASK_PRIORITY_B, VX_FP_TASK, STACK_SIZE,
(FUNCPTR)taskB, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
if (taskId_A==ERROR)
printf("taskA taskSpawn() failed!\n");
if (taskId_B==ERROR)
printf("taskB taskSpawn() failed!\n");
}
void taskA() {
char buf[]="Hello pipe";
/*创建管道*/
if(pipeDevCreate(PIPE_NAME,5,256)==ERROR)
printf("pipeDevCreate() error!\n");
/*打开管道,返回管道描述符*/
if((pipeId=open(PIPE_NAME,O_RDWR,0666))==ERROR)
printf("open() pipe error!\n");
/*将消息写入管道*/
write(pipeId,buf,sizeof(buf));
printf("TaskA write message to Pipe\n");
}
void taskB() {
char buf[255];
/*管道中读取消息*/
read(pipeId,buf,255);
printf("TaskB read message from Pipe:%s\n",buf);
/*关闭管道*/
close(pipeId);
pipeDevDelete(PIPE_NAME,1);
}
运行结果
TaskA write message to Pipe
TaskB read message from Pipe:Hello pipe