Good afternoon, there is the following question: how best to organize the interaction between multiple processes on the same machine? The situation is the following: there are several written system modules on qt5. Some modules display the big picture. Others allow you to edit line items (objects). We need the following interaction in the general list of the selected object If the program for editing it was not launched, then the program is started, and after that it is given a message for editing. If it was launched, it just sends a message for editing.

After editing, the program sends a message to the list that an object must be updated.

What is the easiest way to organize this functionality?

  • Lots of options. You can look at TCP / UDP, then you can spread it to several machines. - Vladimir Martyanov
  • if on one machine, you can use QLocalServer, although I had cases that the messages were lost, but QTcpSocket worked fine. - Alexander
  • How to implement it is easier - the question is not correct, since there is no definite answer to it - this is a matter of taste for everyone, for everyone a “simpler” one. - Mira
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦

3 answers 3

There are several options to do this, they are all described in detail in the documentation and collected there in one place: Inter-Process Communication in Qt . And which one to choose is a matter of taste and situation, and a long debate :) For example, for me, the best thing was d-bus

  • d-bus is a Linux-only solution, so the author will not work - he has windows - ixSci
  • Already have a d-bus and on Windows. On off site proof. And in qt as a cross-platform system, only cross-platform tools are implemented. (@ixSci Thank you very much for editing the answer, I didn’t work out beautifully with the tablet :)) - Mira
  • Hmm, really, a port appeared on Windows. And how does it work normally? And about qt and only cross-platform solution - you are mistaken. The same d-bus implementation in Qt is already a cloud of years and before there was no port on windows. - ixSci
  • It works well :) and the version that only crossplatform chips are on kute is very important to me The answer was written by people with considerable reputations :) this is probably from the category of urban legends) - Mira
  • Probably we will use D-BUS if it starts up under windows - Efimov Evgeny

Since you need to implement interaction within a single machine, the best option would be to use shared memory. With the help of boosts you can realize such communication without particularly serious problems. Example:

 #include <boost/interprocess/shared_memory_object.hpp> #include <boost/interprocess/mapped_region.hpp> #include <cstring> #include <cstdlib> #include <string> int main(int argc, char *argv[]) { using namespace boost::interprocess; if(argc == 1){ //Parent process //Remove shared memory on construction and destruction struct shm_remove { shm_remove() { shared_memory_object::remove("MySharedMemory"); } ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); } } remover; //Create a shared memory object. shared_memory_object shm (create_only, "MySharedMemory", read_write); //Set size shm.truncate(1000); //Map the whole shared memory in this process mapped_region region(shm, read_write); //Write all the memory to 1 std::memset(region.get_address(), 1, region.get_size()); //Launch child process std::string s(argv[0]); s += " child "; if(0 != std::system(s.c_str())) return 1; } else{ //Open already created shared memory object. shared_memory_object shm (open_only, "MySharedMemory", read_only); //Map the whole shared memory in this process mapped_region region(shm, read_only); //Check that memory was initialized to 1 char *mem = static_cast<char*>(region.get_address()); for(std::size_t i = 0; i < region.get_size(); ++i) if(*mem++ != 1) return 1; //Error checking memory } return 0; } 

More here

  • 2
    Although the answer can be considered useful, it is still a bad one. In a Qt man, why do you give him a boost solution? Qt has the same features as presented here. - ixSci

If the factor of ease of implementation of the interprocess communication described in the question is key, then you can take as a basis the tacit rule that every single “message” should be edited in the newly created separate process (even the same editor application).

When you start the editor process, it will suffice to pass the "message" to it as a command line argument via QProcess . You can get the data back through the standard reading channel, to which the editor will send the result.

Disadvantages: the launch of a separate process-editor for each message, which in the case of a "heavy" editor, as well as the large number of simultaneous edits, may be unacceptable.

Plus: probably the easiest way in Qt.

  • It does not fit the author, because not always one application runs from another. Sometimes the second is already running. - Mira
  • @Mira, yes, I know, that's why I put the first word in my answer: "If." - alexis031182
  • Unfortunately, running a separate process on every edit is too much luxury. - Efimov Yevgeny