System V IPC#
System V IPC gives two unrelated processes a shared resource the kernel owns: a semaphore set for coordination, or a shared memory segment for passing bytes with no copying through a pipe. Each resource is identified by an integer key and survives until a process explicitly removes it.
The convenience modules IPC::SysV, IPC::Semaphore, and IPC::SharedMem are not available in PetaPerl, so the recipes here use the raw semget / shmget built-in family with the flag values written as plain numbers. Those built-ins are the layer the modules wrap; using them directly costs only a few named constants you supply yourself.
The flag constants you need#
A handful of small integers control how these calls behave. They are the standard Linux values:
use constant {
IPC_PRIVATE => 0, # "give me a fresh, private key"
IPC_CREAT => 0o1000, # create the resource if absent
IPC_EXCL => 0o2000, # fail if it already exists
IPC_RMID => 0, # control command: remove the resource
};
The low nine bits of the flag word are ordinary permission bits, the same 0600 / 0644 you would pass to chmod. IPC_CREAT | 0600 means “create it, owner read/write”.
A semaphore for coordination#
A semaphore set is a small array of counters the kernel guards. The classic use is a lock: a process decrements the counter to enter a critical section and increments it to leave. This recipe creates a one-element set, initialises it, and removes it:
use constant {
IPC_PRIVATE => 0,
IPC_CREAT => 0o1000,
IPC_RMID => 0,
SETVAL => 16, # semctl command: set one value
GETVAL => 12, # semctl command: read one value
};
# A set with one semaphore.
my $sem = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
defined $sem or die "semget: $!";
# Initialise semaphore 0 to the value 1 (one token available).
semctl($sem, 0, SETVAL, 1) or die "semctl SETVAL: $!";
my $value = semctl($sem, 0, GETVAL, 0);
print "semaphore value: $value\n"; # semaphore value: 1
# Remove the set when finished.
semctl($sem, 0, IPC_RMID, 0) or warn "semctl IPC_RMID: $!";
The wait/signal operations themselves go through semop, which takes a packed string of (sem_num, sem_op, sem_flg) triples built with pack. Decrementing by one waits until a token is free; incrementing by one releases it. As with shared memory, a semaphore set the kernel owns must be removed with IPC_RMID or it lingers - visible under ipcs -s.
Reference cross-links#
Next#
Shared memory and semaphores are the heavyweight channels. For the lighter, stream-oriented ones, see Subprocesses and co-processes and TCP sockets.