文件I/O编程之: 实验内容
(2)编写代码。
本实验中的生产者程序的源代码如下所示,其中用到的lock_set()函数可参见第6.3.2节。
/*producer.c*/
#includestdio.h>
#includeunistd.h>
#includestdlib.h>
#includestring.h>
#includefcntl.h>
#includemylock.h
#defineMAXLEN10/*缓冲区大小最大值*/
#defineALPHABET1/*表示使用英文字符*/
#defineALPHABET_START'a'/*头一个字符,可以用'A'*/
#defineCOUNT_OF_ALPHABET26/*字母字符的个数*/
#defineDIGIT2/*表示使用数字字符*/
#defineDIGIT_START'0'/*头一个字符*/
#defineCOUNT_OF_DIGIT10/*数字字符的个数*/
#defineSIGN_TYPEALPHABET/*本实例选用英文字符*/
constchar*fifo_file=./myfifo;/*仿真FIFO文件名*/
charbuff[MAXLEN];/*缓冲区*/
/*功能:生产一个字符并写入仿真FIFO文件中*/
intproduct(void)
{
intfd;
unsignedintsign_type,sign_start,sign_count,size;
staticunsignedintcounter=0;
/*打开仿真FIFO文件*/
if((fd=open(fifo_file,O_CREAT|O_RDWR|O_APPEND,0644))0)
{
printf(Openfifofileerrorn);
exit(1);
}
sign_type=SIGN_TYPE;
switch(sign_type)
{
caseALPHABET:/*英文字符*/
{
sign_start=ALPHABET_START;
sign_count=COUNT_OF_ALPHABET;
}
break;
caseDIGIT:/*数字字符*/
{
sign_start=DIGIT_START;
sign_count=COUNT_OF_DIGIT;
}
break;
default:
{
return-1;
}
}/*endofswitch*/
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:writeerrorn);
return-1;
}
lock_set(fd,F_UNLCK);/*解锁*/
close(fd);
return0;
}
intmain(intargc,char*argv[])
{
inttime_step=1;/*生产周期*/
inttime_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*/
#includestdio.h>
#includeunistd.h>
#includestdlib.h>
#includefcntl.h>
#defineMAX_FILE_SIZE100*1024*1024/*100M*/
constchar*fifo_file=./myfifo;/*仿真FIFO文件名*/
constchar*tmp_file=./tmp;/*临时文件名*/
/*资源消费函数*/
intcustoming(constchar*myfifo,intneed)
{
intfd;
charbuff;
intcounter=0;
if((fd=open(myfifo,O_RDONLY))0)
{
printf(Functioncustomingerrorn);
return-1;
}
printf(Enjoy:);
lseek(fd,SEEK_SET,0);
while(counterneed)
{
while((read(fd,buff,1)==1)(counterneed))
{
fputc(buff,stdout);/*消费就是在屏幕上简单的显示*/
counter++;
}
fputs(n,stdout);
close(fd);
return0;
}
/*功能:从sour_file文件的offset偏移处开始
将count个字节数据复制到dest_file文件*/
intmyfilecopy(constchar*sour_file,
constchar*dest_file,intoffset,intcount,intcopy_mode)
{
intin_file,out_file;
intcounter=0;
charbuff_unit;
if((in_file=open(sour_file,O_RDONLY|O_NONBLOCK))0)
{
printf(Functionmyfilecopyerrorinsourcefilen);
return-1;
}
if((out_file=open(dest_file,
O_CREAT|O_RDWR|O_TRUNC|O_NONBLOCK,0644))0)
{
printf(Functionmyfilecopyerrorindestinationfile:);
return-1;
}
lseek(in_file,offset,SEEK_SET);
while((read(in_file,buff_unit,1)==1)(countercount))
{
write(out_file,buff_unit,1);
counter++;
}
close(in_file);
close(out_file);
return0;
}
/*功能:实现FIFO消费者*/
intcustom(intneed)
{
intfd;
/*对资源进行消费,need表示该消费的资源数目*/
customing(fifo_file,need);
if((fd=open(fifo_file,O_RDWR))0)
{
printf(Functionmyfilecopyerrorinsource_file:);
return-1;
}
/*为了模拟FIFO结构,对整个文件内容进行平行移动*/
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);
return0;
}
intmain(intargc,char*argv[])
{
intcustomer_capacity=10;
if(argc>1)/*第一个参数指定需要消费的资源数目,默认值为10*/
{
sscanf(argv[1],%d,customer_capacity);
}
if(customer_capacity>0)
{
custom(customer_capacity);
}
exit(EXIT_SUCCESS);
}
(3)先在宿主机上编译该程序,如下所示:
$makeclean;make
(4)在确保没有编译错误后,交叉编译该程序,此时需要修改Makefile中的变量
CC=arm-linux-gcc/*修改Makefile中的编译器*/
$makeclean;make
(5)将生成的可执行程序下载到目标板上运行。
linux操作系统文章专题:linux操作系统详解(linux不再难懂)
评论