dongmi9494 2018-04-17 21:52
浏览 71

维护指向共享内存的指针

From what research I have done there are no libraries out there to create and manage shared memory in Go. I thought it might be easy enough to pull of with some basic cgo code, and as it turns out it is. The concern that I would like to address before I move forward involves the interaction between the Go runtime and Go pointers to shared memory.

To demonstrate my concern, I created a simple Go struct

type Test struct {
    X int32
    Y float64
    Z [5]int32
}

That I expose in two seperate Go programs, A and B along with some cgo code.

#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <string.h>
int get_mem() {
    key_t key = ftok("shared_mem",65);
    return shmget(key,1024,0666|IPC_CREAT);
}

char * attach_mem(int id) {
    return (char*) shmat(id,(void*)0,0);
}

void detach_mem(char * str) {
    shmdt(str);
}

void destroy_mem(int id) {
    shmctl(id,IPC_RMID,NULL);
}

void write(char * str, char * data, int len) {
    memcpy(str, data, len);
}

Program A then creates a Test struct, writes it to shared memory, creates a *Test pointer called ptr to that memory, waits 10 seconds, then prints the value at ptr.

// Program A
func main() {
    id := C.get_mem()
    str := C.attach_mem(id)
    dataLocal := Test{1, 2.0, [5]int32{3, 4, 5, 6, 7}}
    C.write(str, (*C.char)(unsafe.Pointer(&dataLocal)), C.int(unsafe.Sizeof(dataLocal)))
    ptr := (*Test)(unsafe.Pointer(str))
    time.Sleep(10 * time.Second)
    fmt.Println(ptr)
    C.detach_mem(str)
    C.destroy_mem(id)
}

In those 10 seconds, Program B starts, creates a *Test pointer to the shared memory, and modifies one of the fields.

// Program B
func main() {
    id := C.get_mem()
    str := C.attach_mem(id)
    data := (*Test)(unsafe.Pointer(str))
    data.X = 10
    C.detach_mem(str)
}

Obviously this is not a practical IPC implementation, but it works. Program A prints

&{10 2 [3 4 5 6 7]}

My concern is that the Go runtime, namely the garbage collector, won't play nice with this implementation. Specifically, how will Go treat ptr in Program A? I know that the Go GC will move data in the heap around, but does that apply to data referenced by Go pointers that lie outside of the heap? Now that a Go pointer references this data, will it be garbage collected?

If neither of those issues apply, is this approach unsafe or otherwise considered to be bad style? If so what other alternatives are there?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥15 stable diffusion
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿