- Perl 基礎
- Perl - 主頁
- Perl - 簡介
- Perl - 環境
- Perl - 語法概述
- Perl - 資料型別
- Perl - 變數
- Perl - 標量
- Perl - 陣列
- Perl - 散列表
- Perl - IF...ELSE
- Perl - 迴圈
- Perl - 運算子
- Perl - 日期和時間
- Perl - 子例程
- Perl - 引用
- Perl - 格式
- Perl - 檔案 I/O
- Perl - 目錄
- Perl - 錯誤處理
- Perl - 特殊變數
- Perl - 編碼標準
- Perl - 正則表示式
- Perl - 傳送電子郵件
- Perl 高階知識
- Perl - 套接字程式設計
- Perl - 面向物件
- Perl - 資料庫訪問
- Perl - CGI 程式設計
- Perl - 包和模組
- Perl - 程序管理
- Perl - 內嵌文件
- Perl - 函式引用
- Perl 有用資源
- Perl - 問題和解答
- Perl - 快速指南
- Perl - 有用資源
- Perl - 討論
Perl shmctl 函式
說明
此函式利用 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
返回值
如果失敗,此函式返回未定義值,但如果 shmctl( ) 的返回值為 0,則返回 0(但為真)。
示例
以下是展示基本用法示例程式碼 −
#!/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: $! ";
編寫一個讀程式,用於使用 shmread() 檢索與 $key 對應的記憶體段並讀取其內容。
#!/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
廣告