请问下fwrite能不能设置为非阻塞的

kakarot23 2012-10-31 03:10:33
最近要实现一个视频录像功能,

录像的数据有可能会远大于硬盘的写入速度,

而该模块要求在写硬盘时反应要快,宁愿扔掉数据也不要长时间等待.

所以想问下fwrite能不能设置为非阻塞模式,或者有什么方法可以获取FILE*指针相应的缓冲区的非使用的长度.

谢谢!
...全文
416 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
kakarot23 2012-10-31
  • 打赏
  • 举报
回复
fwrite本身就带有缓冲区了,而且也可以通过setvbuf更改其大小

[Quote=引用 7 楼 的回复:]

缓冲到一定量,再写文件,减少IO次数。
引用 6 楼 的回复:
使用缓冲区的话,要么就得设置非阻塞,要么就必须多开一个线程专门写文件,
不然缓冲区就没意义了

多开线程的话就会提高复杂度,这是最后的选择.
[/Quote]
mujiok2003 2012-10-31
  • 打赏
  • 举报
回复
缓冲到一定量,再写文件,减少IO次数。
[Quote=引用 6 楼 的回复:]
使用缓冲区的话,要么就得设置非阻塞,要么就必须多开一个线程专门写文件,
不然缓冲区就没意义了

多开线程的话就会提高复杂度,这是最后的选择.
[/Quote]
kakarot23 2012-10-31
  • 打赏
  • 举报
回复
使用缓冲区的话,要么就得设置非阻塞,要么就必须多开一个线程专门写文件,
不然缓冲区就没意义了

多开线程的话就会提高复杂度,这是最后的选择.
mujiok2003 2012-10-31
  • 打赏
  • 举报
回复

1, 考虑压缩采集的结果,以减少IO的量。
2, 如果要提高IO的速度,考虑用内存映射以减少内存拷贝,替换读写速度更快的硬盘(比如SDD),开启DMA。
3, 循环缓冲区,适当丢弃部分数据。
mujiok2003 2012-10-31
  • 打赏
  • 举报
回复
你可以考虑用一个循环缓冲区。当缓冲区满了,直接丢弃缓冲中内容。
mujiok2003 2012-10-31
  • 打赏
  • 举报
回复
标准库fwrite不提供非阻塞操作,请向你的操作系统所要这特性.
hu7324829 2012-10-31
  • 打赏
  • 举报
回复
我记得select好像是可以对文件描述符 做复用的。
kakarot23 2012-10-31
  • 打赏
  • 举报
回复
忘了说了,是在WINDOWS下
获取USB摄像头的1080p的JPEG格式的图片20180608_1806.7z 电脑上的系统:ubuntu14.04 // http://www.linuxidc.com/Linux/2011-03/33020.htm // V4L2摄像头获取单幅图片测试程序(MMAP模式) // [日期:2011-03-06] 来源:Linux社区 作者:aokikyon [字体:大 中 小] // // #加了点注释 // // #Rockie Cheng // printf #include #include // memset #include #include #include #include // close write usleep read #include #include #include #include #include #include // mmap #include #include #include #include // pthread_create pthread_join #include #define CLEAR(x) memset (&(x), 0, sizeof (x)) #define REQ_COUNT 6 #define uchar unsigned char struct buffer { void * start; size_t length; }; static char * dev_name = "/dev/video0";//摄像头设备名 static int fd = -1; struct buffer * buffers = NULL; // static unsigned int n_buffers = 0; // 2012-7-13 11:33 camera flag // static int iFlagCamera = 0; volatile int iFlagCamera = 0; // 函数名称:thread1 // 函数功能:用于接受键盘的输入,之后通知thread2抓图 // 参数列表: // 返回值 : void thread1(void) { char ch; printf("\n !!!!Warning!!!!\n Max 1000 color bmp\n Input the char: \n"); printf("\n !!!!press o an capture 1 frame picture! \n"); printf("\n !!!!press t an capture 10 frame picture! \n"); printf("\n !!!!press h an capture 100 frame picture! \n"); while(1) { while((ch=getchar()) != '\n') { printf("%c\n", ch); if('o'==ch) { iFlagCamera = 1; printf("thread1=%d\n", iFlagCamera); } else if('t'==ch) { iFlagCamera = 10; printf("thread1=%d\n", iFlagCamera); } else if('h'==ch) { iFlagCamera = 101; printf("thread1=%d\n", iFlagCamera); } else if('q'==ch) { iFlagCamera = 10001; printf("thread1=%d\n", iFlagCamera

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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