Module: SysVIPC

Included in:
MessageQueue, Semaphore, Sembuf, SharedMemory, Shmaddr
Defined in:
lib/SysVIPC.rb

Overview

Document-class: SysVIPC

SysVIPC

Ruby module for System V Inter-Process Communication: message queues, semaphores, and shared memory.

Hosted as project sysvipc on RubyForge.

Copyright © 2001, 2006, 2007 Daiki Ueno Copyright © 2006-2013 James Steven Jenkins

Usage Synopsis

Common Code

All programs using this module must include

require 'SysVIPC'

It may be convenient to add

include SysVIPC

All IPC objects are identified by a key. SysVIPC includes a convenience function for mapping file names and integer IDs into a key:

key = ftok('/a/file/that/must/exist', 0)

Message Queues

Get (create if necessary) a message queue:

mq = MessageQueue.new(key, IPC_CREAT | 0600)

Send a message of type 0:

mq.send(0, 'message')

Receive up to 100 bytes from the first message of type 0:

msg = mq.receive(0, 100)

Semaphores

Get (create if necessary) a set of 5 semaphores:

sm = Semaphore.new(key, 5, IPC_CREAT | 0600)

Initialize semaphores if newly created:

sm.setall(Array.new(5, 1)) if sm.pid(0) == 0

Acquire semaphore 2 (waiting if necessary):

sm.op([Sembuf.new(2, -1)])

Release semaphore 2:

sm.op([Sembuf.new(2, 1)])

Shared Memory

Get (create if necessary) an 8192-byte shared memory region:

sh = SharedMemory.new(key, 8192, IPC_CREAT | 0660)

Attach shared memory:

shmaddr = sh.attach

Write data:

shmaddr.write('testing')

Read 100 bytes of data:

data = shmaddr.read(100);

Detach shared memory:

sh.detach(shmaddr)

Installation

  1. ruby setup.rb config

  2. ruby setup.rb setup

  3. ruby setup.rb install (requires appropriate privilege)

Testing

  1. ./test_sysvipc_l (low-level interface)

  2. ./test_sysvipc_h (high-level interface)

Defined Under Namespace

Classes: MessageQueue, Semaphore, Sembuf, SharedMemory, Shmaddr

Instance Method Summary collapse

Instance Method Details

#check_result(res) ⇒ Object

:nodoc:

Raises:

  • (SystemCallError.new(SysVIPC.errno))


100
101
102
# File 'lib/SysVIPC.rb', line 100

def check_result(res)                                      # :nodoc:
  raise SystemCallError.new(SysVIPC.errno), nil, caller if res == -1
end