0

I am going to synchronize two different processes on Linux. Both processes wants to get access to a device, which can be used by one process at the same time. Until now, I was using a named semaphore for synchronization and it was operating well, until someone killed process currently operating on device. In this situation semaphore is not being released, which results in hang of all processes trying to acquire the semaphore / access device.

This can be simple solved under Windows with a mutex. In this system, named mutex is automatically released when owning thread/process is terminated.

I will be grateful for all comments and/or solutions on Linux.

Thanks in advance! Toreno

  • Doesn't the Linux process get the equivalent of WM_CLOSE even if its process is killed? Surely, it gets _some_ signal (unless killed by kill -9 or whatever (I forget exactly how Linux works, but killing without warning seems unlikely)) – Mawg says reinstate Monica Feb 04 '15 at 09:35
  • 1
    Have you ever heard about C++ RAII idiom http://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii? It will help you to closing/releasing anything you want automatically. – Sergei Nikulov Feb 04 '15 at 09:42
  • Read [Advanced Linux Programming](http://advancedlinuxprogramming.com/) ; there are many ways to synchronize ([sem_overview(7)](http://man7.org/linux/man-pages/man7/sem_overview.7.html), [flock(2)](http://man7.org/linux/man-pages/man2/flock.2.html), etc....) – Basile Starynkevitch Feb 04 '15 at 10:02
  • The [solution here](http://stackoverflow.com/questions/2053679/how-do-i-recover-a-semaphore-when-the-process-that-decremented-it-to-zero-crashe) suggests a file lock.... – Tony Delroy Feb 04 '15 at 10:41
  • `flock()` or anything that doesn't clean up automatically on process termination with some additional process watching if yours hasn't died (on some socket for e.g.) and doing clean up – zoska Feb 04 '15 at 10:53
  • there are many entries on SO about cleaning up after SIGKILL, try them first. – zoska Feb 04 '15 at 12:00

1 Answers1

0

I solved this using flock as Tony D suggested in previous comment. Here is my code if someone would like to use it in future :)

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

class CriticalSection
{
    private:

        char fileName[FILENAME_MAX];
        int fileDescriptor;


    public:

        CriticalSection(const char *sectionName)
        {
            // Mark as not acquired
            fileDescriptor = -1;

            // Build file name path
            snprintf(fileName, sizeof(fileName), "/tmp/.%s", sectionName);
        }

        ~CriticalSection()
        {
            // Release critical section on object deletion
            release();
        }

        void acquire()
        {
            // Test if section is already acquired
            if(fileDescriptor != -1)
                return;

            // Acquire critical section
            fileDescriptor = open(fileName, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); 
            if(fileDescriptor != -1)
                lockf(fileDescriptor, F_LOCK, 0);
        }

        void release()
        {
            // Release critical section
            if(fileDescriptor != -1) {
                close(fileDescriptor);
                fileDescriptor = -1;
            }
        }
};

int main()
{
    // Create critical section
    CriticalSection cs("myappname");

    // Enter crtitical section
    cs.acquire();

    // Critical section code
    printf("Critical section has been acquired successfully!\n");

    // Leave critical section
    cs.release();
    return 0;
}

Have fun, Toreno

  • Of course, it can be simply modified to offer RAII, by calling acquire() function in last line of the constructor. –  Feb 11 '15 at 08:46
  • And it doesn't need `cs.release()` at the end since the destructor will be called automatically (because `cs` is on stack). This will leave you only with 3 lines: constructing `cs`, `printf` and `return` but you will get `unused variable` warnings from compiler. – Th30n Feb 11 '15 at 09:21