2020-11-26 19:32:43 +08:00
|
|
|
# shm
|
2020-11-26 20:23:06 +08:00
|
|
|
[![PkgGoDev](https://pkg.go.dev/badge/github.com/hslam/shm)](https://pkg.go.dev/github.com/hslam/shm)
|
2020-12-18 13:10:26 +08:00
|
|
|
[![Build Status](https://github.com/hslam/shm/workflows/build/badge.svg)](https://github.com/hslam/shm/actions)
|
2020-11-27 02:00:52 +08:00
|
|
|
[![Go Report Card](https://goreportcard.com/badge/github.com/hslam/shm)](https://goreportcard.com/report/github.com/hslam/shm)
|
2020-11-26 20:23:06 +08:00
|
|
|
[![LICENSE](https://img.shields.io/github/license/hslam/shm.svg?style=flat-square)](https://github.com/hslam/shm/blob/master/LICENSE)
|
2020-11-26 19:44:45 +08:00
|
|
|
|
2020-11-28 17:26:07 +08:00
|
|
|
Package shm provides a way to use System V shared memory.
|
2020-11-26 19:44:45 +08:00
|
|
|
|
|
|
|
## Get started
|
|
|
|
|
|
|
|
### Install
|
|
|
|
```
|
|
|
|
go get github.com/hslam/shm
|
|
|
|
```
|
|
|
|
### Import
|
|
|
|
```
|
|
|
|
import "github.com/hslam/shm"
|
|
|
|
```
|
|
|
|
### Usage
|
|
|
|
#### SHM GET Example
|
2020-11-28 17:26:07 +08:00
|
|
|
**Writer**
|
2020-11-26 19:44:45 +08:00
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-11-28 17:26:07 +08:00
|
|
|
"github.com/hslam/ftok"
|
2020-11-26 19:44:45 +08:00
|
|
|
"github.com/hslam/shm"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-11-28 17:26:07 +08:00
|
|
|
key, err := ftok.Ftok("/tmp", 0x22)
|
2020-11-26 19:44:45 +08:00
|
|
|
if err != nil {
|
2020-11-28 17:26:07 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
2020-12-01 16:05:01 +08:00
|
|
|
shmid, data, err := shm.GetAttach(key, 128, shm.IPC_CREAT|0600)
|
2020-11-28 17:26:07 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2020-11-26 19:44:45 +08:00
|
|
|
}
|
|
|
|
defer shm.Remove(shmid)
|
2020-12-01 16:05:01 +08:00
|
|
|
defer shm.Detach(data)
|
2020-11-28 17:26:07 +08:00
|
|
|
context := []byte("Hello World")
|
|
|
|
copy(data, context)
|
|
|
|
fmt.Println(string(data[:11]))
|
|
|
|
time.Sleep(time.Second * 10)
|
2020-11-26 19:44:45 +08:00
|
|
|
}
|
2020-11-28 17:26:07 +08:00
|
|
|
```
|
|
|
|
**Reader**
|
|
|
|
```go
|
|
|
|
package main
|
2020-11-26 19:44:45 +08:00
|
|
|
|
2020-11-28 17:26:07 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/hslam/ftok"
|
|
|
|
"github.com/hslam/shm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
key, err := ftok.Ftok("/tmp", 0x22)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-12-01 16:05:01 +08:00
|
|
|
_, data, err := shm.GetAttach(key, 128, 0600)
|
2020-11-26 19:44:45 +08:00
|
|
|
if err != nil {
|
2020-11-28 17:26:07 +08:00
|
|
|
panic(err)
|
2020-11-26 19:44:45 +08:00
|
|
|
}
|
2020-12-01 16:05:01 +08:00
|
|
|
defer shm.Detach(data)
|
2020-11-26 19:44:45 +08:00
|
|
|
fmt.Println(string(data[:11]))
|
|
|
|
}
|
|
|
|
```
|
|
|
|
#### Output
|
|
|
|
```
|
|
|
|
Hello World
|
|
|
|
```
|
|
|
|
|
|
|
|
#### SHM OPEN Example
|
2020-11-28 17:26:07 +08:00
|
|
|
**Writer**
|
2020-11-26 19:44:45 +08:00
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/hslam/mmap"
|
|
|
|
"github.com/hslam/shm"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
name := "shared"
|
|
|
|
fd, err := shm.Open(name, shm.O_RDWR|shm.O_CREATE, 0600)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-11-28 17:26:07 +08:00
|
|
|
defer shm.Unlink(name)
|
2020-11-27 13:11:57 +08:00
|
|
|
defer shm.Close(fd)
|
2020-11-26 19:44:45 +08:00
|
|
|
length := 128
|
|
|
|
shm.Ftruncate(fd, int64(length))
|
|
|
|
data, err := mmap.Open(fd, 0, length, mmap.READ|mmap.WRITE)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer mmap.Munmap(data)
|
2020-11-28 17:26:07 +08:00
|
|
|
context := []byte("Hello World")
|
|
|
|
copy(data, context)
|
|
|
|
fmt.Println(string(data[:11]))
|
|
|
|
time.Sleep(time.Second * 10)
|
2020-11-26 19:44:45 +08:00
|
|
|
}
|
2020-11-28 17:26:07 +08:00
|
|
|
```
|
|
|
|
**Reader**
|
|
|
|
```go
|
|
|
|
package main
|
2020-11-26 19:44:45 +08:00
|
|
|
|
2020-11-28 17:26:07 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/hslam/mmap"
|
|
|
|
"github.com/hslam/shm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
name := "shared"
|
|
|
|
fd, err := shm.Open(name, shm.O_RDONLY, 0600)
|
2020-11-26 19:44:45 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-11-27 13:11:57 +08:00
|
|
|
defer shm.Close(fd)
|
2020-11-26 19:44:45 +08:00
|
|
|
data, err := mmap.Open(fd, 0, 128, mmap.READ)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer mmap.Munmap(data)
|
|
|
|
fmt.Println(string(data[:11]))
|
|
|
|
}
|
|
|
|
```
|
|
|
|
#### Output
|
|
|
|
```
|
|
|
|
Hello World
|
|
|
|
```
|
|
|
|
|
|
|
|
### License
|
|
|
|
This package is licensed under a MIT license (Copyright (c) 2020 Meng Huang)
|
|
|
|
|
|
|
|
|
|
|
|
### Author
|
|
|
|
shm was written by Meng Huang.
|
|
|
|
|
|
|
|
|