mirror of
https://github.com/rwinkhart/sys.git
synced 2026-08-31 22:36:44 -04:00
unix: add Sysv shared memory support
Implements proposed API from https://golang.org/issue/46084. I chose `SysvShmDesc` since it's a clearer name than `SysvShm`. Initially supports Darwin and Linux. Solaris support has a blocker (https://golang.org/issue/46084#issuecomment-836980018) For golang/go#46084 Change-Id: Ied0f768a74c448254adc3315348417825a7ec63e GitHub-Last-Rev: befbd7af6b2f838753ac163ef387cebdb7957467 GitHub-Pull-Request: golang/sys#110 Reviewed-on: https://go-review.googlesource.com/c/sys/+/327830 Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
committed by
Tobias Klauser
parent
39ccf1dd6f
commit
969570ce7c
@@ -0,0 +1,52 @@
|
||||
// Copyright 2021 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build (darwin && amd64) || (linux && !android)
|
||||
// +build darwin,amd64 linux,!android
|
||||
|
||||
package unix_test
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func ExampleSysvShmGet() {
|
||||
|
||||
// create shared memory region of 1024 bytes
|
||||
id, err := unix.SysvShmGet(unix.IPC_PRIVATE, 1024, unix.IPC_CREAT|unix.IPC_EXCL|0o600)
|
||||
if err != nil {
|
||||
log.Fatal("sysv shm create failed:", err)
|
||||
}
|
||||
|
||||
// warning: sysv shared memory segments persist even after after a process
|
||||
// is destroyed, so it's very important to explicitly delete it when you
|
||||
// don't need it anymore.
|
||||
defer func() {
|
||||
_, err := unix.SysvShmCtl(id, unix.IPC_RMID, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
// to use a shared memory region you must attach to it
|
||||
b, err := unix.SysvShmAttach(id, 0, 0)
|
||||
if err != nil {
|
||||
log.Fatal("sysv attach failed:", err)
|
||||
}
|
||||
|
||||
// you should detach from the segment when finished with it. The byte
|
||||
// slice is no longer valid after detaching
|
||||
defer func() {
|
||||
if err = unix.SysvShmDetach(b); err != nil {
|
||||
log.Fatal("sysv detach failed:", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// Changes to the contents of the byte slice are reflected in other
|
||||
// mappings of the shared memory identifer in this and other processes
|
||||
b[42] = 'h'
|
||||
b[43] = 'i'
|
||||
}
|
||||
Reference in New Issue
Block a user