Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 4 down vote accepted

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.

share|improve this answer
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). –  MichaelT Mar 22 at 2:41
    
Can you point me to some good resources about programming with network sockets? –  Nickolai Mar 22 at 23:05
    
@Nickolai Sorry, I don't know of any good resources. I've barely had to touch sockets. –  Doval Mar 23 at 1:43

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
share|improve this answer

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.

share|improve this answer
    
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 at 23:35
    
This doesn't answer the question. –  Adam Zuckerman Apr 1 at 4:30

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

share|improve this answer
    
This has nothing to do with my question –  Nickolai Mar 22 at 23:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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