Last active
December 9, 2022 14:37
-
-
Save tenfyzhong/767b4a1ed59cc7ead2d446df9fb78e5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @file main.go | |
// @brief | |
// @author tenfyzhong | |
// @email tenfyzhong@qq.com | |
// @created 2017-06-26 17:54:34 | |
package main | |
import ( | |
"flag" | |
"fmt" | |
"os" | |
"syscall" | |
"time" | |
"unsafe" | |
) | |
const ( | |
// IpcCreate create if key is nonexistent | |
IpcCreate = 00001000 | |
) | |
var mode = flag.Int("mode", 0, "0:write 1:read") | |
func main() { | |
flag.Parse() | |
shmid, _, err := syscall.Syscall(syscall.SYS_SHMGET, 2, 4, IpcCreate|0600) | |
if int(shmid) == -1 { | |
fmt.Printf("syscall error, err: %v\n", err) | |
os.Exit(-1) | |
} | |
fmt.Printf("shmid: %v\n", shmid) | |
shmaddr, _, err := syscall.Syscall(syscall.SYS_SHMAT, shmid, 0, 0) | |
if int(shmaddr) == -1 { | |
fmt.Printf("syscall error, err: %v\n", err) | |
os.Exit(-2) | |
} | |
fmt.Printf("shmaddr: %v\n", shmaddr) | |
defer syscall.Syscall(syscall.SYS_SHMDT, shmaddr, 0, 0) | |
if *mode == 0 { | |
fmt.Println("write mode") | |
i := 0 | |
for { | |
fmt.Printf("%d\n", i) | |
*(*int)(unsafe.Pointer(uintptr(shmaddr))) = i | |
i++ | |
time.Sleep(1 * time.Second) | |
} | |
} else { | |
fmt.Println("read mode") | |
for { | |
fmt.Println(*(*int)(unsafe.Pointer(uintptr(shmaddr)))) | |
time.Sleep(1 * time.Second) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
请问我想通过内存共享更加复杂的数据结构(切片)的时候,我读取数据的进程拿到的数据与数据发送方不一致,请问这个有什么解决方法吗