zoukankan      html  css  js  c++  java
  • linux进程 生产者消费者

    #include<stdio.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<string.h>
    #include<fcntl.h>
    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
    ",lock.l_pid);
    		}
    		else if(lock.l_type == F_WRLCK)
    		{
    			printf("Write lock alread set by %d
    ",lock.l_pid);
    		}
    	}
    	lock.l_type = type;
    	if((fcntl(fd,F_SETLKW,&lock)) < 0)
    	{
    		printf("Lock failed:type = %d
    ",lock.l_type);
    		return -1;
    	}
    	switch(lock.l_type)
    	{
    		case F_RDLCK:
    			{
    				printf("Read lock set by %d
    ",getpid());
    			}
    			break;
    		case F_WRLCK:
    			{
    				printf("Write lock set by %d
    ",getpid());
    			}
    			break;
    		case F_UNLCK:
    			{
    				printf("Release lock by %d
    ",getpid());
    			}
    			break;
    	}
    	return 0;
    }
    int custum()
    {
    	int fs,fd;
    	int count;
    	char c;
    	if((fs = open("produce.txt",O_RDWR)) < 0)
    	{
    		printf("open error
    ");
    		return -1;
    	}
    //	if((fd = open("temp.txt",O_RDWR)) < 0)
    //	{
    //		printf("open error2
    ");
    //		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!
    ");
    			lock_set(fs,F_UNLCK);
    			sleep(1);
    			continue;
    		}
    		printf("get a character: %c 
    ",c);
    	//	lseek(fs,0,SEEK_CUR);
    	//	lseek(fd,0,SEEK_SET);
    	//	count = 0;
    		while(((count = read(fs,&c,1)) == 1) /*&& c!= '
    '*/)
    		{
    			printf("read fs: %c
    ", 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!='
    '*/)
    		{
    		//	printf("read fd: %c
    ", 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函数读到最后貌似自动会读一个' '。不加语句c!=' '的话,会将 一直复制下去。

    生产者函数如下:

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

      

  • 相关阅读:
    gitlab 简介
    gitlab安装配置(Ubuntu18和CentOS7)
    CentOS7 安装 vsftpd 服务
    CentOS7.5 系统最小化安装与初始化配置
    ubuntu1604系统初始化
    关于CGI和FastCGI的理解
    CentOS7安装OpenStack(Rocky版)-09.安装Cinder存储服务组件(控制节点)
    CentOS7安装OpenStack(Rocky版)-08.启动一个虚拟机实例
    CentOS7安装OpenStack(Rocky版)-07.安装horizon服务组件(控制节点dashboard)
    CentOS7安装OpenStack(Rocky版)-06.安装Neutron网络服务(控制节点)
  • 原文地址:https://www.cnblogs.com/jiaan/p/9351202.html
Copyright © 2011-2022 走看看