描述
該函數使用 CMD 和 ARG 來控製 ID 引用的共享內存段。您將需要導入 IPC::SysV 模塊以獲取下表中定義的命令標記。
Command Description
IPC_STAT Places the current value of each member of
the data structure associated with ID into
the scalar ARG
IPC_SET Sets the value of the following members o
of the data structure associated with ID to
the corresponding values found in the packed
scalar ARG
IPC_RMID Removes the shared memory identifier specified
by ID from the system and destroys the shared
memory segment and data structure associated
with it
SHM_LOCK Locks the shared memory segment specified by ID
in memory
SHM_UNLOCK Unlocks the shared memory segment specified by ID
用法
以下是此函數的簡單語法 âˆ'
shmctl ID, CMD, ARG
返回值
此函數在失敗時返回 undef 和 0,但如果 shmctl() 的返回值為 0,則返回 true。
示例
以下是顯示其基本用法的示例代碼 -
#!/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 shmget用法及代碼示例
- Perl shmread用法及代碼示例
- Perl shmwrite用法及代碼示例
- Perl shift用法及代碼示例
- Perl shift()用法及代碼示例
- Perl sin()用法及代碼示例
- Perl split用法及代碼示例
- Perl splice用法及代碼示例
- Perl sqrt()用法及代碼示例
- Perl setpriority用法及代碼示例
- Perl semget用法及代碼示例
- Perl send用法及代碼示例
- Perl sysread用法及代碼示例
- Perl sort()用法及代碼示例
- Perl setpwent用法及代碼示例
注:本文由純淨天空篩選整理自 Perl shmctl Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。