balt has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use strict; use IPC::SysV qw(IPC_PRIVATE S_IRUSR S_IWUSR S_IRWXU IPC_CREAT IPC_EXC +L); use IPC::SharedMem; use Storable qw(freeze thaw); my %somehash; $somehash{'one'}{'count'} = 1; $somehash{'one'}{'string'} = "This is a big ol' string"; use constant { STOP => 0, RUN => 1 }; my $shm_mastercontrol = IPC::SharedMem->new(IPC_PRIVATE, 8, S_IRWXU); my $shm_somehash = IPC::SharedMem->new(IPC_PRIVATE, 1024, S_IRWXU); $shm_mastercontrol->write(pack("S", RUN), 0, 2); # forks processes my $thread_PID = fork(); if (not defined $thread_PID) { die "Insufficient resources to fork Runner!\n"; } elsif ($thread_PID == 0) { &thread(); } sub thread() { #my $string; while () { # increment the somehash counter: $somehash{'one'}{'count'}++; print "thread sees: $somehash{'one'}{'count'}\n"; my $serialized = freeze \%somehash; $shm_somehash->write(pack("A*", $serialized), 0, 1024); last if ( unpack("S", $shm_mastercontrol->read(0, 2)) == STOP); select(undef, undef, undef, 1.0); } exit(0); } while () { my $unpacked = unpack("A*", $shm_somehash->read(0, 1024)); # put somehash back from shared memory: %somehash = %{thaw($unpacked)}; print "main sees: $somehash{'one'}{'count'}\n"; print "press some key, q to quit:\n"; chomp (my $key = <STDIN>); if ( $key eq 'q' ) { $shm_mastercontrol->write(pack("S", STOP), 0, 2); waitpid($thread_PID, 0); last; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: can't thaw a hash: Magic number error.
by BrowserUk (Patriarch) on Nov 03, 2015 at 11:04 UTC | |
by balt (Novice) on Nov 03, 2015 at 11:22 UTC | |
by BrowserUk (Patriarch) on Nov 03, 2015 at 11:44 UTC | |
by balt (Novice) on Nov 03, 2015 at 23:43 UTC |