1 ################################################################################
3 # Version 2.x, Copyright (C) 2007-2013, Marcus Holland-Moritz <mhx@cpan.org>.
4 # Version 1.x, Copyright (C) 1997, Graham Barr <gbarr@pobox.com>.
6 # This program is free software; you can redistribute it and/or
7 # modify it under the same terms as Perl itself.
9 ################################################################################
11 package IPC::SharedMem;
13 use IPC::SysV qw(IPC_STAT IPC_RMID shmat shmdt memread memwrite);
15 use vars qw($VERSION);
20 # Figure out if we have support for native sized types
21 my $N = do { my $foo = eval { pack "L!", 0 }; $@ ? '' : '!' };
24 package IPC::SharedMem::stat;
26 use Class::Struct qw(struct);
28 struct 'IPC::SharedMem::stat' => [
46 @_ == 4 or croak 'IPC::SharedMem->new(KEY, SIZE, FLAGS)';
47 my($class, $key, $size, $flags) = @_;
49 my $id = shmget $key, $size, $flags;
51 return undef unless defined $id;
53 bless { _id => $id, _addr => undef, _isrm => 0 }, $class
72 shmctl $self->id, IPC_STAT, $data or return undef;
73 IPC::SharedMem::stat->new->unpack($data);
78 @_ >= 1 && @_ <= 2 or croak '$shm->attach([FLAG])';
79 my($self, $flag) = @_;
80 defined $self->addr and return undef;
81 $self->{_addr} = shmat($self->id, undef, $flag || 0);
88 defined $self->addr or return undef;
89 my $rv = defined shmdt($self->addr);
90 undef $self->{_addr} if $rv;
97 return undef if $self->is_removed;
98 my $rv = shmctl $self->id, IPC_RMID, 0;
99 $self->{_isrm} = 1 if $rv;
111 @_ == 3 or croak '$shm->read(POS, SIZE)';
112 my($self, $pos, $size) = @_;
114 if (defined $self->addr) {
115 memread($self->addr, $buf, $pos, $size) or return undef;
118 shmread($self->id, $buf, $pos, $size) or return undef;
125 @_ == 4 or croak '$shm->write(STRING, POS, SIZE)';
126 my($self, $str, $pos, $size) = @_;
127 if (defined $self->addr) {
128 return memwrite($self->addr, $str, $pos, $size);
131 return shmwrite($self->id, $str, $pos, $size);
141 IPC::SharedMem - SysV Shared Memory IPC object class
145 use IPC::SysV qw(IPC_PRIVATE S_IRUSR S_IWUSR);
148 $shm = IPC::SharedMem->new(IPC_PRIVATE, 8, S_IRWXU);
150 $shm->write(pack("S", 4711), 2, 2);
152 $data = $shm->read(0, 2);
160 A class providing an object based interface to SysV IPC shared memory.
166 =item new ( KEY , SIZE , FLAGS )
168 Creates a new shared memory segment of C<SIZE> bytes size associated
169 with C<KEY>. A new segment is created if
175 C<KEY> is equal to C<IPC_PRIVATE>
179 C<KEY> does not already have a shared memory segment associated
180 with it, and C<I<FLAGS> & IPC_CREAT> is true.
184 On creation of a new shared memory segment C<FLAGS> is used to
185 set the permissions. Be careful not to set any flags that the
186 Sys V IPC implementation does not allow: in some systems setting
187 execute bits makes the operations fail.
191 Returns the shared memory identifier.
193 =item read ( POS, SIZE )
195 Read C<SIZE> bytes from the shared memory segment at C<POS>. Returns
196 the string read, or C<undef> if there was an error. The return value
197 becomes tainted. See L<shmread>.
199 =item write ( STRING, POS, SIZE )
201 Write C<SIZE> bytes to the shared memory segment at C<POS>. Returns
202 true if successful, or false if there is an error. See L<shmwrite>.
206 Remove the shared memory segment from the system or mark it as
207 removed as long as any processes are still attached to it.
211 Returns true if the shared memory segment has been removed or
216 Returns an object of type C<IPC::SharedMem::stat> which is a sub-class
217 of C<Class::Struct>. It provides the following fields. For a description
218 of these fields see you system documentation.
233 =item attach ( [FLAG] )
235 Permanently attach to the shared memory segment. When a C<IPC::SharedMem>
236 object is attached, it will use L<memread> and L<memwrite> instead of
237 L<shmread> and L<shmwrite> for accessing the shared memory segment.
238 Returns true if successful, or false on error. See L<shmat(2)>.
242 Detach from the shared memory segment that previously has been attached
243 to. Returns true if successful, or false on error. See L<shmdt(2)>.
247 Returns the address of the shared memory that has been attached to in a
248 format suitable for use with C<pack('P')>. Returns C<undef> if the shared
249 memory has not been attached.
255 L<IPC::SysV>, L<Class::Struct>
259 Marcus Holland-Moritz <mhx@cpan.org>
263 Version 2.x, Copyright (C) 2007-2013, Marcus Holland-Moritz.
265 Version 1.x, Copyright (c) 1997, Graham Barr.
267 This program is free software; you can redistribute it and/or
268 modify it under the same terms as Perl itself.