Differences between revisions 3 and 4
Revision 3 as of 2009-03-25 10:36:37
Size: 2264
Editor: pgas
Comment: A attempt to generalize faq #27
Revision 4 as of 2009-03-25 12:38:25
Size: 2302
Editor: GreyCat
Comment: clean up a bit
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
Line 5: Line 4:
stdin/stdout to communicate, some form of inter-process communication stdin/stdout to communicate; some form of inter-process communication (IPC)
Line 10: Line 9:
Process A write in a file, Process B reads the file. This method is not synchronized and as so is not
safe if B can read the file while A writes in it, a lockdir or a
Process A writes in a file, and Process B reads the file. This method is not synchronized and therefore is not
safe if B can read the file while A writes in it.  A lockdir or a
Line 16: Line 15:
mkdir to test for the existences of a dir and creates it in one
atomic operation, it thus can be used as a lock, not a very
`mkdir` can be used to test for the existence of a dir and create it in one
atomic operation; it thus can be used as a lock, although not a very
Line 42: Line 41:
for more examples with a lock directory   for more examples with a lock directory.
Line 77: Line 76:
you run more than one instance of scriptA you run more than one instance of scriptA.
Line 81: Line 80:
Named pipes is a much richer form of IPC they are described on their
own page: NamedPipes
Named pipes are a much richer form of IPC. They are described on their
own page: NamedPipes.

How can two unrelated processes communicate?

Two unrelated processes cannot use the arguments, the environment or stdin/stdout to communicate; some form of inter-process communication (IPC) is required.

A file

Process A writes in a file, and Process B reads the file. This method is not synchronized and therefore is not safe if B can read the file while A writes in it. A lockdir or a signal can probably help

A directory as a lock

mkdir can be used to test for the existence of a dir and create it in one atomic operation; it thus can be used as a lock, although not a very efficient one.

Script A:

    until mkdir /tmp/dir;do # wait until we can create the dir
      sleep 1
    done
    echo foo > file         # write in the file this section is critical
    rmdir /tmp/dir          # remove the lock

Script B:

    until mkdir /tmp/dir;do #wait until we can create the dir
      sleep 1
    done
    read var < file         # read in the file this section is, critical
    echo "$var"             # Script A cannot write in the file
    rmdir /tmp/dir          # remove the lock

See Faq #45 and mutex for more examples with a lock directory.

Signals

Signals are probably the simplest form of IPC:

ScriptA:

    trap 'flag=go' USR1 #set up the signal handler for the USR1 signal

    # echo $$ > /tmp/ScriptA.pid #if we want to save the pid in a file

    flag=""
    while [[ $flag != go ]]; do # wait for the green light from Script B
      sleep 1;
    done
    echo we received the signal

You must find or know the pid of the other script to send it a signal using kill:

     #kill all the
     pkill -USR1 -f ScriptA 
     
     #if ScriptA saved its pid in a file
     kill -USR1 $(</var/run/ScriptA.pid)

     #if ScriptA is a child:
     ScriptA & pid=$!
     kill -USR1 $pid

The first 2 methods are not bullet proof and will cause trouble if you run more than one instance of scriptA.

Named Pipes

Named pipes are a much richer form of IPC. They are described on their own page: NamedPipes.

BashFAQ/027 (last edited 2010-05-11 05:25:30 by p5B09615D)