c++ - aio_write() always fails with error EINVAL -


I am trying to use the aio_write (1) function I code below is my program. However, aio_write () Always returns to EINVAL

I have read the error cause of anvil: One or more aio_offset, aio_reqprio, aio_nbytes are invalid. However, I think that the value set by me is not wrong. / P>

Can anyone help?

[UPDATE] This code is consistent and scalable! But the result is wrong! I think this is because buffer_out is overwritten before it is written in the file. Is there a better and more organized way to do this? AiIer (1) may be questioned, but it is like a busy waiting and hurts the performance.

Is there a better way to avoid buffer overwriting and maintain speed?

  #include & lt; Iostream & gt; # Include & lt; Cstring & gt; # Include & lt; Aio.h & gt; # Include & lt; Stdlib.h & gt; # Include & lt; Stdio.h & gt; # Include & lt; Sstream & gt; # Include & lt; Errno.h & gt; using namespace std; # Defined truth # 1 wrong identity 0 int main () {int i = 0; Inf offset = 0, fd_offset = 0; Int slen = 0; Four * buffer, * buffer_out; Int buffer_size = 30; Int is_first_write = true; Structure aiocb aio_param; Int fd_out = open ("result.log", O_APPEND | O_RDWR); Buffer = (four *) molk (buffer_size); Buffer_out = (four *) molk (buffer_size); Memeset (buffer, 0, buffer_size); Memeset (buffer_out, 0, buffer_size); For {I = 0; i <10; i ++} {stringstream ss; SS & lt; & Lt; "Log *****" & lt; & Lt; I & lt; & Lt; "-" & lt; & Lt; "******** \ n"; Slen = strlen (ss.str (..c_str ()); // cout & lt; & Lt; "Upset to write:" & lt; & Lt; Offset & lt; & Lt; Endl; // buffer is not enough to take the current string if (offset + slenan> buffer_size) {// current buffer content (& lt; 4k) in file message (& amp; aio_param, 0, sizeof (aio_param)) Write; Aio_param.aio_fildes = fd_out; Aio_param.aio_offset = fd_offset; While (aio_error (& aio_param)! = 0 & amp; is_first_write == incorrect) {cout & lt; & Lt; "Wait for the last one to finish" & lt; & Lt; Endl;} aio_param.aio_buf = memcpy (buffer_out, buffer, buffer_size); // buffer_out should be written before copying again! Aio_param.aio_nbytes = offset; // aio_offset =? The O_APPEND file should end for the file aio_param.aio_sigevent.sigev_notify = SIGEV_NONE; If (aio_write (& aio_param) == -1) {cout & lt; & Lt; "ERR: aio_write () == - 1" & lt; & Lt; Endl; Cout & lt; & Lt; "Errno:" & lt; & Lt; Wrong & lt; & Lt; Endl; Return 1; } And {is_first_write = false; Fd_offset + = offset; Cout & lt; & Lt; "Aio_write ()" & lt; & Lt; Offset & lt; & Lt; "Bytes Offset File Offset" and Lieutenant; & Lt; Fd_offset & lt; & Lt; Endl; Offset = 0; // sleep (1); // clear buffer memmet (buffer, 0, buffer_size); }} // Current string is buffer, memy (buffer + offset, sssr). Write to C_str (), slen); Offset + = slain; } Closed (fd_out); Return 0; }  

The wrong result is:

  log ***** 2 - ******** LOG ***** 4 - ******** LOG ***** 4 - ******** LOG ***** 4 - ******** LOG ***** 5- * * ****** LOG ***** 5 - ******** LOG ***** 6 - ******** LOG ***** 8 - *** * ****  

(It should be 0 to 8 without any duplication.)

Since you are not assigning all the values ​​of aio_param (and it is not unreliable on the stack), before I use it Recommend it to be void:

  memset (& aio_param, 0, sizeof (aio_param));  

In addition, I'm thinking that you might aio_offset ?


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -