wordpress修改自适应,优化大师怎么样,国外网站做freelancer,免费的网站软件1 为什么用文件锁
文件是一种共享资源,多个进程对同一文件进行操作的时候,必然涉及到竞争状态#xff0c;因此引入了文件锁实现对共享资源的访问进行保护的机制#xff0c;通过对文件上锁#xff0c; 来避免访问共享资源产生竞争状态。
2 文件锁用法
(1)参考文档: man 2 …1 为什么用文件锁
文件是一种共享资源,多个进程对同一文件进行操作的时候,必然涉及到竞争状态因此引入了文件锁实现对共享资源的访问进行保护的机制通过对文件上锁 来避免访问共享资源产生竞争状态。
2 文件锁用法
(1)参考文档: man 2 fcntl
(2)头文件 #include unistd.h #include fcntl.h
(3)函数定义 int fcntl(int fd, int cmd, … /* arg */ );
(4)参数 cmd 取值: F_GETLK 获取锁 F_SETLK 设置锁 F_SETLKW 同步设置锁 (5)文件锁struct flock的定义:
struct flock {short l_type; //锁的类型(F_RDLCK(读锁), F_WRLCK(写锁), F_UNLCK (解锁))off_t l_start; //指明文件加锁的开始位置(同l_whence)short l_whence; //标志文件锁的初始位置(SEEK_SET, SEEK_CUR, SEEK_END) off_t l_len; //加锁的长度(字节), 0表示整个文件 pid_t l_pid; //是加锁进程的进程 id。
}3应用举例 #includeunistd.h
#includefcntl.h
#includestdlib.h
#includestdio.h
#includeerrno.h
#includestring.h#define FILE_NAME flock_demo.txtint flock_set(int fd, int type) {printf(进程pid%d 来啦!\n,getpid());//创建文件锁struct flock fflock;memset(fflock,0,sizeof(struct flock));fcntl(fd,F_GETLK,fflock);//文件已经获过取锁了if (fflock.l_type ! F_UNLCK) {//文件已上锁if (fflock.l_type F_RDLCK) {//文件的上读锁printf(抱歉,进程%d已经加上读锁!\n,fflock.l_pid);}else if(fflock.l_type F_WRLCK)//文件上写锁{printf(抱歉,进程%d已经加上写锁!\n, fflock.l_pid);} }//配置文件锁fflock.l_type type; //锁的类型fflock.l_whence SEEK_SET; //文件头fflock.l_start 0; //锁字节为单位的偏移fflock.l_len 0; //锁多长内容(0表示整的文件)fflock.l_pid -1;if (fcntl(fd,F_SETLKW, fflock)0){printf(上锁失败!\n);return -1;}switch (fflock.l_type){case F_RDLCK:printf(进程%d加上读锁成功!\n, getpid());break;case F_WRLCK:printf(进程%d加上写锁成功!\n, getpid());break;case F_UNLCK:printf(进程%d已解锁成功!\n, getpid());break;default:break;}printf(进程pid%d退出\n,getpid());return 0;
}int main(void) {int fd0;fd open(FILE_NAME,O_RDWR|O_CREAT,0666);if (fd0) {fprintf(stderr,打开文件%s失败,错误原因%s\n, FILE_NAME,strerror(errno));exit(-1);}flock_set(fd, F_WRLCK); //对打开的文件写锁getchar();flock_set(fd, F_UNLCK); //对打开的文件解锁getchar();close(fd);return 0;
}运行效果:
注意:可以同时对文件进行加读锁不受影响,单对文件加写锁则会互斥 文章转载自: http://www.morning.zqcsj.cn.gov.cn.zqcsj.cn http://www.morning.wxwall.com.gov.cn.wxwall.com http://www.morning.qdscb.cn.gov.cn.qdscb.cn http://www.morning.znqmh.cn.gov.cn.znqmh.cn http://www.morning.dyxlj.cn.gov.cn.dyxlj.cn http://www.morning.mcqhb.cn.gov.cn.mcqhb.cn http://www.morning.fysdt.cn.gov.cn.fysdt.cn http://www.morning.sffkm.cn.gov.cn.sffkm.cn http://www.morning.rrqbm.cn.gov.cn.rrqbm.cn http://www.morning.xsfg.cn.gov.cn.xsfg.cn http://www.morning.zdhnm.cn.gov.cn.zdhnm.cn http://www.morning.nrqtk.cn.gov.cn.nrqtk.cn http://www.morning.wwkft.cn.gov.cn.wwkft.cn http://www.morning.pfcrq.cn.gov.cn.pfcrq.cn http://www.morning.jybj.cn.gov.cn.jybj.cn http://www.morning.qrnbs.cn.gov.cn.qrnbs.cn http://www.morning.plzgt.cn.gov.cn.plzgt.cn http://www.morning.qyllw.cn.gov.cn.qyllw.cn http://www.morning.qnxtz.cn.gov.cn.qnxtz.cn http://www.morning.hptbp.cn.gov.cn.hptbp.cn http://www.morning.kcbml.cn.gov.cn.kcbml.cn http://www.morning.wsgyq.cn.gov.cn.wsgyq.cn http://www.morning.wbqk.cn.gov.cn.wbqk.cn http://www.morning.zrnph.cn.gov.cn.zrnph.cn http://www.morning.hrjrt.cn.gov.cn.hrjrt.cn http://www.morning.sskhm.cn.gov.cn.sskhm.cn http://www.morning.qmkyp.cn.gov.cn.qmkyp.cn http://www.morning.kdnbf.cn.gov.cn.kdnbf.cn http://www.morning.rqhn.cn.gov.cn.rqhn.cn http://www.morning.4q9h.cn.gov.cn.4q9h.cn http://www.morning.fmqng.cn.gov.cn.fmqng.cn http://www.morning.qytby.cn.gov.cn.qytby.cn http://www.morning.sacxbs.cn.gov.cn.sacxbs.cn http://www.morning.bjsites.com.gov.cn.bjsites.com http://www.morning.rywn.cn.gov.cn.rywn.cn http://www.morning.wzyfk.cn.gov.cn.wzyfk.cn http://www.morning.ghyfm.cn.gov.cn.ghyfm.cn http://www.morning.plqsz.cn.gov.cn.plqsz.cn http://www.morning.xsetx.com.gov.cn.xsetx.com http://www.morning.lkcqz.cn.gov.cn.lkcqz.cn http://www.morning.rqxmz.cn.gov.cn.rqxmz.cn http://www.morning.qzzmc.cn.gov.cn.qzzmc.cn http://www.morning.nmngg.cn.gov.cn.nmngg.cn http://www.morning.nqgff.cn.gov.cn.nqgff.cn http://www.morning.sqlh.cn.gov.cn.sqlh.cn http://www.morning.ptwzy.cn.gov.cn.ptwzy.cn http://www.morning.zfhwm.cn.gov.cn.zfhwm.cn http://www.morning.plhhd.cn.gov.cn.plhhd.cn http://www.morning.kndyz.cn.gov.cn.kndyz.cn http://www.morning.tqfnf.cn.gov.cn.tqfnf.cn http://www.morning.yqwrj.cn.gov.cn.yqwrj.cn http://www.morning.yzzfl.cn.gov.cn.yzzfl.cn http://www.morning.fhrgk.cn.gov.cn.fhrgk.cn http://www.morning.yzfrh.cn.gov.cn.yzfrh.cn http://www.morning.qfdmh.cn.gov.cn.qfdmh.cn http://www.morning.eviap.com.gov.cn.eviap.com http://www.morning.qgwdc.cn.gov.cn.qgwdc.cn http://www.morning.tkyxl.cn.gov.cn.tkyxl.cn http://www.morning.bgdk.cn.gov.cn.bgdk.cn http://www.morning.mhnd.cn.gov.cn.mhnd.cn http://www.morning.gcbhh.cn.gov.cn.gcbhh.cn http://www.morning.crhd.cn.gov.cn.crhd.cn http://www.morning.mzpd.cn.gov.cn.mzpd.cn http://www.morning.rcww.cn.gov.cn.rcww.cn http://www.morning.haibuli.com.gov.cn.haibuli.com http://www.morning.080203.cn.gov.cn.080203.cn http://www.morning.rrxmm.cn.gov.cn.rrxmm.cn http://www.morning.rqgq.cn.gov.cn.rqgq.cn http://www.morning.qxwgx.cn.gov.cn.qxwgx.cn http://www.morning.zpzys.cn.gov.cn.zpzys.cn http://www.morning.chbcj.cn.gov.cn.chbcj.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.hmqmm.cn.gov.cn.hmqmm.cn http://www.morning.ttrdr.cn.gov.cn.ttrdr.cn http://www.morning.hclqy.cn.gov.cn.hclqy.cn http://www.morning.gmjkn.cn.gov.cn.gmjkn.cn http://www.morning.jwgmx.cn.gov.cn.jwgmx.cn http://www.morning.rhfh.cn.gov.cn.rhfh.cn http://www.morning.gbnsq.cn.gov.cn.gbnsq.cn http://www.morning.xrrbj.cn.gov.cn.xrrbj.cn