perl IPC::Shareable 共享内存

server.pl

#!/usr/bin/perl

use strict;
use IPC::Shareable;

my $key = 'data';
my %options = (
	create => 1,
	exclusive => 1,
	mode => 0644,
	destroy => 1,
);

my %colors;
tie %colors, 'IPC::Shareable', $key, { %options }
	or die "Server: tied failed";

%colors = (
	red => [
		'fire truck',
		'leaves in the fall',
		],
	blue => [
		'sky',
		'police cars',
		],
);

((print "Server: there are 2 colors\n"), sleep 2) while scalar keys %colors == 2;
print "Server: here are all my colors:\n";
foreach my $c (keys %colors){
	print "Server: these are $c: ",
		join(', ', @{$colors{$c}}), "\n";
}
exit;

 client.pl

#!/usr/bin/perl -w
#
use strict;

use IPC::Shareable;

my $key = 'data';

my %options = (
	create => 0,
	exclusive => 0,
	mode => 0644,
	destory => 0,
);

my %colors;

tie %colors, "IPC::Shareable", $key, { %options } or 
	die "Client: tied failed\n";

foreach my $c (keys %colors){
	print "Client: these are $c: ",
		join(', ', @{$colors{$c}}), "\n";
}

delete $colors{'red'};
exit;

 

上一篇:Android颜色配置文件?


下一篇:PHP GIF / PNG True Colorize滤镜,可保留亮度和Alpha