前言
我和前桌上课传纸条,这是一种通信方式。
 而我们为什么能过在上课的时候通信?
 因为我们通过在纸条上写字进行了数据的传递。
 本质上而言,我们两个都能看见一份公共的资源并对其进行读写,那就是小纸条!
 
进程间通信的本质,就是两个进程看到同一份公共的资源,准确来说是同一个文件,并对这个资源进行read和write操作
进程间通信,实际上可以转化为两个进程对同一个文件的读写操作
目录
什么是管道
- 管道是Unix中最古老的进程间通信的形式。
- 我们把从一个进程连接到另一个进程的一个数据流称为一个“管道”

1. 匿名管道
匿名管道用于具有血缘关系的进程进行进程间通信
 父子进程关闭不需要的文件描述符,来达到构建单向通信的信道的目的
我们回答2个问题,既然父子各自需要关闭一个,那曾经为什么要读写都打开呢?
- 如果父进程只以读方式打开文件,那么子进程继承下去的文件描述符也只是打开了读方式,没法打开写方式;反之亦然
那为什么非要关闭一个读或写呢?不关闭也行啊
- 防止误操作
另外,文件系统中文件读写指针的读写位置只有一个,用ftell()查看,如果要能双向通信,就得再添加一个读写位置,比较麻烦
创建管道系统接口:int pipe(int pipefd[2]);
- fd[0] read
- fd[1] write

代码
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <wait.h>
#include <sys/types.h>
int main()
{
  int pipefd[2] = {0};
  int ret = pipe(pipefd);
  if(ret < 0) {
    perror("pipe");
    return 1;
  }
  printf("pipefd[0]: %d pipefd[1]: %d\n", pipefd[0], pipefd[1]);
  
  pid_t id = fork();
  if(id < 0) {
    perror("fork");
    return 2;
  }
  else if(id == 0) {
    // child to write
    close(pipefd[0]); // 关闭子进程读端
    const char* msg = "I am child to write via pipe\n";
    int cnt = 5;
    while(cnt) {
      write(pipefd[1], msg, strlen(msg));
      sleep(1);
      cnt--;
    }
    close(pipefd[1]);
    exit(0);
  }
  
  // father to read 
  close(pipefd[1]);
  char buffer[64];
  buffer[0] = 0;
  while(1) {
    ssize_t size = read(pipefd[0], buffer, sizeof(buffer) - 1);
    if(size < 0) {
      perror("read");
      return 3;
    }
    else if(size == 0) {
      break;
    }
    buffer[size] = 0;
    printf("father get: %s", buffer);
  }
  close(pipefd[0]);
  int status = 0;
  pid_t waitret = waitpid(id, &status, 0);
  printf("wait succeed! exit code: %d.\n", (status>>8)&0xff);
  return 0;
}
匿名管道的特性
 1.进程间同步
- 如果管道里没有数据,父进程在干什么?等待,等管道内部有数据就绪,好比你妈在炒菜,10分钟炒一道,在她炒菜的时候,你就乖乖坐一边等,啥也别干
- 如果管道已经写满,写端等待,等管道内部有空闲时间
2.管道单向通信,是半双工的,数据只能向一个方向流动;需要双方通信时,需要建立起两个管道
 3.管道面向字节流:读端写端都是认为我们是按照字节的方式进行写入的
 4.管道只能保证具有血缘关系的进程通信,常见于父子
 5.管道可以保证一定程度(4KB以内)数据读取的原子性:数据要么不被读取,要读就要全部被读走
| 读端 | 写端 | 现象 | 
|---|---|---|
| 不读 | 写 | 管道被写满阻塞 | 
| 读 | 不写 | 读端被阻塞 | 
| 不读并关闭 | 写 | OS向写端发送SIGPIPE信号 | 
| 读 | 不写并关闭 | 读到0文件结束退出 | 
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <wait.h>
#include <sys/types.h>
int main()
{
  int pipefd[2] = {0};
  int ret = pipe(pipefd);
  if(ret < 0) {
    perror("pipe");
    return 1;
  }
  printf("pipefd[0]: %d pipefd[1]: %d\n", pipefd[0], pipefd[1]);
  
  pid_t id = fork();
  if(id < 0) {
    perror("fork");
    return 2;
  }
  else if(id == 0) {
    // child to write
    close(pipefd[0]); // 关闭子进程读端
    const char* msg = "I am child to write via pipe\n";
    int cnt = 5;
    while(cnt) {
      write(pipefd[1], msg, strlen(msg));
      sleep(1);
      cnt--;
    }
    close(pipefd[1]);
    exit(0);
  }
  
  // father to read 
  // 读一次之后关闭读端
  close(pipefd[1]);
  char buffer[64];
  buffer[0] = 0;
  ssize_t size = read(pipefd[0], buffer, sizeof(buffer) - 1);
  if(size < 0) {
    perror("read");
    return 3;
  }
  buffer[size] = 0;
  printf("father get: %s", buffer);
  close(pipefd[0]);
  printf("write close\n");
  int status = 0;
  pid_t waitret = waitpid(id, &status, 0);
  printf("wait succeed! exit code: %d, sig: %d\n", (status>>8)&0xff, (status)&0x7f);
  return 0;
}

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <wait.h>
#include <sys/types.h>
int main()
{
  int pipefd[2] = {0};
  int ret = pipe(pipefd);
  if(ret < 0) {
    perror("pipe");
    return 1;
  }
  printf("pipefd[0]: %d pipefd[1]: %d\n", pipefd[0], pipefd[1]);
  
  pid_t id = fork();
  if(id < 0) {
    perror("fork");
    return 2;
  }
  else if(id == 0) {
    // child to write
    close(pipefd[0]); // 关闭子进程读端
    const char* msg = "a"; //让写端一次写一个字节,那么写了多少次就是多少字节
    int cnt = 0;
    while(1) {
      write(pipefd[1], msg, strlen(msg));
      cnt++;
      printf("write: %d\n", cnt);
    }
    close(pipefd[1]);
    exit(0);
  }
  
  // father to read 
  close(pipefd[1]);
  char buffer[64];
  buffer[0] = 0;
  while(1) {
    sleep(100);
    ssize_t size = read(pipefd[0], buffer, sizeof(buffer) - 1);
    if(size < 0) {
      perror("read");
      return 3;
    }
    else if(size == 0) {
      break;
    }
    buffer[size] = 0;
    printf("father get: %s", buffer);
  }
  close(pipefd[0]);
  int status = 0;
  pid_t waitret = waitpid(id, &status, 0);
  printf("wait succeed! exit code: %d.\n", (status>>8)&0xff);
  return 0;
}

 而我们查看系统资源得:
 
 结论:一切以实际为准:管道大小是64kb,而4kb以内管道写入是原子性的
另外,我们的竖线|是创建了匿名管道:
sleep 100 | sleep 200 | sleep 300 &

 我们发现,通过管道创建的3个进程是兄弟进程
管道也是文件,管道的生命周期随进程

2. 命名管道
实验一:通过命令行体验一下命名管道
mkfifo myfifo #创建命名管道
while :; do echo "helloworld" ; sleep 1 ;done > myfifo #写
cat < myfifo #读
可以看到,通过命名管道,我们实现了echo 和 cat两个进程之间的通信
 所以说,如果我们需要2个毫不相关的进程间通信,就需要一个都能看到的资源
 而普通文件,需要刷新磁盘
 fifo管道文件,只存在于内存,不刷到磁盘
 所以我们通过管道文件,就能实现进程间通信
实验二:实现server和client,使用命名管道实现进程间通信
server
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define PATH "./.fifo"
int main() {
  int ret = mkfifo(PATH, 0644);
  if(ret < 0) {
    perror("mkfifo");
    return 1;
  }
  int fd = open(PATH, O_RDONLY, 0644);
  if(fd < 0) {
    perror("open");
    return 1;
  }
  while(1) {
    char buffer[128];
    buffer[0] = 0;
    ssize_t size = read(fd, buffer, sizeof(buffer) - 1);
    if(size < 0) {
      perror("read");
      return 3;
    }
    else if(size == 0) {
      printf("lient quit...\n");
      break;
    }
    else {
      buffer[size] = 0;
      printf("client$ %s", buffer);
    }
  }
  close(fd);
  return 0;
}
client
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define PATH "./.fifo"
int main() {
  // wirte
  
  ssize_t fd = open(PATH, O_WRONLY, 0644);
  if(fd < 0) {
    perror("open");
    return 1;
  }
  char msg[128];
  while(1) {
    printf("client# ");
    fflush(stdout);
    msg[0] = 0;
    ssize_t size = read(0, msg, 127);
    msg[size] = 0;
    write(fd, msg, strlen(msg));
  }
  close(fd);
  return 0;
}









