1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
// 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
// +build darwin,amd64 linux
package unix_test
import (
"runtime"
"testing"
"golang.org/x/sys/unix"
)
func TestSysvSharedMemory(t *testing.T) {
// create ipc
id, err := unix.SysvShmGet(unix.IPC_PRIVATE, 1024, unix.IPC_CREAT|unix.IPC_EXCL|0o600)
// ipc isn't implemented on android, should fail
if runtime.GOOS == "android" {
if err != unix.ENOSYS {
t.Fatalf("expected android to fail, but it didn't")
}
return
}
// The kernel may have been built without System V IPC support.
if err == unix.ENOSYS {
t.Skip("shmget not supported")
}
if err != nil {
t.Fatalf("SysvShmGet: %v", err)
}
defer func() {
_, err := unix.SysvShmCtl(id, unix.IPC_RMID, nil)
if err != nil {
t.Errorf("Remove failed: %v", err)
}
}()
// attach
b1, err := unix.SysvShmAttach(id, 0, 0)
if err != nil {
t.Fatalf("Attach: %v", err)
}
if len(b1) != 1024 {
t.Fatalf("b1 len = %v, want 1024", len(b1))
}
b1[42] = 'x'
// attach again
b2, err := unix.SysvShmAttach(id, 0, 0)
if err != nil {
t.Fatalf("Attach: %v", err)
}
if len(b2) != 1024 {
t.Fatalf("b2 len = %v, want 1024", len(b1))
}
b2[43] = 'y'
if b2[42] != 'x' || b1[43] != 'y' {
t.Fatalf("shared memory isn't shared")
}
// detach
if err = unix.SysvShmDetach(b2); err != nil {
t.Fatalf("Detach: %v", err)
}
if b1[42] != 'x' || b1[43] != 'y' {
t.Fatalf("shared memory was invalidated")
}
}
|