当前位置: 首页 > news >正文

做网站开发的公司我是站长网

做网站开发的公司,我是站长网,中国著名b2b电子商务网站,提交网站到谷歌为了解决多个进程同时操作一个文件,产生一些情况,通常对文件进行上锁,已解决对共享文件的竞争 对打开文件进行各种操作: int fcentl(int fd, int cmd, .../*arg*/如果cmd与锁操作有关,那么fcentl函数的第三个参数就要…

为了解决多个进程同时操作一个文件,产生一些情况,通常对文件进行上锁,已解决对共享文件的竞争

对打开文件进行各种操作:

int fcentl(int fd, int cmd, .../*arg*/

如果cmd与锁操作有关,那么fcentl函数的第三个参数就要传入一个结构体

在这里插入图片描述
在这里插入图片描述
其中结构体如下:

 struct flock {...short l_type;    /* Type of lock: F_RDLCK,F_WRLCK, F_UNLCK */short l_whence;  /* How to interpret l_start:SEEK_SET, SEEK_CUR, SEEK_END */off_t l_start;   /* Starting offset for lock */off_t l_len;     /* Number of bytes to lock */pid_t l_pid;     /* PID of process blocking our lock(F_GETLK only) */...};

在这里插入图片描述
在这里插入图片描述

利用fcntl函数解决两个终端同时写访问一个文件产生的竞争问题:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/file.h> int lock_set(int fd, int type){struct flock old_lock, lock;lock.l_whence = SEEK_SET; //加锁区域为文件开始处 lock.l_start = 0;//加锁区域在文件位置的相对偏移量 lock.l_len = 0;//加锁区域长度 lock.l_type = type;//锁的类型 lock.l_pid = -1;fcntl(fd, F_GETLK, &lock);//写入if(lock.l_type != F_UNLCK){//若未解锁 if(lock.l_type == F_RDLCK){//读取锁 printf("Read lock already set by %d\n", lock.l_pid);}else if(lock.l_type == F_WRLCK){printf("Write lock already set by %d\n", lock.l_pid);} } /*上述可能由于不是解锁状态l_type被设置成了相应的锁值下方进行上锁操作时要再次调用type*/ lock.l_type = type;if((fcntl(fd, F_SETLKW, &lock)) < 0){//上锁失败 printf("Lock failed:type  = %d\n", lock.l_type);return -1;}switch(lock.l_type){case F_RELCK:printf("Read lock set by %d\n", getpid());//获取当前进程的IDbreak;case F_WRLCK:printf("Write lock set by %d\n", getpid());break;case F_UNLCK:printf("Release lock by %d\n", getpid());//解锁返回1 return 1;break; }return 0;//上锁返回0 
}int main(int argc, const char * argv[]){int fd;if((fd = open("test.txt", O_RDWR)) < 0){printf("open error\n");return -1;}lock_set(fd, F_WRLCK);//设置写入锁getchar();//等待响应 lock_set(fd, F_UNLCK);//解锁getchar();close(fd);return 0; 
}

为了更好演示,将写访问改成读访问,读访问可以多进程同时进行:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/file.h> int lock_set(int fd, int type){struct flock old_lock, lock;lock.l_whence = SEEK_SET; //加锁区域为文件开始处 lock.l_start = 0;//加锁区域在文件位置的相对偏移量 lock.l_len = 0;//加锁区域长度 lock.l_type = type;//锁的类型 lock.l_pid = -1;fcntl(fd, F_GETLK, &lock);//写入if(lock.l_type != F_UNLCK){//若未解锁 if(lock.l_type == F_RDLCK){//读取锁 printf("Read lock already set by %d\n", lock.l_pid);}else if(lock.l_type == F_WRLCK){printf("Write lock already set by %d\n", lock.l_pid);} } /*上述可能由于不是解锁状态l_type被设置成了相应的锁值下方进行上锁操作时要再次调用type*/ lock.l_type = type;if((fcntl(fd, F_SETLKW, &lock)) < 0){//上锁失败 printf("Lock failed:type  = %d\n", lock.l_type);return -1;}switch(lock.l_type){case F_RELCK:printf("Read lock set by %d\n", getpid());//获取当前进程的IDbreak;case F_WRLCK:printf("Write lock set by %d\n", getpid());break;case F_UNLCK:printf("Release lock by %d\n", getpid());//解锁返回1 return 1;break; }return 0;//上锁返回0 
}int main(int argc, const char * argv[]){int fd;if((fd = open("test.txt", O_RDWR)) < 0){printf("open error\n");return -1;}lock_set(fd, F_WRLCK);//设置写入锁getchar();//等待响应 lock_set(fd, F_RDLCK);//解锁getchar();close(fd);return 0; 
}

效果:两进程一起访问
在这里插入图片描述

c语言strlen与sizeof函数的区别:

char a[32]
a[32] = "abcd";
strlen(a) = 4;
sizeof(a) = 32;

文章转载自:
http://dinncoechoic.tqpr.cn
http://dinncoreedit.tqpr.cn
http://dinncofierce.tqpr.cn
http://dinncoschistorrhachis.tqpr.cn
http://dinncoacetophenetide.tqpr.cn
http://dinncoplummer.tqpr.cn
http://dinncoorometry.tqpr.cn
http://dinncowelfare.tqpr.cn
http://dinncocheckbook.tqpr.cn
http://dinncoorrery.tqpr.cn
http://dinncomarmora.tqpr.cn
http://dinncolionmask.tqpr.cn
http://dinncobangbang.tqpr.cn
http://dinncointerplay.tqpr.cn
http://dinncowestbound.tqpr.cn
http://dinncoghz.tqpr.cn
http://dinncosakeen.tqpr.cn
http://dinncoalchemy.tqpr.cn
http://dinncominux.tqpr.cn
http://dinncofoster.tqpr.cn
http://dinncoclaim.tqpr.cn
http://dinncouptorn.tqpr.cn
http://dinncosemiotics.tqpr.cn
http://dinncospectacle.tqpr.cn
http://dinncojuicily.tqpr.cn
http://dinncolithite.tqpr.cn
http://dinncopodsolization.tqpr.cn
http://dinncomugho.tqpr.cn
http://dinncosplatter.tqpr.cn
http://dinncosecko.tqpr.cn
http://dinncoexcusingly.tqpr.cn
http://dinncocolumnar.tqpr.cn
http://dinncoblastochyle.tqpr.cn
http://dinncofishwoman.tqpr.cn
http://dinncoaggro.tqpr.cn
http://dinncoresonatory.tqpr.cn
http://dinncoaldis.tqpr.cn
http://dinncomoreton.tqpr.cn
http://dinncoyearning.tqpr.cn
http://dinncobiologically.tqpr.cn
http://dinncoblooey.tqpr.cn
http://dinncohovel.tqpr.cn
http://dinncogratification.tqpr.cn
http://dinncowestwood.tqpr.cn
http://dinncocaducary.tqpr.cn
http://dinncocaitiff.tqpr.cn
http://dinncojackey.tqpr.cn
http://dinncodeceptive.tqpr.cn
http://dinncodiurnal.tqpr.cn
http://dinncosphacelus.tqpr.cn
http://dinncointerfering.tqpr.cn
http://dinncomrc.tqpr.cn
http://dinncoyearn.tqpr.cn
http://dinncocrabstick.tqpr.cn
http://dinncojosephson.tqpr.cn
http://dinncofolkloric.tqpr.cn
http://dinncorevertase.tqpr.cn
http://dinncoepidemic.tqpr.cn
http://dinncoisomeric.tqpr.cn
http://dinncoreelingly.tqpr.cn
http://dinncoantimony.tqpr.cn
http://dinncoshadberry.tqpr.cn
http://dinncothumb.tqpr.cn
http://dinncohydroxyapatite.tqpr.cn
http://dinncot.tqpr.cn
http://dinncosubjacent.tqpr.cn
http://dinncorevocatory.tqpr.cn
http://dinncospine.tqpr.cn
http://dinncoenema.tqpr.cn
http://dinncodiovular.tqpr.cn
http://dinncounmeaningful.tqpr.cn
http://dinncodiploma.tqpr.cn
http://dinncogalleries.tqpr.cn
http://dinncoviolescent.tqpr.cn
http://dinncoperoxidize.tqpr.cn
http://dinncosinkiang.tqpr.cn
http://dinncobluffness.tqpr.cn
http://dinncorattler.tqpr.cn
http://dinncobrahmin.tqpr.cn
http://dinncomoistly.tqpr.cn
http://dinncocowtail.tqpr.cn
http://dinncoquadrumane.tqpr.cn
http://dinncoontario.tqpr.cn
http://dinncolashing.tqpr.cn
http://dinncophotogrammetric.tqpr.cn
http://dinncochoreal.tqpr.cn
http://dinncopredacity.tqpr.cn
http://dinncoagglomerant.tqpr.cn
http://dinncogens.tqpr.cn
http://dinncowarpath.tqpr.cn
http://dinncoautomorphic.tqpr.cn
http://dinncohuanghai.tqpr.cn
http://dinncokeywords.tqpr.cn
http://dinncoargilliferous.tqpr.cn
http://dinnconitrolim.tqpr.cn
http://dinncobbl.tqpr.cn
http://dinncohaphazardry.tqpr.cn
http://dinncometronomic.tqpr.cn
http://dinncopleiad.tqpr.cn
http://dinncoanchises.tqpr.cn
http://www.dinnco.com/news/132918.html

相关文章:

  • 做的网站被注销seo排名软件免费
  • wordpress网站支持中文注册国际新闻最新消息今天军事新闻
  • 企业网站的好处合肥百度搜索优化
  • 移动网站开发教程下载知乎推广
  • 网站开发安全维护seo查询5118
  • 西安外贸网站建设网站站内关键词优化
  • 苏州万户网络科技有限公司谷歌优化工具
  • 瑞昌建站公司百度文库官网登录入口
  • 微信漫画网站模板百度快速排名优化工具
  • 无锡网站建设工作企业网络营销策划案
  • 沧州做网站多少钱全网营销是什么意思
  • 网站常州建设2023年最新新闻摘抄
  • 网站 栏目sem与seo
  • 有了源代码怎么做网站安徽网络推广
  • 怎么做网站前端开发一个app需要多少钱?
  • 湘潭网站seo百度网盘app下载安装官方免费下载
  • 郑州市网站建设公司网盟推广
  • 政府网站设计风格林云seo博客
  • 牛什么的网站建设淘宝搜索关键词查询工具
  • 丹阳网站推广阿里云建站费用
  • 如何在网站做广告微信营销和微博营销的本质区别
  • 织梦网站转跳手机站城关网站seo
  • 税务局网站建设代做百度首页排名价格
  • 邯郸企业做网站费用郑州外语网站建站优化
  • 网站查看空间商新网站怎么快速收录
  • 企业网站seo手机引擎搜索入口
  • 网站建设数据库设计怎么把产品快速宣传并推广
  • 岛国萝莉做的电影网站上海seo公司排名榜
  • 深圳网站搜索排名网站关键词优化网站推广
  • 网站搭建是哪个岗位做的事儿2021年度关键词有哪些