目录

Named Pipes

管道用于相关过程之间的通信。 我们可以使用管道进行无关的进程通信吗,比方说,我们想从一个终端执行客户端程序,从另一个终端执行服务器程序? 答案是否定。那么我们如何才能实现无关的流程沟通,简单的答案就是命名管道。 即使这适用于相关流程,但使用命名管道进行相关流程通信也没有任何意义。

我们使用一个管道进行单向通信,使用两个管道进行双向通信。 相同条件是否适用于命名管道。 答案是否定的,我们可以使用单个命名管道,可以用于双向通信(服务器和客户端之间的通信,以及客户端和服务器之间的通信),因为命名管道支持双向通信。

命名管道的另一个名称是FIFO (First-In-First-Out) 。 让我们看一下系统调用(mknod())来创建一个命名管道,这是一种特殊的文件。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int mknod(const char *pathname, mode_t mode, dev_t dev);

此系统调用将创建特殊文件或文件系统节点,例如普通文件,设备文件或FIFO。 系统调用的参数是pathname,mode和dev。 路径名以及模式和设备信息的属性。 路径名是相对的,如果未指定目录,则将在当前目录中创建。 指定的模式是文件模式,它指定文件类型,如文件类型和文件模式,如下表所示。 dev字段用于指定设备信息,例如主设备号和次设备号。

文件类型 描述 文件类型 描述
S_IFBLK 块特别 S_IFREG 常规文件
S_IFCHR 性格特别 S_IFDIRDirectory
S_IFIFO FIFO特别 S_IFLNK 符号链接
文件模式 描述 文件模式 描述
S_IRWXU 由所有者读取,写入,执行/搜索 S_IWGRP 写权限,组
S_IRUSR 读取权限,所有者 S_IXGRP 执行/搜索权限,组
S_IWUSR 写权限,所有者 S_IRWXO 他人读,写,执行/搜索
S_IXUSR 执行/搜索权限,所有者 S_IROTH 阅读许可,其他人
S_IRWXG 按组读,写,执行/搜索 S_IWOTH 写许可,其他人
S_IRGRP 读取权限,组 S_IXOTH 执行/搜索权限,其他人

文件模式也可以用八进制表示法表示,例如0XYZ,其中X代表所有者,Y代表组,Z代表其他。 X,Y或Z的值可以在0到7之间。读,写和执行的值分别为4,2,1。 如果需要结合使用read,write和execute,则相应地添加值。

比方说,如果我们提到0640,那么这意味着对所有者进行读写(4 + 2 = 6),对组进行读取(4),对其他进行无权限(0)。

此调用在成功时返回零,在失败时返回-1。 要了解失败的原因,请使用errno variable或perror()函数进行检查。

#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode)

此库函数创建FIFO特殊文件,该文件用于命名管道。 此函数的参数是文件名和模式。 文件名可以是绝对路径或相对路径。 如果未给出完整路径名(或绝对路径),则将在执行进程的当前文件夹中创建该文件。 文件模式信息如mknod()系统调用中所述。

此调用在成功时返回零,在失败时返回-1。 要了解失败的原因,请使用errno variable或perror()函数进行检查。

让我们考虑在一个终端上运行服务器并在另一个终端上运行客户端的程序。 该程序只执行单向通信。 客户端接受用户输入并将消息发送到服务器,服务器在输出上打印消息。 该过程一直持续到用户输入字符串“end”。

让我们用一个例子来理解这一点 -

Step 1 - 创建两个进程,一个是fifoserver,另一个是fifoclient。

Step 2 - 服务器进程执行以下操作 -

  • 如果未创建,则创建名为“MYFIFO”的命名管道(使用系统调用mknod())。

  • 打开命名管道以用于只读目的。

  • 这里,为Owner创建了具有读写权限的FIFO。 读取群组,没有其他人的权限。

  • 无限次等待来自客户端的消息。

  • 如果从客户端收到的消息不是“结束”,则打印该消息。 如果消息是“结束”,则关闭fifo并结束该过程。

Step 3 - 客户端流程执行以下操作 -

  • 打开命名管道以便写入。

  • 接受用户的字符串。

  • 检查,如果用户输入“结束”或“结束”以外。 无论哪种方式,它都会向服务器发送一条消息。 但是,如果字符串为“end”,则会关闭FIFO并结束该过程。

  • 无限重复,直到用户输入字符串“end”。

现在让我们来看看FIFO服务器文件。

/* Filename: fifoserver.c */
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define FIFO_FILE "MYFIFO"
int main() {
   int fd;
   char readbuf[80];
   char end[10];
   int to_end;
   int read_bytes;
   /* Create the FIFO if it does not exist */
   mknod(FIFO_FILE, S_IFIFO|0640, 0);
   strcpy(end, "end");
   while(1) {
      fd = open(FIFO_FILE, O_RDONLY);
      read_bytes = read(fd, readbuf, sizeof(readbuf));
      readbuf[read_bytes] = '\0';
      printf("Received string: \"%s\" and length is %d\n", readbuf, (int)strlen(readbuf));
      to_end = strcmp(readbuf, end);
      if (to_end == 0) {
         close(fd);
         break;
      }
   }
   return 0;
}

编译和执行步骤 (Compilation and Execution Steps)

Received string: "this is string 1" and length is 16
Received string: "fifo test" and length is 9
Received string: "fifo client and server" and length is 22
Received string: "end" and length is 3

现在,让我们来看看FIFO客户端示例代码。

/* Filename: fifoclient.c */
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define FIFO_FILE "MYFIFO"
int main() {
   int fd;
   int end_process;
   int stringlen;
   char readbuf[80];
   char end_str[5];
   printf("FIFO_CLIENT: Send messages, infinitely, to end enter \"end\"\n");
   fd = open(FIFO_FILE, O_CREAT|O_WRONLY);
   strcpy(end_str, "end");
   while (1) {
      printf("Enter string: ");
      fgets(readbuf, sizeof(readbuf), stdin);
      stringlen = strlen(readbuf);
      readbuf[stringlen - 1] = '\0';
      end_process = strcmp(readbuf, end_str);
      //printf("end_process is %d\n", end_process);
      if (end_process != 0) {
         write(fd, readbuf, strlen(readbuf));
         printf("Sent string: \"%s\" and string length is %d\n", readbuf, (int)strlen(readbuf));
      } else {
         write(fd, readbuf, strlen(readbuf));
         printf("Sent string: \"%s\" and string length is %d\n", readbuf, (int)strlen(readbuf));
         close(fd);
         break;
      }
   }
   return 0;
}

让我们来看看到达的输出。

编译和执行步骤 (Compilation and Execution Steps)

FIFO_CLIENT: Send messages, infinitely, to end enter "end"
Enter string: this is string 1
Sent string: "this is string 1" and string length is 16
Enter string: fifo test
Sent string: "fifo test" and string length is 9
Enter string: fifo client and server
Sent string: "fifo client and server" and string length is 22
Enter string: end
Sent string: "end" and string length is 3

使用命名管道进行双向通信

管道之间的通信意味着是单向的。 管道通常仅限于单向通信,并且至少需要两个管道用于双向通信。 管道仅用于相互关联的过程。 管道不能用于不相关的进程通信,例如,如果我们想要从一个终端执行一个进程而从另一个终端执行另一个进程,则管道不可能。 我们是否有简单的方法在两个流程之间进行通信,以简单的方式说不相关的流程? 答案是肯定的。 命名管道用于两个或多个不相关进程之间的通信,并且还可以具有双向通信。

我们已经看到了命名管道之间的单向通信,即从客户端到服务器的消息。 现在,让我们看一下双向通信,即客户端向服务器和接收消息的服务器发送消息,并使用相同的命名管道向客户端发回另一条消息。

以下是一个例子 -

Step 1 - 创建两个进程,一个是fifoserver_twoway,另一个是fifoclient_twoway。

Step 2 - 服务器进程执行以下操作 -

  • 如果未创建,则在/ tmp目录中创建名为“fifo_twoway”的命名管道(使用库函数mkfifo())。

  • 打开命名管道以进行读写。

  • 这里,为Owner创建了具有读写权限的FIFO。 读取群组,没有其他人的权限。

  • 无限次地等待来自客户端的消息。

  • 如果从客户端收到的消息不是“结束”,则打印消息并反转该字符串。 反向字符串被发送回客户端。 如果消息是“结束”,则关闭fifo并结束该过程。

Step 3 - 客户端流程执行以下操作 -

  • 打开命名管道以进行读写。

  • 接受来自用户的字符串。

  • 检查,如果用户输入“结束”或“结束”以外。 无论哪种方式,它都会向服务器发送一条消息。 但是,如果字符串为“end”,则会关闭FIFO并结束该过程。

  • 如果消息的发送不是“结束”,它将等待来自客户端的消息(反向字符串)并打印反转的字符串。

  • 无限重复,直到用户输入字符串“end”。

现在,我们来看看FIFO服务器示例代码。

/* Filename: fifoserver_twoway.c */
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define FIFO_FILE "/tmp/fifo_twoway"
void reverse_string(char *);
int main() {
   int fd;
   char readbuf[80];
   char end[10];
   int to_end;
   int read_bytes;
   /* Create the FIFO if it does not exist */
   mkfifo(FIFO_FILE, S_IFIFO|0640);
   strcpy(end, "end");
   fd = open(FIFO_FILE, O_RDWR);
   while(1) {
      read_bytes = read(fd, readbuf, sizeof(readbuf));
      readbuf[read_bytes] = '\0';
      printf("FIFOSERVER: Received string: \"%s\" and length is %d\n", readbuf, (int)strlen(readbuf));
      to_end = strcmp(readbuf, end);
      if (to_end == 0) {
         close(fd);
         break;
      }
      reverse_string(readbuf);
      printf("FIFOSERVER: Sending Reversed String: \"%s\" and length is %d\n", readbuf, (int) strlen(readbuf));
      write(fd, readbuf, strlen(readbuf));
      /*
      sleep - This is to make sure other process reads this, otherwise this
      process would retrieve the message
      */
      sleep(2);
   }
   return 0;
}
void reverse_string(char *str) {
   int last, limit, first;
   char temp;
   last = strlen(str) - 1;
   limit = last/2;
   first = 0;
   while (first < last) {
      temp = str[first];
      str[first] = str[last];
      str[last] = temp;
      first++;
      last--;
   }
   return;
}

编译和执行步骤 (Compilation and Execution Steps)

FIFOSERVER: Received string: "LINUX IPCs" and length is 10
FIFOSERVER: Sending Reversed String: "sCPI XUNIL" and length is 10
FIFOSERVER: Received string: "Inter Process Communication" and length is 27
FIFOSERVER: Sending Reversed String: "noitacinummoC ssecorP retnI" and length is 27
FIFOSERVER: Received string: "end" and length is 3

现在,我们来看看FIFO客户端示例代码。

/* Filename: fifoclient_twoway.c */
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define FIFO_FILE "/tmp/fifo_twoway"
int main() {
   int fd;
   int end_process;
   int stringlen;
   int read_bytes;
   char readbuf[80];
   char end_str[5];
   printf("FIFO_CLIENT: Send messages, infinitely, to end enter \"end\"\n");
   fd = open(FIFO_FILE, O_CREAT|O_RDWR);
   strcpy(end_str, "end");
   while (1) {
      printf("Enter string: ");
      fgets(readbuf, sizeof(readbuf), stdin);
      stringlen = strlen(readbuf);
      readbuf[stringlen - 1] = '\0';
      end_process = strcmp(readbuf, end_str);
      //printf("end_process is %d\n", end_process);
      if (end_process != 0) {
         write(fd, readbuf, strlen(readbuf));
         printf("FIFOCLIENT: Sent string: \"%s\" and string length is %d\n", readbuf, (int)strlen(readbuf));
         read_bytes = read(fd, readbuf, sizeof(readbuf));
         readbuf[read_bytes] = '\0';
         printf("FIFOCLIENT: Received string: \"%s\" and length is %d\n", readbuf, (int)strlen(readbuf));
      } else {
         write(fd, readbuf, strlen(readbuf));
         printf("FIFOCLIENT: Sent string: \"%s\" and string length is %d\n", readbuf, (int)strlen(readbuf));
         close(fd);
         break;
      }
   }
   return 0;
}

编译和执行步骤 (Compilation and Execution Steps)

FIFO_CLIENT: Send messages, infinitely, to end enter "end"
Enter string: LINUX IPCs
FIFOCLIENT: Sent string: "LINUX IPCs" and string length is 10
FIFOCLIENT: Received string: "sCPI XUNIL" and length is 10
Enter string: Inter Process Communication
FIFOCLIENT: Sent string: "Inter Process Communication" and string length is 27
FIFOCLIENT: Received string: "noitacinummoC ssecorP retnI" and length is 27
Enter string: end
FIFOCLIENT: Sent string: "end" and string length is 3
<上一篇.Pipes
Shared Memory.下一篇>
↑回到顶部↑
WIKI教程 @2018