[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

PHP forked process referring to parent process variables

I have written a multi-thread (process) socket server script. It consists of a Listener class (one process) and XMLSocket class (separate client processes). The problem is that I cannot refer from the XMLSocket process back to the Listener process the socket array or any of its variables. Once the Listener spawns a new child/connection, the Listener class variables can be passed on to the XMLSocket class as 'copy' only but not by reference. Hence the spawned children/connection will not know of any later changes in the Listener client array or any of the Listener's variable but only the 'status' of the Listener at the time of the process is forked. I would need to have the spawned connections refer back to the parent process variables since I may use this as a chat server, hence each child has to be able to refer to the 'parent' list of spawned connections for communication.

I guess this is the difference between multi-thread and mutli-process. However, I would like to know if there is a way to work around this problem. Answers telling me to use other programming languages are not accepted. The scope of this question covers PHP only.
0
bjai
Asked:
bjai
1 Solution
 
shmertCommented:
I don't think you'll get pass-by-reference working between processes, since the variables passed back and forth are dealing with totally different locations in memory.  One cheesy way to do this would be to serialize() your object to a file, and then notify some other thread that the object has changed, and to unserialize() it.
0
 
bjaiAuthor Commented:
Hmm, but the socket handle/resources cannot be stored in an external file/memory? It can't be serialized anyway?

I had in mind maybe using shared memory functions but still such method can only be invoked by the parent process and the child process can't do much to 'fetch' things from the parent. Say the parent process has created 1000 child process/connection, the parent process has to run a non-blocking select and loop thru the 1000 child processes to update them. Even then, there will be delay in the 'parent status'  of each of the child processes and this gets worse as more children are spawned. Also this will take up quite a lot of resources also, each child process stores its own copy of the parent process data. So afterall, it all gets very messy in terms of coding.

I have thin knowledge about linuxthreads (e.g. clone() ), maybe that is useful?

0
 
bjaiAuthor Commented:
I have decided to award the points if someone can provide a C++ code example of the 'Listener' and 'child socket' threads OR provide a detailed approach using PHP (no code required). I realize that this question is not an easy one (in PHP), I understand PHP has its limitation, but maybe this is a good brain teaser for those PHP developers out there :)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
shmertCommented:
How about java?
0
 
ThGCommented:
bjai, keep this question open, i want to give it a try tomorrow. Do you have something (client/server code) to work on?
0
 
bjaiAuthor Commented:
I have some php source code but it's not fully functional as there is limitation as mentioned above. I am quite happy to send it out. But its quite long and consist of serveral class files so would only email it.
0
 
ChmodCommented:
PAQ'd & 500 points refunded

Chmod
Community Support Moderator @ Experts Exchange
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now