Interaction of two c/c++ programs

*爱你&永不变心* 提交于 2019-12-13 13:24:57

问题


I'm in complete lack of understanding in this. Maybe this is too broad for stack, but here it goes:

Suppose I have two programs (written in C/C++) running simultaneously, say A and B, with different PIDs.

What are the options to make then interact with each other. For instance, how do I pass information from one to another like having one being able to wait for a signal from the other, and respond accordingly.

I know MPI, but MPI normally works for programs that are compiled using the same source (so, it works more for parallel computing than just interaction from completely different programs built to interact with each other).

Thanks


回答1:


You must lookout for "IPC" (inter process communication). There are several types:

  • pipes
  • signals
  • shared memory
  • message queues
  • semaphores
  • files (per suggestion of @JonathanLeffler :-)
  • RPC (suggested by @sftrabbit)
    Which is usually more geared towards Client/Server
    • CORBA
    • D-Bus



回答2:


You use one of the many interprocess communication mechanisms, like pipes (one applications writes bytes into a pipe, the other reads from it. Imagine stdin/stdout.) or shared memory (a region of memory is mapped into both programs virtual address space and they can communicate through it).




回答3:


The same source doesn't matter - once your programs are compiled the system doesn't know or care where they came from.

There are different ways to communicate between them depending on how much data, how fast, one way or bidirectional, predicatable rate etc etc....

The simplest is possibly just to use the network - note that if you are on the same machine the network stack will automatically use some higher performance system to actually send the data (ie shared memory)



来源:https://stackoverflow.com/questions/13103411/interaction-of-two-c-c-programs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!