Play and learn 300 000+ tabs online

Tuesday, June 22, 2010

How to write program to run Only one executable in Unix

The following program to make sure that we are running only one instance of a program in Unix :-) 

The concept is based on the named key Creation when creating any IPC mechanism.

int main()
{
sem_t *mysem;
mysem = sem_open("MyUniqueName", O_CREAT|O_EXCL);
if ( mysem == NULL )
{
fprintf(stderr, "I am already running!\n");
exit(1);
}

/* the app code here*/

/* end of the app */

sem_unlink("MyUniqueName");
sem_close(mysem);
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.