Re: pipes
- From: Logan Shaw <lshaw-usenet@xxxxxxxxxxxxx>
- Date: Wed, 29 Aug 2007 20:51:07 -0500
fjblurt@xxxxxxxxx wrote:
On Aug 29, 12:39 pm, CptDondo <y...@xxxxxxxxxxxxxxxx> wrote:
I only have one process that would write to this area. So if in the
server I set
p[0] = -1;
write ...
p[0] = 0;
then the client would know not to try to read the area until p[0] == 0.
Would this work? Or are the writes buffered somehow?
It won't work, but not for the reason you think. The server may start
writing in between the client checking the flag and finishing the
reading.
A robust approach is to lock the file when reading or writing the
data. See section 2.5 of the FAQ for this newsgroup for information
on file locking. Since file locking is designed for exactly this sort
of situation, you can be sure that it will be free of races, as long
as you use it on every access.
Correct me if I'm wrong, but if you need more fine-grained locking, you
should be able to use sem_init() to create any number of semaphores in
the shared memory area.
That way the data collection routine could write a history of several
samples into the area, and a client could read them at its leisure.
And you could have a separate lock for each one, thus reducing the
chances that the client and server would need the lock at the same time.
The obvious way to do this would be with a circular buffer.
(This would be helpful if the client need to collect the data and
write it to a log file or something; it wouldn't have to worry much
about running super frequently so that it doesn't miss data.)
- Logan
.
- References:
- Prev by Date: Re: daemonizing without setsid()
- Next by Date: Re: pipes
- Previous by thread: Re: pipes
- Next by thread: Re: pipes
- Index(es):
Relevant Pages
|
|