Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Portable Conditional Wait for Inter-Process?

1 view
Skip to first unread message

Pierre-Luc Drouin

unread,
Mar 21, 2010, 11:59:28 AM3/21/10
to freebsd...@freebsd.org
Hi,

I am looking for a portable solution to put a process into a timed wait
(a la pthread_cond_timedwait) such that it can be waken up by another
process using something similar to pthread_cond_broadcast (there might
be many waiters). I guess shared pthread condition variables are not
supported by FreeBSD, right? Is there a portable way to do this? The
only solution I can think of right now is to use alarm and sigwait on
the waiters side and kill(wpid,SIGCONT) on the other side with a list of
pids.

Thanks!

Daniel Eischen

unread,
Mar 21, 2010, 1:07:50 PM3/21/10
to Pierre-Luc Drouin, freebsd...@freebsd.org

Yeah, shared mutexes and CVs are not yet supported by FreeBSD.
You can use sigtimedwait() to conditionally sleep and kill
on the other side, but with caveats:

o Only one thread per process can be woken with
sigwait() and friends

o You must ensure that no other threads (other than
the waiter) have the "wake" signal unmasked.

You might consider using killpg() to send a signal to
multiple processes in a process group.

FreeBSD current (9.0) recently added support for shared semapores,
so you could give those a try. You might be able to use
sem_timedwait() instead of pthread_cond_timedwait() depending on
your needs, but you are limited to waking only one thread
at a time via sem_post().

You could also have a pipe or domain socket between the sender
and each waiting processes. I suppose you could use aio_write()
on the sender side to write to selected file descriptors with
one operation. The waiters would be using poll() or select()
with a timeout.

--
DE

Pierre-Luc Drouin

unread,
Mar 21, 2010, 1:22:24 PM3/21/10
to Daniel Eischen, freebsd...@freebsd.org
Daniel Eischen wrote:
> On Sun, 21 Mar 2010, Pierre-Luc Drouin wrote:
>
>> Hi,
>>
>> I am looking for a portable solution to put a process into a timed
>> wait (a la pthread_cond_timedwait) such that it can be waken up by
>> another process using something similar to pthread_cond_broadcast
>> (there might be many waiters). I guess shared pthread condition
>> variables are not supported by FreeBSD, right? Is there a portable
>> way to do this? The only solution I can think of right now is to use
>> alarm and sigwait on the waiters side and kill(wpid,SIGCONT) on the
>> other side with a list of pids.
>
> Yeah, shared mutexes and CVs are not yet supported by FreeBSD.
> You can use sigtimedwait() to conditionally sleep and kill
> on the other side, but with caveats:
>
> o Only one thread per process can be woken with
> sigwait() and friends
>
> o You must ensure that no other threads (other than
> the waiter) have the "wake" signal unmasked.
This should not be a problem in my case..

>
> You might consider using killpg() to send a signal to
> multiple processes in a process group.
yes this is a good idea

>
> FreeBSD current (9.0) recently added support for shared semapores,
> so you could give those a try. You might be able to use
> sem_timedwait() instead of pthread_cond_timedwait() depending on
> your needs, but you are limited to waking only one thread
> at a time via sem_post().
yeah semaphores would not be ideal for my application... I would also
prefer to stick with 8.0 for now...

>
> You could also have a pipe or domain socket between the sender
> and each waiting processes. I suppose you could use aio_write()
> on the sender side to write to selected file descriptors with
> one operation. The waiters would be using poll() or select()
> with a timeout.
>
Thanks for all the good ideas!

Daniel Eischen

unread,
Mar 22, 2010, 11:54:54 AM3/22/10
to Pierre-Luc Drouin, freebsd...@freebsd.org
On Sun, 21 Mar 2010, Pierre-Luc Drouin wrote:

> Regarding process groups, how can I set two processes to belong to the same
> process group if they don't have a parent-child relation?
>
> Thanks!

Well, from skimming the man pages, I don't think you can
do that. You can't somehow launch your clients and server from
the same parent?

--
DE

Pierre-Luc Drouin

unread,
Mar 24, 2010, 11:56:34 AM3/24/10
to Daniel Eischen, freebsd...@freebsd.org
Hi,

finally what I did is to create an array in a shared memory segment (I
was already using shared memory for these processes) where I write the
PIDs of the waiters such that the other process can SIGALRM them when it
is ready. The waiters wait for this signal with sigtimedwait and the
other process SIGALRM them as well if a signal is sent to it. I use a
lock-free algorithm with atomic primitives to save and load the PIDs to
the shared segment. It seems to be working quite well.

Thanks

0 new messages