fifo页置换linux程序,测试linux
8种机械键盘轴体对比本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?本文通过具体的实例带你深入了解进程通信里的有名管道通信一、硬件工具一块已经烧录最小系统进去的开发板pc机U盘或TF卡或NFS二、软件工具ubuntu及虚拟机交叉编译器(此处采用的是Arm-2009q3)编辑器(此处采用的是notepad)超级终端vim编辑器三、读本文前得了解的知识交叉编译,文件函数的操作(open,wr.

8种机械键盘轴体对比
本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?
本文通过具体的实例带你深入了解进程通信里的有名管道通信
一、硬件工具
一块已经烧录最小系统进去的开发板
pc机
U盘或TF卡或NFS
二、软件工具
ubuntu及虚拟机
交叉编译器(此处采用的是Arm-2009q3)
编辑器(此处采用的是notepad)
超级终端
vim编辑器
三、读本文前得了解的知识
交叉编译,文件函数的操作(open,write,read等),对linux进程的了解,fork函数 mkfifo函数等。具体还有疑问可以在评论中一起交流。
四、有名管道 fifo
有名管道可以实现无亲缘关系的通信(无名管道只能用于有亲缘进程之间的通信)。有名管道实现进程间通信的速度很快,同时遵行先进先出原则。man 3 mkfifo
我们可以看到如下图mkfifo函数得用法:

为了更好的体验有名管道得通信,我们首先编写creatc.c测试mkfifo函数。#include
#include
#include
#include
#include
#include
void filecopy(FILE *,char *);
int main(void)
{
FILE *fp1;
long int i = 100000;
char buf[] = "I want to study Linux!n";
char *file1 = "data.txt";
printf("begin!n");
if((fp1 = fopen(file1,"a+")) == NULL ){ //fopen返回值为FILE的指针
//“a+”:Open for reading and appending (writing at end of file).
//The file is created if it does not exist
printf("can't open %sn",file1);
}
while(i--)
filecopy(fp1,buf);//调用 filecopy 将数据写入 file1 文件中
fclose(fp1);
printf("over!n");
return 0;
}
void filecopy(FILE *ifp,char *buf)
{
char c;
int i,j;
j = 0;
i = strlen(buf)-1;
while(i--){
putc(buf[j],ifp);//int putc(int c, FILE *stream);
//writes the character c, cast to an unsigned char, to stream
j++;
}
putc('n',ifp);
}
紧接着编写简单的writepipe.c测试对有名管道得写#include
#include
#include
#include //PIPE_BUF
#include
#include
#include
#include
int main()
{
const char *fifo_name = "my_fifo";
char *file1 = "data.txt";
int pipe_fd = -1;
int data_fd = -1;
int res = 0;
const int open_mode = O_WRONLY;
int bytes_sent = 0;
char buffer[PIPE_BUF + 1]; //最后
if(access(fifo_name, F_OK) == -1) //检验权限函数,看文件是否存在
{
//管道文件不存在
//创建命名管道
res = mkfifo(fifo_name, 0777);
if(res != 0)
{
fprintf(stderr, "Could not create fifo %sn", fifo_name);
exit(EXIT_FAILURE);
}
}
printf("Process %d opening FIFO O_WRONLYn", getpid());
//以只写阻塞方式打开FIFO文件,
pipe_fd = open(fifo_name, open_mode);
//以只读方式打开数据文件
data_fd = open(file1, O_RDONLY);
printf("Process %d result %dn", getpid(), pipe_fd);
if(pipe_fd != -1) //打开FIFO文件成功
{
int bytes_read = 0;
//向数据文件读取数据
bytes_read = read(data_fd, buffer, PIPE_BUF);
buffer[bytes_read] = '
更多推荐
所有评论(0)