https://github.com/rustatian/ipc
Linux, Unix, and Windows implementation of SysV5 shared memory and semaphores.
https://github.com/rustatian/ipc
interprocess segment semaphore shared-memory-communication
Last synced: about 1 month ago
JSON representation
Linux, Unix, and Windows implementation of SysV5 shared memory and semaphores.
- Host: GitHub
- URL: https://github.com/rustatian/ipc
- Owner: rustatian
- License: gpl-3.0
- Created: 2020-09-10T21:54:44.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-04-07T09:23:32.000Z (about 1 month ago)
- Last Synced: 2025-04-11T16:32:41.918Z (about 1 month ago)
- Topics: interprocess, segment, semaphore, shared-memory-communication
- Language: Go
- Homepage:
- Size: 190 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Linux and Windows implementation of shared memory and semaphores
# How to use
## Semaphores (interprocess):Process1: Initialize a semaphore, and Add to semaphores set (semaphore 0 in example) value 1. And start doing some interprocess work (write to the shared memory for example).
```go
s, err := NewSemaphore(0x12334, 1, 0666, true, IPC_CREAT)
if err != nil {
panic(err)
}
err = s.Add(0)
if err != nil {
panic(err)
}
```
After work will be done, just unlock semaphore:
```go
err = s.Done(0) // 0 here is the 1-st semaphore in the set identified by semaphore ID.
if err != nil {
panic(err)
}
```
Process2: Attach to the same semaphore. And `Wait` until Process1 released semaphore.
```go
s, err := NewSemaphore(0x12334, 1, 0666, false, IPC_CREAT)
if err != nil {
panic(err)
}
err = s.Wait()
if err != nil {
panic(err)
}
```## Shared Memory (interprocess):
Initialize shared memory segment with a key, required size and flags:
```go
seg1, err := NewSharedMemorySegment(0x1, 1024, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, IPC_CREAT)
if err != nil {
t.Fatal(err)
}
```Write the specified amount of data and detach from the segment:
```go
// write data to the shared memory
// testData is less or equal to 1024 specified in prev declaration
seg1.Write([]byte(testData))
err = seg1.Detach()
if err != nil {
t.Fatal(err)
}
```From the another process, initialize shared memory segment with the same key, size, but with ReadOnly flag:
```go
seg2, err := NewSharedMemorySegment(0x1, 1024, 0, SHM_RDONLY)
if err != nil {
t.Fatal(err)
}
```Read specified amount of data and detach from the segment:
```go
buf := make([]byte, len(testData), len(testData))
err = seg2.Read(buf)
if err != nil {
t.Fatal(err)
}
err = seg2.Detach()
if err != nil {
t.Fatal(err)
}```