3

What I'm wondering is how running programs communicate with each other, and if someone could post some sample code for how to do this, so I can try it out myself, just for educational purposes.

For example, I've worked with databases before, and in my code I always have to "establish a connection to the database." The database service has to be running before I start my program, or else it will fail. What exactly is going on with that connection and how does it work?

4 Answers 4

6

Any sort of communication with the outside world is ultimately mediated by the operating system. There's various mechanisms available for Inter-process communication, but pipes and network sockets are probably some of the most common. If you've ever piped the output of one program into another on a command line shell, those processes were communicating with a pipe. The connection to the database you mentioned is using a network socket, perhaps with a custom protocol. For communicating separate machines in a fairly universal way, you'd probably go with a web service, which generally means passing information through HTTP and possibly XML.

3
  • 2
    As an aside, MySQL will try to connect to a unix socket file if the host is localhost (see dev.mysql.com/doc/refman/5.5/en/connecting.html ) rather than through the loopback to improve performance (no TCP overhead).
    – user40980
    Mar 22, 2014 at 2:41
  • Can you point me to some good resources about programming with network sockets?
    – Nickolai
    Mar 22, 2014 at 23:05
  • @Nickolai Sorry, I don't know of any good resources. I've barely had to touch sockets.
    – Doval
    Mar 23, 2014 at 1:43
1

There high-level communication possibilities:

  1. Remote Procedure Calls (Web Services, CORBA etc. included)
  2. File Transfer
  3. Shared Database
  4. Messaging (AMQP, ZeroMQ etc.)

There are also low-level communication possibilities:

  1. Sockets
  2. Pipes
  3. Shared-memory
  4. Signal
0

This is not such an easy question to be answered because it envolves distinct types of work.

For the case of a database, for example, it's usually a connection to a server through TCP/IP protocol, and the database needs to be running because the server needs to be connected so that it receives the request from your code (think that you can't access an offline website).

However, for the case of other programs, it depends a lot. If it's just a module written in the same language, you could just import. If it's a Unix process, you could call system function passing the command. If you want something to be multi-platform, you could use a Restful or a SOAP web service (that'd require internet connection), and so on.

2
  • Well, in my example the database was running locally, so offline/online isn't relevant. Also, import is an example of one program using another program's code. It has nothing to do with one program calling another one
    – Nickolai
    Mar 22, 2014 at 23:35
  • This doesn't answer the question. Apr 1, 2014 at 4:30
0

To ensure that the database service is running , You can either check whether the service is running, and start or stop it accordingly.

for Windows you should use Windows API to do that accordingly.

here is the example code

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686315(v=vs.85).aspx

and also a different approach here

http://code.msdn.microsoft.com/windowsdesktop/CppWindowsService-cacf4948

1
  • This has nothing to do with my question
    – Nickolai
    Mar 22, 2014 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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