- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 aio_write(1) 函数。下面的代码是我的程序。但是,aio_write() 总是返回 EINVAL。
看了EINVAL的错误原因:aio_offset、aio_reqprio、aio_nbytes中的一个或多个无效。但是,我无法弄清楚我设置的值不正确的原因!
有人能帮忙吗?
[更新] 此代码是可编译和可运行的!但是结果不正确!我认为这是因为 buffer_out 在写入文件之前被覆盖了。有没有更好更简洁的方法来做到这一点? aio_error(1) 是可以查询到的,但是像忙等待一样影响性能。
有没有更好的方法来避免缓冲区被覆盖并保持速度?
#include <iostream>
#include <cstring>
#include <aio.h>
#include <stdlib.h>
#include <stdio.h>
#include <sstream>
#include <errno.h>
using namespace std;
#define true 1
#define false 0
int main()
{
int i = 0;
int offset = 0, fd_offset = 0;
int slen = 0;
char* buffer, *buffer_out;
int buffer_size = 30;
int is_first_write = true;
struct aiocb aio_param;
int fd_out = open("result.log", O_APPEND | O_RDWR);
buffer = (char*) malloc(buffer_size);
buffer_out = (char*) malloc(buffer_size);
memset(buffer, 0, buffer_size);
memset(buffer_out, 0, buffer_size);
for( i = 0; i < 10; i++)
{
stringstream ss;
ss << "LOG *****" << i << "-" << "********\n";
slen = strlen(ss.str().c_str());
//cout<<"written to offset: " << offset << endl;
//buffer is not large enough to take the current string
if(offset + slen > buffer_size)
{
//write the current buffer content (< 4K) into the file
memset(&aio_param, 0, sizeof(aio_param));
aio_param.aio_fildes = fd_out;
aio_param.aio_offset = fd_offset;
while( aio_error(&aio_param) != 0 && is_first_write == false ) {cout << "wait for the previous one finish" <<endl;}
aio_param.aio_buf = memcpy(buffer_out, buffer, buffer_size); //the buffer_out has to be written before it's copied to again!
aio_param.aio_nbytes = offset;
//aio_offset = ? should be the end of file for O_APPEND mode
aio_param.aio_sigevent.sigev_notify = SIGEV_NONE;
if( aio_write(&aio_param) == -1 )
{
cout<<"ERR: aio_write()==-1"<<endl;
cout<<"errno:" << errno <<endl;
return 1;
}else{
is_first_write = false;
fd_offset += offset;
cout<<"aio_write()" << offset << " bytes succeed at file offset " << fd_offset<<endl;
offset = 0;
// sleep(1);
//clean the buffer
memset(buffer, 0, buffer_size);
}
}
//write the current string to the buffer
memcpy(buffer + offset, ss.str().c_str(), slen);
offset += slen;
}
close(fd_out);
return 0;
}
错误的结果是:
LOG *****2-********
LOG *****4-********
LOG *****4-********
LOG *****4-********
LOG *****5-********
LOG *****5-********
LOG *****6-********
LOG *****8-********
(应该是0到8,不能重复。)
最佳答案
由于您没有分配 aio_param
的所有值(并且它在堆栈上未初始化),我建议在使用前将其清零:
memset(&aio_param, 0, sizeof(aio_param));
此外,我认为您可能想要设置 aio_offset
?
关于c++ - aio_write() 总是失败并出现错误 EINVAL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21844284/
更改或释放传递给 aio_write 以便写入文件的缓冲区是否安全。或者我应该等待操作完成?该函数是将所有缓冲区内容复制到队列还是仅复制对数据的引用? 最佳答案 来自man 7 aio: he con
有没有办法检查是否没有 AIO 写入给定文件?我在我的 Unix 类(class)上制作了一个项目,该项目将是一个上下文无关(基于 UDP)的国际象棋服务器,并且所有数据都必须存储在文件中。应用程序将
我试图从本质上模仿 sendfile(2) 的功能以异步方式使用 aio_read(3)和 aio_write(3) . 一切似乎都工作正常,除了测试大型(> 150k)文件。 我有一个简单的stru
我正在寻找 POSIX aio_write 函数的简单示例。 到目前为止我尝试过的内容 下面的内容并不是太重要。只需跳转即可回答 下面的代码创建一个文件,但不向其中写入任何内容。 aio_error
我正在将 write 重写为 aio_write,这样我就可以处理超时(而且我不想为 select 而烦恼) .我从手册页中不明白的一件事是 aio_write 怎么会失败。 对于write,我最感兴
我知道 POSIX write 函数可以成功返回,即使它没有写入整个缓冲区(如果被信号中断)。您必须检查短写并恢复它们。 但是 aio_write 有同样的问题吗?我不认为它会发生,但文档中没有提到它
我正在尝试向文件中写入一个简单的内容,但 AIO 似乎不起作用。可能是什么问题?我知道有一些不必要的额外 header 。 #include #include #include #include #i
我正在尝试使用 aio_write(1) 函数。下面的代码是我的程序。但是,aio_write() 总是返回 EINVAL。 看了EINVAL的错误原因:aio_offset、aio_reqprio、
Linux man-page for aio_write说 The buffer area being written out must not be accessed during the oper
我开始使用aio写一个简单的客户端服务器程序。我的客户端程序: int main(int argc, char const *argv[]) { int port = 6000; s
我在 Linux 2.6.16.46 上玩这段代码: io.aio_fildes = open(name, O_CREAT | O_TRUNC | O_WRONLY | O_SYNC, 00300);
我有一个 C 程序,它写入 32768 个 block ,每个 block 大小为 16K(总大小为 512MB),到运行 3.18.1 内核的系统上的 ext4 文件系统。该程序的常规写入系统调用版
我实现了需要读写的游戏服务器。所以我接受传入连接并使用 aio_read() 开始读取它,但是当我需要发送一些东西时,我停止使用 aio_cancel() 读取然后使用 aio_write ()。在
我正在尝试用“a”字符填充我的文件,我需要使用 aio_write。这是我的写作功能 int da_aio_write(const int d, struct aiocb *aiorp, void *
aio_write() 和 O_NONBLOCK write() 有什么区别?此外,我使用 O_NONBLOCK 函数将 write() 用于使用文件描述符的文本文件,并通过在函数前后放置一个计时器来
我有一个使用 linux aio_write 和 aio_read 的测试实用程序。此测试实用程序包装我的静态库并对其进行测试。这个库是多线程黑盒。 到目前为止,它运行良好。但是现在我们对这个黑盒进行
我在带有 rtkaio 库的 linux 上使用异步 io。在我的测试中,一切都完美无缺,但是,在我的实际应用程序中,我发现应该返回得非常快的 aio_write 却非常慢。将 128KB 写入 O_
我是一名优秀的程序员,十分优秀!