why you not use ipcs or semaphores ??
#include <stdio.h>
#ifndef IpcMax
#define IpcMax 2048
#endif
#ifdef MAIN
int myusage(char *);
extern int ipc_lock(int,int,char *,char *);
#else
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/errno.h>
#include <time.h>
extern char *sys_errlist[];
extern int errno;
#endif
/* request for locking facility - ipc */
#define LOCKipc 'f'
#define KILLipc 'k'
#define ASKSipc 'q'
#define READipc 'r'
#define WRITipc 'w'
#define VERBipc 's'
/******** the code above should be in a xxxinclude/ipc_request.h file ******/
#include <xxxinclude/ipc_request.h>
#ifdef MAIN
int main(int argc, char **argv)
{
extern char *basename(char *);
char *myname = basename(*argv);
if(argc != 3) exit(myusage(myname));
++argv; argc = 0;
if(**argv != '-') exit(myusage(myname));
for(++*argv; **argv; ++*argv){
switch(**argv){
case VERBipc:
argc = 1; break;
case LOCKipc:
case KILLipc:
exit(myipc_lock(**argv,argc,*++argv,""

);
case ASKSipc:
case READipc:
exit(myipc_lock(**argv,-1,*++argv,""

);
default:
break;
}
}
exit(myusage(myname));
}
int myusage(char *name)
{
fprintf(stderr,"\nUSAGE: %s -[%c]%c exec-name",name,VERBipc,ASKSipc);
fprintf(stderr,"\n query ipc-lock status for exec-name"

;
fprintf(stderr,"\nUSAGE: %s -[%c]%c exec-name",name,VERBipc,LOCKipc);
fprintf(stderr,"\n force lock ipc for exec-name"

;
fprintf(stderr,"\nUSAGE: %s -[%c]%c exec-name",name,VERBipc,KILLipc);
fprintf(stderr,"\n unlock/remove ipc for exec-name"

;
fprintf(stderr,"\n NOTA: use '%c' for silent-mode",VERBipc);
fprintf(stderr,"\n to do this by hand, get the qid, enter: ipcs -q"

;
fprintf(stderr,"\n then kill the ipc, enter: ipcrm -q <qid>"

;
return(1);
}
#else /* ifdef MAIN this comment is used, do not delete !!! */
int how2unlock(char *name, char *interpreter, char *exec, char *ipcid)
{
printf("\n\n\t*** ERROR ***\n"

;
printf("\n\t'%s' is a SINGLE-USER application !!!",name);
printf("\n\tit uses the 'ipcs'unix-feature for locking, to check this enter"

;
printf("\n\n\tipcs -q\n"

;
printf("\n\tyou will get on stdout, the 'ipcs'queue id = %s.",ipcid);
printf("\n\tPlease check if '%s' is running, to do this enter",name);
printf("\n\n\tps -ef|grep %s\n",exec);
printf("\n\t'%s' is RUNNING if you get a line like:",name);
printf("\n\t<any strings> %s <any strings> %s",interpreter,exec);
printf("\n\n\tThe reason of this message even if '%s' is NOT RUNNING:",name);
printf("\n\tthe last session of '%s' was killed by 'kill', so it got",name);
printf("\n\tno chances to cleanup his 'ipcs'lock, this is still in memory."

;
printf("\n\tThe only way to unlock is to manually kill the 'ipcs' to do this enter"

;
printf("\n\n\tipcrm -q %s\n",ipcid);
printf("\n\tExiting...\n\n"

;
return(1);
}
int myipc_lock(int wie, int verb,char *name, char *was)
{
struct msqid_ds dummy;
struct { long type ; char mssg[IpcMax+1]; } ipc;
register int qid, len;
key_t mykey_que();
if(0 >(qid = msgget(mykey_que(name),IPC_CREAT|0666))){
if(verb) fprintf(stderr,"myipc_lock[%d] ERROR %d %s\n",__LINE__,errno,sys_errlist[errno]);
return(-1);
}
ipc.type = 1L; memset(ipc.mssg,NULL,IpcMax);
len = 1+msgrcv(qid,(char *)&(ipc.type),IpcMax,ipc.type,IPC_NOWAIT);
switch(wie){
case LOCKipc:
strcpy(ipc.mssg,name); len = 1;
break;
case WRITipc:
strcpy(ipc.mssg,was); len = 1;
break;
case READipc:
if(0 >verb) printf("[ %d ] %s\n",qid,ipc.mssg);
else strcpy(was,ipc.mssg);
break;
case ASKSipc:
if(verb){
if(!len) printf("\n\t*** %s: UNLOCKED ***\n\n",name);
else{
printf("\n\t[ %d ] *** LOCKED by %s ***\n\n",qid,ipc.mssg);
break;
}
}
if(len){ sprintf(was,"%d",qid); break; }
case KILLipc:
if(!msgctl(qid,IPC_RMID,&dummy)) return(0);
if(verb) fprintf(stderr,"myipc_lock[%d] ERROR %d %s\n",__LINE__,errno,sys_errlist[errno]);
return(-1);
}
/*** write or restore message removed by 'msgrcv' ***/
if(len && msgsnd(qid,&ipc,strlen(ipc.mssg),0)){
if(verb) fprintf(stderr,"myipc_lock[%d] ERROR %d %s\n",__LINE__,errno,sys_errlist[errno]);
return(-1);
}
return(len);
}
key_t mykey_que(register char *input)
{
key_t key = (key_t)0;
register int pos = 0;
while(sizeof(key_t) >pos && input[pos]) key = (key<< 8) | input[pos++];
return(key);
}
#endif
/******** the code above is your ipc-lock */
/******** here your main exec using the lock mechanisme above ***/
#define MYIPCNAME "abcdefgh"
#include <xxxinclude/ipc_request.h>
int main(int argc, char **argv)
{
extern int myipc_lock(int,int,char *,char *);
extern char *basename(char *);
char buff[MaxBuff+1];
myname = basename(*argv);
if(myipc_lock(ASKSipc,0,MYIPCNAME,buff))
exit(how2unlock(myname,"/bin/sh",MYIPCNAME,buff));
if(myipc_lock(WRITEpc,0,MYIPCNAME,buff)) exit(1);
/*
your code
*/
exit(myipc_lock(KILLipc,0,MYIPCNAME,""

);
}
/***************************************
write it in (exemple) myipc_lock.c myexec.c
then link: ln -s myipc_lock.c .omyipc_lock.c
complile: (g)cc YourOptions -o .omyipc_lock.o .omyipc_lock.c
make libs: /usr/ccs/bin/ar rv libAAA.a .omyipc_lock.o
complile: (g)cc YourOptions -DMAIN -L. -o myipc_lock myipc_lock.c -llibAAA.a
complile: (g)cc YourOptions -DMAIN -L. -o myexec myexec.c -lAAA -llibAAA.a
********/