2

The documentation of popen and proc_open mention that respectively pclose and proc_close MUST be called. Why is this? Why can't I start a process and have its termination be independent of the parent?

This would be useful, for example, in the case that a fatal error occurs in the parent process. This gives rise to the question what does happen if the parent exits without calling pclose or proc_close. I would expect the child process to be orphaned, but what happens to the pipes, for example.

Edit: some testing with both popen and proc_open seems to show that PHP kills child processes on termination, be it by calling exit or upon a fatal error.

Edit: A brief summary of the context: For monitoring a web-application*, I need a 'server' process that receives short messages from one or more 'client' processes. In some situations a new server must be started when the program is run. To make this work independent of the origin of the call (for example an HTTP request), I want the client to start this process. Because (a) the server might be connected to multiple clients and (b) it has to respond client crashes, I need it to terminate independently of the client that started it.

*This is a very large system that is already in use, so I can't change its structure.

Oebele
  • 581
  • 1
  • 4
  • 17
  • The manual says that it must be closed by pclose() ("must" in the context of "instead of fclose"). But If you do not call the function explicitly, the garbage collection with automagically/implicitly invoke stream_resource_regular_dtor which will lead to a call of php_stdiop_close, just like pclose. – VolkerK Feb 03 '16 at 13:36
  • A child process that doesn't depend on the parent is a daemon (that's how daemonization is achieved, by forking and exiting the main process). You described exactly that, so the question that presents itself is - why do you use `popen` or `proc_open` if what you require is a `pcntl_fork` and some custom work to make children independent on the parent? – Mjh Feb 03 '16 at 16:09
  • @Mjh I was unaware of that function, that might indeed be the better option. It seems it just calls the OS's fork command, right? – Oebele Feb 03 '16 at 16:17
  • Exactly, it will call OS's `fork`. Naturally, it doesn't work with Windows (I'm sure you know that but I'm leaving that sentence for future readers). However, it might be wise to explain what exactly you're trying to do with process control, we might be able to steer you in right direction. – Mjh Feb 03 '16 at 16:21
  • @Mjh: added context in the question – Oebele Feb 04 '16 at 08:37
  • @Mjh: pcntl_fork seems not to be what I want , as I want to run a completely separate piece of software (I wonder how I missed that yesterday). The only relation to the existing process should be a communication channel. Also, from [the PHP docs](http://php.net/manual/en/intro.pcntl.php): "Process Control should not be enabled within a web server environment and unexpected results may happen if any Process Control functions are used within a web server environment." – Oebele Feb 04 '16 at 09:17
  • From what you described, what you're after is exchanging messages between processes. Sockets / ZeroMQ? – Mjh Feb 04 '16 at 09:23
  • @Mjh: yes indeed, that's what I am looking at. Not being able to fork in a web server environment is quite an issue though, and I suppose that applies to anything that starts another process: I expect things like popen also depend on fork... any ideas? Maybe I should keep a server running that can be signaled to create a new instance, or maybe even change my complete architecture to use a persistent server... (PS: we should move this discussion to chat, but I have too little rep...) – Oebele Feb 04 '16 at 09:50
  • I'd love to help, we can move to the chat but the sad fact is that I'm at work for the next 8 hours so it will have to wait until I'm free. In the mean time, maybe someone else can chip in and help you sort the problem out. – Mjh Feb 04 '16 at 10:01

0 Answers0