实验4文件操作Word格式.docx

上传人:b****1 文档编号:14237497 上传时间:2022-10-20 格式:DOCX 页数:15 大小:103.99KB
下载 相关 举报
实验4文件操作Word格式.docx_第1页
第1页 / 共15页
实验4文件操作Word格式.docx_第2页
第2页 / 共15页
实验4文件操作Word格式.docx_第3页
第3页 / 共15页
实验4文件操作Word格式.docx_第4页
第4页 / 共15页
实验4文件操作Word格式.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

实验4文件操作Word格式.docx

《实验4文件操作Word格式.docx》由会员分享,可在线阅读,更多相关《实验4文件操作Word格式.docx(15页珍藏版)》请在冰豆网上搜索。

实验4文件操作Word格式.docx

(1)实验流程图本实验的两个程序的流程图如图:

图6.4节流程图

(2)代码

头部文件代码:

mylock.h

structmyflock

{

shortl_type;

/*文件锁类型:

F_RDLOCK读取锁;

F_WRLCK写入锁;

FJJNLCK解锁*/

off_tl_start;

/*相对位移量*/

shortl_whence;

/*相对位移量的起点SEEK_SET;

SEEK_CUR;

SEEK_END:

*/

off_tl_len;

/*加锁区域长度*/

pid_tl_pid;

/**/

};

/*lock_set*/

intlock_set(intfd,inttype){

structmyflockold_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("

Readlockalreadysetby%d\n"

lock.l_pid);

}

elseif(lock.l_type==F_WRLCK)/*该文件已有写入锁*/

Writelockalreadysetby%d\n"

/*l_type可能已被F_GETLK修改过*/

/*根据不同的type值进行阻塞式上锁或解锁*/

if((fcntl(fd,F_SETLKW,&

ock))<

0)

Lockfailed:

type=%d\n"

lock.l_type);

return1;

switch(lock.l_type)

caseF_RDLCK:

caseF_WRLCK:

caseF_UNLCK:

default:

}/*endofswitch

Readlocksetby

break;

Writelocksetby

Releaselockby

*/

%d\n"

getpid());

return1

 

return0;

生产者程序的源代码:

producer.c

/*producer.c*/

#include<

stdio.h>

unistd.h>

stdlib.h>

string.h>

fcntl.h>

#include"

mylock.h"

#defineMAXLEN

10

#defineALPHABET

1

#defineALPHABET,

START'

a'

#defineCOUNT_OF.

.ALPHABET

#defineDIGIT

2

/*缓冲区大小最大值*/

/*表示使用英文字符*/

/*头一个字符,可以用’A'

26/*字母字符的个数*/

/*表示使用数字字符*/

#defineCOUNT_OF_DIGIT10

/*数字字符的个数*/

#defineSIGN_TYPEALPHABET

/*本实例用英文字符*/

constchar*fifo_file="

./myfifo"

;

/*

!

"

FIFO文件名*/

#defineDIGIT_START'

O'

/*头一个数字字符*/

charbuff[MAXLEN];

/*缓冲区*/

/*函数product。

产生一个字符并写入仿真FIFO文件中*/

introduct(void)

intfd;

unsignedintsign_type,sign_start,sign_count,size;

staticunsignedintcounter=0;

/*打开!

FIFO文件*/

if((fd=open(fifo_file,O_CREAT|O_RDWR|O_APPEND,0644))<

Openfifofileerror\n"

);

exit

(1);

sign_type=SIGN_TYPE;

switch(sign_type)

caseALPHABET:

/*英文字符*/

caseDIGIT:

sign_start=ALPHABET_START;

sign_count=COUNT_OF_ALPHABET;

/*数字字符*/

sign_start=DIGIT_START;

sign_count=COUNT_OF_DIGIT;

}break;

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)))<

Producer:

writeerror'

n"

return-1;

lock_set(fd,F_UNLCK);

/*解锁*/

close(fd);

}intmain(intargc,char*argv[])

inttime_step=1;

/*生产周期*/

inttime_life=10;

/*需要生产的资源数*/

if(argc>

1)

{/*第一个参数表示生产周期*/

sscanf(argv[1],"

%d"

&

time_step);

2)

{/*第二个参数表示需要生产的资源数*/

sscanf(argv[2],"

time_life);

while(time_life--)

if(product()<

sleep(time_step);

exit(EXIT_SUCCESS);

消费者程序的源代码:

customer.c

/*customer.c*/#include<

#include<

#include"

#defineMAX_FILE_SIZE

100*

1024

*1024

/*

100M*/

constchar*fifo_file=

仿真FIFO

文件名

constchar*tmp_file=

./tmp"

临时文件名

/*资源消费函数customing*/

intcustoming(constchar*myfifo,intneed)

charbuff;

intcounter=0;

if((fd=open(myfifo,O_RDONLY))<

Functioncustomingerror'

Enjoy:

lseek(fd,SEEK_SET,0);

while(counter<

need)

while((read(fd,&

buff,1)==1)&

&

(counter<

need))

fputc(buff,stdout);

/*-.就是在屏幕上/0的显示*/

counter++;

fputs("

\n"

stdout);

/*myfilecopy()函数:

实现从sour_file文件的offset偏移处开始将count个字节数据复制到dest_file文件*/

intmyfilecopy(constchar*sour_file,constchar*dest_file,intoffset,intcount,intcopy_mode)

{---

intin_file,out_file;

charbuff_unit;

if((in_file=open(sour_file,O_RDONLY|O_NONBLOCK))<

Functionmyfilecopyerrorinsourcefile\n"

if((out_file=open(dest_file,O_CREAT|O_RDWR|O_TRUNC|O_NONBLOCK,0644))<

Functionmyfilecopyerrorindestinationfile:

lse

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 其它模板

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1