如何在 Golang 中使用共享内存?

golang 如何共享或读取其他进程共享内存?我查了一些资料,没有找到相关资料。谁能给我一个例子?


Helenr
浏览 464回答 2
2回答

梦里花落0921

在 Go 的世界里,不要通过共享内存来交流;通过通信共享内存。如果你真的想试一试,你可以用 cgo 调用 C API:包装器.c:#include <stdlib.h>&nbsp;#include <string.h>#include <sys/shm.h>#include <sys/types.h>int my_shm_open(char* filename, int open_flag){&nbsp; &nbsp; int shm_id;&nbsp; &nbsp; key_t key;&nbsp; &nbsp; key = ftok(filename, 0x03);&nbsp; &nbsp; if(key == -1){&nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; }&nbsp; &nbsp; if(open_flag)&nbsp; &nbsp; &nbsp; &nbsp; shm_id = shmget(key, 4096, IPC_CREAT|IPC_EXCL|0600);&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; shm_id = shmget(key, 0, 0);&nbsp; &nbsp; if(shm_id == -1){&nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; }&nbsp; &nbsp; return shm_id;}int my_shm_update(int shm_id, char* content){&nbsp; &nbsp; char* addr;&nbsp; &nbsp; addr = (char*)shmat(shm_id, NULL, 0);&nbsp; &nbsp; if(addr == (char*)-1){&nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; }&nbsp; &nbsp; if(strlen(content) > 4095)&nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; strcpy(addr, content);&nbsp; &nbsp; shmdt(addr);&nbsp; &nbsp; return 0;}int my_shm_close(int shm_id){&nbsp; &nbsp; shmctl(shm_id, IPC_RMID, NULL);&nbsp; &nbsp; return 0;}char* my_shm_read(char* filename){&nbsp; &nbsp; int shm_id;&nbsp; &nbsp; char* addr;&nbsp; &nbsp; char* s;&nbsp; &nbsp; shm_id = my_shm_open(filename, 0);&nbsp; &nbsp; if(shm_id == -1)&nbsp; &nbsp; &nbsp; &nbsp; return NULL;&nbsp; &nbsp; addr = (char*)shmat(shm_id, NULL, 0);&nbsp; &nbsp; if(addr == (char*)-1){&nbsp; &nbsp; &nbsp; &nbsp; return NULL;&nbsp; &nbsp; }&nbsp; &nbsp; s = (char*)malloc(strlen(addr) + 1);&nbsp; &nbsp; strcpy(s, addr);&nbsp; &nbsp; shmdt(addr);&nbsp; &nbsp; return s;}阅读器package main// #include <stdlib.h>// #include "wrapper.c"import "C"import "unsafe"import "fmt"func read(filename string) string {&nbsp; &nbsp; f := C.CString(filename)&nbsp; &nbsp; defer C.free(unsafe.Pointer(f))&nbsp; &nbsp; s := C.my_shm_read(f)&nbsp; &nbsp; defer C.free(unsafe.Pointer(s))&nbsp; &nbsp; return C.GoString(s)}func main() {&nbsp; &nbsp; fmt.Println(read("/tmp"))}作家.去:package main// #include <stdlib.h>// #include "wrapper.c"import "C"import "unsafe"import (&nbsp; &nbsp; "log"&nbsp; &nbsp; "time")type errorString struct {&nbsp; &nbsp; s string}func (e *errorString) Error() string {&nbsp; &nbsp; return e.s}func open(file string) (int, error) {&nbsp; &nbsp; f := C.CString(file)&nbsp; &nbsp; defer C.free(unsafe.Pointer(f))&nbsp; &nbsp; r := int(C.my_shm_open(f, C.int(1)))&nbsp; &nbsp; if r == -1 {&nbsp; &nbsp; &nbsp; &nbsp; return 0, &errorString{"error"}&nbsp; &nbsp; }&nbsp; &nbsp; return r, nil}func update(shm_id int, content string) error {&nbsp; &nbsp; c := C.CString(content)&nbsp; &nbsp; defer C.free(unsafe.Pointer(c))&nbsp; &nbsp; r := int(C.my_shm_update(C.int(shm_id), c))&nbsp; &nbsp; if r == -1 {&nbsp; &nbsp; &nbsp; &nbsp; return &errorString{"update error"}&nbsp; &nbsp; }&nbsp; &nbsp; return nil}func close(shm_id int) error {&nbsp; &nbsp; C.my_shm_close(C.int(shm_id))&nbsp; &nbsp; return nil}func main() {&nbsp; &nbsp; id, err := open("/tmp")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; defer close(id)&nbsp; &nbsp; err = update(id, "hello world")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; time.Sleep(1e9 * 100)}运行编写器,然后运行阅读器go run filename。

慕田峪9158850

golang:它是一种编程语言,所以它与操作系统级别的东西的共享内存无关。并不是说 golang 不使用共享内存,但这不是它需要定义的。posix使用共享内存,可以使用syscall包,里面包含了很多系统调用,只要参考c系统调用接口就行了。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go