博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux进程 生产者消费者
阅读量:4565 次
发布时间:2019-06-08

本文共 3560 字,大约阅读时间需要 11 分钟。

#include
#include
#include
#include
#include
int lock_set(int fd,int type){ struct flock 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 alread set by %d\n",lock.l_pid); } else if(lock.l_type == F_WRLCK) { printf("Write lock alread set by %d\n",lock.l_pid); } } 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_RDLCK: { printf("Read lock set by %d\n",getpid()); } break; case F_WRLCK: { printf("Write lock set by %d\n",getpid()); } break; case F_UNLCK: { printf("Release lock by %d\n",getpid()); } break; } return 0;}int custum(){ int fs,fd; int count; char c; if((fs = open("produce.txt",O_RDWR)) < 0) { printf("open error\n"); return -1; }// if((fd = open("temp.txt",O_RDWR)) < 0)// {// printf("open error2\n");// return -1;// } while(1) { lock_set(fs,F_WRLCK); lseek(fs,0,SEEK_SET); fd=open("temp.txt",O_RDWR|O_CREAT|O_TRUNC,0777); count=read(fs,&c,1); if(count <= 0) { printf("no product!\n"); lock_set(fs,F_UNLCK); sleep(1); continue; } printf("get a character: %c \n",c); // lseek(fs,0,SEEK_CUR); // lseek(fd,0,SEEK_SET); // count = 0; while(((count = read(fs,&c,1)) == 1) /*&& c!= '\n'*/) { printf("read fs: %c\n", c); write(fd,&c,count); } close(fs); fs = open("produce.txt",O_RDWR|O_TRUNC); //liuzx_bj@hqyj.com lseek(fs,0,SEEK_SET); lseek(fd,0,SEEK_SET); while(((count = read(fd,&c,1)) == 1) /*&& c!='\n'*/) { // printf("read fd: %c\n", c); write(fs,&c,count); } unlink("temp.txt"); close(fd); lock_set(fs,F_UNLCK); sleep(2); }}int main(int argc, const char *argv[]){ custum(); return 0;}

  运行程序时,生产文件里有ABC 3个产品,运行时发现最后一个C产品一直会重复取出,经检查发现:我将produce.txt中的剩余产品复制到temp.txt中,之后再复制回来,我只是将文件读写位置调到了起始,但是以读写方式打开文件produce.txt,其中的产品仍为ABC,将temp.txt中的BC复制回去时,只是覆盖AB为BC最后的C仍存在,所以会一直有C。解决这个问题是:关闭produce.txt的文件符,重新以读写,清除原文件内容的方式打开,就会成功。只是还有一个问题read函数读到最后貌似自动会读一个'\n'。不加语句c!='\n'的话,会将\n一直复制下去。

生产者函数如下:

#include
#include
#include
#include
#include
int lock_set(int fd,int type){ struct flock 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 alread set by %d\n",lock.l_pid); } else if(lock.l_type == F_WRLCK) { printf("Write lock alread set by %d\n",lock.l_pid); } } 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_RDLCK: { printf("Read lock set by %d\n",getpid()); } break; case F_WRLCK: { printf("Write lock set by %d\n",getpid()); } break; case F_UNLCK: { printf("Release lock by %d\n",getpid()); } break; } return 0;}int produce(){ int fd; char a='A'; if((fd = open("produce.txt",O_WRONLY|O_APPEND)) < 0) { printf("open failed\n"); return -1; } while(1) { lock_set(fd,F_WRLCK); write(fd,&a,1); printf("hava produce one character %c \n",a); a++; lock_set(fd,F_UNLCK); sleep(3); } close(fd);}int main(int argc, const char *argv[]){ produce(); return 0;}

  

转载于:https://www.cnblogs.com/jiaan/p/9351202.html

你可能感兴趣的文章
报文格式【定长报文】
查看>>
RDLC报表钻取空白页问题
查看>>
OS X升级到10.10之后使用pod出现问题的解决方法
查看>>
多路电梯调度的思想
查看>>
jQuery-对Select的操作
查看>>
过滤器、监听器、拦截器的区别
查看>>
为什么要进行需求分析?通常对软件系统有哪些需求?
查看>>
Oracle RAC环境下ASM磁盘组扩容
查看>>
添加web引用和添加服务引用有什么区别?
查看>>
一些模板
查看>>
jquery和dom元素相互转换
查看>>
放大的X--HDOJ-201307292012
查看>>
题目831-签到-nyoj-20140818
查看>>
百词斩-斩家秘籍
查看>>
php反射
查看>>
hdu 1018 Big Number 数学结论
查看>>
【MUI】百度地图定位功能
查看>>
bzoj3687 简单题
查看>>
STL容器简介
查看>>
HashMap遍历的两种方式,推荐使用entrySet()
查看>>