Funzione Perl shmget

Descrizione

Questa funzione restituisce l'ID del segmento di memoria condivisa per il segmento corrispondente a KEY. Viene creato un nuovo segmento di memoria condivisa di almeno SIZE byte, a condizione che KEY non abbia già un segmento associato o che KEY sia uguale alla costante IPC_PRIVATE.

Sintassi

Di seguito è riportata la semplice sintassi per questa funzione:

shmget KEY, SIZE, FLAGS

shmget KEY

Valore di ritorno

Questa funzione restituisce undef in caso di errore e ID memoria condivisa in caso di successo.

Esempio

Di seguito è riportato il codice di esempio che mostra il suo utilizzo di base:

#!/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: $! ";

Scrivete un programma di lettura che recuperi il segmento di memoria corrispondente a $ key e ne legga il contenuto usando 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;

Ora esegui prima il programma writer.pl in background e poi reader.pl, quindi produrrà il seguente risultato.

$perl writer.pl&
$perl reader.pl

Pennyfrathingale