XCIII. 공유 메모리 함수

Shmop는 Unix 공유 메모리 세그먼트를 php에서 읽고, 기록하고, 생성하고, 삭제하기 쉽도록 하는 함수 모음이다. 이 함수는 윈도우즈에서는 공유 메모리를 지원하지 않기 때문에 동작하지 않는다. shmop를 사용하기 위해서는 --enable-shmop 인자로 php를 컴파일해야한다.

참고: 이번 장에서는 PHP 4.0.3의 shm_() 단어로 시작되는 함수를 설명할 것이다. 그러나 PHP 4.0.4 이후 버전의 경우 shmop_()로 이름만 바뀌었다.

예 1. 공유 메모리 작동의 개관

<?php
   
// Create 100 byte shared memory block with system id if 0xff3
$shm_id = shmop_open(0xff3, "c", 0644, 100);
if(!$shm_id) {
	echo "Couldn't create shared memory segment\n";
}

// Get shared memory block's size
$shm_size = shmop_size($shm_id);
echo "SHM Block Size: ".$shm_size. " has been created.\n";

// Lets write a test string into shared memory
$shm_bytes_written = shmop_write($shm_id, "my shared memory block", 0);
if($shm_bytes_written != strlen("my shared memory block")) {
	echo "Couldn't write the entire length of data\n";
}

// Now lets read the string back
$my_string = shmop_read($shm_id, 0, $shm_size);
if(!$my_string) {
	echo "Couldn't read from shared memory block\n";
}
echo "The data inside shared memory was: ".$my_string."\n";

//Now lets delete the block and close the shared memory segment
if(!shmop_delete($shm_id)) {
	echo "Couldn't mark shared memory block for deletion.";
}
shmop_close($shm_id);
   
?>

차례
shmop_close -- 공유 메모리 블럭을 닫음
shmop_delete -- 공유 메모리 블럭을 삭제
shmop_open -- 공유 메모리 블럭을 열거나 생성
shmop_read -- 공유 메모리 블럭으로부터 데이터를 판독
shmop_size -- Get size of shared memory block
shmop_write -- 공유 메모리 블럭에 자료 기록