`
tower
  • 浏览: 238939 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Linux下查看FIFO剩余字节的方法

阅读更多

FIFO做为一种有名管道的形式,在unix下编程经常会用到,在AIX和Solaris上用ls -l能像查看普通文件一样,很方便的看到当前FIFO里面还剩余多少字节未被读取,但是linux下却不行。通过调用ioctl可以实现这个功能:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

int main()
{
	int fd = open("test.q", O_RDWR);
	if (fd<0)
	{
		perror("open fifo:");
		return -1;
	}
	
	int n;
	int ret;
	while(1){	
		ret = ioctl(fd, FIONREAD, &n);	
		printf("%d %d\n", ret, n);
		sleep(5);
	}
}

 

上面是一个实例代码,主要功能是

ret = ioctl(fd, FIONREAD, &n);	

 

通过使用上述程序,你可以使用下面这个命令往test.q这个FIFO里面写数据,上面的程序会打印出test.q里面剩下的字节数

cat > test.q

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics