1.文件读写及上锁:
实验内容:
在 Linux 中 FIFO 是一种进程之间的管道通信机制。 Linux 支持完整的 FIFO 通信机制。
本实验内容比较有趣,通过使用文件操作,仿真 FIFO (先进先出)结构以及生产者-消费者运行模型。
本实验中需要打开两个虚拟终端,分别运行生产者程序(producer)和消费者程序(customer)。此时两个进程同时对同一个文件进行读写操作。因为这个文件是临界资源,所以可以使用文件锁机制来保证两个进程对文件的访问都是原子操作。
先启动生产者进程,它负责创建仿真 FIFO 结构的文件(其实是一个普通文件)并投入生产,就是按照给定的时间间隔,向 FIFO 文件写入自动生成的字符(在程序中用宏定义选择使用数字还是使用英文字符),生产周期以及要生产的资源数通过参数传递给进程(默认生产周期为1s,要生产的资源数为10 个字符)。
后启动的消费者进程按照给定的数目进行消费,首先从文件中读取相应数目的字符并在屏幕上显示。
然后从文件中删除刚才消费过的数据。为了仿真 FIFO 结构,此时需要使用两次复制来实现文件内容的偏移。每次消费的资源数通过参数传递给进程,默认值为10 个字符。
本实验的两个程序的流程图如下:
本实验中的生产者程序的源代码如下所示,其中用到了 lock_set() 函数:
/* lock_set.c */
int lock_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 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 */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#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]; /* 缓冲区 */
/* 功能:生产一个字符并写入仿真FIFO 文件中 */
int product(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;
}
sleep(time_step);
}
exit(EXIT_SUCCESS);
}
本实验中的消费者程序的源代码如下所示:
/* customer.c */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#define MAX_FILE_SIZE 100 * 1024 * 1024 /* 100M*/
const char *fifo_file = "./myfifo"; /* 仿真FIFO 文件名 */
const char *tmp_file = "./tmp"; /* 临时文件名 */
/* 资源消费函数 */
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); /* 消费就是在屏幕上简单的显示 */
counter++;
}
fputs("\n", stdout);
close(fd);
return 0;
}
/* 功能:从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;
}
/* 功能:实现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);
在宿主机上编译该程序,结果如下所示:
$ make clean; make
终端一:
$ ./producer 1 20 /* 生产周期为1s,需要生产的资源数为20 个 */
Write lock set by 21867
Release lock by 21867
Write lock set by 21867
Release lock by 21867
……
终端二:
$ ./customer 5 /* 需要消费的资源数为5 个 */
Enjoy:abcde /* 消费资源,即打印到屏幕上 */
Write lock set by 21872 /* 为了仿真FIFO 结构,进行两次复制 */
Release lock by 21872
在两个进程结束之后,仿真 FIFO 文件的内容如下:
$ cat myfifo
fghijklmnopqr /* a~e 的5 个字符已经被消费,就剩下后面15 个字符 */
2.文件IO查询函数select()和poll()的使用
实验内容:
在本实例中,分别用select()函数和poll()函数实现同一个功能,以下功能说明是以select()函数为例描述的。
本实例中主要实现通过调用 select() 函数来监听 3 个终端的输入(分别重定向到两个管道文件的虚拟终端以及主程序所运行的虚拟终端),并分别进行相应的处理。在这里我们建立了一个select() 函数监视的读文件描述符集,其中包含3 个文件描述符,分别为一个标准输入文件描述符和两个管道文件描述符。通过监视主程序的虚拟终端标准输入来实现程序的控制(例如:程序结束);以两个管道作为数据输入,主程序将从两个管道读取的输入字符串写入到标准输出文件(屏幕)。
为了充分表现 select() 调用的功能,在运行主程序的时候,需要打开 3 个虚拟终端:首先用mknod 命令创建两个管道 in1 和 in2。接下来,在两个虚拟终端上分别运行 cat>in1 和 cat>in2。同时在第三个虚拟终端上运行主程序。在程序运行之后,如果在两个管道终端上输入字符串,则可以观察到同样的内容将在主程序的虚拟终端上逐行显示。如果想结束主程序,只要在主程序的虚拟终端下输入以‘q’或‘Q’字符开头的字符串即可。如果三个文件一直在无输入状态中,则主程序一直处于阻塞状态。为了防止无限期的阻塞,在 select 程序中设置超时值(本实例中设置为60s),当无输入状态持续到超时值时,主程序主动结束运行并退出。而 poll 程序中依然无限等待,当然 poll()函数也可以设置超时参数。
程序流程图如下:
/* multiplex_select */
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#define MAX_BUFFER_SIZE 1024 /* 缓冲区大小*/
#define IN_FILES 3 /* 多路复用输入文件数目*/
#define TIME_DELAY 60 /* 超时值秒数 */
#define MAX(a, b) ((a > b) ? (a) : (b))
int main(void)
{
int fds[IN_FILES];
char buf[MAX_BUFFER_SIZE];
int i, res, real_read, maxfd;
struct timeval tv;
fd_set inset, tmp_inset;
/*首先以只读非阻塞方式打开两个管道文件*/
fds[0] = 0;
if ((fds[1] = open("in1", O_RDONLY | O_NONBLOCK)) < 0)
{
printf("Open in1 error\n");
return 1;
}
if ((fds[2] = open("in2", O_RDONLY | O_NONBLOCK)) < 0)
{
printf("Open in2 error\n");
return 1;
}
/*取出两个文件描述符中的较大者*/
maxfd = MAX(MAX(fds[0], fds[1]), fds[2]);
/*初始化读集合inset,并在读集合中加入相应的描述集*/
FD_ZERO(&inset);
for (i = 0; i < IN_FILES; i++)
{
FD_SET(fds[i], &inset);
}
FD_SET(0, &inset);
tv.tv_sec = TIME_DELAY;
tv.tv_usec = 0;
/*循环测试该文件描述符是否准备就绪,并调用select 函数对相关文件描述符做对应操作*/
while (FD_ISSET(fds[0], &inset) || FD_ISSET(fds[1], &inset) || FD_ISSET(fds[2], &inset))
{
/* 文件描述符集合的备份, 这样可以避免每次进行初始化 */
tmp_inset = inset;
res = select(maxfd + 1, &tmp_inset, NULL, NULL, &tv);
switch (res)
{
case -1:
{
printf("Select error\n");
return 1;
}
break;
case 0: /* Timeout */
{
printf("Time out\n");
return 1;
}
break;
default:
{
for (i = 0; i < IN_FILES; i++)
{
f(FD_ISSET(fds[i], &tmp_inset))
{
memset(buf, 0, MAX_BUFFER_SIZE);
real_read = read(fds[i], buf, MAX_BUFFER_SIZE);
if (real_read < 0)
{
if (errno != EAGAIN)
{
return 1;
}
}
else if (!real_read)
{
close(fds[i]);
FD_CLR(fds[i], &inset);
}
else
{
if (i == 0)
{ /* 主程序终端控制 */
if ((buf[0] == 'q') || (buf[0] == 'Q'))
{
return 1;
}
}
else
{ /* 显示管道输入字符串 */
buf[real_read] = '\0';
printf("%s", buf);
}
}
} /* end of if */
} /* end of for */
}
break;
} /* end of switch */
} /*end of while */
return 0;
}
运行结果:
main.c: In function ‘main’:
main.c:66:17: warning: implicit declaration of function ‘f’ [-Wimplicit-function-declaration]
66 | f(FD_ISSET(fds[i], &tmp_inset))
| ^
main.c:66:48: error: expected ‘;’ before ‘{’ token
66 | f(FD_ISSET(fds[i], &tmp_inset))
| ^
| ;
67 | {
| ~
用poll()函数实现的代码如下:
/* multiplex_poll.c */
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <poll.h>
#define MAX_BUFFER_SIZE 1024 /* 缓冲区大小*/
#define IN_FILES 3 /* 多路复用输入文件数目*/
#define TIME_DELAY 60 /* 超时时间秒数 */
#define MAX(a, b) ((a > b) ? (a) : (b))
int main(void)
{
struct pollfd fds[IN_FILES];
char buf[MAX_BUFFER_SIZE];
int i, res, real_read, maxfd;
/*首先按一定的权限打开两个源文件*/
fds[0].fd = 0;
if ((fds[1].fd = open("in1", O_RDONLY | O_NONBLOCK)) < 0)
{
printf("Open in1 error\n");
return 1;
}
if ((fds[2].fd = open("in2", O_RDONLY | O_NONBLOCK)) < 0)
{
printf("Open in2 error\n");
return 1;
}
/*取出两个文件描述符中的较大者*/
for (i = 0; i < IN_FILES; i++)
{
fds[i].events = POLLIN;
}
/*循环测试该文件描述符是否准备就绪,并调用select 函数对相关文件描述符做对应操作*/
while (fds[0].events || fds[1].events || fds[2].events)
{
if (poll(fds, IN_FILES, 0) < 0)
{
printf("Poll error\n");
return 1;
}
for (i = 0; i < IN_FILES; i++)
{
if (fds[i].revents)
{
memset(buf, 0, MAX_BUFFER_SIZE);
real_read = read(fds[i].fd, buf, MAX_BUFFER_SIZE);
if (real_read < 0)
{
if (errno != EAGAIN)
{
return 1;
}
}
else if (!real_read)
{
close(fds[i].fd);
fds[i].events = 0;
}
else
{
if (i == 0)
{
if ((buf[0] == 'q') || (buf[0] == 'Q'))
{
return 1;
}
}
else
{
buf[real_read] = '\0';
printf("%s", buf);
}
} /* end of if real_read*/
} /* end of if revents */
} /* end of for */
} /*end of while */
exit(0);
}
运行结果:
Open in1 error
Exited with error status 1
select()函数的运行结果如下,与poll()函数相似:
$ mknod in1 p
$ mknod in2 p
$ cat > in1
SELECT CALL
TEST PROGRAMME
END
$ cat > in2
select call
test programme
end
$ ./multiplex_select
SELECT CALL
select call
TEST PROGRAMME
test programme
END
End
q /* 在终端上输入‘q’或‘Q’则立刻结束程序运行 */
3. 查阅《嵌入式Linux应用程序开发标准教程》相关章节,自行编写程序,用虚拟机的串口读取试验箱的U-boot启动信息并保存到文件中。
以下是一个简单的示例程序,可以使用虚拟机的串口读取试验箱的U-boot启动信息并保存到文件中。请注意,该示例仅供参考,具体实现方式可能因系统环境和串口配置而有所不同。
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#define SERIAL_PORT "/dev/ttyS0" // 串口设备文件路径
#define BUFFER_SIZE 1024 // 缓冲区大小
int main() {
int serial_fd;
char buffer[BUFFER_SIZE];
ssize_t num_bytes;
FILE *file;
// 打开串口设备文件
serial_fd = open(SERIAL_PORT, O_RDONLY);
if (serial_fd == -1) {
perror("Failed to open serial port");
return 1;
}
// 配置串口参数
struct termios options;
tcgetattr(serial_fd, &options);
cfsetispeed(&options, B115200); // 设置波特率为115200
options.c_cflag |= (CLOCAL | CREAD);
tcsetattr(serial_fd, TCSANOW, &options);
// 读取串口数据
num_bytes = read(serial_fd, buffer, BUFFER_SIZE);
if (num_bytes == -1) {
perror("Failed to read from serial port");
close(serial_fd);
return 1;
}
// 创建文件并保存数据
file = fopen("u-boot.log", "w");
if (file == NULL) {
perror("Failed to create file");
close(serial_fd);
return 1;
}
fwrite(buffer, sizeof(char), num_bytes, file);
fclose(file);
// 关闭串口设备
close(serial_fd);
return 0;
}
需要根据实际情况修改代码中的串口设备文件路径和波特率,然后编译并运行程序。在程序运行期间,它将读取串口数据并保存到名为"u-boot.log"的文件中。