Files
sys/unix/example_sysvshm_test.go
Tobias Klauser b1ebd4e100 unix: enable Sysv shared memory support on darwin/arm64
Keep it disabled on ios though.

For golang/go#45696
For golang/go#46084

Change-Id: I3d551227a4ebc0eebabdd16b175aa6a75ea9de19
Reviewed-on: https://go-review.googlesource.com/c/sys/+/353509
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2021-10-03 12:29:50 +00:00

52 lines
1.4 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)
// +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 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'
}