printf("Process %d result %d\n", 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 pipe\n");
exit(EXIT_FAILURE);
}
bytes += res;
}
close(pipe_fd);
}
else
{
exit(EXIT_FAILURE);
}
printf("Process %d finish\n", getpid());
exit(EXIT_SUCCESS);
}
Linux进程通信:命名管道FIFO小结,有实例
消费者程序fifo3.c:
view plaincopy to clipboardprint?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#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 buffer[BUFFER_SIZE + 1];
int bytes = 0;
memset(buffer, '\0', sizeof(buffer));
printf("Process %d opeining FIFO O_RDONLY\n", getpid());
pipe_fd = open(FIFO_NAME, open_mode);
printf("Process %d result %d\n", 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 read\n", getpid(), bytes);
exit(EXIT_SUCCESS);
Linux进程通信:命名管道FIFO小结,有实例
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#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 buffer[BUFFER_SIZE + 1];
int bytes = 0;
memset(buffer, '\0', sizeof(buffer));
printf("Process %d opeining FIFO O_RDONLY\n", getpid());
pipe_fd = open(FIFO_NAME, open_mode);
printf("Process %d result %d\n", 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 read\n", getpid(), bytes);
exit(EXIT_SUCCESS);
}
Linux进程通信:命名管道FIFO小结,有实例
编译这两个程序:
gcc –o fifo2 fifo2.c
gcc –o fifo3 fifo3.c
运行这两个程序:
[root@localhost chaper12]# ./fifo2 & à后台执行,写数据
[2] 23121
Process 23121 opening FIFO O_WRONLY
[root@localhost chaper12]# time ./fifo3à读数据
Process 24155 opeining FIFO O_RDONLY
Process 23121 result 3
Process 24155 result 3
Process 23121 finish
Process 24155 finished, 10485760 bytes read
[2]- Done ./fifo2
real 0m0.214s
user 0m0.000s
sys 0m0.179s
以上两个程序均是使用阻塞模式FIFO。Linux会安排好这两个进程之间的调试,使它们在可以运行的时候运行,在不能运行的时候阻塞。因此,写进程将在管道满时阻塞,读进程将
Linux进程通信:命名管道FIFO小结,有实例
在管道空时阻塞。
虚拟机上,time命令显示,读进程只运行了0.2秒的时间,却读取了10M字节的数据。这说明管道在程序之间传递数据是非常有效的。
二、实验:使用FIFO的客户/服务器应用程序
利用FIFO实现一个客户/服务器的应用程序,服务器进程接受请求,对它们进程处理,最后把结果数据返回给发送请求的客户方。
首先建立一个头文件client.h,它定义了客户和服务器程序都要用到的数据结构,并包含了必要的头文件。
view plaincopy to clipboardprint?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#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 data[MESSAGE_SIZE + 1];
}message;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说医药卫生Linux进程通信:命名管道FIFO小结(2)在线全文阅读。
相关推荐: