[mycode=php]
#!/usr/bin/php -f
/**********************************************************
说明:该类访问共享内存
功能:添加、删除、获取共享内存信息,创建或删除共享内存
***********************************************************/
require_once(“/home/junjie2/php_module/Shm.class.php”);
if(!isset($argv[1]) || isset($argv[1]) && ( $argv[1] == “-h” || $argv[1] == “–help”)){
show_help();
}
$shm = new Shm();
$op = $argv[1];
if($op == ‘create’){ //创建
$name = $argv[2]; //键标识
$mod = $argv[3];
$size = $argv[4];
if(strlen($name) != 1){
echo “key name only can be a one charactor string\n”;
exit(1);
}
$shm_key = @ftok(__FILE__, $name);
if($shm_key == -1){
echo $php_errormsg;
exit(1);
}
if(!$shm->create($shm_key,$mod,$size)){
echo $shm->get_error() .”\n”;
exit(1);
}
echo “The key of shm is : 0x”.dechex($shm->get_key()) .”\n”;
exit(0);
}
if($op == ‘get’){ //获取变量
$key = $argv[2]; //键标识
$var = $argv[3];
$shm->set_key($key);
$val = $shm->get_var($var);
show($val) ;
echo “\n”;
exit(0);
}
if($op == ‘put’){ //创建
$key = $argv[2]; //键标识
$var = $argv[3];
$val = $argv[4];
$shm->set_key($key);
if(!$shm->put_var($var,$val)){
echo $shm->get_error() .”\n”;
exit(1);
}
exit(0);
}
if($op == ‘remove’){ //创建
$key = $argv[2]; //键标识
$var = $argv[3];
$shm->set_key($key);
if(!$shm->remove_var($var,$val)){
echo $shm->get_error() .”\n”;
exit(1);
}
exit(0);
}
if($op == ‘delete’){ //创建
$key = $argv[2]; //键标识
$shm->set_key($key);
if(!$shm->delete()){
echo $shm->get_error() .”\n”;
exit(1);
}
exit(0);
}
show_help();
exit(0);
function show_help(){
global $argv;
echo “usage:” . $argv[0] .” \n”;
echo “create name mod size \n”;
echo “get key var \n”;
echo “put key var val \n”;
echo “remove key var \n”;
echo “delete key \n”;
exit(0);
}
function show($var) {
if(is_array($var)){
print_r($var);
}else{
echo $var;
}
}
?>
[/mycode]