当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Perl shmwrite用法及代码示例



描述

此函数将 STRING 从 SIZE 字节的位置 POS 写入 ID 指定的共享内存段。 SIZE 大于 STRING 的长度。 shmwrite 附加空字节以填充到 SIZE 字节。

用法

以下是此函数的简单语法 âˆ'

shmwrite ID, STRING, 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 shmwrite Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。