01: While(-f counter.lock){
02: select(undef,undef,undef,0.1);}
03: open(LOCKFILE,">counter.lock);
04: dbmopen(%COUNTERS, $DOCUMENT_ROOT/DBM_FILES/counters,0666);
05: if(!(defined($counters{'my_counter'})){
06: $counters{'my_counter'}=0;}
07: $counters{'my_counter'})++;
08: $count=$counters{'my_counter'};
09: dbmclose (counters);
10: close(LOCKFILE);
11: unlink(counter.lock);
首先检查锁定标志文件是否存在,如果存在,就说明另一个进程正在使用该文件,于是等待直到该文件(此处命名为counter.lock)不存在为止。此处用select()的特殊形式循环等待,此语句使程序进入休眠状态一段时间,该时间段由最后一个参数定义。之所以不用sleep()函数是因为其基本单位为秒,对这种文件锁定而言太长了,几个微秒就足够了。
02: select(undef,undef,undef,0.1);}
03: open(LOCKFILE,">counter.lock);
04: dbmopen(%COUNTERS, $DOCUMENT_ROOT/DBM_FILES/counters,0666);
05: if(!(defined($counters{'my_counter'})){
06: $counters{'my_counter'}=0;}
07: $counters{'my_counter'})++;
08: $count=$counters{'my_counter'};
09: dbmclose (counters);
10: close(LOCKFILE);
11: unlink(counter.lock);
首先检查锁定标志文件是否存在,如果存在,就说明另一个进程正在使用该文件,于是等待直到该文件(此处命名为counter.lock)不存在为止。此处用select()的特殊形式循环等待,此语句使程序进入休眠状态一段时间,该时间段由最后一个参数定义。之所以不用sleep()函数是因为其基本单位为秒,对这种文件锁定而言太长了,几个微秒就足够了。
回复Comments
{commenttime}{commentauthor}
{CommentUrl}
{commentcontent}