Coder Social home page Coder Social logo

rustatian / ipc Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 1.0 151 KB

Linux, Unix, and Windows implementation of SysV5 shared memory and semaphores.

License: GNU General Public License v3.0

Go 100.00%
shared-memory-communication semaphore segment interprocess

ipc's Introduction

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).

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:

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.

  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:

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:

// 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:

seg2, err := NewSharedMemorySegment(0x1, 1024, 0, SHM_RDONLY)
if err != nil {
	t.Fatal(err)
}

Read specified amount of data and detach from the segment:

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)
}

ipc's People

Contributors

rustatian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

pkafma-aon

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.