From b21fd44a405a479d18e16627668ee95a1580cd16 Mon Sep 17 00:00:00 2001 From: hslam <791874158@qq.com> Date: Sun, 29 Nov 2020 04:42:18 +0800 Subject: [PATCH] add ipc --- ftok.go | 15 +++++++++++++++ ipc.go | 28 ++++++++++++++++++++++++++++ msg.go | 28 ++++++++++++++++++++++++++++ sem.go | 33 +++++++++++++++++++++++++++++++++ shm.go | 23 +++++++++++++++++++++++ 5 files changed, 127 insertions(+) create mode 100644 ftok.go create mode 100644 ipc.go create mode 100644 msg.go create mode 100644 sem.go create mode 100644 shm.go diff --git a/ftok.go b/ftok.go new file mode 100644 index 0000000..b1d5d33 --- /dev/null +++ b/ftok.go @@ -0,0 +1,15 @@ +// Copyright (c) 2020 Meng Huang (mhboy@outlook.com) +// This package is licensed under a MIT license that can be found in the LICENSE file. + +package ipc + +import ( + "github.com/hslam/ftok" +) + +// 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 Ftok(pathname string, projectid uint8) (int, error) { + return ftok.Ftok(pathname, projectid) +} diff --git a/ipc.go b/ipc.go new file mode 100644 index 0000000..bba1930 --- /dev/null +++ b/ipc.go @@ -0,0 +1,28 @@ +// Copyright (c) 2020 Meng Huang (mhboy@outlook.com) +// This package is licensed under a MIT license that can be found in the LICENSE file. + +// 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. +package ipc + +const ( + // IPC_CREAT creates if key is nonexistent + IPC_CREAT = 00001000 + + // IPC_EXCL fails if key exists. + IPC_EXCL = 00002000 + + // IPC_NOWAIT returns error no wait. + IPC_NOWAIT = 04000 + + // 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 +) diff --git a/msg.go b/msg.go new file mode 100644 index 0000000..b723e3c --- /dev/null +++ b/msg.go @@ -0,0 +1,28 @@ +// Copyright (c) 2020 Meng Huang (mhboy@outlook.com) +// This package is licensed under a MIT license that can be found in the LICENSE file. + +package ipc + +import ( + "github.com/hslam/msg" +) + +// Msgget calls the msgget system call. +func Msgget(key int, msgflg int) (uintptr, error) { + return msg.Get(key, msgflg) +} + +// Msgsnd calls the msgsnd system call. +func Msgsnd(msgid uintptr, msgType uint, msgText []byte, flags uint) error { + return msg.Snd(msgid, msgType, msgText, flags) +} + +// Msgrcv calls the msgrcv system call. +func Msgrcv(msgid uintptr, msgType uint, flags uint) ([]byte, error) { + return msg.Rcv(msgid, msgType, flags) +} + +// Msgrm removes the shm with the given id. +func Msgrm(msgid uintptr) error { + return msg.Remove(msgid) +} diff --git a/sem.go b/sem.go new file mode 100644 index 0000000..afb1998 --- /dev/null +++ b/sem.go @@ -0,0 +1,33 @@ +// Copyright (c) 2020 Meng Huang (mhboy@outlook.com) +// This package is licensed under a MIT license that can be found in the LICENSE file. + +package ipc + +import ( + "github.com/hslam/sem" +) + +// Semget calls the semget system call. +func Semget(key int) (uintptr, error) { + return sem.Get(key) +} + +// Semp calls the semop P system call. +func Semp(semid uintptr, flg int16) (bool, error) { + return sem.P(semid, flg) +} + +// Semv calls the semop V system call. +func Semv(semid uintptr, flg int16) (bool, error) { + return sem.V(semid, flg) +} + +// Semgetvalue calls the semctl GETVAL system call. +func Semgetvalue(semid uintptr) (int, error) { + return sem.GetValue(semid) +} + +// Semrm removes the semaphore with the given id. +func Semrm(semid uintptr) error { + return sem.Remove(semid) +} diff --git a/shm.go b/shm.go new file mode 100644 index 0000000..6b064e4 --- /dev/null +++ b/shm.go @@ -0,0 +1,23 @@ +// Copyright (c) 2020 Meng Huang (mhboy@outlook.com) +// This package is licensed under a MIT license that can be found in the LICENSE file. + +package ipc + +import ( + "github.com/hslam/shm" +) + +// Shmgetat calls the shmget and shmat system call. +func Shmgetat(key int, size int, shmFlg int) (uintptr, []byte, error) { + return shm.GetAt(key, size, shmFlg) +} + +// Shmdt calls the shmdt system call. +func Shmdt(b []byte) error { + return shm.Dt(b) +} + +// Shmrm removes the shm with the given id. +func Shmrm(shmid uintptr) error { + return shm.Remove(shmid) +}