LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-08-2013, 12:06 AM   #1
hambone55
LQ Newbie
 
Registered: Dec 2013
Posts: 5

Rep: Reputation: Disabled
How to Port Windows Interprocess Event with AutoResetEvent to Linux


My current Windows code uses a named event for inter-process communication. There can be many processes waiting on the event, when it's signaled, all the waiting processes are released and then the event returns to a non-signaled state. I use the PulseEvent function for this.

How do I do this in Linux? The pthread functions seem to be inter-thread communication. I don't see where semaphores provide the same behavior as the Windows event. The Posix semaphore seems to only release one waiting thread. If the semaphore could release all waiting threads then that would work. Is there something in Linux I'm missing?
 
Old 12-09-2013, 12:00 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,138

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
What initial value did you give in sem_init?
 
Old 12-09-2013, 01:28 PM   #3
hambone55
LQ Newbie
 
Registered: Dec 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
I don't think the init value matters since it still wouldn't provide the necessary behavior. The behavior needed is: the gate is closed, people queue up waiting for the gate to open, when the gate opens all those waiting are allowed through, then the gate closes again.

A semaphore would be more like football. You can have 11 people on the field at one time. As players come off the field, others can go on to take their place. This is fundamentally a different type of problem from the gated problem which pthread conditions support but only within the same process.
 
Old 12-09-2013, 03:28 PM   #4
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,138

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Are you just looking for pause and kill? You can wait for a signal, and send a signal to all members of a process group. You just need initialization to set up the process group and register the handler, and a signal handler for the signal you want to use.
 
Old 12-14-2013, 12:57 AM   #5
hambone55
LQ Newbie
 
Registered: Dec 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
It looks like a futex will provide the behavior of the Windows event.
 
Old 12-17-2013, 05:47 PM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,642
Blog Entries: 4

Rep: Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933
One of the thorniest complexities of the Wine project was to provide suitable emulation of Windows process-synchronization primitives under the (very different!) auspices of Unix/Linux/POSIX. In fact, one of the main purposes of their wineserver process is specifically to support these very-advanced semantics.

But in this case, I think that condition variables will provide you with what you need.

Last edited by sundialsvcs; 12-17-2013 at 05:50 PM.
 
Old 12-17-2013, 06:32 PM   #7
hambone55
LQ Newbie
 
Registered: Dec 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Condition variables won't work but futex works perfect.
 
Old 12-17-2013, 10:55 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by hambone55 View Post
Condition variables won't work but futex works perfect.
Well, there are issues: https://en.wikipedia.org/wiki/Futex -> http://www.akkadia.org/drepper/futex.pdf .

The whole thing feels quite moot to me.

I don't know how it is implemented under Windows. I.e. I am not also saying how it's implemented in Linux - that's why I've decided to quickly go through the above PDF.

Intuitively the WINE idea to have a special server looks more appealing to me.
 
Old 12-17-2013, 11:18 PM   #9
hambone55
LQ Newbie
 
Registered: Dec 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
After you realize the man pages are misleading since you have to invoke the futex function via a syscall, it actually is quite trivial. You allocate a small chunk of named shared memory which is your value and leave it set to 0. Using the wait and wake options are all the threads/processes need to use. Whenever a wake is called, you provide a value for int max so all threads waiting are released. This provides the behavior of the Event in Windows using AutoResetEvent option. This is orders of magnitude less overhead than using a WINE approach.
 
Old 12-17-2013, 11:55 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by hambone55 View Post
After you realize the man pages are misleading since you have to invoke the futex function via a syscall, it actually is quite trivial. You allocate a small chunk of named shared memory which is your value and leave it set to 0. Using the wait and wake options are all the threads/processes need to use. Whenever a wake is called, you provide a value for int max so all threads waiting are released. This provides the behavior of the Event in Windows using AutoResetEvent option. This is orders of magnitude less overhead than using a WINE approach.
The syscall interface looks quite awkward. In Russian there is a joke:

Q: Что будет, если скрестить ужа с ежом ?
A: 2 километра колючей проволоки.

Translation:

Q: What are you going to get if you crossbreed adder (animal) with hedgehog ?
A: 2km of barbed wire.

So reading about the interface ( http://man7.org/linux/man-pages/man2/futex.2.html ) I had that feeling of the 2km of barbed wire of the above origin.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Linux equivalent for Windows Event Log GreenScuba Linux - Newbie 4 01-18-2017 04:00 PM
Linux-Windows Security: ClamAV and Event Viewer ElvisImprsntr Linux - Security 3 09-29-2007 08:23 AM
collecting windows event logs on a linux server kav Linux - Software 1 06-22-2007 03:28 PM
Interprocess Communication in linux. hemanexp Linux - Software 0 01-16-2004 06:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration