Linux管道问题,请大家帮忙解决,谢谢!

hchcsdn 2013-08-12 09:56:09
在linux+xenomai实时系统中,管道先写100次,实时层主要代码如下。
#define FIFO_WRITE(x,y,z) rt_pipe_write(&x,y,z,P_NORMAL)
RT_TASK task_desc;
RT_PIPE pipe_desc;
int count = 0;
void task_body(void *arg)
{
rt_task_set_periodic(NULL,TM_NOW,10000000);//10ms
int err =0;
char buf[8];
memset(buf,0,8);
unsigned long *pNum = (unsigned long*)buf;
unsigned long *pData = (unsigned long*)(buf+4);
*pNum = 4;
*pData = 0;
while(1)
{

if((*pData)++<100){
FIFO_WRITE(pipe_desc,buf,8);
}
rt_task_wait_period(NULL);
}
}
static int __init pipe_init(void)
{
int err;
err = rt_pipe_create(&pipe_desc, "liuwx_pipe", P_MINOR_AUTO,4096);
if (err)
{
printk("rt_pipe_create failed!\n");
return 0;
}
/* ... */
err = rt_task_create(&task_desc, "task_body", 4096, 80, T_FPU|T_CPU(0));
if(!err){
rt_task_start(&task_desc, task_body, NULL);
printk("create task_body success! \n");
}
else{
printk("create task_body failed! \n");
}
printk("pipe init success! \n");
return 0;
}
static void __exit pipe_exit(void)
{
rt_pipe_delete(&pipe_desc);
rt_task_delete(&task_desc);
printk("pipe module exit!\n");
}

module_init(pipe_init);
module_exit(pipe_exit);

非实时层读管道主要代码:
int main(int argc, char *argv[])
{
char devname[256], pMessage[8];
memset(pMessage,0,sizeof(pMessage));
int *pData = (int *)&pMessage[4];
while(1)
{
sprintf(devname, "/proc/xenomai/registry/native/pipes/liuwx_pipe", PIPE_MINOR);
pipe_fd = open(devname, O_RDWR);
if (pipe_fd < 0)
{
printf("open pipe faild!\n");
return 0;
}
read(pipe_fd, pMessage, 8);
count++;
printf("user reserve data:> %d:%d:%d:%d:%d readcnt:%d\n",
pMessage[0],pMessage[1],pMessage[2],pMessage[3],*pData,count);
close(pipe_fd);
}
return 0;
}
在非实时内核中读取管道,但为什么只能读到一次,其它99次管道数据都读不到。难道是写一次,必须读一次,然后再写一次吗?无法先写多次,再读吗?但书上说只有写管道变满时,管道的write()调用将被阻塞,等待某些数据被读取,以便腾出足够的空间供write()调用写,但只写了100次,应该不会满。请大家帮忙,非常谢谢!
...全文
162 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiyu1988 2013-08-12
  • 打赏
  • 举报
回复
引用 2 楼 hchcsdn 的回复:
while是让它一直读。 另外open打开管道,读完一次,close关闭管道,请问是不是会把管道中的数据清除了?
read(pipe_fd, pMessage, 8); close(pipe_fd); 每次只读8个字节,读完后就close,只有一次的数据 pipe一端关闭,第二次打开,缓冲区就重置了
hchcsdn 2013-08-12
  • 打赏
  • 举报
回复
while是让它一直读。 另外open打开管道,读完一次,close关闭管道,请问是不是会把管道中的数据清除了?
空的 2013-08-12
  • 打赏
  • 举报
回复
话说 rt_pipe_write 默认是阻塞的么 ulimit -a,pipe size管道大小,一般4k lz 读管道的那个 while有点冗余
qiyu1988 2013-08-12
  • 打赏
  • 举报
回复
引用 4 楼 hchcsdn 的回复:
请问非实时层是否可以打开管道(open)后,一直读取管道,不关闭管道(close)?
读写端都开着就行,话说你把close移出循环体测试下不就好了?
hchcsdn 2013-08-12
  • 打赏
  • 举报
回复
请问非实时层是否可以打开管道(open)后,一直读取管道,不关闭管道(close)?

23,127

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧