how does aio_write() work

N

nass

hello everyone i am looking into asynchronous file writing
(appending).
what the program is doing:
i am running a loop and when the condition is met (basically a timer)
a buffer gets appended to a file. every so often (another timer) the
filename is changed so the next write must create a new file and start
appending into that. it is really a timed Binary log.

i have managed to do that 'saving' with stream to file operations
(where file
is of type fstream), and i have also managed to do it with fopen().
but it is a requirement that the execution is not halted so i looked
into threads... and then i came across asynchronous write.

now looking into aio_write and the struct aiocb that it takes as input
argument, i see that i must provide a file descriptor instead of ther
usual FILE *, that fopen returns.

and i am wondering if i should be opening the file prior to
aio_write(), or just opening the file once for evey new filename (*ie
when the file doesnt exist), getting the file descriptor, and closing
the file, so that when the time for aio_write() comes , aio_write()
will open, write and close the file on its own.

any ideas?
 
C

Chris Dollin

nass wrote:

(with subject: how does aio_write() work)
hello everyone i am looking into asynchronous file writing
(appending).

aio_write isn't a standard C function at all (fx:man) -- it
seems to be POSIX, so you should get informed & topical answers
in comp.unix.programmer.
 
S

Stephen Sprunk

nass said:
hello everyone i am looking into asynchronous file writing
(appending).

All OT for clc. Try comp.unix.programmer.
what the program is doing:
i am running a loop and when the condition is met (basically a timer)
a buffer gets appended to a file. every so often (another timer) the
filename is changed so the next write must create a new file and start
appending into that. it is really a timed Binary log.

i have managed to do that 'saving' with stream to file operations
(where file is of type fstream), and i have also managed to do it with
fopen(). but it is a requirement that the execution is not halted so i
looked into threads... and then i came across asynchronous write.

now looking into aio_write and the struct aiocb that it takes as input
argument, i see that i must provide a file descriptor instead of ther
usual FILE *, that fopen returns.

Once you start playing in the POSIX world, it's best to stay there. i.e.
use open() instead of fopen(). Of course, that puts you completely
off-topic for here.
and i am wondering if i should be opening the file prior to
aio_write(), or just opening the file once for evey new filename (*ie
when the file doesnt exist), getting the file descriptor, and closing
the file, so that when the time for aio_write() comes , aio_write()
will open, write and close the file on its own.

No, it won't. aio_write() works just like write(), except it may be
asynchronous. Once you close a fd, it becomes invalid; you can't just keep
writing to it.

I don't think you want AIO here at all. Just create a standard FILE*, stuff
it in a global variable, and fwrite() to it from the first timer's function.
The second timer's function would fopen() the next file, change the global
to the new file, and fclose() the old file. There's probably a race
condition or reentrancy issue in there somewhere, though, which is why you
need to talk to folks who work with threads on a regular basis (i.e. not on
clc).

S
 
S

santosh

Stephen said:
All OT for clc. Try comp.unix.programmer.

I don't think you want AIO here at all. Just create a standard FILE*, stuff
it in a global variable, and fwrite() to it from the first timer's function.
The second timer's function would fopen() the next file, change the global
to the new file, and fclose() the old file. There's probably a race
condition or reentrancy issue in there somewhere, though, which is why you
need to talk to folks who work with threads on a regular basis (i.e. not on
clc).

One possibility is comp.programming.threads
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top