Synchronizing multiple processes

So, I’m at work and my teammate comes over and asks me if I know a solution to synchronize multiple processes such that only one of them can have access to a resource(say a file) at a time. My immediate thought was – a Mutex! But wait, don’t the processes have to share the same address space(which would work in the case of threads spawned by the same process). While thinking about the same, I realized that apt has a similar kind of lock, where it would not let me run multiple apt processes at the same time, because it has a lock on a file. But I had no idea how this was implemented – because for one, the main question that was at the back of my mind was – is a write operation on a file an atomic operation? After some googling, I found the docs and wrote a simple program to demonstrate its use.

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

#define ECHO_MSG(x) printf("%s\n", x)

int main(int argc, char *argv[])
    const char filepath[30] = "/Users/darthvader/testlock";
    struct flock *new_wt_lock = (struct flock *) malloc(sizeof(struct flock));
    FILE *fp;
    memset(new_wt_lock, 0, sizeof(struct flock));
    new_wt_lock->l_type = F_WRLCK;
    new_wt_lock->l_len = 0;
    fp = fopen(filepath, "w+");
    if(fp == NULL)
        perror("Error opening file for writing");
    ECHO_MSG("Attempting to acquire write lock"); 
    if( fcntl(fileno(fp), F_SETLK, new_wt_lock) == -1)
      ECHO_MSG("Unable to acquire lock");
    ECHO_MSG("Doing random shit...");
    ECHO_MSG("Stopped doing random shit");
    if(fcntl(fileno(fp), F_UNLCK, new_wt_lock) < 0)
        perror("Error releasing lock");
    ECHO_MSG("Released Lock...Exiting");

To test its operation,

$ gcc getlock.c -o getlock

Screen Shot 2013-03-09 at 7.13.45 PM

Open 2 terminals and run the same program.  You will notice that the  latter cannot lock the file. Try it again after the program   exits and it  works like a charm

Screen Shot 2013-03-09 at 7.13.21 PM.

Hope you enjoyed this simple locking solution!


2 comments on “Synchronizing multiple processes

  1. Great stuff! However, there seems to be a cleaner way given in Unix Network Programming by W. R. Stevens. The designers of Unix had a lot of foresight and built in file-locking mechanisms right into the system!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s