tags:

views:

79

answers:

5

Hello

I am creating a C program that is called from a shell script when a specific event is occurring. The C program gets an argument from the shell script like this:

> ./c-program.bin HELLO

Now the C program is running until it recieves a specific character as an argument. The problem is that if a second event occurs, and the C program is now called like this:

./c-program.bin WORLD

Then it is a new instance of the program that is started that knows nothing about the string from the first event. What i would like to achieve is something like this:

[EVENT0] ./c-program.bin HELLO
[EVENT1] ./c-program.bin WORLD
[EVENT2] ./c-program.bin *

c-program output:

HELLO WORLD

Any ideas on how to only have one instance of the program? The platform is Linux. The project is in its planning fase therefore, i do not have any specific code so far, i am trying to sort out the different problems first.

+1  A: 

You either need a intermediate app, or probably a separate shell script

The intermediate script would cache the parameters to disk until you pass a '*', at which point it then passes the entire cached string to the final c program.

So your first script would call the second script, passing:

secondscript HELLO
secondscript WORLD
secondscript *

When the second script receives a parameter of * it passes all the previous parameters to your c program

./c-program.bin HELLO WORLD

Compared to the later answers, this still seems by far the easiest option. You could script this in 10 minutes. Pseudocode for the intermediate app/script:

passedstring = args[0]

if passedstring = "*"
    string cache = readcontents(cachefile)
    call c program passing cache
    clearcontents(cachefile)
else
    append(cachefile, passedstring)   

If however you want the challenge of developing a fancy dancin app that can check for another version of itself in memory, then pass stuff around in sockets, go for it :)

PaulG
Hmm, this would make the C program more simple, but i guess that the same problem with multiple instances of the intermediate script will arise, so i have to save the arguments in a file until '*' is recieved?
sver332
@sver332. Yes sorry, I didn't make that clear at all. Cache them to disk, then call the final app when it receives the token that indicates message is complete.
PaulG
+1  A: 

In outline, you have a magical "output everything" argument and want to accumulate all other arguments across multiple calls until you get the request to be magical? Easy enough, but requires some shared state. First hack would put that state in the filesystem (a database would be better, but is more complex).

Donal Fellows
+1  A: 

I would use a pid file stored somewhere. If you start the program, it should unsure that an other one is not running (using the pid file + verification). If not, create a named pipe where he would read data. Then put it in background, thanks to sub-process (fork) for instance.

Then an other instance is started, finding the pidfile and detecting that an instance is already launched. This second program would send argv[i] to the named pipe, so the first instance should print the data.

An other idea is to use a unix socket in a given file, like mysql.sock for instance.

Aif
+1  A: 

Your program should check if another instance is running, and if that is the case pass its argument to that instance, and exit. The first instance to come alive should become the "server", that is stay alive ready to receive data. You can use shared memory or pipes for passing data between processes.

Andy
+1  A: 

What you want isn't very clear. I think you are after a long-running process that accumulates "messages" of some sort, and then allows retrieval of those messages.

Here's one possible solution:

  1. Try to create a fifo at a known location. If this succeeds, fork a daemon that listens on that fifo, accumulating messages until it receives a *, at which point it echos all messages back to the fifo, including the *.
  2. The original process (whether it spawned a daemon or not) opens the fifo for writing, and pushes the message.
  3. If the argument was *, it then reads the fifo and outputs everything it gets until it receives the echoed *.
Marcelo Cantos