这是防止一个进程执行两次的做法;
以前写的今天没找到,防止丢失,记录一下!
/*added by wylhistory*/
#include
#include
#include
#include
static Bool exist_mplayer_instance ()
{
char *home=getenv("HOME");
if(!home){
home="/tmp/";
}
char *name=strcat(home,"/.mplayer_proc.pid");
int fd=open(name,O_RDWR|O_APPEND|O_CREAT,S_IRWXU);
if(fd <0){
perror("open error\n");
close(fd);
return TRUE;
}
int val;
if((val = fcntl(fd, F_GETFD, 0) <0 )) {
perror("fcntl");
close(fd);
return TRUE;
}
val |= FD_CLOEXEC;
if(fcntl(fd, F_SETFD, val) <0 ) {
perror("fcntl again");
close(fd);
return TRUE;
}
if(flock(fd,LOCK_EX|LOCK_NB)<0){
perror("flock error\n");
return TRUE;
}
return FALSE;
}
/*above is added by wylhistory*/
int main(int argc,char* argv[]){
/*added by wylhistory for ensure the one instance*/
if(exist_mplayer_instance()){
fprintf(stdout, "there is a mplayer running,so exit");
/*mp_msg(MSGT_CPLAYER, MSGL_FATAL, "there is a mplayer running,so exit");*/
exit(-1);
}
/*above is added by wylhistory*/
dosomething{...}
}
阅读(1462) | 评论(0) | 转发(0) |