进程间的通信的管道的创建及使用

来源:互联网 发布:思迅软件v7 编辑:程序博客网 时间:2024/04/27 15:27

方式分为:

有名管道

创建方式及使用方法

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

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>

#define FIFO "/home/farsight/aaa"

char *buf = "hello b.c!\n";

int main()
{
 int ret;
 int fd;

 ret = mkfifo(FIFO, 0777);
 if (-1 == ret) {
  if (EEXIST == errno) {
   if ((fd = open(FIFO, O_WRONLY)) < 0) {                      
    perror("open fifo failed!\n");
    return -1;
   }

   printf("fd : %d\n", fd);
  }
  else {
   perror("mkfifo failed!");
   return -1;
  }
 }
 if ((fd = open(FIFO, O_WRONLY)) < 0) {
  perror("open fifo failed!\n");
  return -1;
 }

 if (-1 == write(fd, buf, strlen(buf) + 1)) {
  perror("write: ");
  return -1;
 }

 exit(0);
}

——————————————————————————————————————

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

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>

#define FIFO "/home/farsight/aaa"                     //绝对路径


int main()
{
 int ret;
 int fd;
 char buf[128] = {0};

 ret = mkfifo(FIFO, 0777);                                      //创建管道
 if (-1 == ret) {                                                          //出现错误时判断是否是EEXIST=errno,如果是就打开文件;
  if (EEXIST == errno) {
   if ((fd = open(FIFO, O_RDONLY)) < 0) {
    perror("open fifo failed!\n");
    return -1;
   }

   printf("fd : %d\n", fd);
  }
  else {
   perror("mkfifo failed!");
   return -1;
  }
 }else {
  if ((fd = open(FIFO, O_RDONLY)) < 0) {                     //没出现错误就打开文件。
   perror("open fifo failed!\n");
   return -1;
  }
 }
 if (-1 == read(fd, buf, 128)) {                                            // 读入文件。
  perror("read: ");
  return -1;
 }

 printf("b.c : %s\n", buf);
 exit(0);
}

—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— 

无名管道

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

int main()
{
 int fd[2];
 int fd1[2];
 pid_t pid;

 if (pipe(fd) == -1) {
  perror("pipe ");
  return -1;
 }

 if (pipe(fd1) == -1) {
  perror("pipe ");
  return -1;
 }
 
 pid = fork();
 if (-1 == pid) {
  perror("fork ");
  return -1;
 } else if (0 == pid) {//child
  close(fd[0]);
  close(fd1[1]);
  char buf[128] = {0};

  if (-1 == write(fd[1], "aaaa", strlen("aaaa")+1)){
   perror("child process write failed!\n");
  }
  if (-1 == read(fd1[0], buf, 128)) {
   perror("child process read failed!\n");
   exit(-1);
  }

  printf("child process: %s\n", buf);
  exit(0);

 } else {//father
  close(fd[1]);
  close(fd1[0]);

  char buf[128] = {0};

  if (-1 == read(fd[0], buf, 128)) {
   perror("child process read failed!\n");
  }
  printf("father process: %s\n", buf);
  if (-1 == write(fd1[1], "11111111", strlen("11111111")+1)){
   perror("child process write failed!\n");
   exit(-1);
  }

  exit(0);
 
 }
 

 printf("%d, %d\n", fd[0], fd[1]);

 return 0;
}

 

0 0
原创粉丝点击