Linux下GO语言内存共享,CGO实现
2015-12-02 16:07
417 查看
package ce import ( "errors" "fmt" "os" "os/exec" ) /* #include <stdio.h> #include <sys/shm.h> #include <stdlib.h> #include <sys/ipc.h> #include <string.h> int Size; int Shm_id; key_t Key; char *Shm_add; int getMem(char *pathname,int size) { Key = ftok((char *)pathname,0x03); if(Key==-1) { return -1; } Shm_id=shmget(Key,size,IPC_CREAT|IPC_EXCL|0600); if (Shm_id==-1) { return -2; } Shm_add=shmat(Shm_id,0,0); if (Shm_add==(void *)-1) { return -3; } Size=size; return 0; } int close() { int result; result=shmdt(Shm_add); if (result != 0) { return result; } result=shmctl(Shm_id, IPC_RMID, NULL); if (result != 0) { return result; } return 0; } int write(char *str) { int len; len=strlen(str); if (len >= Size) { return 0; } strcpy((char *)Shm_add,(char *)str); return len; } char* read() { return (char *)Shm_add; } */ import "C" type Mem struct { Shm_id C.int size int } func GetShareMem(path string, size int) (*Mem, error) { info, err := os.Lstat(path) if err != nil { return nil, err } if info.IsDir() { return nil, errors.New("Must Is File") } cstr := C.CString(path) num := C.getMem(cstr, C.int(size)) C.free(unsafe.Pointer(cstr)) switch num { case 0: return &Mem{C.Shm_id, size}, nil case -1: return nil, errors.New("Get Key error") case -2: return nil, errors.New("Get Shm_id error") case -3: C.close() return nil, errors.New("Map AddrSpace error") } C.close() return nil, errors.New("Unknow error") } func (self *Mem) Close() error { num := C.close() if num != 0 { cmd := exec.Command("ipcrm", "-m", fmt.Sprint(self.Shm_id)) return cmd.Run() } return nil } func (self *Mem) Read() string { return C.GoString(C.read()) } func (self *Mem) Write(body string) C.int { if len(body) > self.size { return C.int(0) } return C.write(C.CString(body)) }
package main import ( "flag" "fmt" ) /* #include <stdio.h> #include <sys/shm.h> #include <sys/ipc.h> char* GetMem(int Shm_id) { char *Shm_add; Shm_add=shmat(Shm_id,0,0); if (Shm_add==(void *)-1) { return (char *)-1; } return (char *)Shm_add; } int close(char *Shm_add) { int result; result=shmdt(Shm_add); if (result != 0) { return result; } } */ import "C" type ShareMem struct { Addr *C.char } func main() { i := flag.Int("m", 0, "-m=12345") flag.Parse() x := GetShareMem(*i) if x == nil { fmt.Println("open error") } defer x.Close() fmt.Println(x.Read()) } func GetShareMem(shm_id int) *ShareMem { m,err := C.GetMem(C.int(shm_id)) if err != nil { return nil } return &ShareMem{m} } func (self *ShareMem) Close() { C.close(self.Addr) } func (self *ShareMem) Read() string { return C.GoString(self.Addr) }
package main import ( "fmt" "ce" ) func main() { m, err := ce.GetShareMem("/tmp/.tmpfile", 1024) if err != nil { fmt.Println(err) } defer m.Close() m.Write("fyxichen") fmt.Println(m.Read()) }
相关文章推荐
- Go 语言 Channel 实现原理精要
- Go语言将支持Android
- Golang实现的聊天程序服务端和客户端代码分享
- Go语言入门教程之Arrays、Slices、Maps、Range操作简明总结
- GO语言异常处理机制panic和recover分析
- go语言执行windows下命令行的方法
- Go语言计算两个经度和纬度之间距离的方法
- Go语言排序与接口实例分析
- Go语言导出内容到Excel的方法
- go语言实现sqrt的方法
- Go语言MessageBox用法实例
- Go语言判断指定文件是否存在的方法
- go语言制作的zip压缩程序
- go语言channel实现多核并行化运行的方法
- Go语言MD5加密用法实例
- go语言使用scp的方法实例分析
- Go语言中的switch用法实例分析
- Golang学习笔记(三):控制流
- go语言检测文件是否存在的方法
- Go语言里的结构体文法实例分析