Module: SysVIPC
- Defined in:
- lib/sysvipc.rb,
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, 2007, 2009 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(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
-
ruby setup.rb config
-
ruby setup.rb setup
-
ruby setup.rb install
(requires appropriate privilege)
Testing
-
./test_sysvipc_l
(low-level interface) -
./test_sysvipc_h
(high-level interface)
Defined Under Namespace
Classes: MessageQueue, Semaphore, Sembuf, SharedMemory, Shmaddr
Constant Summary collapse
- VERSION =
'0.1.0'
Instance Method Summary collapse
-
#check_result(res) ⇒ Object
:nodoc:.
Instance Method Details
#check_result(res) ⇒ Object
:nodoc:
107 108 109 |
# File 'lib/sysvipc.rb', line 107 def check_result(res) # :nodoc: raise SystemCallError.new(SysVIPC.errno), nil, caller if res == -1 end |