Perl shmread 函式



描述

此函式將共享記憶體段 ID 複製到標量 VAR,位置為 POS,最多為 SIZE 個位元組。

語法

以下是此函式的簡單語法 −

shmread ID, VAR, POS, SIZE

返回值

此函式在失敗時返回 0,在成功時返回 1。

示例

以下是顯示其基本用法示例的程式碼 −

#!/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

$key = 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, &OPC_RMID, 0 ) or die "Can't shmctl: $! ";

編寫一個讀取器程式,它檢索與 $key 對應的記憶體段,並使用 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;

現在首先在後臺執行 writer.pl 程式,然後執行 reader.pl,它會產生以下結果。

$perl writer.pl&
$perl reader.pl

Pennyfrathingale
perl_function_references.htm
廣告
© . All rights reserved.