shmop.examples.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 36 行

HTML
36
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Examples</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="shmop.constants.html">Predefined Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.shmop.html">Shared Memory Functions</a></div> <div class="up"><a href="book.shmop.html">Shared Memory</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div> <h1>Examples</h1> <div id="shmop.examples-basic" class="section">  <p class="para">   <div class="example">    <p><b>Example #1 Shared Memory Operations Overview</b></p>     <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;100&nbsp;byte&nbsp;shared&nbsp;memory&nbsp;block&nbsp;with&nbsp;system&nbsp;id&nbsp;of&nbsp;0xff3<br /></span><span style="color: #0000BB">$shm_id&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">shmop_open</span><span style="color: #007700">(</span><span style="color: #0000BB">0xff3</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"c"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0644</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$shm_id</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Couldn't&nbsp;create&nbsp;shared&nbsp;memory&nbsp;segment\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Get&nbsp;shared&nbsp;memory&nbsp;block's&nbsp;size<br /></span><span style="color: #0000BB">$shm_size&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">shmop_size</span><span style="color: #007700">(</span><span style="color: #0000BB">$shm_id</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"SHM&nbsp;Block&nbsp;Size:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$shm_size&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"&nbsp;has&nbsp;been&nbsp;created.\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Lets&nbsp;write&nbsp;a&nbsp;test&nbsp;string&nbsp;into&nbsp;shared&nbsp;memory<br /></span><span style="color: #0000BB">$shm_bytes_written&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">shmop_write</span><span style="color: #007700">(</span><span style="color: #0000BB">$shm_id</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"my&nbsp;shared&nbsp;memory&nbsp;block"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$shm_bytes_written&nbsp;</span><span style="color: #007700">!=&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #DD0000">"my&nbsp;shared&nbsp;memory&nbsp;block"</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Couldn't&nbsp;write&nbsp;the&nbsp;entire&nbsp;length&nbsp;of&nbsp;data\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Now&nbsp;lets&nbsp;read&nbsp;the&nbsp;string&nbsp;back<br /></span><span style="color: #0000BB">$my_string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">shmop_read</span><span style="color: #007700">(</span><span style="color: #0000BB">$shm_id</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$shm_size</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$my_string</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Couldn't&nbsp;read&nbsp;from&nbsp;shared&nbsp;memory&nbsp;block\n"</span><span style="color: #007700">;<br />}<br />echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;data&nbsp;inside&nbsp;shared&nbsp;memory&nbsp;was:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$my_string&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//Now&nbsp;lets&nbsp;delete&nbsp;the&nbsp;block&nbsp;and&nbsp;close&nbsp;the&nbsp;shared&nbsp;memory&nbsp;segment<br /></span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">shmop_delete</span><span style="color: #007700">(</span><span style="color: #0000BB">$shm_id</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Couldn't&nbsp;mark&nbsp;shared&nbsp;memory&nbsp;block&nbsp;for&nbsp;deletion."</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">shmop_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$shm_id</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="shmop.constants.html">Predefined Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.shmop.html">Shared Memory Functions</a></div> <div class="up"><a href="book.shmop.html">Shared Memory</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?