From 0ade19845bc827615a636e5c073d498c2244ec07 Mon Sep 17 00:00:00 2001 From: Jarkko Hietaniemi Date: Tue, 7 Jul 1998 05:32:53 +0300 Subject: add extension to support SysV IPC Message-Id: <199807062332.CAA25792@alpha.hut.fi> Subject: [PATCH] 5.004_70: IPC::SysV p4raw-id: //depot/perl@1372 --- pod/perlipc.pod | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) (limited to 'pod/perlipc.pod') diff --git a/pod/perlipc.pod b/pod/perlipc.pod index 2348d396c0..59c5ad9f01 100644 --- a/pod/perlipc.pod +++ b/pod/perlipc.pod @@ -1306,29 +1306,33 @@ you weren't wanting it to. Here's a small example showing shared memory usage. - $IPC_PRIVATE = 0; - $IPC_RMID = 0; + use IPC::SysV qw(IPC_PRIVATE IPC_RMID S_IRWXU S_IRWXG S_IRWXO); + $size = 2000; - $key = shmget($IPC_PRIVATE, $size , 0777 ); - die unless defined $key; + $key = shmget(IPC_PRIVATE, $size, S_IRWXU|S_IRWXG|S_IRWXO) || die "$!"; + print "shm key $key\n"; $message = "Message #1"; - shmwrite($key, $message, 0, 60 ) || die "$!"; - shmread($key,$buff,0,60) || die "$!"; + shmwrite($key, $message, 0, 60) || die "$!"; + print "wrote: '$message'\n"; + shmread($key, $buff, 0, 60) || die "$!"; + print "read : '$buff'\n"; - print $buff,"\n"; + # the buffer of shmread is zero-character end-padded. + substr($buff, index($buff, "\0")) = ''; + print "un" unless $buff eq $message; + print "swell\n"; - print "deleting $key\n"; - shmctl($key ,$IPC_RMID, 0) || die "$!"; + print "deleting shm $key\n"; + shmctl($key, IPC_RMID, 0) || die "$!"; Here's an example of a semaphore: + use IPC::SysV qw(IPC_CREAT); + $IPC_KEY = 1234; - $IPC_RMID = 0; - $IPC_CREATE = 0001000; - $key = semget($IPC_KEY, $nsems , 0666 | $IPC_CREATE ); - die if !defined($key); - print "$key\n"; + $key = semget($IPC_KEY, 10, 0666 | IPC_CREAT ) || die "$!"; + print "shm key $key\n"; Put this code in a separate file to be run in more than one process. Call the file F: @@ -1375,9 +1379,8 @@ Call this file F: semop($key,$opstring) || die "$!"; The SysV IPC code above was written long ago, and it's definitely -clunky looking. It should at the very least be made to C -and C. Better yet, check out the IPC::SysV modules -on CPAN. +clunky looking. For a more modern look, see the IPC::SysV module +which is included with Perl starting from Perl 5.005. =head1 NOTES -- cgit v1.2.1