百科狗-知识改变命运!
--

shm_put_var() - semaphore函数

乐乐12个月前 (11-21)阅读数 39#技术干货
文章标签返回值

shm_put_var()

(PHP 4, PHP 5, PHP 7)

Inserts or updates a variable in shared memory

说明

shm_put_var(resource $shm_identifier,int $variable_key, mixed $variable): bool

shm_put_var() - semaphore函数

shm_put_var() inserts or updates the$variablewith the given$variable_key.

Warnings(E_WARNING level)will be issued if$shm_identifieris not a valid SysV shared memory index or if there was not enough shared memory remaining to complete your request.

参数

$shm_identifier

A shared memory resource handle as returned by shm_attach()

$variable_key

The variable key.

$variable

The variable. All variable types that serialize() supports may be used: generally this means all types except for resources and some internal objects that cannot be serialized.

返回值

成功时返回TRUE,或者在失败时返回FALSE

参见

  • shm_get_var()Returns a variable from shared memory
  • shm_has_var()Check whether a specific entry exists
shm_put_var has no protection against race conditions. If two scripts insert the same key at the same time php might segfault.
sadly troy is right
the following script will return:
resource(5) of type (stream)
int(0) 
Will it ever support resource identifiers like pfsockopen() pointers?
The main problem is that when we run PHP as a Apache Module we never know in which process the next request will bind to, making impossible to have true persistent socket connections unless we can store the pointer to it or directly open the socket inode with fopen() like functions and retrieve the same resource pointer again.
I thought I could use shm, but it seems that shm doesn't allow o store resource pointers... sad... :(
This isn't entirely accurate. Not all variable types are supported, you can't put a resource variable into shared memory.
When you try to take it out, it will be a zero.
seems to me shm_put_var() doesn't work well with an integer key 0. I changed to 1 and it works like a charm.
Yes, it is possible to maintain a real level of resource persistence using shared memory. All vars in PHP are stored in common hashtables as zvals, including resource identifiers. There are hashtables available that outlive the request as long as the entire PHP process isn't shut down. All you need is to store the identifiers in such a hashtable and a way to keep track of them and you can receive the original resource.
I don't know why PHP doesn't provide a way of setting/getting persistent resources, but it is likely do to the many types of SAPI's available for it and the fact that not all of them *can* facilitate this, including CGI which still if far from obsolete. 
Another problem is that having such access in userspace is bound to create issues where multiple php processes are trying to access the same resource. Looking at it from this angel you can see that there's really no safe way to safely use such getter/setters without a better synchronism scheme in PHP.
Quote: "Will it ever support resource identifiers like pfsockopen() pointers ... we run PHP as a Apache Module ... no way to have true persistent sockets"
Sorry, but that doesn't make sense to me... the socket is still persistent, if you wish to resume it, simply call pfsockopen() with the same host and port - and you will get the same socket. There is no need to pass the actual resource variable.
If there is something amazingly special and unique about each socket, you can do the following - and this should apply to any persistent resource:
To differentiate between or obtain a specific resource - simply serialize/store an index of each resource's unique ID, along with the particulars that make that resource unique.
You can get a unique resource identifier as an integer value like so:

As pfsockopen() uses the hostname and port as a unique key to resume a persistent connection, you can add a DNS wildcard, or a number of manual entries in /etc/hosts (or windows equiv.) as follows:
resource-0.host.com 192.168.100.1
resource-1.host.com 192.168.100.1
resource-2.host.com 192.168.100.1
resource-3.host.com 192.168.100.1
Then, after consulting your serialized list of resources, you can connect to a specific resource by using it's resource id.
eg: $pf = pfsockopen("resource-$rid.host.com", $port, $timeout);
The new resource will be identical to the original in every way.
For file based stream resources you could do something similar with symlinks, or use the next method...
For URL based or other resources with "paths" (I don't know if there *are* persistent functions that involve such things) you could differentiate between them with using extraneous information in the path. eg:
 http://host.com/resource-4/../script.php
 http://resource4@host.com/script.php
 /tmp/././././file.txt
In the first example, the extraneous "resource-4" would be ignored by the webserver.
In the second, the superfluous username would be ignored by the webserver. (Something similar for mysql_pconnect could be done with multiple usernames).
And in the the third example, four sequential occurrences of the "do nothing" string "./" would indicate resource #4. 
If this isn't enough, then you can use the fact that PHP shared memory resources are themselves interoperable with those created by their .c counterparts.  That allows you to write a thin .c application to handle the dirty work.
Or you could attempt to reconnect to your own webserver, using persistent streams and the methods outlined above, to achieve the end result.  I can't think of an example of where something so extreme would be necessary, but I'm sure it's not outside the realm of possibility.
I personally use an 117 MB binary database, which is stored in shared memory, both from the command line (using a complied .c application), and from the web (via PHP, and ftok()/shmop_open()/shmop_read()).
Use as few variable_keys as you can. With large arrays of data, rather make the array multi-dimensional and store under one variable_key than use variable_key as your index. The benefit is especially noticeable when repeated fetching from the end of the array is necessary and updates are less frequent.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)