如何在Linux系统上用PHP启动后台进程并重复将用户ajax输入写入其stdin

On a new PHP session on a linux web server, I want to start a process (specifically ghostscript) running in the background in console input mode and then repeatedly write new data to its input. This data write needs to occur every time new user data arrives via ajax. The user data is interpreted by the PHP script into postscript commands for ghostscript and then needs to be sent to gs. The gs output will be to a named file. The reason for doing this is that I need speed and want to avoid the overhead of starting gs on each new input from the user. (Once gs is running and waiting for input it takes about 40ms to process my data, but restarting gs and processing the data is taking around 250ms.) Although I can get gs to run in the background and accept input from the shell, I am having difficulty getting it to do this from a PHP script. I've been through all the variations of exec, shell_exec, popen, system, coproc, etc. that I can think of and I seem to be missing something fundamental. Thanks for any insights.

If you have appropriate access rights on the server, may start a socket and make ghostscript accept input from that socket.

E.g.

on a tty:
$ socket -sl 11555

on another tty:
$ nc 0 11555 | ghostscript

Now whatever you write to the socket, gs receives it as input. As your AJAX requests arrive to the server, you can do whatever processing you need to, and then write the resulting postscript commands to the socket from PHP.