进程间共享变量

来源:互联网 发布:泰康人寿java面试问题 编辑:程序博客网 时间:2024/05/22 15:25

在php中可以实现在进程间共享信息的途径,并保证能快速地访问这些共享数据。有两种方式:shmop 和 System V这两种共享内存扩展方式。

 

shmop使用的例子:

// create key//$shmop_key = ftok(__FILE__, 'p');  在win环境下,不支持这样的功能,故重写function myftok($filename = "", $proj = "")    {        if( empty($filename) || !file_exists($filename) )        {            return -1;        }        else        {            $filename = $filename . (string) $proj;            for($key = array(); sizeof($key) < strlen($filename); $key[] = ord(substr($filename, sizeof($key), 1)));            return dechex(array_sum($key));        }    }if(!function_exists('ftok')){    $shmop_key = myftok(__FILE__, 'p');}$shmop_id = shmop_open($shmop_key, "c", 0600, 16384);$population = shmop_read($shmop_id, 0, 0);$population += 45;$shmop_bytes_written = shmop_write($shmop_id, $population, 0);if ($shmop_bytes_written != strlen($population)) {   echo "Can't write the all of: $population\n";} else {    echo "Can write the all of: $population\n";}shmop_close($shmop_id);


shmop功能具有与我们熟悉的文件操作类似的接口。可以打开一个内存片段,从中读取数据,向其中写入数据,并关闭该内存片段。与文件操作相同的是,其中没有内置的数据分段,全部内容只是一系列连续的字符而已。

 

shmop内存片段是以键来区分的,但与文件使用字符串命名不同,这些键是用整型数来表示的,所以不太容易记忆。因此,最好是用ftok()函数来把与人类友好的名称——这里使用的是类似文件名形式的_FILE_——转换成shmop_open()能够接受的适当格式。这个ftok()函数也可以接受一个one-fscharacter参数,即“项目标识符(project identifier)”(对于PHP而言,是这里的P),用于在万一用到相同的字符串时避免发生冲突。

 

共享内存片段的大小时固定的,很有可能向其中写入超出容量限制的数据。可以通过比较shmop_write()的返回值与写入的数据的字符串长度,来检测出是否存在溢出问题。

 

================================================================================

 

System V使用的例子:

$semaphore_id = 100;$segment_id = 200;// get a handle to the semaphore associated with the shared memory// segment we want$sem = sem_get($semaphore_id,1,0600);// ensure exclusive access to the semaphoresem_acquire($sem) or die("Can't acquire semaphore");// get a handle to our shared memory segment$shm = shm_attach($segment_id,16384,0600);// retrieve a value from the shared memory segment$population = shm_get_var($shm,'population');// manipulate the value$population += 1;// release the handle to the shared memory segmentshm_detach($shm);// release the semaphore so other processes can acquire itsem_release($sem);echo $population;

 

System V共享内存,也是把数据存储在共享内存片段中,并通过信号机制(semaphore)来保证对共享内存片段的排他性访问。

System V共享内存功能的行为类似一个数组。

要获得对一个信号的控制权,需要使用sem_get()来取得相应信号的ID。 sem_get()接受的第一个参数是一个以整型数表示的信号键。可以把这个键设定为任何你认为合适的整型数,只要所有需要访问这一特定信号的程序都使用该键就没有问题。如果不存在指定的信号键,那么就会创建该信号。进程可以访问的这个信号的最大数值由sem_get()的第二个参数(这里的1)决定,而对这个信号的权限则通过sem_get()的第三个参数设置。

sem_get()会返回一个指向潜在的系统信号的标识符。通过这个ID可以使用sem_acquire()函数来得到对相应信号的控制权。这个函数等到信号可以取得时(可能是等到其他进程释放这个信号)会取得信号并返回true。如果发生了错误则返回false。可能的错误包括无效的权限或没有足够的内存创建信号。一旦得到了信号,就可以从相应的共享内存片段中读取数据了。

当加了内存片段之后,就可以通过shm_get_var($shm,  'population')来取得相应变量的值。这个函数搜索以$shm标识的共享内存片段,并取得名为population变量的值。可以在共享内存中存储任何类型的变量。当取得了该变量后,就可以和操纵其他变量一样来操纵这个变量。接着,shm_put_var($shm,’population’,$population)把$population的值作为一个名为population的变量的值写入到共享内存片段中。

至此,对共享内存的操作已经结束了。因此,需要用shm_ detach()来断开与内存片段的连接,并用sem_release()来释放相应的信号,以便其他进程能够使用这个信号。

 

Windows平台上虽然不能使用System V共享内存,但可以正常使用shmop功能。

原创粉丝点击