- Perl Basics
- Perl - Home
- Perl - Introduction
- Perl - Environment
- Perl - Syntax Overview
- Perl - Data Types
- Perl - Variables
- Perl - Scalars
- Perl - Arrays
- Perl - Hashes
- Perl - IF...ELSE
- Perl - Loops
- Perl - Operators
- Perl - Date & Time
- Perl - Subroutines
- Perl - References
- Perl - Formats
- Perl - File I/O
- Perl - Directories
- Perl - Error Handling
- Perl - Special Variables
- Perl - Coding Standard
- Perl - Regular Expressions
- Perl - Sending Email
- Perl Advanced
- Perl - Socket Programming
- Perl - Object Oriented
- Perl - Database Access
- Perl - CGI Programming
- Perl - Packages & Modules
- Perl - Process Management
- Perl - Embedded Documentation
- Perl - Functions References
- Perl Useful Resources
- Perl - Questions and Answers
- Perl - Quick Guide
- Perl - Useful Resources
- Perl - Discussion
Perl shmget Function
Description
This function returns the shared memory segment ID for the segment matching KEY. A new shared memory segment is created of at least SIZE bytes, providing that either KEY does not already have a segment associated with it or that KEY is equal to the constant IPC_PRIVATE.
Syntax
Following is the simple syntax for this function −
shmget KEY, SIZE, FLAGS shmget KEY
Return Value
This function returns undef on failure and Shared memory ID on success.
Example
Following is the example code showing its basic usage −
#!/usr/bin/perl # Assume this file name is writer.pl use IPC::SysV; #use these next two lines if the previous use fails. eval 'sub IPC_CREAT {0001000}' unless defined &IPC_CREAT; eval 'sub IPC_RMID {0}' unless defined &IPC_RMID; $key = 12345; $size = 80; $message = "Pennyfarthingale."; # Create the shared memory segment $id = shmget($key, $size, &IPC_CREAT | 0777 ) or die "Can't shmget: $!"; # Place a string in itl shmwrite( $id, $message, 0, 80 ) or die "Can't shmwrite: $!"; sleep 20; # Delete it; shmctl( $id, &IPC_RMID, 0 ) or die "Can't shmctl: $! ";
Write a reader program which retrieves the memory segment corresponding to $key and reads its contents using shmread();.
#!/usr/bin/perl # Assume this file name is reader.pl $key = 12345; $size = 80; # Identify the shared memory segment $id = shmget( $key, $size, 0777 ) or die "Can't shmget: $!"; # Read its contents itno a string shmread($id, $var, 0, $size) or die "Can't shmread: $!"; print $var;
Now First run writer.pl program in background and then reader.pl then it will produces the following result.
$perl writer.pl& $perl reader.pl Pennyfrathingale
perl_function_references.htm
Advertisements