mirror of
https://github.com/golang/sys.git
synced 2026-01-29 07:02:06 +03:00
Change-Id: I8b05b5cea86d41423dac9fd96a5000a6daa1e8c3 Reviewed-on: https://go-review.googlesource.com/c/sys/+/690816 Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com> Reviewed-by: Mark Freeman <mark@golang.org> Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Knyszek <mknyszek@google.com>
51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
// 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 && !ios) || (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 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'
|
|
}
|