---
title: "进程间通信（九）"
author: "winzenghua"
date: "2009-08-15T10:49:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2009-08-15T10:49:00Z"
id: 948802
url: "https://java4gis.iteye.com/blog/948802"
---

**管道**

下面是管道实现文件，pipe\_imp.c，其中有客户端与服务器端函数。

**试验－－管道实现头文件**

1 首先是#include：

#include "cd\_data.h"  
#include "cliserv.h"

2 我们定义一些在此文件的其他函数中所需要的值：

static int server\_fd = -1;  
static pid\_t mypid = 0;  
static char client\_pipe\_name\[PATH\_MAX + 1] = {‘\\0’};  
static int client\_fd = -1;  
static int client\_write\_fd = -1;

**服务器端函数**

下面我们需要探讨服务器端函数。下面的试验部分向我们展示了打开与关闭有名管道以及由客户端读取消息的函数。第二个试验部分则显示了打开，发送与关闭客户端管道的代码，客户端管道基于进程ID。

**试验－－服务器函数**

1 server\_starting例程创建服务器要由其中读取命令的有名管道。然后打开这个管道用于读取。open操作将会阻塞，直到一个客户端打开这个管道用于写入。我们使用阻塞模式，从而服务器在等待发送给他的命令时可以执行在管道上执行阻塞读取。

int server\_starting(void)  
{  
#if DEBUG\_TRACE  
printf(“%d :- server\_starting()\\n”, getpid());  
#endif  
unlink(SERVER\_PIPE);  
if (mkfifo(SERVER\_PIPE, 0777) == -1) {  
fprintf(stderr, “Server startup error, no FIFO created\\n”);  
return(0);  
}  
if ((server\_fd = open(SERVER\_PIPE, O\_RDONLY)) == -1) {  
if (errno == EINTR) return(0);  
fprintf(stderr, “Server startup error, no FIFO opened\\n”);  
return(0);  
}  
return(1);  
}

2 当服务器结束时，他移除有名管道，从而客户端可以检测到没有服务器在运行。

void server\_ending(void)  
{  
#if DEBUG\_TRACE  
printf(“%d :- server\_ending()\\n”, getpid());  
#endif  
(void)close(server\_fd);  
(void)unlink(SERVER\_PIPE);  
}

4 下面代码中所显示的read\_request\_from\_client函数将会阻塞服务器管道中的读取，直到有客户端向其中写入消息：

int read\_request\_from\_client(message\_db\_t \*rec\_ptr)  
{  
int return\_code = 0;  
int read\_bytes;  
#if DEBUG\_TRACE  
printf(“%d :- read\_request\_from\_client()\\n”, getpid());  
#endif  
if (server\_fd != -1) {  
read\_bytes = read(server\_fd, rec\_ptr, sizeof(\*rec\_ptr));  
...  
}  
return(return\_code);  
}

4 在没有客户端打开管道用于写入的情况下，read操作会返回0；也就是说，客户端会检测到EOF。然后服务器会关闭管道并重新打开，从而他会在客户端打开这个管道之前阻塞。这与服务器第一次启动时的情况相类似；我们已经重新初始化了服务器。在前面的函数中插入下面这段代码：

if (read\_bytes == 0) {  
(void)close(server\_fd);  
if ((server\_fd = open(SERVER\_PIPE, O\_RDONLY)) == -1) {  
if (errno != EINTR) {  
fprintf(stderr, “Server error, FIFO open failed\\n”);  
}  
return(0);  
}  
read\_bytes = read(server\_fd, rec\_ptr, sizeof(\*rec\_ptr));  
}  
if (read\_bytes == sizeof(\*rec\_ptr)) return\_code = 1;

服务器是单进程的，也许会同时服务多个客户端。因为每一个客户端使用一个不同的管道来接收他的响应，服务器需要写入不同的管道来向不同的客户端发送响应。因为文件描述符是限制资源，服务器只在他在有数据要发送的情况下才会打开客户端管道用于写入。

我们将打开，写入与关闭客户端管道分为三个独立的函数。当我们要向搜索返回多个结果时我们需要这样做，从而我们可以打管道一次，写入多个响应，然后再关闭管道。

**试验－－搭建管道**

1 首先，我们打开客户管道：

int start\_resp\_to\_client(const message\_db\_t mess\_to\_send)  
{  
#if DEBUG\_TRACE  
printf(“%d :- start\_resp\_to\_client()\\n”, getpid());  
#endif  
(void)sprintf(client\_pipe\_name, CLIENT\_PIPE, mess\_to\_send.client\_pid);  
if ((client\_fd = open(client\_pipe\_name, O\_WRONLY)) == -1) return(0);  
return(1);  
}

2 消息都是通过调用这个函数来发送的。我们会在稍后来看一下相对应的客户端函数。

int send\_resp\_to\_client(const message\_db\_t mess\_to\_send)  
{  
int write\_bytes;  
#if DEBUG\_TRACE  
printf(“%d :- send\_resp\_to\_client()\\n”, getpid());  
#endif  
if (client\_fd == -1) return(0);  
write\_bytes = write(client\_fd, &mess\_to\_send, sizeof(mess\_to\_send));  
if (write\_bytes != sizeof(mess\_to\_send)) return(0);  
return(1);  
}

3 最后，我们关闭客户端管道：

void end\_resp\_to\_client(void)  
{  
#if DEBUG\_TRACE  
printf(“%d :- end\_resp\_to\_client()\\n”, getpid());  
#endif  
if (client\_fd != -1) {  
(void)close(client\_fd);  
client\_fd = -1;  
}  
}

**客户端函数**

填充服务器的是pipe\_imp.c中的客户端函数。他们与服务器端函数十分类似，所不同的就是send\_mess\_to\_server函数。

试验－－客户端函数

1 在检测服务器可以访问之后，client\_starting函数初始化客户端管道：

int client\_starting(void)  
{  
#if DEBUG\_TRACE  
printf(“%d :- client\_starting\\n”, getpid());  
#endif  
mypid = getpid();  
if ((server\_fd = open(SERVER\_PIPE, O\_WRONLY)) == -1) {  
fprintf(stderr, “Server not running\\n”);  
return(0);  
}  
(void)sprintf(client\_pipe\_name, CLIENT\_PIPE, mypid);  
(void)unlink(client\_pipe\_name);  
if (mkfifo(client\_pipe\_name, 0777) == -1) {  
fprintf(stderr, “Unable to create client pipe %s\\n”,  
client\_pipe\_name);  
return(0);  
}  
return(1);  
}

2 client\_ending函数关闭文件描述符并且删除多余的有名管道：

void client\_ending(void)  
{  
#if DEBUG\_TRACE  
printf(“%d :- client\_ending()\\n”, getpid());  
#endif  
if (client\_write\_fd != -1) (void)close(client\_write\_fd);  
if (client\_fd != -1) (void)close(client\_fd);  
if (server\_fd != -1) (void)close(server\_fd);  
(void)unlink(client\_pipe\_name);  
}

3 send\_mess\_to\_server函数通过服务器管道发送请求：

int send\_mess\_to\_server(message\_db\_t mess\_to\_send)  
{  
int write\_bytes;  
#if DEBUG\_TRACE  
printf(“%d :- send\_mess\_to\_server()\\n”, getpid());  
#endif  
if (server\_fd == -1) return(0);  
mess\_to\_send.client\_pid = mypid;  
write\_bytes = write(server\_fd, &mess\_to\_send, sizeof(mess\_to\_send));  
if (write\_bytes != sizeof(mess\_to\_send)) return(0);  
return(1);  
}

与我们在前面所看到的服务器端函数类似，客户端使用三个函数由服务器得到返回的结果。

**试验－－得到服务器结果**

1 这个客户端函数来监听服务器响应。他使用只读取模式打开一个客户端管道，然后作为只写模式响应这个管道文件。

int start\_resp\_from\_server(void)  
{  
#if DEBUG\_TRACE  
printf(“%d :- start\_resp\_from\_server()\\n”, getpid());  
#endif  
if (client\_pipe\_name\[0] == ‘\\0’) return(0);  
if (client\_fd != -1) return(1);  
client\_fd = open(client\_pipe\_name, O\_RDONLY);  
if (client\_fd != -1) {  
client\_write\_fd = open(client\_pipe\_name, O\_WRONLY);  
if (client\_write\_fd != -1) return(1);  
(void)close(client\_fd);  
client\_fd = -1;  
}  
return(0);  
}

2 下面是由服务器得到匹配数据库记录的主要read操作：

int read\_resp\_from\_server(message\_db\_t \*rec\_ptr)  
{  
int read\_bytes;  
int return\_code = 0;  
#if DEBUG\_TRACE  
printf(“%d :- read\_resp\_from\_server()\\n”, getpid());  
#endif  
if (!rec\_ptr) return(0);  
if (client\_fd == -1) return(0);  
read\_bytes = read(client\_fd, rec\_ptr, sizeof(\*rec\_ptr));  
if (read\_bytes == sizeof(\*rec\_ptr)) return\_code = 1;  
return(return\_code);  
}

3 最后，下面的客户端函数标识服务器响应的结束：

void end\_resp\_from\_server(void)  
{  
#if DEBUG\_TRACE  
printf(“%d :- end\_resp\_from\_server()\\n”, getpid());  
#endif  
/* This function is empty in the pipe implementation \*/  
}

**工作原理**

在start\_resp\_from\_server中用于写入的客户端open操作：

client\_write\_fd = open(client\_pipe\_name, O\_WRONLY);

用来在阻止当服务器需要快速响应客户端的多个请求所引起的竞争条件。

要详细的解释这一点，考虑下面的事件序列：

1 客户端向服务器发送请求  
2 服务器读取请求，打开客户端管道并且发送响应，但是在他关闭客户端之前会被挂起。  
3 客户端打开他的管道用于读取，读取第一个响应然后关闭管道。  
4 然后客户端发送一个新的命令并且打开客户端管道用于读取。  
5 服务器继续运行，关闭其客户端管道。

不幸的时，此时客户端正尝试读取管道，查找其下一个请求的响应，但是read操作会返回0字节，因为并没有进程使得客户端打开用于写入。

通过允许客户打开其管道用于读写，从而移除了重复重新打开管道的需要，我们可以避免这个竞争条件。注意，客户端并没有写入管道，从而并没有垃圾数据的危险。

**程序总结**

现在我们将我们的CD数据库程序分为客户端与服务器两部分，从而可以使得我们独立开发用户界面与底层数据库技术。我们可以看到一个定义良好的数据库接口可以使得程序的每一个主要元素都充分利用计算机资源。如果我们更为深入一些，我们可以将管道实现改变为网络实现，并且使用一个数据库服务器。我们将会在第15章了解更多的关于网络的内容。

**小结**

在这一章，我们了解了在进程之间使用管道来传递数据。首先，我们了解了无名管道，使用popen与pipe调用来进行创建，然后讨论了如何使用管道与dup调用，我们可以将数据由一个程序传递到另一个的标准输入。然后我们了解了有名管道，并且了解了如何在无关的程序之间传递数据。最后，我们实现了一个简单的客户端/服务器例子，使用FIFO提供给我们的不仅是处理同步，还有双向的数据流。