Linux进程通信:命名管道FIFO小结.doc_第1页
Linux进程通信:命名管道FIFO小结.doc_第2页
Linux进程通信:命名管道FIFO小结.doc_第3页
Linux进程通信:命名管道FIFO小结.doc_第4页
Linux进程通信:命名管道FIFO小结.doc_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Linux下进程之间通信可以用命名管道FIFO完成。命名管道是一种特殊类型的文件,因为Linux中所有事物都是文件,它在文件系统中以文件名的形式存在。在程序中,我们可以使用两个不同的函数调用来建立管道:#include #include int mkfifo(const char *filename, mode_t mode);int mknode(const char *filename, mode_t mode | S_IFIFO, (dev_t) 0 ); 下面先来创建一个管道:view plaincopy to clipboardprint?#include #include #include #include int main() int res = mkfifo(/tmp/my_fifo, 0777); if (res = 0) printf(FIFO createdn); exit(EXIT_SUCCESS); #include #include #include #include int main() int res = mkfifo(/tmp/my_fifo, 0777); if (res = 0) printf(FIFO createdn); exit(EXIT_SUCCESS); 编译这个程序:gcc o fifo1.c fifo 运行这个程序:$ ./fifo1 用ls命令查看所创建的管道$ ls -lF /tmp/my_fifo prwxr-xr-x 1 root root 0 05-08 20:10 /tmp/my_fifo|注意:ls命令的输出结果中的第一个字符为p,表示这是一个管道。最后的|符号是由ls命令的-F选项添加的,它也表示是这是一个管道。虽然,我们所设置的文件创建模式为“0777”,但它被用户掩码(umask)设置(022)给改变了,这与普通文件创建是一样的,所以文件的最终模式为755。打开FIFO一个主要的限制是,程序不能是O_RDWR模式打开FIFO文件进行读写操作,这样做的后果未明确定义。这个限制是有道理的,因为我们使用FIFO只是为了单身传递数据,所以没有必要使用O_RDWR模式。如果一个管道以读/写方式打开FIFO,进程就会从这个管道读回它自己的输出。如果确实需要在程序之间双向传递数据,最好使用一对FIFO,一个方向使用一个。当一个Linux进程被阻塞时,它并不消耗CPU资源,这种进程的同步方式对CPU而言是非常有效率的。有关Linux下命名管道FIFO的读写规则可以参见之前所写的一篇文章:Linux命名管道FIFO的读写规则。 一、实验:使用FIFO实现进程间通信两个独立的程序:1. 生产者程序,它在需要时创建管道,然后尽可能快地向管道中写入数据。2. 消费者程序,它从FIFO中读取数据并丢弃它们。 生产者程序fifo2.c:view plaincopy to clipboardprint?#include #include #include #include #include #include #include #define FIFO_NAME /tmp/Linux/my_fifo #define BUFFER_SIZE PIPE_BUF #define TEN_MEG (1024 * 1024 * 10) int main() int pipe_fd; int res; int open_mode = O_WRONLY; int bytes = 0; char bufferBUFFER_SIZE + 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(); pipe_fd = open(FIFO_NAME, open_mode); printf(Process %d result %dn, getpid(), pipe_fd); if (pipe_fd != -1) while (bytes TEN_MEG) res = write(pipe_fd, buffer, BUFFER_SIZE); if (res = -1) fprintf(stderr, Write error on pipen); exit(EXIT_FAILURE); bytes += res; close(pipe_fd); else exit(EXIT_FAILURE); printf(Process %d finishn, getpid(); exit(EXIT_SUCCESS); #include #include #include #include #include #include #include #define FIFO_NAME /tmp/Linux/my_fifo#define BUFFER_SIZE PIPE_BUF#define TEN_MEG (1024 * 1024 * 10)int main() int pipe_fd; int res; int open_mode = O_WRONLY; int bytes = 0; char bufferBUFFER_SIZE + 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(); pipe_fd = open(FIFO_NAME, open_mode); printf(Process %d result %dn, getpid(), pipe_fd); if (pipe_fd != -1) while (bytes TEN_MEG) res = write(pipe_fd, buffer, BUFFER_SIZE); if (res = -1) fprintf(stderr, Write error on pipen); exit(EXIT_FAILURE); bytes += res; close(pipe_fd); else exit(EXIT_FAILURE); printf(Process %d finishn, getpid(); exit(EXIT_SUCCESS); 消费者程序fifo3.c:view plaincopy to clipboardprint?#include #include #include #include #include #include #include #define FIFO_NAME /tmp/Linux/my_fifo #define BUFFER_SIZE PIPE_BUF int main() int pipe_fd; int res; int open_mode = O_RDONLY; char bufferBUFFER_SIZE + 1; int bytes = 0; memset(buffer, 0, sizeof(buffer); printf(Process %d opeining FIFO O_RDONLYn, getpid(); pipe_fd = open(FIFO_NAME, open_mode); printf(Process %d result %dn, getpid(), pipe_fd); if (pipe_fd != -1) do res = read(pipe_fd, buffer, BUFFER_SIZE); bytes += res; while(res 0); close(pipe_fd); else exit(EXIT_FAILURE); printf(Process %d finished, %d bytes readn, getpid(), bytes); exit(EXIT_SUCCESS); #include #include #include #include #include #include #include #define FIFO_NAME /tmp/Linux/my_fifo#define BUFFER_SIZE PIPE_BUFint main() int pipe_fd; int res; int open_mode = O_RDONLY; char bufferBUFFER_SIZE + 1; int bytes = 0; memset(buffer, 0, sizeof(buffer); printf(Process %d opeining FIFO O_RDONLYn, getpid(); pipe_fd = open(FIFO_NAME, open_mode); printf(Process %d result %dn, getpid(), pipe_fd); if (pipe_fd != -1) do res = read(pipe_fd, buffer, BUFFER_SIZE); bytes += res; while(res 0); close(pipe_fd); else exit(EXIT_FAILURE); printf(Process %d finished, %d bytes readn, getpid(), bytes); exit(EXIT_SUCCESS); 编译这两个程序:gcc o fifo2 fifo2.cgcc o fifo3 fifo3.c 运行这两个程序:rootlocalhost chaper12# ./fifo2 & 后台执行,写数据2 23121Process 23121 opening FIFO O_WRONLYrootlocalhost chaper12# time ./fifo3读数据Process 24155 opeining FIFO O_RDONLYProcess 23121 result 3Process 24155 result 3Process 23121 finishProcess 24155 finished, 10485760 bytes read2- Done ./fifo2 real 0m0.214suser 0m0.000ssys 0m0.179s 以上两个程序均是使用阻塞模式FIFO。Linux会安排好这两个进程之间的调试,使它们在可以运行的时候运行,在不能运行的时候阻塞。因此,写进程将在管道满时阻塞,读进程将在管道空时阻塞。虚拟机上,time命令显示,读进程只运行了0.2秒的时间,却读取了10M字节的数据。这说明管道在程序之间传递数据是非常有效的。 二、实验:使用FIFO的客户/服务器应用程序 利用FIFO实现一个客户/服务器的应用程序,服务器进程接受请求,对它们进程处理,最后把结果数据返回给发送请求的客户方。 首先建立一个头文件client.h,它定义了客户和服务器程序都要用到的数据结构,并包含了必要的头文件。view plaincopy to clipboardprint?#include #include #include #include #include #include #include #define SERVER_FIFO_NAME /tmp/Linux/chaper12/server_fifo #define CLIENT_FIFO_NAME /tmp/Linux/chaper12/client_%d_fifo #define BUFFER_SIZE PIPE_BUF #define MESSAGE_SIZE 20 #define NAME_SIZE 256 typedef struct message pid_t client_pid; char dataMESSAGE_SIZE + 1; message; #include #include #include #include #include #include #include #define SERVER_FIFO_NAME /tmp/Linux/chaper12/server_fifo#define CLIENT_FIFO_NAME /tmp/Linux/chaper12/client_%d_fifo#define BUFFER_SIZE PIPE_BUF#define MESSAGE_SIZE 20#define NAME_SIZE 256typedef struct message pid_t client_pid; char dataMESSAGE_SIZE + 1;message; 接下来是服务器程序server.c,在这一部分,是以只读阻塞模式打开服务器管道,用于接收客户发送过来的数据,这些数据采用message结构体封装。view plaincopy to clipboardprint?#include client.h int main() int server_fifo_fd; int client_fifo_fd; int res; char client_fifo_nameNAME_SIZE; message msg; char *p; if (mkfifo(SERVER_FIFO_NAME, 0777) = -1) fprintf(stderr, Sorry, create server fifo failure!n); exit(EXIT_FAILURE); server_fifo_fd = open(SERVER_FIFO_NAME, O_RDONLY); if (server_fifo_fd = -1) fprintf(stderr, Sorry, server fifo open failure!n); exit(EXIT_FAILURE); sleep(5); while (res = read(server_fifo_fd, &msg, sizeof(msg) 0) p = msg.data; while (*p) *p = toupper(*p); +p; sprintf(client_fifo_name, CLIENT_FIFO_NAME, msg.client_pid); client_fifo_fd = open(client_fifo_name, O_WRONLY); if (client_fifo_fd = -1) fprintf(stderr, Sorry, client fifo open failure!n); exit(EXIT_FAILURE); write(client_fifo_fd, &msg, sizeof(msg); close(client_fifo_fd); close(server_fifo_fd); unlink(SERVER_FIFO_NAME); exit(EXIT_SUCCESS); #include client.hint main() int server_fifo_fd; int client_fifo_fd; int res; char client_fifo_nameNAME_SIZE; message msg; char *p; if (mkfifo(SERVER_FIFO_NAME, 0777) = -1) fprintf(stderr, Sorry, create server fifo failure!n); exit(EXIT_FAILURE); server_fifo_fd = open(SERVER_FIFO_NAME, O_RDONLY); if (server_fifo_fd = -1) fprintf(stderr, Sorry, server fifo open failure!n); exit(EXIT_FAILURE); sleep(5); while (res = read(server_fifo_fd, &msg, sizeof(msg) 0) p = msg.data; while (*p) *p = toupper(*p); +p; sprintf(client_fifo_name, CLIENT_FIFO_NAME, msg.client_pid); client_fifo_fd = open(client_fifo_name, O_WRONLY); if (client_fifo_fd = -1) fprintf(stderr, Sorry, client fifo open failure!n); exit(EXIT_FAILURE); write(client_fifo_fd, &msg, sizeof(msg); close(client_fifo_fd); close(server_fifo_fd); unlink(SERVER_FIFO_NAME); exit(EXIT_SUCCESS); 客户端程序client.c,这个程序用于向服务器发送消息,并接收来自服务器的回复。view plaincopy to clipboardprint?#include client.h int main() int server_fifo_fd; int client_fifo_fd; int res; char client_fifo_nameNAME_SIZE; message msg; msg.client_pid = getpid(); sprintf(client_fifo_name, CLIENT_FIFO_NAME, msg.client_pid); if (mkfifo(client_fifo_name, 0777) = -1) fprintf(stderr, Sorry, create client fifo failure!n); exit(EXIT_FAILURE); server_fifo_fd = open(SERVER_FIFO_NAME, O_WRONLY); if (server_fifo_fd = -1) fprintf(stderr, Sorry, open server fifo failure!n); exit(EXIT_FAILURE); sprintf(msg.data, Hello from %d, msg.client_pid); printf(%d sent %s , msg.client_pid, msg.data); write(server_fifo_fd, &msg, sizeof(msg); client_fifo_fd = open(client_fifo_name, O_RDONLY); if (client_fifo_fd = -1) fprintf(stderr, Sorry, client fifo open failure!n); exit(EXIT_FAILURE); res = read(client_fifo_fd, &msg, sizeof(msg); if (res 0) printf(received:%sn, msg.data); close(client_fifo_fd); close(server_fifo_fd); unlink(client_fifo_name); exit(EXIT_SUCCESS); #include client.hint main() int server_fifo_fd; int client_fifo_fd; int res; char client_fifo_nameNAME_SIZE; message msg; msg.client_pid = getpid(); sprintf(client_fifo_name, CLIENT_FIFO_NAME, msg.client_pid); if (mkfifo(client_fifo_name, 0777) = -1) fprintf(stderr, Sorry, create client fifo failure!n); exit(EXIT_FAILURE); server_fifo_fd = open(SERVER_FIFO_NAME, O_WRONLY); if (server_fifo_fd = -1) fprintf(stderr, Sorry, open server fifo failure!n); exit(EXIT_FAILURE); sprintf(msg.data, Hello from %d, msg.client_pid); printf(%d sent %s , msg.client_pid, msg.data); write(server_fifo_fd, &msg, sizeof(msg); client_fifo_fd = open(client_fifo_name, O_RDONLY); if (client_fifo_fd = -1) fprintf(stderr, Sorry, client fifo open failure!n); exit(EXIT_FAILURE); res = read(client_fifo_fd, &msg, sizeof(msg); if (res 0) printf(received:%sn, msg.data); close(client_fifo_fd); close(server_fifo_fd); unlink(client_fifo_name); exit(EXIT_SUCCESS); 编译程序:gcc o server server.cgcc o client client.c 测试这个程序,我们需要一个服务器进程和多个客户进程。为了让多个客户进程在同一时间启动,我们使用了shell命令:rootlocalhost chaper12# ./

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论