Chinaunix首页 | 论坛 | 博客
  • 博客访问: 371639
  • 博文数量: 43
  • 博客积分: 1493
  • 博客等级: 上尉
  • 技术积分: 660
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-01 10:57
文章分类

全部博文(43)

文章存档

2015年(1)

2013年(1)

2011年(6)

2010年(13)

2009年(13)

2008年(9)

分类: LINUX

2011-04-09 11:26:27

这是防止一个进程执行两次的做法;
以前写的今天没找到,防止丢失,记录一下!

/*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{...}
}
阅读(1399) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~