summaryrefslogtreecommitdiffstats
path: root/contrib/perl5/eg/sysvipc/ipcmsg
blob: 646d8b6aed622c81dd9347ed5edf981f334b0e21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
	if $running_under_some_shell;

require 'sys/ipc.ph';
require 'sys/msg.ph';

$| = 1;

$mode = shift;
die "usage: ipcmsg {r|s}\n" unless $mode =~ /^[rs]$/;
$send = ($mode eq "s");

$id = msgget(0x1234, ($send ? 0 : &IPC_CREAT) | 0644);
die "Can't get message queue: $!\n" unless defined($id);
print "message queue id: $id\n";

if ($send) {
	while (<STDIN>) {
		chop;
		unless (msgsnd($id, pack("LA*", $., $_), 0)) {
			die "Can't send message: $!\n";
		}
	}
}
else {
	$SIG{'INT'} = $SIG{'QUIT'} = "leave";
	for (;;) {
		unless (msgrcv($id, $_, 512, 0, 0)) {
			die "Can't receive message: $!\n";
		}
		($type, $message) = unpack("La*", $_);
		printf "[%d] %s\n", $type, $message;
	}
}

&leave;

sub leave {
	if (!$send) {
		$x = msgctl($id, &IPC_RMID, 0);
		if (!defined($x) || $x < 0) {
			die "Can't remove message queue: $!\n";
		}
	}
	exit;
}
OpenPOWER on IntegriCloud