Skip to content
/ IPC Public

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

License

Notifications You must be signed in to change notification settings

rustatian/IPC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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