Linux under PHP pcntl_fork analog multithreading

Source: Internet
Author: User

Start using PHP to write the background service for a while. Also under such a drive, constantly learning PHP grammar, experience this has always been thought to be mysterious and the charm of the Magic language at a distance. Initially looking at PHP multi-threading data is to improve the processing capacity of the program, Get the most out of your Linux multitasking. Never thought that multi-threading would be useless to bring a series of unexpected gains. Let the following many problems solved, not alone to enjoy the special one by one.
What this article is talking about is the Pcntl_fork function from PHP. Because this function relies on the implementation of the operating system fork, so this article only applies to the Linux/unix.ok, then first look at the use of this function. That's what the PHP manual says:
<?php
$pid = Pcntl_fork ();
if ($pid = =-1) {
Die (' could not fork ');
} else if ($pid) {
We are the parent
Pcntl_wait ($status); Protect against Zombie Children
} else {
We are the child
}
?>
Creating a child process through pcntl_fork, if the return value is-1, then the child process creation failed. The process ID that was created is returned to the parent process, and 0 is returned to the child process. It is not easy to understand, the cost of a lot of time to understand later, I am accustomed to write:
<?php
$pid = Pcntl_fork ();
if ($pid = =-1) {
Create a failure let's quit.
Die (' could not fork ');
}
else{
if ($pid) {
From here to write the code is the parent process, because it is written by the system program, remember to exit the time to give a return value
Exit (0);
}
else{
The code that starts here is executed in a new process, and it's also best to give a return value if it quits normally.
Exit (0);
}
}
?>
Such a change to understand more, if your parent process want to know that the child process gracefully exit, you can add the previous pcntl_wait.
The use of functions is clear, what is the use of the actual work?
1. Background program
Command line program good write, service program write, I think this service program is the most difficult to write. Think of the original want to write a service under Windows what, but also to register the service is to such an old laborious. Now full-time management Linux want to let a command line program run in the background, directly behind the command to add a & It's done. But it always feels like dirt. With pcntl_fork suddenly found that the world is so beautiful. When the master process successfully creates the child process and obtains the ID of the child process, he does not forget to say: "I have run successfully, my ID is: xxxx (the child process ID)", Back to the system returned a 0 (normal exit), Haha, dead dead are so dignified.
In front of the program is the memory of the situation, of course, it is necessary to pay attention to the release of memory and the log files to print information, rather than to the screen (a printing information program to exit Yo). Another scenario is that the program is called by other scripts, and the other scripts only care if the program is working properly. If the program is going to run for a long time to run, it's best not to let the script wait. So pcntl_fork again come in handy:)
2. Delay processing.
Sometimes, when we quit the program to clean up their own produce, such as to delete themselves (of course, Linux can delete the running files, just for example), this time can start another process, and then the end of their own, Turn things over to another process. When we write a service program, we are sure to write the log file recorder running condition (or who knows the program is not in that sleep: 0). When the program quits normally we can write a log saying the program exited, but when the program received the great kill on Linux -How do you record your exit behavior when you are 9? This has something to do with the PHP process signal, which doesn't seem to matter much.
Another scenario: A well-developed program generally supports start,stop,restart such parameters. Start to say, stop also said, since start and stop are saying, this restart first stop and then start on it. It seems that there is not much relationship with Pcntl_fork, when you receive the restart of the signal can not still kill and then start it, is it too yellow too violent? or be gentle, let the current process exit, let the other process pull it up again. It seems difficult to achieve this in Windows, For example, when the program is updated, it is generally a single-write update program, or when the program exits, another start of a batch process starts itself:)
3. The process of non-death
In fact, it is the legendary double process. In the early years of the Vientiane Network management in order to achieve the purpose of not being malicious end of the use of this trick. Although we do not have to worry about the program is malicious stop, but also discouragement the main program because the task is too heavy to resist oneself first hang up (this situation is not going to happen).
Speaking so much, just a few thoughts on writing a program, an extension to a function usage. I thought so much of you?

Linux under PHP pcntl_fork analog multithreading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.