Resizing shmop ?

Resizing shmop ?

am 30.10.2007 08:20:36 von vmirage

Hi,

I need to resize the shmop but it seems that I can't seem to get it
right. Here's what I did

// initialize with 100 byte
$shm_id = shmop_open(0xff3, "c", 0644, 100);
echo shmop_size($shm_id);
shmop_delete($shm_id);
shmop_close($shm_id);

// trying to resize to 200 byte but not working
$shm_id = shmop_open(0xff3, "c", 0644, 200);
echo shmop_size($shm_id);
shmop_delete($shm_id);
shmop_close($shm_id);


However, if I try creating shmop with a large memory allocation, i can
open the shmop with smaller memory allocation. But it still does not
resize the allocation.

The delete doesn't seem to delete even though no resources are using
the shmop.

Thanks

Re: Resizing shmop ?

am 30.10.2007 15:47:49 von dafox

vmirage schrieb:

> I need to resize the shmop but it seems that I can't seem to get it
> right. Here's what I did

> // initialize with 100 byte
> $shm_id = shmop_open(0xff3, "c", 0644, 100);
> echo shmop_size($shm_id);
> shmop_delete($shm_id);
> shmop_close($shm_id);

> // trying to resize to 200 byte but not working
> $shm_id = shmop_open(0xff3, "c", 0644, 200);
> echo shmop_size($shm_id);
> shmop_delete($shm_id);
> shmop_close($shm_id);

> However, if I try creating shmop with a large memory allocation, i can
> open the shmop with smaller memory allocation. But it still does not
> resize the allocation.

How do you make sure, that allocation doesn't work? Run the following
script as root using the command line interface. The memory usage is
displayed using the "ipcs -m" command.

// Create 100 byte shared memory block with system id of 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, str_repeat('x', 100), 0);
if ($shm_bytes_written != 100) {
echo "Couldn't write the entire length of data\n";
}

// Show me what you got
echo shell_exec('ipcs -m');


// Recreate it with a larger size
$shm_id = shmop_open(0xff3, "c", 0644, 200);

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, str_repeat('x', 200), 0);

if ($shm_bytes_written != 200) {
echo "Couldn't write the entire length of data\n";
}

// Show me what you got
echo shell_exec('ipcs -m');

// 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);
?>

This should output:

SHM Block Size: 100 has been created.

------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000ff3 622592 root 644 100 1

The data inside shared memory was: xxx...
SHM Block Size: 200 has been created.

------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000ff3 655360 root 644 200 1

The data inside shared memory was: xxx...