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"); exit(EXIT_FAILURE); } ECHO_MSG("Attempting to acquire write lock"); if( fcntl(fileno(fp), F_SETLK, new_wt_lock) == -1) { ECHO_MSG("Unable to acquire lock"); perror("error"); exit(EXIT_FAILURE); } ECHO_MSG("Doing random shit..."); sleep(25); ECHO_MSG("Stopped doing random shit"); if(fcntl(fileno(fp), F_UNLCK, new_wt_lock) < 0) { perror("Error releasing lock"); exit(EXIT_FAILURE); } ECHO_MSG("Released Lock...Exiting"); }
To test its operation,
$ gcc getlock.c -o getlock
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
Hope you enjoyed this simple locking solution!

