Integrate with Sarathy.
[p5sagit/p5-mst-13.2.git] / eg / sysvipc / ipcshm
CommitLineData
d9d8d8de 1#!/usr/bin/perl
2eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
5f05dabc 3 if $running_under_some_shell;
d9d8d8de 4
5require 'sys/ipc.ph';
6require 'sys/shm.ph';
7
8$| = 1;
9
10$mode = shift;
11die "usage: ipcshm {r|s}\n" unless $mode =~ /^[rs]$/;
12$send = ($mode eq "s");
13
14$SIZE = 32;
15$id = shmget(0x1234, $SIZE, ($send ? 0 : &IPC_CREAT) | 0644);
e5d73d77 16die "Can't get shared memory: $!\n" unless defined($id);
17print "shared memory id: $id\n";
d9d8d8de 18
19if ($send) {
20 while (<STDIN>) {
21 chop;
22 unless (shmwrite($id, pack("La*", length($_), $_), 0, $SIZE)) {
23 die "Can't write to shared memory: $!\n";
24 }
25 }
26}
27else {
28 $SIG{'INT'} = $SIG{'QUIT'} = "leave";
29 for (;;) {
30 $_ = <STDIN>;
31 unless (shmread($id, $_, 0, $SIZE)) {
32 die "Can't read shared memory: $!\n";
33 }
34 $len = unpack("L", $_);
35 $message = substr($_, length(pack("L",0)), $len);
36 printf "[%d] %s\n", $len, $message;
37 }
38}
39
40&leave;
41
42sub leave {
43 if (!$send) {
44 $x = shmctl($id, &IPC_RMID, 0);
45 if (!defined($x) || $x < 0) {
46 die "Can't remove shared memory: $!\n";
47 }
48 }
49 exit;
50}