hello all,
I'm trying to copy a file using async io.
/* async I/O structures */
struct aiocb cb;
const struct aiocb* list[1] = {&cb};
int aioRet;
memset(&cb, 0, sizeof(cb));
/* set the appropriate values in control buffers */
cb.aio_fildes = fromfd;
cb.aio_buf = buffer0;
cb.aio_nbytes = BUFSIZE;
cb.aio_sigevent.sigev_notify = SIGEV_NONE;
while(1) {
/* read data into buf1 using async read*/
aioRet = aio_read(&cb);
if(aioRet == -1) {
exit(EXIT_FAILURE);
}
/* wait till aio is finished */
while(1) {
if(aio_error(&cb) != EINPROGRESS) {
printf("I'm not in progress\n");
break;
}
}
/* write to the file */
if (aio_error(&cb) == 0) {
if(aio_return(&cb) > 0) {
write(tofd, buffer0, cb.aio_nbytes);
}
if(aio_return(&cb) == -1 ) {
fprintf(stderr, "%s", aio_return error : ");
perror("");
exit(EXIT_FAILURE);
}
/* end of the file . exit */
if(aio_return(&cb) == 0) {
break;
}
}
}
close(fromfd);
close(tofd);
}
this is not my complete code, but this is the logic I'm using.
Everytime I try to use this, I get an error message saying :
"aio_return error : Invalid argument "
I can't move on with my programme because of this. Please help me. Any
help would be really appreciated.
thanks in advance.
|
|
0
|
|
|
|
Reply
|
danu82 (14)
|
8/3/2006 5:02:48 PM |
|
In article <1154624568.814585.28530@p79g2000cwp.googlegroups.com>,
"danushka" <danu82@gmail.com> wrote:
> hello all,
> I'm trying to copy a file using async io.
>
> /* async I/O structures */
> struct aiocb cb;
> const struct aiocb* list[1] = {&cb};
> int aioRet;
>
> memset(&cb, 0, sizeof(cb));
>
> /* set the appropriate values in control buffers */
> cb.aio_fildes = fromfd;
> cb.aio_buf = buffer0;
> cb.aio_nbytes = BUFSIZE;
> cb.aio_sigevent.sigev_notify = SIGEV_NONE;
>
> while(1) {
>
> /* read data into buf1 using async read*/
> aioRet = aio_read(&cb);
> if(aioRet == -1) {
> exit(EXIT_FAILURE);
> }
>
> /* wait till aio is finished */
> while(1) {
> if(aio_error(&cb) != EINPROGRESS) {
> printf("I'm not in progress\n");
> break;
> }
> }
>
> /* write to the file */
> if (aio_error(&cb) == 0) {
> if(aio_return(&cb) > 0) {
> write(tofd, buffer0, cb.aio_nbytes);
> }
> if(aio_return(&cb) == -1 ) {
> fprintf(stderr, "%s", aio_return error : ");
> perror("");
> exit(EXIT_FAILURE);
> }
> /* end of the file . exit */
> if(aio_return(&cb) == 0) {
> break;
> }
> }
>
> }
>
> close(fromfd);
> close(tofd);
> }
>
> this is not my complete code, but this is the logic I'm using.
> Everytime I try to use this, I get an error message saying :
> "aio_return error : Invalid argument "
That's not the actual error. You can't call fprintf() between calling
aio_return() and calling perror(), because fprintf() may (and usually
does) modify errno. Change your code to
if (aio_return(&cb) == -1) {
perror("aio_return error:");
exit(EXIT_FAILURE);
};
and then you'll get the actual error message.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
|
|
0
|
|
|
|
Reply
|
Barry
|
8/4/2006 3:06:21 AM
|
|
Barry Margolin <barmar@alum.mit.edu> wrote, on Thu, 03 Aug 2006:
> In article <1154624568.814585.28530@p79g2000cwp.googlegroups.com>,
> "danushka" <danu82@gmail.com> wrote:
>> /* write to the file */
>> if (aio_error(&cb) == 0) {
>> if(aio_return(&cb) > 0) {
>> write(tofd, buffer0, cb.aio_nbytes);
>> }
>> if(aio_return(&cb) == -1 ) {
>> fprintf(stderr, "%s", aio_return error : "); perror("");
>> exit(EXIT_FAILURE);
>> }
>> /* end of the file . exit */
>> if(aio_return(&cb) == 0) {
>> break;
>> }
>> }
>> this is not my complete code, but this is the logic I'm using. Everytime
>> I try to use this, I get an error message saying : "aio_return error :
>> Invalid argument "
>
> That's not the actual error. You can't call fprintf() between calling
> aio_return() and calling perror(), because fprintf() may (and usually
> does) modify errno.
It is true that fprintf() can change errno, but in this case it
probably hasn't. The SUSv3 description of the EINVAL error for
aio_return() is:
"The aiocbp argument does not refer to an asynchronous operation
whose return status has not yet been retrieved."
Note the "not yet been retrieved" part. Danushka's code tries to
retrieve the return status for &cb twice, and that is the reason
the second aio_return() call fails with EINVAL.
The code needs to call aio_return() once, saving the return value
for use in the > 0, == -1 and == 0 checks.
--
Geoff Clare <netnews@gclare.org.uk>
|
|
0
|
|
|
|
Reply
|
Geoff
|
8/4/2006 12:29:07 PM
|
|
Geoff Clare wrote:
> Barry Margolin <barmar@alum.mit.edu> wrote, on Thu, 03 Aug 2006:
>
> > In article <1154624568.814585.28530@p79g2000cwp.googlegroups.com>,
> > "danushka" <danu82@gmail.com> wrote:
>
> >> /* write to the file */
> >> if (aio_error(&cb) == 0) {
> >> if(aio_return(&cb) > 0) {
> >> write(tofd, buffer0, cb.aio_nbytes);
> >> }
> >> if(aio_return(&cb) == -1 ) {
> >> fprintf(stderr, "%s", aio_return error : "); perror("");
> >> exit(EXIT_FAILURE);
> >> }
> >> /* end of the file . exit */
> >> if(aio_return(&cb) == 0) {
> >> break;
> >> }
> >> }
>
> >> this is not my complete code, but this is the logic I'm using. Everytime
> >> I try to use this, I get an error message saying : "aio_return error :
> >> Invalid argument "
> >
> > That's not the actual error. You can't call fprintf() between calling
> > aio_return() and calling perror(), because fprintf() may (and usually
> > does) modify errno.
>
> It is true that fprintf() can change errno, but in this case it
> probably hasn't. The SUSv3 description of the EINVAL error for
> aio_return() is:
>
> "The aiocbp argument does not refer to an asynchronous operation
> whose return status has not yet been retrieved."
>
> Note the "not yet been retrieved" part. Danushka's code tries to
> retrieve the return status for &cb twice, and that is the reason
> the second aio_return() call fails with EINVAL.
>
> The code needs to call aio_return() once, saving the return value
> for use in the > 0, == -1 and == 0 checks.
>
> --
> Geoff Clare <netnews@gclare.org.uk>
Thanks Geoff and Barry for the reply.
I resolved the problem. The problem is, as Geoff said, I'm calling
aio_return more than once. So when I store the value and then check for
that value it worked fine.
thanks guys.
|
|
0
|
|
|
|
Reply
|
danushka
|
8/7/2006 8:16:04 PM
|
|
|
3 Replies
355 Views
(page loaded in 0.075 seconds)
|