i want to call more than one process from the main program which is written in C++.

how to call them with the most efficient way given that one process may open and close more than one time.

the program also can call processes that are written in different languages other than C++

  • 2
    You may be overthinking the problem. How expensive are these programs to run? How often are they called? You could consider implementing a microservice, that controlled invocation of the other programs, if that suited your architecture. – BobDalgleish Apr 24 '18 at 19:00
  • "i thought that this way will have a lot of overhead because" ... and then follow 3 topics I dont' understand why they should be some explanation of the former. Sorry, but I have no clue what your issues are. Voting to close as "unclear". – Doc Brown Apr 24 '18 at 19:03
  • 1
    @ahmednader: do you have a measureable performance problem? Do you have facts (like measurements) showing you that running those programs in separate processes is causing a bottleneck which could be solved by not starting them as separate processes? Do you actually know your performance requirements? Be aware that we do not know what programs you are talking of, and if you are talking about miliseconds, seconds, minutes or hours of running time. – Doc Brown Apr 24 '18 at 19:16
  • 1
    Well, your granularity needs to be right. If you're standing up a new process that performs some operation that only takes a few milliseconds to complete, then yeah, you're going to take a performance hit, because it takes longer to stand up the process than it does to perform the operation. – Robert Harvey Apr 24 '18 at 19:43
  • 1
    It is operating system specific. Running processes on Windows is very different than on Linux – Basile Starynkevitch Apr 25 '18 at 4:51

This sounds similar to how CGI worked in the earliest days of the internet. Each web request would launch a process to return the contents of the page.

The performance eventually became poor and an alternate solution was found.

One solution was called FastCGI. With a FastCGI interface, the process could be left open and loaded. New requests were fed to the process via stdin and retrieved by stdout, same as a CGI. The primary difference was that the process was not closed at the end of a request.

There are many management issues involved like memory, caching, and unused processes. Looking into how these problems were solved with FastCGI could elude to a solution for you.

  • FastCGI is for web applications. We don't know if OP intend do develop a web app or not – Basile Starynkevitch Apr 25 '18 at 4:51
  • 2
    He says "on every web request". That suggests it's a web app... – Sean Burton Apr 25 '18 at 10:31

Running programs in processes is operating system specific. The only standard way to do that otherwise is by using system which is probably not suitable. You might use some C++ frameworks (like POCO or Qt or perhaps Boost) which provide a similar interface and is cross-platform.

BTW, without more context and application domain, your question is too general. Robotic neurosurgery, web chat systems, chess playing competitions, high-frequency trading, etc... could all need such an approach, but have different constraints and requires different approaches

Assume your OS is Linux or POSIX (details matter).

What you want to do is similar to a specialized Unix shell. You need to be familiar with system calls related to them. So read something about Linux programming, e.g. ALP or something newer.

On Linux, creating a new process (with fork(2)) and executing a program in it (with execve(2)) is usually quite fast (a few milliseconds), but not that much (starting a dozen of programs & processes every second is reasonable, starting thousands of them might not be). Of course, you need to understand other system calls, e.g. wait(2), pipe(2), dup2(2), poll(2) etc...

You could use several pipe(7)-s and have some event loop around poll(2) to handle them.

You could also organize your system with several longer-running programs using some kind of message passing with your monitor. Look into RPC techniques (perhaps JSONRPC)

You could define some client-server model (maybe using HTTP for that, e.g. with client HTTP libraries like libcurl and server HTTP libraries like libonion). You could even define your own application text-based protocol, but then be sure to document it precisely (look into existing protocols like SMTP, MQTT for inspiration). Study also libraries like 0mq or MPI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.