Mylock.h
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
/*structmyflock
{
short l_type;
off_t l_start;
short l_whence;
off_t l_len;
pid_t l_pid;
};*/
intlock_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 setby %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());
return 1;
}
break;
default:
break;
}
return 0;
}
Producer.c
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include"mylock.h"
#defineMAXLEN 10
#defineALPHABET 1
#defineALPHABET_START 'a'
#defineCOUNT_OF_ALPHABET 26
#define DIGIT 2
#defineDIGIT_START '0'
#defineCOUNT_OF_DIGIT 10
#defineSIGN_TYPE ALPHABET
const char*fifo_file="./myfifo";
charbuff[MAXLEN];
intproduct(void)
{
int fd;
unsigned int sign_type,sign_start,sign_count,size;
static unsigned int counter = 0;
if((fd=open(fifo_file,O_CREAT|O_RDWR|O_APPEND,0644))< 0)
{
printf("Open fifo fileerror\n");
exit(1);
}
sign_type=SIGN_TYPE;
switch(sign_type)
{
case ALPHABET:
{
sign_start=ALPHABET_START;
sign_count=COUNT_OF_ALPHABET;
}
break;
case DIGIT:
{
sign_start=DIGIT_START;
sign_count=COUNT_OF_DIGIT;
}
break;
default:
{
return -1;
}
}
sprintf(buff,"%c",(sign_start+counter));
counter=(counter+1)%sign_count;
lock_set(fd,F_WRLCK);
if((size=write(fd,buff,strlen(buff)))<0)
{
printf("Producer:writeerror\n");
return -1;
}
lock_set(fd,F_UNLCK);
close(fd);
return 0;
}
int main(intargc,char *argv[])
{
int time_step=1;
int time_life=10;
if(argc>1)
{
sscanf(argv[1],"%d",&time_step);
}
if(argc>2)
{
sscanf(argv[2],"%d",&time_life);
}
while(time_life--)
{
if(product()<0)
{ break;}
sleep(time_step);
}
exit(EXIT_SUCCESS);
}
Customer.c
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include"mylock.h"
#defineMAX_FILE_SIZE 100*1024*1024
const char*fifo_file = "./myfifo";
const char*tmp_file = "./tmp";
intcustoming(const char *myfifo,int need)
{
int fd;
char buff;
int counter = 0;
if((fd=open(myfifo,O_RDONLY)) < 0)
{
printf("Function customingerror\n");
return -1;
}
printf("Enjoy:");
lseek(fd,SEEK_SET,0);
while(counter < need)
{
while((read(fd,&buff,1)==1)&& (counter < need))
{
fputc(buff,stdout);
counter++;
}
}
fputs("\n",stdout);
close(fd);
return 0;
}
intmyfilecopy(const char *sour_file,const char *dest_file,int offset,int count,intcopy_mode)
{
int in_file,out_file;
int counter = 0;
char buff_unit;
if((in_file =open(sour_file,O_RDONLY|O_NONBLOCK)) < 0)
{
printf("Function myfilecopyerror in source file\n");
return -1;
}
if((out_file =open(dest_file,O_CREAT|O_RDWR|O_TRUNC| \
O_NONBLOCK,0664)) < 0)
{
printf("Function myfilecopyerror in destination file:");
return -1;
}
lseek(in_file,offset,SEEK_SET);
while((read(in_file,&buff_unit,1) ==1) && (counter < count))
{
write(out_file,&buff_unit,1);
counter++;
}
close(in_file);
close(out_file);
return 0;
}
intcustom(int need)
{
int fd;
customing(fifo_file,need);
if((fd=open(fifo_file,O_RDWR)) < 0)
{
printf("Function myfilecopyerror in source_file");
return -1;
}
lock_set(fd,F_WRLCK);
myfilecopy(fifo_file,tmp_file,need,MAX_FILE_SIZE,0);
myfilecopy(tmp_file,fifo_file,0,MAX_FILE_SIZE,0);
lock_set(fd,F_UNLCK);
unlink(tmp_file);
close(fd);
return 0;
}
int main(intargc,char *argv[])
{
int customer_capacity = 10;
if(argc > 1)
{
sscanf(argv[1],"%d",&customer_capacity);
}
if(customer_capacity > 0)
{
custom(customer_capacity);
}
exit(EXIT_SUCCESS);
}