Unix Programming - Opening file for simultaneous read and write

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2004 > Opening file for simultaneous read and write





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Opening file for simultaneous read and write
pxlai625

2004-03-21, 4:34 pm

I am trying to open a WAV file that I can write to and read from at
the same time (preferably with mmap). I want to be able to record to
this file and be able to play what is being recorded while it is still
recording. How would I do this?

- Patrick
Victor Wagner

2004-03-21, 4:34 pm

pxlai625 <pxlai625@yahoo.com> wrote:
: I am trying to open a WAV file that I can write to and read from at
: the same time (preferably with mmap). I want to be able to record to
: this file and be able to play what is being recorded while it is still
: recording. How would I do this?

What's the problem? Describe what you are trying to do and what doesn't
work.
--
Dijkstra probably hates me.
-- Linus Torvalds, in kernel/sched.c
pxlai625

2004-03-22, 12:34 am

vitus@45.free.net (Victor Wagner) wrote in message news:<c3kuju$jbo$1@wagner.wagner.home>...
> pxlai625 <pxlai625@yahoo.com> wrote:
> : I am trying to open a WAV file that I can write to and read from at
> : the same time (preferably with mmap). I want to be able to record to
> : this file and be able to play what is being recorded while it is still
> : recording. How would I do this?
>
> What's the problem? Describe what you are trying to do and what doesn't
> work.


I already described what I'm trying to do. The problem is I don't know
if it is safe to use open(), read(), and write() of the same file
descriptor in a multi threaded application. I need a safe way for one
thread to read() from a file and another thread to write() to a file
simultaneously.

- Patrick
Andrei Voropaev

2004-03-22, 3:33 am

On 2004-03-22, pxlai625 <pxlai625@yahoo.com> wrote:
> vitus@45.free.net (Victor Wagner) wrote in message news:<c3kuju$jbo$1@wagner.wagner.home>...
>
> I already described what I'm trying to do. The problem is I don't know
> if it is safe to use open(), read(), and write() of the same file
> descriptor in a multi threaded application. I need a safe way for one
> thread to read() from a file and another thread to write() to a file
> simultaneously.


Uuuu. Use mutexes in this case. Or better to say mutexes and conditional
variables. So, open("name", O_RDWR) shall give you read/write access and
other stuff shall make sure that you don't read and write
simultaneously from 2 different threads.

Andrei
Marc Rochkind

2004-03-22, 7:34 pm


"pxlai625" <pxlai625@yahoo.com> wrote in message
news:10f19540.0403211234.7d8f0e7b@posting.google.com...
> I am trying to open a WAV file that I can write to and read from at
> the same time (preferably with mmap). I want to be able to record to
> this file and be able to play what is being recorded while it is still
> recording. How would I do this?
>
> - Patrick


I'm guessing the problem is that you don't want the reader to get ahead of
the writer; you want to play something only after it's been recorded.

I don't know enough about the structure of WAV files to say exactly, but one
idea that you can perhaps modify appropriately is to have the writer and
reader share a variable that holds the current size of the file, set by the
writer and read by the reader. Use mutexes to control access to this
variable, and make the variable of the appropriate type.

Then, maybe you can arrange for the reader to read only as much as the
writer has written. You may want to use a condition variable that the reader
can block on waiting for the size to increase.

You can probably combine the two, using a single condition that represents
the number of unread bytes. The reader can then block on the condition that
it is greater than zero.

Hope some combination of the above works for you!

--
Marc Rochkind
"Advanced UNIX Programming" (publishing April 2004)
www.basepath.com/aup


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com