ipc

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 16, 2021 License: MIT Imports: 5 Imported by: 0

README

ipc

PkgGoDev Build Status codecov Go Report Card LICENSE

Package ipc provides a way to use System V IPC. System V IPC includes three interprocess communication mechanisms that are widely available on UNIX systems: message queues, semaphore, and shared memory.

Features

Get started

Install
go get github.com/hslam/ipc
Import
import "github.com/hslam/ipc"
Usage
Example
package main

import (
	"encoding/binary"
	"flag"
	"fmt"
	"github.com/hslam/ipc"
	"os"
	"os/signal"
	"syscall"
)

var send = flag.Bool("s", true, "send")

func main() {
	flag.Parse()
	key, _ := ipc.Ftok("/tmp", 0x22)
	semnum := 0
	semid, err := ipc.Semget(key, 1, 0666)
	if err != nil {
		semid, err = ipc.Semget(key, 1, ipc.IPC_CREAT|ipc.IPC_EXCL|0666)
		if err != nil {
			panic(err)
		}
		_, err := ipc.Semsetvalue(semid, semnum, 1)
		if err != nil {
			panic(err)
		}
	}
	defer ipc.Semrm(semid)
	shmid, data, _ := ipc.Shmgetattach(key, 128, ipc.IPC_CREAT|0600)
	defer ipc.Shmrm(shmid)
	defer ipc.Shmdetach(data)
	msgid, _ := ipc.Msgget(key, ipc.IPC_CREAT|0600)
	defer ipc.Msgrm(msgid)
	var text string
	quit := make(chan os.Signal)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	go func() {
		defer close(quit)
		if *send {
			fmt.Println("Enter:")
			buf := make([]byte, 10)
			for {
				fmt.Scanln(&text)
				if _, err := ipc.Semp(semid, semnum, ipc.SEM_UNDO); err != nil {
					return
				}
				copy(data, text)
				if _, err := ipc.Semv(semid, semnum, ipc.SEM_UNDO); err != nil {
					return
				}
				n := binary.PutUvarint(buf, uint64(len(text)))
				if err := ipc.Msgsend(msgid, 1, buf[:n], 0600); err != nil {
					return
				}
			}
		} else {
			fmt.Println("Recv:")
			for {
				m, err := ipc.Msgreceive(msgid, 1, 0600)
				if err != nil {
					return
				}
				length, _ := binary.Uvarint(m)
				if _, err := ipc.Semp(semid, semnum, ipc.SEM_UNDO); err != nil {
					return
				}
				text = string(data[:length])
				if _, err := ipc.Semv(semid, semnum, ipc.SEM_UNDO); err != nil {
					return
				}
				fmt.Println(text)
			}
		}
	}()
	<-quit
}
Output

Enter a word.

$ go run main.go -s=true
Enter:
HelloWorld

In another terminal receive this word.

$ go run main.go -s=false
Recv:
HelloWorld
License

This package is licensed under a MIT license (Copyright (c) 2020 Meng Huang)

Author

ipc was written by Meng Huang.

Documentation

Overview

Package ipc provides a way to use System V IPC. System V IPC includes three interprocess communication mechanisms that are widely available on UNIX systems: message queues, semaphore, and shared memory.

Index

Constants

View Source
const (
	// IPC_CREAT creates if key is nonexistent
	IPC_CREAT = 01000

	// IPC_EXCL fails if key exists.
	IPC_EXCL = 02000

	// IPC_NOWAIT returns error no wait.
	IPC_NOWAIT = 04000

	// IPC_PRIVATE is private key
	IPC_PRIVATE = 00000

	// SEM_UNDO sets up adjust on exit entry
	SEM_UNDO = 010000

	// IPC_RMID removes identifier
	IPC_RMID = 0
	// IPC_SET sets ipc_perm options.
	IPC_SET = 1
	// IPC_STAT gets ipc_perm options.
	IPC_STAT = 2
)

Variables

This section is empty.

Functions

func Ftok

func Ftok(pathname string, projectid uint8) (int, error)

Ftok uses the given pathname (which must refer to an existing, accessible file) and the least significant 8 bits of proj_id (which must be nonzero) to generate a key_t type System V IPC key.

func Msgget

func Msgget(key int, msgflg int) (int, error)

Msgget calls the msgget system call.

func Msgrcv

func Msgrcv(msgid int, msgp uintptr, msgsz int, msgtyp uint, msgflg int) (int, error)

Msgrcv calls the msgrcv system call.

func Msgreceive

func Msgreceive(msgid int, msgType uint, flags int) ([]byte, error)

Msgreceive calls the msgrcv system call.

func Msgrm

func Msgrm(msgid int) error

Msgrm removes the shm with the given id.

func Msgsend

func Msgsend(msgid int, msgType uint, msgText []byte, flags int) error

Msgsend calls the msgsnd system call.

func Msgsnd

func Msgsnd(msgid int, msgp uintptr, msgsz int, msgflg int) error

Msgsnd calls the msgsnd system call.

func Semget

func Semget(key int, nsems int, semflg int) (int, error)

Semget calls the semget system call.

The semget() system call returns the System V semaphore set identifier associated with the argument key.

A new set of nsems semaphores is created if key has the value IPC_PRIVATE or if no existing semaphore set is associated with key and IPC_CREAT is specified in semflg.

If semflg specifies both IPC_CREAT and IPC_EXCL and a semaphore set already exists for key, then semget() fails with errno set to EEXIST.

The argument nsems can be 0 (a don't care) when a semaphore set is not being created. Otherwise, nsems must be greater than 0 and less than or equal to the maximum number of semaphores per semaphore set.

If successful, the return value will be the semaphore set identifier, otherwise, -1 is returned, with errno indicating the error.

func Semgetvalue

func Semgetvalue(semid int, semnum int) (int, error)

Semgetvalue calls the semctl GETVAL system call.

func Semop

func Semop(semid int, sops uintptr, nsops int) (bool, error)

Semop calls the semop system call.

semop() performs operations on selected semaphores in the set indi‐ cated by semid. Each of the nsops elements in the array pointed to by sops is a structure that specifies an operation to be performed on a single semaphore. The elements of this structure are of type struct sembuf, containing the following members:

unsigned short sem_num; /* semaphore number */ short sem_op; /* semaphore operation */ short sem_flg; /* operation flags */

Flags recognized in sem_flg are IPC_NOWAIT and SEM_UNDO. If an operation specifies SEM_UNDO, it will be automatically undone when the process terminates.

The set of operations contained in sops is performed in array order, and atomically, that is, the operations are performed either as a complete unit, or not at all. The behavior of the system call if not all operations can be performed immediately depends on the presence of the IPC_NOWAIT flag in the individual sem_flg fields, as noted be‐ low.

func Semoperate

func Semoperate(semid int, sops []Sembuf) (bool, error)

Semoperate calls the semop system call. Flags recognized in SemFlg are IPC_NOWAIT and SEM_UNDO. If an operation specifies SEM_UNDO, it will be automatically undone when the process terminates.

func Semp

func Semp(semid int, semnum int, semflg int) (bool, error)

Semp calls the semop P system call. Flags recognized in semflg are IPC_NOWAIT and SEM_UNDO. If an operation specifies SEM_UNDO, it will be automatically undone when the process terminates.

func Semrm

func Semrm(semid int) error

Semrm removes the semaphore with the given id.

func Semsetvalue

func Semsetvalue(semid int, semnum int, semun int) (bool, error)

Semsetvalue calls the semctl SETVAL system call.

func Semv

func Semv(semid int, semnum int, semflg int) (bool, error)

Semv calls the semop V system call. Flags recognized in semflg are IPC_NOWAIT and SEM_UNDO. If an operation specifies SEM_UNDO, it will be automatically undone when the process terminates.

func Shmat

func Shmat(shmid int, shmFlg int) (uintptr, error)

Shmat calls the shmat system call.

func Shmdetach

func Shmdetach(b []byte) error

Shmdetach calls the shmdt system call with []byte b.

func Shmdt

func Shmdt(addr uintptr) error

Shmdt calls the shmdt system call.

func Shmget

func Shmget(key int, size int, shmFlg int) (int, error)

Shmget calls the shmget system call.

func Shmgetattach

func Shmgetattach(key int, size int, shmFlg int) (int, []byte, error)

Shmgetattach calls the shmget and shmat system call.

func Shmrm

func Shmrm(shmid int) error

Shmrm removes the shm with the given id.

Types

type Sembuf

type Sembuf struct {
	SemNum uint16
	SemOp  int16
	SemFlg int16
}

Sembuf represents an operation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL