Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mechanism to syncronise processes

Status
Not open for further replies.

ringd

Programmer
Jul 11, 2000
35
GB
Hello!

I'm trying to find a method where I can syncronise 2 unrelated processes. This is so that the 2 processes are able to use the same "resource" - one process will wait whilst the other has hold of it.

Any suggestions would be appreciated!

Cheers,
Dave.
 


Create a "signal" file which when present will prevent the other process from execuiting.

Other proceess will check for presence of "signal" file and not execute if it exists.
[3eyes]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Hello!

Thanks for replying. I thought of doing that but there is a very small window between checking the file does not exist then creating it which could cause the mechanism to fail.

If the process was delayed / interupted during this window and the the other process was to jump in, then there would be problems.

One thing I have tested (but it did not work) was to create a "lock" file using touch. I was hoping that it would indicate if a file already exists (by the return code) but it doesn't.

Any more suggestions would be welcome!

Cheers,
Dave.
 
How about this:
Each process (eg proc1 and proc2) creates its own unique "lock file" (eg /path/to/lock_file_proc1 ) before it checks to see how many "lock files" are present. If there is more than one "lock file", then it removes its own and waits an appropriate amount of time before trying again. If there is only one "lock file" then the process uses the "resource" and last thing before it finishes it removes its own "lock file".
One problem that may occur due to system crashes or unforeseen process termination or interruption is that of "lock files" being present when no process is running. So each process (while waiting 'an appropriate amount of time before trying again') could check to see that the other process is running and if not then remove the other "lock file".

I hope that helps.

Mike
 
The "[tt]open()[/tt]" system call can do an "exclusive create", which is used for creating lock files. Even if two processes go to get the flock file at the same time, the low level open/create is atomic enough that one will get it and the other will fail.

Here's some C code from the man page for "open", slightly modified for this example (with no error checking)...
Code:
/* lockfile.c - check lock by trying to create a lock file */
/* From the man page for "open" */
/* Compile: [g]cc lockfile.c -o lockfile */

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

#define LOCKFILE "/tmp/mylockfile"

int main(int argc, char ** argv)
{
    exit( open(LOCKFILE, O_WRONLY | O_CREAT | O_EXCL,
        S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1 );
}
Then, your code would look like this to use it...
Code:
#!/bin/ksh

while ( ! lockfile )
do
    print "Didn't get lock, waiting: $(date)"
    sleep 5
done

# You got the lock, do what you want to with the resource
print "GOT THE LOCK! $(date)"

# Remove the lock file when done so others can play
rm /tmp/mylockfile
You could also modify it to take the filename as a command line parameter to be more flexible. Also maybe add some error checking.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top