1

When I used proc_open, it returns a resource handle represented as Resource id #4. This code runs on the server, and I want to use this 'handle' again using the id. But after some kind of data transfer between the client and the server, all I got is the resource's id, Resource id #4, instead of the pointer.

Is is possible to recreate the resource with the only id?

dahui
  • 115
  • 1
  • 8

1 Answers1

1

No, this could never reliably work, because in a new request you have no clue whether the resource is even still accessible - in a preforked environment like Apache the second request might be executed by a completely different process, in which accessing the same resource would be impossible on the OS level. Also, in a clustered and/or load balanced environment the second request could even end up on a completely different server.

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
  • But the process created by `proc_open` should not be closed until I use `proc_close`, thus the process is still running. So is it possible to use the process or create a new handle of it when accessible to its process id? – dahui May 06 '13 at 16:13
  • As said, there is probably not even be a relation to the process in question in a regular hosting situation - Apache preforks 10 childs at least in a normal situation, that makes a 10% chance this could work at best. You could use [proc_get_status](http://www.php.net/manual/en/function.proc-get-status.php) to retrieve the PID for the process, and do some stuff with that later on with shell based commands, but that's the best you can get. – Niels Keurentjes May 06 '13 at 16:15
  • That's too bad. Can the resource just be stored somewhere in the memory of the server? When the request comes, I just use the resource stored? – dahui May 06 '13 at 16:20
  • A `Resource` is just something local to the running PHP instance. If you want to store something that'll remain valid on the server for a while, use the `PID` as suggested above. – Niels Keurentjes May 06 '13 at 16:22
  • I see. I will try some shell based commands on the process. It will be quite complicated since I want to use `STDIN` to input a string to the program the process is running and retrieve the result from `STDOUT` – dahui May 06 '13 at 16:28
  • One more question please.. How can I get the `STDIN` and `STDOUT` of a process using its `PID`? – dahui May 07 '13 at 02:24
  • Keep in mind that it's very bad practice to do so, but [here's a workaround](http://stackoverflow.com/questions/12312764/how-can-i-get-a-processs-stdin-by-a-process-id). – Niels Keurentjes May 07 '13 at 02:25
  • I know it's bad. Here is the thing. I'm writing a online coding website and users can compile and run his code on the server. In order to allow the user to use sentences like `scanf("%d",&a)`, the server should be able to interact with the user. I used `proc_open` to do so so the process created can be maintained.. Is there a better way? – dahui May 07 '13 at 02:34
  • I think the proper solution would be to write your own daemon that spawns and manages the processes, and then communicate with that daemon from PHP through sockets or pipes. I think it's the only clean and proper solution. – Niels Keurentjes May 07 '13 at 02:38