int base_class::getWriteLock(string& filename)
{
int fd=open(filename.c_str(),O_RDWR|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if(fd<0)
{
cerr<<"open "<<filename<<"fail!"<<endl;
return -1;
}
struct flock fl;
fl.l_type=F_WRLCK;
fl.l_start=0;
fl.l_whence=SEEK_SET;
fl.l_len=0;
int retval=fcntl(fd,F_SETLK,&fl);
if(retval==-1&& (errno==EACCES||errno==EAGAIN))
{
fcntl(fd,F_GETLK,&fl);
cerr<<"进程已存在 pid:"<<fl.l_pid<<endl;
}
return retval;
}
{
int fd=open(filename.c_str(),O_RDWR|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if(fd<0)
{
cerr<<"open "<<filename<<"fail!"<<endl;
return -1;
}
struct flock fl;
fl.l_type=F_WRLCK;
fl.l_start=0;
fl.l_whence=SEEK_SET;
fl.l_len=0;
int retval=fcntl(fd,F_SETLK,&fl);
if(retval==-1&& (errno==EACCES||errno==EAGAIN))
{
fcntl(fd,F_GETLK,&fl);
cerr<<"进程已存在 pid:"<<fl.l_pid<<endl;
}
return retval;
}