进程间通信 - 命名管道

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

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

命名管道的另一个名称是 FIFO(先进先出).让我们看一下系统调用(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_IFDIR目录
S_IFIFOFIFO特殊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.如果需要结合读,写和执行,那么相应地添加值.

如果我们提到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".

让我们通过示例 : 来理解这一点;

步骤1 : 创建两个进程,一个是fifoserver,另一个是fifoclient.

步骤2 : 服务器进程执行以下 :

  • 创建名为"MYFIFO"的命名管道(使用系统调用mknod()) ,如果没有创建.

  • 打开命名管道以供只读使用.

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

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

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

步骤3 : 客户端进程执行以下 :

  • 打开命名管道以进行写入.

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

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

  • 无限重复,直到用户输入字符串"end"./p>

现在让我们来看看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;
}

编制和执行步骤

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;
}

让我们在到达的输出处取一个.

编制和执行步骤

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

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

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

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

以下是一个示例 :

步骤1 : 创建两个进程,一个是fifoserver_twoway,另一个是fifoclient_twoway.

步骤2 : 服务器进程执行以下 :

  • 创建名为"fifo_twoway"的命名管道(使用库函数mkfifo())在/tmp目录中,如果没有创建.

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

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

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

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

步骤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;
}

编制和执行步骤

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;
}

编制和执行步骤

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