南昌大学实验报告
学生姓名:陈星任学号:6100412040 专业班级:网工121班实验类型:□验证□综合□设计□创新实验日期:2015.5.10 实验成绩:
一、实验项目名称
《文件操作》
二、实验目的
通过编写文件读写及上锁的程序,进一步熟悉Linux 中文件I/O 相关的应用开发,并且熟练掌握open()、read()、write()、fcntl()等函数的使用。
三、实验内容
在Linux 中FIFO 是一种进程之间的管道通信机制。Linux 支持完整的FIFO通信机制。本实验内容,通过使用文件操作,仿真FIFO(先进先出)结构以及生产者-消费者运行模型。
本实验中需要打开两个虚拟终端,分别运行生产者程序(producer)和消费者程序(customer)。此时两个进程同时对同一个文件进行读写操作。因为这个文件是临界资源,所以可以使用文件锁机制来保证两个进程对文件的访问都是原子操作。
先启动生产者进程,它负责创建仿真FIFO 结构的文件(其实是一个普通文件)并投入生产,就是按照给定的时间间隔,向FIFO 文件写入自动生成的字符(在程序中用宏定义选择使用数字还是使用英文字符),生产周期以及要生产的资源数通过参数传递给进程(默认生产周期为1s,要生产的资源数为10 个字符)。
后启动的消费者进程按照给定的数目进行消费,首先从文件中读取相应数目的字符并在屏幕上显示,然后从文件中删除刚才消费过的数据。为了仿真FIFO 结构,此时需要使用两次复制来实现文件内容的偏移。每次消费的资源数通过参数传递给进程,默认值为10 个字符。
四、实验步骤
(1)实验流程图本实验的两个程序的流程图如图:
(2)代码
头部文件代码:mylock.h
struct myflock
{
short l_type; /*文件锁类型:F_RDLOCK 读取锁;F_WRLCK 写入锁;F_UNLCK 解锁*/ off_t l_start; /*相对位移量*/
short l_whence; /*相对位移量的起点SEEK_SET;SEEK_CUR; SEEK_END: */
off_t l_len; /* 加锁区域长度*/
pid_t l_pid; /* */
} ;
/* lock_set */
int lock_set(int fd, int type)
{
struct myflock 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 set by %d\n", lock.l_pid);
}
}
/* l_type 可能已被F_GETLK 修改过*/
lock.l_type = 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;
} /* end of switch */
return 0;
}
生产者程序的源代码:producer.c
/* producer.c */
#include
#include
#include
#include
#include
#include "mylock.h"
#define MAXLEN 10 /* 缓冲区大小最大值*/
#define ALPHABET 1 /* 表示使用英文字符*/
#define ALPHABET_START 'a' /* 头一个字符,可以用'A'*/
#define COUNT_OF_ALPHABET 26 /* 字母字符的个数*/
#define DIGIT 2 /* 表示使用数字字符*/
#define DIGIT_START '0' /* 头一个数字字符*/
#define COUNT_OF_DIGIT 10 /* 数字字符的个数*/
#define SIGN_TYPE ALPHABET /* 本实例用英文字符*/
const char *fifo_file = "./myfifo"; /* !" FIFO 文件名*/
char buff[MAXLEN]; /* 缓冲区*/
/* 函数product() 产生一个字符并写入仿真FIFO 文件中*/
int roduct(void)
{
int fd;
unsigned int sign_type, sign_start, sign_count, size;
static unsigned int counter = 0;
/* 打开!" FIFO 文件*/
if ((fd = open(fifo_file, O_CREAT|O_RDWR|O_APPEND, 0644)) < 0) {
printf("Open fifo file error\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;
}
} /*end of switch*/
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: write error\n"); return -1;
}
lock_set(fd, F_UNLCK); /* 解锁*/
close(fd);
return 0;
}
int main(int argc ,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;
}
消费者程序的源代码:customer.c
/* customer.c */
#include
#include
#include
#include
#include
#include "mylock.h"
#define MAX_FILE_SIZE 100 * 1024 * 1024 /* 100M*/ const char *fifo_file = "./myfifo"; /* 仿真FIFO 文件名*/ const char *tmp_file = "./tmp"; /* 临时文件名*/
/* 资源消费函数customing */
int customing(const char *myfifo, int need)
{
int fd;
char buff;
int counter = 0;
if ((fd = open(myfifo, O_RDONLY)) < 0)
{
printf("Function customing error\n");
return -1;
}
printf("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);
close(fd);
return 0;
}
/* myfilecopy()函数: 实现从sour_file 文件的offset 偏移处开始将count 个字节数据复制到dest_file 文件*/ int myfilecopy(const char *sour_file,const char *dest_file, int offset, int count, int copy_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 myfilecopy error in source file\n"); return -1;
}
if ((out_file = open(dest_file, O_CREAT|O_RDWR|O_TRUNC|O_NONBLOCK, 0644)) < 0)
{
printf("Function myfilecopy error 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;
}
/* custom()函数:实现FIFO 消费者*/
int custom(int need)
{
int fd;
/* 对资源进行消费,need 表示该消费的资源数目*/
customing(fifo_file, need);
if ((fd = open(fifo_file, O_RDWR)) < 0)
{
printf("Function myfilecopy error in source_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);
return 0;
}
int main(int argc ,char *argv[])
{
int customer_capacity = 10;
if (argc > 1) /* 第一个参数指定需要消费的资源数目,默认值为10 */
{
sscanf(argv[1], "%d", &customer_capacity);
}
if (customer_capacity > 0)
{
custom(customer_capacity);
exit(EXIT_SUCCESS);
}
(3) 分别编译生产者程序producer.c 和消费者程序customer.c
(4) 确保编译没有错误后,先在控制台终端1上运行生产者程序:./producer 1 20
再在控制台终端2上运行消费者程序:./customer 5
五、实验数据及处理结果
生产者程序:
消费者程序:
六、实验体会或对改进实验的建议
启动生产者进程,它负责创建模仿FIFO 结构的文件并投入生产,就是按照给定的时间间隔(1S),向FIFO 文件写入自动生成的字符。
启动的消费者进程按照给定的数目进行消费,首先从文件中读取相应数目的字符并在屏幕上显示,然后从文件中删除刚才消费过的数据。实验的过程就是要理解生产者和消费者在算法(FIFO)中具体的运行情况(以文件模拟的)。从而加深多算法的理解。实验代码在指导书都已经给出,所以这个实验过程并没有太大的困难。
七、参考资料
《Linux程序设计》