Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


PHP forked process referring to parent process variables

Posted on 2003-11-11
Medium Priority
Last Modified: 2010-08-05
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.
Question by:bjai
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 11

Expert Comment

ID: 9732268
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.

Author Comment

ID: 9732911
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?


Author Comment

ID: 9732966
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 :)
Independent Software Vendors: 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!

LVL 11

Expert Comment

ID: 9733189
How about java?
LVL 14

Expert Comment

ID: 9736329
bjai, keep this question open, i want to give it a try tomorrow. Do you have something (client/server code) to work on?

Author Comment

ID: 9736882
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.

Accepted Solution

Chmod earned 0 total points
ID: 9768326
PAQ'd & 500 points refunded

Community Support Moderator @ Experts Exchange

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question