vxworks任务通信之管道

/*管道
 * :任务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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值