Standalone Single Instance in Linux

Given code will help to create standalone single instance of application in Linux operating system.

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

int main()
{
    const char sem_name[] = “tmp”;
    sem_t * mutex;

    printf(“semaphore application started!\n”);
    mutex = sem_open(sem_name, O_CREAT | O_EXCL, 0600, 0);
    if(mutex == SEM_FAILED)
    {
        printf(“semaphore FAILED!\n”);
    }
    else
    {
        printf(“Named Semaphore CREATED!\n”);
        sleep(10);
        sem_unlink(sem_name);
        printf(“semaphore UNLINK!\n”);
    }
    return 0;
}

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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