tags:

views:

78

answers:

2

I need to write some service for my application. I want each client to have limited persistent connections (like only allow first 10 clients to connect).

I know I can listen on the port with PHP by socket_listen(). The parent process accept the connection, then pcntl_fork() the process to have children to handle connection.

But as far as I know, PHP resources doesn't persist when fork()ed. I wonder if it is possible to do this with PHP, or I have to do this in C?

A: 

Maybe you could try share it using memcache (http://www.php.net/manual/en/book.memcache.php). (i never tried this, may it may works)

ts
-1 no resources are only valid in the process which created them - in theory they'd be portable across lightweight threads - but PHP does not implement thread handling.
symcbean
my bad. Even with shmop() ?
ts
+1  A: 

1) Why bother forking? Run your daemon as a single process and use socket_select() ( or stream_select) to listen for requests.

See Aleksey Zapparov's code here for a ready-written solution.

2) Why bother with the pain of writing your own socket code - use [x]inetd to manage the servers and do al the communication on stdio (note that unlike solution 1, there will be a seperate process for each client - therefore the handling code will be non-blocking)

-- You are correct in saying that PHP resources should not be available in a forked process - but give no indication of how this relates to your current problem. Is it just so that you can count the number of connections? Or something else? In the case of the former, there are much easier ways of doing this. Using solution 1, just increment and decrement a counter variable when clients connect/disconnect. In the case of 2, the same approach but keep the variable in a datafile/database (you might also want to store info about the connections and run occasional audits). Alternatively limit the connections on the firewall.

C.

symcbean
My problem is that I have an Online Judge system. I want to distribute the test running over a number of machines (to reduce the noise in result, which may cause in solution that run in edge time).The daemon is a Queue Manager. It reads from database whether there is new submission. So each clients must be connected all the time for Queue Manager daemon to distribute the solution for each client to process, then waited for result from client. But I need concurrency in result saving so I consider using daemon and forked process. And with my own socket code I can port it to Windows easily too.
Nat
What you are describing seems to be a semi-synchronous request/reply message publishing system - which is a different kettle of fish altogether. Certainly solution 1 is still viable. I assume by "Windows" you mean one of Microsoft's platforms - in which case solution 2 is rather esoteric. But 1 still works. However from your limited description, most of the connection allocation should be controlled via data.
symcbean
Connection allocation controlled via data? Yes I do mean Microsoft Windows. But with solution one I wonder if the socket_select is blocking and I need to broadcast message...
Nat