简体   繁体   中英

Create a background process in php

I am working on a website where we want users able to create new processes on the server and interact with the server. For example, the user wants to run the C code:

#include<sdio.h>

int main()
{
    int a;
    scanf("%d", &a);
    printf("%d", a);
}

In the server, we create a process and keep it alive until the process stops running. When using functions like exec , we cannot do scanf thing. When using proc_open , we cannot keep the child process alive.. or we can but we don't know how..

The procedure can be: user sends instruction -> server creates a new process to do the instruction -> when doing the instruction, server sends stdout to the web page and gets stdin from the user's input -> process stops running..

I had a bespoke chip and pin solution that fired off a background PHP CLI script using an "&" in the exec call. The front-end and the background script then communicated by simply passing strings via 2 ascii files. This was crude but effective. Obviously both processes had to check/read its incoming file, decide what to do(eg exeute your C++ code on the background process), then respond if necessary, by writing to its output file. Put these three steps into a loop and you have a crude listener/daemon. This was further complicated by the background job having to simultaneously listen to a socket that was the communication channel to the authorisation service. It was a single user scenario but could be adapted to multi-user by using unique files or writing to keyed database tables.

the call would look like this:

exec ("php mydaemon.php 2>&1>/dev/null &");

I'm assuming a Linux server - hence the linux "&" directive for run in background. The 2>&1>/dev/null just supresses stdout and stderr.

Not so sure how you would solve it in Windows - but there must be a Wscript or VBscript equivalent??

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM