3

I’ve written a small database engine in C that works by reading commands you input in the console and it outputs the result. Is there any way of me writting some PHP code that could send arguments to the console and recieve the output back to PHP without restarting the compiled program. Is there a better way of doing this?

5
  • proc_open Commented Dec 5, 2018 at 21:26
  • You have two options: First, use shell_exec() function to invoke a program in the command line. Second, open a socket on your database engine, and send request through it. Commented Dec 5, 2018 at 21:28
  • @Sakura Kinomoto: he's asking about communicating with a database engine he wrote, which communicates over stdin/stdout. Neither of those options would do what he wants. Commented Dec 5, 2018 at 21:39
  • shell_exec are valid for what he wants. The function returns the output to the program on stdout, but the program needs to accept parameters from call parameters. The second option requires to code a change on the engine, but its a better option to do it. Commented Dec 5, 2018 at 21:42
  • It doesn't sound like the program accepts arguments, because then you would obviously need to restart it to send new requests. proc_open would work similarly to what you're describing (though simpler), and wouldn't require a code change in the engine. You get a set of handles for stdin/stdout (and stderr) and you use them to communicate with a long-running process… Commented Dec 5, 2018 at 21:54

1 Answer 1

1

You say you want the PHP to send and receive messages to your program without restarting the compiled program.

So I don't think using shell_exec or proc_open will work how you want, since these commands both load a fresh instance of the compiled program.

Instead, I suggest you look into sockets, and how you would rewrite your database engine to use those instead of STDIN/STDOUT. Then you can use PHP's socket functions to communicate between your applications. And you'll have just one instance of your compiled program running in the background, even with multiple hits to your PHP script.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.