拥有梦想是一种智力,实现梦想是一种能力

 

概述

若是一个多线程的进程,由于各个线程共享一个地址空间,可以直接通过变量的形式进行通信。而进程,由于各个进程独占一个地址空间,我们需要一种通信机制来完成进程间的数据交互。本章介绍的是有名管道,进程间的通信机制有以下几种:

  • 无名管道(pipe)
  • 有名管道 (fifo)
  • 信号(signal)
  •  
  • 共享内存(share memory)
  • 消息队列(message queue)
  • 信号灯集(semaphore set)
  •  
  • 套接字(socket)

 

之间有区分与各自的运用场景,其中套接字通常使用在网络服务,其他只能在本地场景下使用。笔者以后会逐一学习,本章介绍有名管道。

 

 

有名管道

 

有名管道本质

是进程间通信机制,有名管道并不是普通的文件,但与无名管道不同的是,它提供一个路径名与之关联,以FIFO的文件形式存在于文件系统中。

 

有名管道特点

  • 对应管道文件,可用于任意进程之间进行通信
  • 打开管道时可指定读写方式
  • 通过文件IO操作,内容存放在内存中
     

 

创建有名管道

 #include  <unistd.h>
 #include <fcntl.h>
 int  mkfifo(const char *path, mode_t mode);

  •  成功时返回0,失败时返回EOF
  •  path  创建的管道文件路径
  •  mode 管道文件的权限,如0666

 

示例

有名管道读写

进程A:循环从键盘输入并写入有名管道myfifo,输入quit时退出
进程B:循环统计进程A每次写入myfifo的字符串的长度

 

main.c   创建有名管道

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc,  char *argv[])
{
   if(mkfifo(“myfifo”, 0666) < 0) 
   {
     perror(“mkfifo”);
     exit(-1);
   }
   
   return 0;
}

 

B.c   写有名管道

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define   N 32

int main(void) 
{
  char buf[N];
  int pfd;

  if ((pfd = open(“myfifo”, O_WRONLY)) < 0) 
  {
    perror(“open”);  exit(-1);
  }
  
  while ( 1 ) 
  {
    fgets(buf, N, stdin);
    if (strcmp(buf, “quit\n”) == 0) break;  
      write(pfd, buf, N);
  }
  close(pfd);

  return 0;
}

 

A.c 读有名管道

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define   N 32
  
int main(void)
{
  char buf[N];
  int pfd;

  if ((pfd = open(“myfifo”, O_RDONLY)) < 0) 
  {
    perror(“open”); 
    exit(-1);
  }
  
  while (read(pfd, buf, N) > 0) 
  {
    printf(“the length of string is %d\n”, strlen(buf));
  }
  printf("peer closed\n");
  close(pfd);
 
  return 0;
}