POSIX/SYSTEM V semaphores in golang

2,373 views
Skip to first unread message

Shubhro Sinha

unread,
Mar 29, 2016, 4:34:15 AM3/29/16
to golang-nuts
Hi,

I wanted to use system V / POSIX  semaphores across a daemon written in go and another in C. However I find that the syscall package does not include any function related to that. Is it intentional? Is there any alternate way to use them?

-Shubhro

Marvin Stenger

unread,
Mar 29, 2016, 5:09:53 AM3/29/16
to golang-nuts
I guess you could make use of cgo. But I'm not sure, if that is the only/best way.

Ian Lance Taylor

unread,
Mar 29, 2016, 9:25:28 AM3/29/16
to Marvin Stenger, golang-nuts
On Tue, Mar 29, 2016 at 2:09 AM, Marvin Stenger
<marvin.s...@gmail.com> wrote:
> I guess you could make use of cgo. But I'm not sure, if that is the
> only/best way.

Somebody could add support to the golang.org/x/sys/unix package.

Ian

> Am Dienstag, 29. März 2016 10:34:15 UTC+2 schrieb Shubhro Sinha:
>>
>> Hi,
>>
>> I wanted to use system V / POSIX semaphores across a daemon written in go
>> and another in C. However I find that the syscall package does not include
>> any function related to that. Is it intentional? Is there any alternate way
>> to use them?
>>
>> -Shubhro
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Konstantin Khomoutov

unread,
Mar 29, 2016, 9:42:17 AM3/29/16
to Ian Lance Taylor, Marvin Stenger, golang-nuts
On Tue, 29 Mar 2016 06:25:12 -0700
Ian Lance Taylor <ia...@golang.org> wrote:

> On Tue, Mar 29, 2016 at 2:09 AM, Marvin Stenger
> <marvin.s...@gmail.com> wrote:
> > I guess you could make use of cgo. But I'm not sure, if that is the
> > only/best way.
>
> Somebody could add support to the golang.org/x/sys/unix package.

According to manual pages on my Linux system, 4 syscalls and
accompanying data types cover the whole semaphores thing.

So, this task appears to be tractable.

Sam Whited

unread,
Mar 29, 2016, 11:39:55 AM3/29/16
to Konstantin Khomoutov, Ian Lance Taylor, Marvin Stenger, golang-nuts
On Tue, Mar 29, 2016 at 8:41 AM, Konstantin Khomoutov
<flat...@users.sourceforge.net> wrote:
> According to manual pages on my Linux system, 4 syscalls and
> accompanying data types cover the whole semaphores thing.
>
> So, this task appears to be tractable.

If you're using existing semaphores created by another process this is
true, but if you want to create your own semaphore you'll need to be
able to call sem_open(3) (for named semaphores; there are other
methods for closing, unlinking, creating unnamed semaphores, etc.).

I assume this is what he's asking about, and it would probably make
sense to stick something like that in x/sys/unix.

—Sam

A full list (I think?) would include:

- sem_open(3)
- sem_wait(3)
- sem_post(3)
- sem_close(3)
- sem_unlink(3)
- sem_init(3)
- sem_destroy(3)

--
Sam Whited
pub 4096R/54083AE104EA7AD3
https://blog.samwhited.com

Konstantin Khomoutov

unread,
Mar 29, 2016, 12:26:42 PM3/29/16
to Sam Whited, Konstantin Khomoutov, Ian Lance Taylor, Marvin Stenger, golang-nuts
No, the 3rd section of manual pages on Linux-based systems means
facilities provided by libraries -- either libc or other user-space
stuff. This means cgo. Go-specific code should strive to rely on
system calls only; and for x/sys/unix it is, I assume, a must except
we're talking about some "weird" platforms like Solaris (?) which must
route all calls to the kernel through libc. On Linux-based systems,
syscalls are described by the pages in the 2nd section, so here we go:

~% apropos semaphore | grep -F '(2)'
semctl (2) - System V semaphore control operations
semget (2) - get a System V semaphore set identifier
semop (2) - System V semaphore operations
semtimedop (2) - System V semaphore operations

Quick googling reveals that indeed this appears to be a rather complete
set of stuff to deal with semaphores.

shu...@ionosnetworks.com

unread,
Apr 15, 2016, 1:07:07 AM4/15/16
to golang-nuts, s...@samwhited.com, flat...@users.sourceforge.net, ia...@golang.org, marvin.s...@gmail.com
I ended up using syscall.Sycall() function to call the SYS V semaphore system calls and wrapped them up in a package.

-Shubhro

tema....@gmail.com

unread,
May 11, 2016, 7:08:23 PM5/11/16
to golang-nuts
Can you share this package?
Thanks

вторник, 29 марта 2016 г., 11:34:15 UTC+3 пользователь Shubhro Sinha написал:

shubhro sinha

unread,
May 11, 2016, 9:05:45 PM5/11/16
to tema....@gmail.com, golang-nuts

https://github.com/shubhros/drunkendeluge/tree/master/semaphore

-Shubhro

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/zpwbWye5o7o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages