github.com/Kalvelign/golang-windows-sys-lib@v0.0.0-20221121121202-63da651435e1/unix/example_sysvshm_test.go (about) 1 // Copyright 2021 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build (darwin && !ios) || (linux && !android) 6 // +build darwin,!ios linux,!android 7 8 package unix_test 9 10 import ( 11 "log" 12 13 "golang.org/x/sys/unix" 14 ) 15 16 func ExampleSysvShmGet() { 17 // create shared memory region of 1024 bytes 18 id, err := unix.SysvShmGet(unix.IPC_PRIVATE, 1024, unix.IPC_CREAT|unix.IPC_EXCL|0o600) 19 if err != nil { 20 log.Fatal("sysv shm create failed:", err) 21 } 22 23 // warning: sysv shared memory segments persist even after after a process 24 // is destroyed, so it's very important to explicitly delete it when you 25 // don't need it anymore. 26 defer func() { 27 _, err := unix.SysvShmCtl(id, unix.IPC_RMID, nil) 28 if err != nil { 29 log.Fatal(err) 30 } 31 }() 32 33 // to use a shared memory region you must attach to it 34 b, err := unix.SysvShmAttach(id, 0, 0) 35 if err != nil { 36 log.Fatal("sysv attach failed:", err) 37 } 38 39 // you should detach from the segment when finished with it. The byte 40 // slice is no longer valid after detaching 41 defer func() { 42 if err = unix.SysvShmDetach(b); err != nil { 43 log.Fatal("sysv detach failed:", err) 44 } 45 }() 46 47 // Changes to the contents of the byte slice are reflected in other 48 // mappings of the shared memory identifer in this and other processes 49 b[42] = 'h' 50 b[43] = 'i' 51 }