golang 结合cgo 操作共享内存,包括虚拟内存mmap版和内存shm版
/*
#cgo linux LDFLAGS: -lrt
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
//-----------------cgo_mmap----------------------------
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
int my_shm_new(char *name) {
//shm_unlink(name);
return shm_open(name, O_RDWR|O_CREAT|O_EXCL, FILE_MODE);
}
int my_shm_open(char *name) {
return shm_open(name, O_RDWR, FILE_MODE);
}
void my_shm_free(char *name){
shm_unlink(name);
}
//------------------cgo_shm-------------------------
int my_shmmem_open(char *file,int size,int open_flag){
int shm_id;
key_t key;
key = ftok(file, 0x111);
if(key == -1){
return -1;
}
if(open_flag)
shm_id = shmget(key, size, IPC_CREAT|IPC_EXCL|0600);
else
shm_id = shmget(key, 0, 0);
if(shm_id == -1){
return -1;
}
return shm_id;
}
int my_shmmem_rm(int shm_id){
shmctl(shm_id, IPC_RMID, NULL);
return 0;
}
*/