Tuesday, June 15, 2010
青木瓜泡茶 痛風不見了 治療尿酸效果更佳
Monday, June 14, 2010
[Backup for peronal reference]
Process Forking with PHP
Posted March 4th, 2004 in PHP
Unix supports the process of forking which allows a parent process to spawn a subprocess and continue running both processes concurrently. It is possible to do this in PHP using the PHP Process Control Functions. Note that you should never attempt to use these process control forking functions when using a webserver; you shuld only fork applications when using the PHP command line client.
Before you can use the PHP process control functions you must compile the PCNTL extensions into PHP using the --enable-pcntl configure option (ie ./configure --enable-pcntl along with all the other configuration options you would like to compile into the PHP binary). No additional libraries need to be pre-installed. Note that these process control extensions will not work on non-Unix platforms (ie Microsoft Windows).
Basic Forking Example
A very basic and commonly used example for forking a process in PHP is as follows:
$pid = pcntl_fork();
if($pid) {
// parent process runs what is here
print "parent\n";
}
else {
// child process runs what is here
print "child\n";
}
Running this will output the following:
child
parent
Child process is a copy of the parent process
What actually happens when you call the pcntl_fork() function is that a child process is spawned which is exactly the same as the parent process and continues processing from the line below the function call. All variables and objects etc are copied into the child process as-is but these are new copies which belong to the new process. Modifying them in the child process does not affect the values in the parent (or any other forked) process.
The parent process will have a value assigned to $pid whereas the child process will not, hence the if test. Note that in the above example, both processes would continue running whatever code is after the if statement, something which is rarely mentioned in examples of PHP process forking on the web.
To illustrate this, we'll modify the example above slightly to add additional output as follows:
$pid = pcntl_fork();
print "start\n";
if($pid) {
// parent process runs what is here
print "parent\n";
}
else {
// child process runs what is here
print "child\n";
}
print "end\n";
Running this will display the following:
start
child
end
start
parent
end
Making the parent process wait until the child has finished
So ideally you want to let either the child or parent continue processing the rest of the script and make the other process exit after the process is forked. If you exit from the parent process, however, you'll end up with "zombie" processes running which do not belong to any process. Therefore the parent process needs to wait until all the child processes have finished running before exiting itself. You can do this using the pcntl_waitpid() function, which will cause the parent process to wait until the child process has completed. You can then either just let the parent process exit, or do any tidy up code that is required.
An example of doing this is as follows:
$pid = pcntl_fork();
if($pid) {
// this is the parent process
// wait until the child has finished processing then end the script
pcntl_waitpid($pid, $status, WUNTRACED);
exit;
}
// the child process runs its stuff here and then ends
...
The exit call in the parent process ensures that processing stops at that point and the parent does not execute any of the code intended for the child. Another way of doing the same thing without the exit code would be as follows:
$pid = pcntl_fork();
if($pid) {
// this is the parent process
// wait until the child has finished processing then end the script
pcntl_waitpid($pid, $status, WUNTRACED);
}
else {
// the child process runs its stuff here
}
Exit codes from the child process
You could optionally have an exit call at the end of the child part of the if statement.
The $status parameter passed to pcntl_waitpid() stores the return value from the child process. If the child process returns 0 (ie success) then it will also be zero. On my Linux desktop the value returned as $status would be the value returned from the exit call multipled by 256. So if the child process ended with exit(2) my system returned 512 as the $status value. Whether this is the same across all Unix systems I do not know.
Getting the parent process to wait until the child process has completed is useful for then doing something else based on the return value of the child process as shown in the following example:
$pid = pcntl_fork();
if($pid) {
// this is the parent process
// wait until the child has finished processing then end the script
pcntl_waitpid($pid, $status, WUNTRACED);
if($status > 0) {
// an error occurred so do some processing to deal with it
}
}
else {
// the child process runs its stuff here
...
if(...successful condition...) {
exit(0); // this indicates success
}
else {
exit(1); // this indicates failure
}
}
Forking multiple child processes
This final example illustrates how you could fork several children from the parent process with PHP. The loop runs three times and forks a child process for each loop, storing the child pids into an array. After running the stuff for the child process the child then exits. A second loop runs in the parent after the first to ensure all child processes have finished running before resuming its own process.
Note it is very important in this sort of process that the child explicitly exits in its section of the script, otherwise each child will continue running through the first, and then second, loop.
$pids = array();
for($i = 0; $i < 3; $i++) {
$pids[$i] = pcntl_fork();
if(!$pids[$i]) {
// child process
...
exit();
}
}
for($i = 0; $i < 3; $i++) {
pcntl_waitpid($pids[$i], $status, WUNTRACED);
}
// complete parent processing now all children have finished
...
The PHP manual pages for process control functions can be found at www.php.net/manual/en/ref.pcntl.php. There are a number of user contributed notes for each of the functions which should also help with your understanding of process forking in PHP.
Sunday, April 4, 2010
MY FM 重金礼聘 在线 自动计算系统
如果参与者能成功的回答到最后一提常识题,那他/她就能赢取大奖,少则数百令吉,多则上万的奖金。有于该游戏的赞助商为MAGNUM, 所以数学题的答案有点像是4D 那样。参与者每成功回答一题数学题,他/她就能赢取RM50.00, 那只要能把三题数学题给答好,RM150.00 就拿定了。
DJ首先会向参与者发出 10 个 数字,然后参与者必须利用这 10 个数字完成以下数学题:
假设该10个数字为 1,2,3,4,5,6,7,8,9,9
题 1) DJ会给参与者一个数字,然后参与者必须从10个数字里选出4个数字,如果这4个数字的总和与DJ的数字一样,那就算答对了。(假设DJ要的是12, 那参与者必须回答 1236 或 1245, 1+2+3+6=12, 1+2+4+5=12)
题 2) DJ会给参与者一个数字,然后参与者必须从10个数字里选出4个数字,如果这4个数字相减与DJ的数字一样,那就算答对了。(假设DJ要的是3, 那参与者必须回答 9123 或 9213, 9-1-2-3=3, 9-2-1-3=3)
题 3) DJ会给参与者一个数字,然后参与者必须从10个数字里选出4个数字,如果这4个数字的数学计算(加减加)与DJ的数字一样,那就算答对了。(假设DJ要的是25, 那参与者必须回答 8919 或 9819, 8+9-1+9=25, 9+8-1+9=25)
对于一般的市民,通常要在30秒内答对3题数学题是有一定的难度,有的参与者找了好几位助手都折腰哩!
讲了一大堆的废话,就是想告诉你,昨晚半夜孩子发梦,一脚踢到我头上,无辜的我,摸着无辜的头,睡意全失, 在早上两点钟,为 my fm 200万广大的听众写下来 “MY FM 重金礼聘 在线 自动计算系统”, 网址为 http://kimwah.net/myfm/
有了“MY FM 重金礼聘 在线 自动计算系统”,那RM150.00的奖金肯定是随手可得。
如果你有严重的“斗鸡眼”和“发瘟手”,连那几个数字也可能错误输入的话,千万别使用这系统,免得我给你咒到半夜又给孩子发梦踢醒。
如果你是有良心的话,在赢取MY FM + MAGNUM 的奖金时,记得捐助一点点给 WorldVision, 慈济,Makna 或 Unicef。
Monday, August 10, 2009
Thursday, July 16, 2009
養不起的未來
幾年前他的母親去世,他回故鄉去奔喪,他一直不喜歡母親,
因為她一直不斷向這一個兒子要錢,只要他一個月沒有寄錢回家,
母親就打電話給他破口大罵,真是所謂的死要錢,而且北原武越出名,她索取的錢越多。
回到家之後,他還是忍不住大哭一場,
想到他一直在外,沒有好好供養媽媽,
雖然是死要錢的媽媽,他還是覺得虧待母親。
等辦完喪事,北原武正要離開家的時候,
他的大哥把一個小包袱給了他說:
這是媽媽叫我一定要交給你的。
北原武小心翼翼地打開小包袱看到一本存摺與一封信,存款是用他的名義開戶,存款金
額高達數千萬日幣,信中母親寫道:
“武兒,在這幾個兒女當中,我最擔憂的就是你,
你從小就不喜歡唸書,又亂花錢,對朋友太慷慨,
當你說要去東京打拼時,我就很擔心你會變成一個落魄的窮光蛋,
因此我每月從不間斷地要你寄錢回家,
一方面可以刺激你去賺更多的錢,
另一方面也為了替你儲蓄;你給我的錢,
我一毛都沒有花,你大哥一家把我養得好好的, 你的錢就是你的錢,現在就拿去好好利用吧!” 看完了信,北原武哭倒在地上,久久站不起來...
王永慶曾經說過一句話:
你賺的一塊錢不是你的一塊錢,你存的一塊錢才是你的一塊錢。
現在的經濟景氣越來越差,全世界都進入了二低一高的時代低成長、低利率、高通膨;
不要小看你隨手花掉的小錢:
一杯咖啡、一包香煙、一件衣服...
省下它,就有可能改變你的一生,
如果你身邊也有存不了錢的朋友,
告訴他北原武的故事,你就有可能成為他這輩子的貴人!
養不起的未來 --不是收入太少,而是開銷太大