What is pcntl_fork in php?
There is a great unknown in the world of the PHP language. You may not know that in the program you can run more than one process at a time.
I understand that this doesn't work on non- Unix systems.
Let's start from the beginning. A process is a program that is running.
A new process is created every time you run a script.
There is no greater mystery, it is that from a script that is running we can create an equal child process that runs in parallel.
Start with a simple example.
This is all that we need.
The function that creates a child process is called pcntl_fork.
The result will be this if you run this code.
How is that possible?
There is only one echo when the message is displayed twice.
Clear! A second process has been created with the pcntl_fork function and the two have been executed in parallel.
This is a fork.
Your system may not be able to be forked or fail for a reason. We do a check to detect this situation.
We must act if the new process fails.
How can we know if the original process is the father or the child? The function that returned the $pid variable is called pcntl_fork.
The result is:
The child process has a zero value. The parent has the value greater than zero.
If I tell you what I'm going to tell you, I'll feel smarter. The Process IDentifier is used to identify all processes.
Each time you start a process, it gets a different PID.
The values that are stored in $pid are what the processes are called.
We can use them to find out if we are in the parent or child.
The getmypid function tells us the current process's PID.
The result will be similar.
We don't know if the father's messages will be displayed first and the son's second.
It will always be displayed in the order that I have put it in, but it might not happen that way with a script like this. Adding a little sleep is what we're trying to do.
We will have a similar result in this case.
The child knows all the variables that the parent has with their values when we do a fork. We can see it with an example.
The example's output would be:
But what if the child modifies the variable? The child should modify the example so that they can. We add sleep to make sure that the parent displays the value of the variable after the child has modified it.
The result is:
The variable $name of the parent is not affected if the child changes it. The son copies the father's variables at the time of the fork, but they are not the same as the father's variables.
I haven't commented yet, but it's important that we don't forget to call the function in the parent process.
This time, I'm going to show you how to manage the creation of threads of a process to distribute the execution load in several instances. Each instance or thread will carry out a task.
A system for sending push notifications can be done by forking processes in the same thread, so that each thread takes care of sending the push notification.
When an algorithm is programmed, we have two ways to solve the execution of the code and these are: sequentially, which is nothing more than iterating the code in the same php process, or in parallel, which is based on creating several instances the execution of the php process to distribute the load and provide efficiency to our algorithm.
I am going to give you an example so that you can see it in a better way. A system that is responsible for getting a list of pending push notifications is possible.
Many of the notifications have a number of devices that are numerous.
If we execute our algorithm in sequential order, it will process notification by notification, one by one, that is, it takes the first one, selects the devices to which it should send push notifications.
The process won't move on to sending the next notification until it has finished sending all devices. This makes us think that the large-scale process can become a big problem.
This is where process forking begins. It is possible to make the creation of threads and the sending of push notifications to each of its devices by each thread.
The theoretical basis of forking or parallelization tells us that when the parent process launches a child process, a process identical to the parent is created in memory, with its own process identifier (pid) (different from the parent) and running from the instruction following the one that created the child process itself.
I will describe some of the methods that are available to us. You can review this link to learn more about these methods.
The two methods that we will be using in our example are the forking methods.
There are many other things that you can review on the official page of the website.
We are going to get a mental idea of the push notification sending system where each notification can have a number of devices to send.
The first thing we will do is create an abstract class (BaseTask.php) which will represent the structure that must extend the class (Task.php) which contains some specific methods that in our case we think will be where we will implement the process of sending the notification. push to each device.
The following methods were changed by the Task class.
We will create a class calledThreadManager which will be in charge of managing the creation of threads.
The thread manager is started by a single method called start.
This is where the pcntl_fork method is used to create a child thread that is identical to the parent but will perform different tasks.
If the pid is returned by the pcntl_fork method, the start method is valid.
If we are in a parent process, we verify that as many children have been created as defined in the maxThreads parameter. If this happens, we will have to wait for each child to respond before we can kill the child process.
The number of children created again after there are no more child threads is indicated by the parameter maxThreads.
Let's see how to use the methods of the classes defined above.
The class ThreadManager and the class Task are instantiated.
We use the while iterator to make sure that we get the push notifications.
When theThreadManager class reaches the maximum number of threads allowed, it will stop.
The ThreadManager class can continue to create child threads when done with these threads.
To launch this, we need to go to the terminal and execute the project files.
If we want to communicate with each other, we need to implement some kind of communication between processes, such as sending signals, communication by tcp sockets, shared memory, etc.
Downloads, searching for files, pinging a network range, and many other tasks get better performance when done with concurrent processes.
If we can determine who is the parent of the new process, we can call the system fork function.
Both the parent and child have different PIDs.
The following script is used to download the images of a issuu document.
The iddoc pini p fin is a php version.
There are 4 parameters.
Related Questions
No More Questions available at this moment!
More Questions
- What is diabetes tea?
- What is tmc therapy?
- What is ime in computer?
- Whos hoh in big brother?
- What is the Amazon offer code for Tuna London Women Sweatshirts?
- How to say chh?
- What is the best outdoor hd antenna to buy?
- Which cirque du soleil show in vegas is the best?
- What is ipms in telecom?
- What should i do if my dog ate cinnamon?