<p>I have this program, we'll call it Host. Host does all kinds of good stuff, but it needs to be able to accept input through the command line <strong>while it's running</strong>. This means it has to somehow send its other process data and then quit. For example, I need to be able to do this:</p>

<pre><code>./Host --blahblah 3 6 3 5
</code></pre>

<p>This should somehow end up calling some function in Host called</p>

<pre><code>handleBlahBlah(int x1, int y1, int x2, int y2){
  //do some more sweet stuff
}
</code></pre>

<p>Host is a C program, and does not need to support multiple instances.</p>

<p>An example of this is Amarok music player. With Amarok running and playing, you can type "amarok --pause" and it will pause the music.</p>

<p>I need to be able to do this in Linux or Windows. Preferably Linux.</p>

<p>What is the cleanest way to implement this?</p>

[{'answer_id': 10445, 'author': 'Ed S.', 'author_id': 1053, 'author_profile': 'https://Stackoverflow.com/users/1053', 'pm_score': -1, 'selected': False, 'text': '<p>So, I may be missing the point here, but by deafult a C program\'s main function takes two arguments; argc, a count of the number of arguments (at least one), and argv (or arg vector), the argument list.  You could just parse through the arguments and call the correct method.\nFor example:</p>\n\n<pre><code> int main(int argc, *argv[])\n {\n     /*loop through each argument and take action*/\n      while (--argc &gt; 0)\n      {\n           printf(%s%s, *++argv, (argc &gt; 1) ? " " : "");\n      }\n } \n</code></pre>\n\n<p>would print all of the arguments to screen.  I am no C guru, so I hope I haven\'t made any mistakes.</p>\n\n<p>EDIT:  Ok, he was after something else, but it wasn\'t really clear before the question was edited.  Don\'t have to jump on my rep...</p>\n'}, {'answer_id': 10450, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 3, 'selected': False, 'text': '<p>If you were on Windows, I\'d tell you to use a hidden window to receive the messages, but since you used <code>./</code>, I assume you want something Unix-based.</p>\n\n<p>In that case, I\'d go with a <a href="http://en.wikipedia.org/wiki/Named_pipe" rel="noreferrer">named pipe</a>.  Sun has a <a href="http://developers.sun.com/solaris/articles/named_pipes.html" rel="noreferrer">tutorial</a> about named pipes that might be useful.</p>\n\n<p>The program would probably create the pipe and listen.  You could have a separate command-line script which would open the pipe and just echo its command-line arguments to it.</p>\n\n<p>You <em>could</em> modify your program to support the command-line sending instead of using a separate script.  You\'d do the same basic thing in that case.  Your program would look at it\'s command-line arguments, and if applicable, open the pipe to the "main" instance of the program, and send the arguments through.</p>\n'}, {'answer_id': 10451, 'author': 'Stu', 'author_id': 414, 'author_profile': 'https://Stackoverflow.com/users/414', 'pm_score': 3, 'selected': False, 'text': '<p>If it needs to be cross-platform, you might want to consider making the running instance listen on a TCP port, and have the instance you fire up from the command-line send a message to that port.</p>\n'}, {'answer_id': 14352, 'author': 'T Percival', 'author_id': 954, 'author_profile': 'https://Stackoverflow.com/users/954', 'pm_score': 2, 'selected': False, 'text': '<p>I suggest using either a <a href="http://en.wikipedia.org/wiki/Unix_domain_socket" rel="nofollow noreferrer">Unix socket</a> or <a href="http://en.wikipedia.org/wiki/D-Bus" rel="nofollow noreferrer">D-Bus</a>. Using a socket might be faster if you\'re familiar with Unix sockets programming and only want a few operations, whereas D-Bus might make it easier to concentrate on implementing the functionality in a familiar object-oriented way.</p>\n\n<p>Take a look at <a href="http://beej.us/guide/bgipc/output/html/multipage/index.html" rel="nofollow noreferrer">Beej\'s Guide to Unix IPC</a>, particularly the chapter on <a href="http://beej.us/guide/bgipc/output/html/multipage/unixsock.html" rel="nofollow noreferrer">Unix sockets</a>.</p>\n'}, {'answer_id': 2990496, 'author': 'pbernatchez', 'author_id': 336867, 'author_profile': 'https://Stackoverflow.com/users/336867', 'pm_score': 1, 'selected': False, 'text': '<p>What no one has said here is this:\n"you can\'t get there from here".</p>\n\n<p>The command line is only available as it was when your program was invoked.</p>\n\n<p>The example of invoking "<strong>fillinthename</strong> arguments ..." to communicate with <strong>fillinthename</strong> once <strong>fillinthename</strong> is running can only be accomplished by using two instances of the program which communicate with each other.</p>\n\n<p>The other answers suggest ways to achieve the communication.</p>\n\n<p>An <strong>amarok</strong> like program needs to detect the existence of another instance\nof itself in order to know which role it must play, the major role of\npersistent message receiver/server, or the minor role of one shot \nmessage sender.</p>\n\n<p>edited to make the word fillinthename actually be displayed.</p>\n'}, {'answer_id': 3461155, 'author': 'bta', 'author_id': 79566, 'author_profile': 'https://Stackoverflow.com/users/79566', 'pm_score': 0, 'selected': False, 'text': '<p>One technique I have seen is to have your <code>Host</code> program be merely a "shell" for your real program.  For example when you launch your application normally (e.g.: <code>./Host</code>), the program will fork into the "main app" part of your code.  When you launch your program in a way that suggests you want to signal the running instance (e.g.: <code>./Host --send-message restart</code>), the program will fork into the "message sender" part of your code.  It\'s like having two apps in one.  Another option that doesn\'t use <code>fork</code> is to make <code>Host</code> purely a "message sender" app and have your "main app" as a separate executable (e.g.: <code>Host_core</code>) that <code>Host</code> can launch separately.</p>\n\n<p>The "main app" part of your program will need to open up some kind of a communication channel to receive messages, and the "message sender" part will need to connect to that channel and use it to send messages.  There are several different options available for sending messages between processes.  Some of the more common methods are <a href="http://www.tldp.org/LDP/lpg/node15.html" rel="nofollow noreferrer">pipes</a> and <a href="http://www.kernel.org/doc/man-pages/online/pages/man2/socket.2.html" rel="nofollow noreferrer">sockets</a>.  Depending on your OS, you may have additional options available; for instance, QNX has <a href="http://www.qnx.com/developers/docs/6.3.2/neutrino/lib_ref/c/channelcreate.html" rel="nofollow noreferrer">channels</a> and BeOS/Haiku have <a href="http://api.haiku-os.org/classBMessage.html" rel="nofollow noreferrer">BMessages</a>.  You may also be able to find a library that neatly wraps up this functionality, such as <a href="http://code.google.com/p/lcm/" rel="nofollow noreferrer">lcm</a>.</p>\n'}]
['https://Stackoverflow.com/questions/10443', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/432/']