tags:

views:

53

answers:

1

Say someone executes the following in a terminal:

echo 'start working' > /etc/.example

and when this is executed, the example program would "start working."

On UNIX(-like) systems, how would something like that be implemented, and what is this kind of behavior called?

+6  A: 

There are two general ways to do this.

One is that the file being written to is actually a named pipe that the program is reading from, and it receives the "start working" string as it would from any other sort of input stream (such as a network socket, or standard input).

The other (specific to Linux) is that the file being written to is a regular file, and the program is monitoring the file for changes using inotify (or dnotify on older systems). When it receives an inotify event indicating that the file has changed, it will read its contents and obtain the "start working" string.

Note that both of these methods require that the program in question is already running in the background (and just not doing anything). There is really no way to launch a program in this manner, unless there is for example a background process responsible for launching programs that is doing one of the above two things.

Tyler McHenry
Thank you! I knew about FIFOs from a while back, but I wasn't able to remember a lot about *nix IPC; I didn't know about inotify. What are the differences between the two methods you described?
Yktula
The main difference is that FIFOs (named pipes) is a stream, and inotify is a filesystem interface. Inotify tells you when files are created, modified, deleted and moved so that your program can respond appropriately (responding to writing some text to a file being one example). It's not necessarily *intended* for IPC but can certainly be used that way. Named pipes are more traditional and work like the standard input descriptor or a network socket; you can `select()` on it, and if you try to read from it when there is no data, the call blocks until there is.
Tyler McHenry