Go to file
hslam 548f24153e add go mod 2021-01-17 02:58:26 +08:00
.github/workflows migrate from travis-ci to actions 2020-12-18 13:10:26 +08:00
.gitignore Initial commit 2020-11-26 19:32:43 +08:00
LICENSE update LICENSE 2020-11-26 19:35:19 +08:00
README.md migrate from travis-ci to actions 2020-12-18 13:10:26 +08:00
go.mod add go mod 2021-01-17 02:58:26 +08:00
go.sum add go mod 2021-01-17 02:58:26 +08:00
shm.go godoc 2020-11-28 17:25:57 +08:00
shm_bsd.go fix linux bug 2020-11-26 22:51:56 +08:00
shm_freebsd.go fix linux bug 2020-11-26 22:51:56 +08:00
shm_linux.go fix linux bug 2020-11-26 22:51:56 +08:00
shm_unix.go update methods 2020-12-01 16:05:01 +08:00
shm_unix_test.go update methods 2020-12-01 16:05:01 +08:00
shm_windows.go update shm 2020-11-30 18:04:30 +08:00
shm_windows_test.go update test 2020-11-28 17:25:41 +08:00

README.md

shm

PkgGoDev Build Status Go Report Card LICENSE

Package shm provides a way to use System V shared memory.

Get started

Install

go get github.com/hslam/shm

Import

import "github.com/hslam/shm"

Usage

SHM GET Example

Writer

package main

import (
	"fmt"
	"github.com/hslam/ftok"
	"github.com/hslam/shm"
	"time"
)

func main() {
	key, err := ftok.Ftok("/tmp", 0x22)
	if err != nil {
		panic(err)
	}
	shmid, data, err := shm.GetAttach(key, 128, shm.IPC_CREAT|0600)
	if err != nil {
		panic(err)
	}
	defer shm.Remove(shmid)
	defer shm.Detach(data)
	context := []byte("Hello World")
	copy(data, context)
	fmt.Println(string(data[:11]))
	time.Sleep(time.Second * 10)
}

Reader

package main

import (
	"fmt"
	"github.com/hslam/ftok"
	"github.com/hslam/shm"
)

func main() {
	key, err := ftok.Ftok("/tmp", 0x22)
	if err != nil {
		panic(err)
	}
	_, data, err := shm.GetAttach(key, 128, 0600)
	if err != nil {
		panic(err)
	}
	defer shm.Detach(data)
	fmt.Println(string(data[:11]))
}

Output

Hello World

SHM OPEN Example

Writer

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)
	}
	defer shm.Unlink(name)
	defer shm.Close(fd)
	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)
	context := []byte("Hello World")
	copy(data, context)
	fmt.Println(string(data[:11]))
	time.Sleep(time.Second * 10)
}

Reader

package main

import (
	"fmt"
	"github.com/hslam/mmap"
	"github.com/hslam/shm"
)

func main() {
	name := "shared"
	fd, err := shm.Open(name, shm.O_RDONLY, 0600)
	if err != nil {
		panic(err)
	}
	defer shm.Close(fd)
	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.