php forking child process to execute infinte loops -


I am relatively new to PHP, but I can do programs in other languages ​​(JS, Java, C / C ++) I can. I have a problem and I do not seem to be able to solve, so I hope someone can help me here :)

I created a server_class.php file which is the connection between multiple customers Manage. Customers connect to the PHP server via the web when I launch server_class.php, then it executes two applications (they are in endless loops) and print data to the terminal. When a client connects to the PHP server, I want the server to start sending the output of each application to each customer, so that customers can see the current output of each application. I have partially acquired it, however, it only sends the output of one application and not the second.

The work given below is executed when a connection is made between the server and the client:

  the private function is current ($ client) {$ this-> Output ("Start client process"); $ Pid = pcntl_fork (); If ($ pid == -1) {$ this-> Output ("Fork is finished!"); } Elseif ($ pid) {// Process $ client- & gt; Setpid ($ pid); } And {$ this- & gt; Output ("Starting AP1 data pipe ..."); Exit ($ this-> launchAppOneProc ($ customer)); $ This- & gt; Output ("Starting AP2 data pipe ..."); Exhaust ($ this-> launchAppTwoProc ($ customer)); }}  

OK, once the connection between the customer and the server is executed, this function is executed. As you can see, I create a new process which then implements two methods: launchAppOneProc and launchAppTwoProc These two functions contain the following code:

  Launch Personal Function AppOneProc ( $ Customer) {while (@ob_and_flush ()); // Any time all production buffers end (! Feof ($-> appone_proc)) {$ appone_text = fread ($ this-> Epon_proc, 4096); $ This- & gt; Send ($ client, $ APON_TEX); Flush (); } $ This- & gt; Output ("AppOne has stopped running!"); }  

The above mentioned task is similar to launch of AppTVProc (), the function prints the $ {data-> output-> text specified in the terminal of server_class.php

then the problem is That it only executes the launchAppOneProc () function and the next function does not execute LaunchAppTwoProc ().

How can I execute both tasks but any thoughts?

Thanks David

OK, I got a solution to my problem, some confusion To make it clean, I have two applications that are running in the background and writing buffers. When a client connects to the server, they both connect to the buffers and start receiving data. So I did a new process for each channel. When the client joins the server, two processes are created and assigned to the client. Each process is connected to a buffer and starts sending the data to the client.

  private function startProc ($ customer) {$ this- & gt; Output ("Start client process"); {// Create new process for $ ($ I = 0; $ i & lt; 2; ++ $ i) $ Pid = pcntl_fork (); Switch ($ pid) {case -1: // failed to create new process $ -This-> Output ("Failed to create new process!"); break; Case 0: // Child process was created, execute code ... switch ($ i) {case 0: $ this- & gt; Output ("Connect to AppOne Data Pipe ..."); $ This- & gt; LaunchAppOneProc ($ customer); break; Case 1: $ this-> Output ("Connect to AppTwo data pipe ..."); $ This- & gt; LaunchAppTwoProc ($ customer); break; } Default: // Back to Parents Switch ($ i) {Case 0: $ Client-> SetAppOnePid ($ pid); break; Case 1: $ client- & gt; SetAppTwoPid ($ pid); break; I do know that I can use an array to save the process ID. But for now, it works after the client is disconnected, for each client leaving just that, each process should be terminated like this: 

  posix_kill ($ client-> GetAppOnePid (), SIGTERM); Posix_kill ($ client-> getAppTwoPid (), SIGTERM); I hope that it will help anyone understand the same problem. 


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -