Linux进程通信:命名管道FIFO小结(2)

2021-09-24 14:45

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>

Linux进程通信:命名管道FIFO小结(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:吉林省长春外国语学校2012届高三第一次月考(历史)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: