• IPC 有命管道


    IPC  有命管道
          创建一个有名管道  /tmp/stduent.data


          A 和 B 是 相对独立的两支程序,现在 A  传送  10 个Student 到  B

          B 将Student 读取出来,显示在界面上

       
          然后 B  传送  10 个 Teacher 到  A  ,A 显示 teacher 在界面上

    A.cpp

    #include "public.h"

    #define FIFO_NAME "/temp/Linux/my_fifo"
    #define BUFFER_SIZE PIPE_BUF
    #define TEN_MEG (1024*1024*10)

    typedef struct students
    {
     int no;
     char username[100];
    }*StuNode,SNode;

    int main()
    {

     int pipe_fd;
     int ret;
     int open_mode=O_WRONLY;
     int bytes=0;
     char buffer[BUFFER_SIZE+1];
     SNode stus[10];


     for (int i=0;i<10;i++)
     {
      stus[i].no=i+1;
      strcpy(stus[i].username,"student");
     }


     if (access(FIFO_NAME,F_OK)==-1)
     {
      ret=mkfifo(FIFO_NAME,0777);
      if (ret!=0)
      {
       fprintf(stderr,"Could not create fifo %s\n",FIFO_NAME);
       exit(EXIT_FAILURE);
      }
     }
     printf("Process %d opening FIFO O_WRONLY\n",getpid());
     pipe_fd=open(FIFO_NAME,open_mode);
     printf("Process %d result %d\n",getpid(),pipe_fd);
     if (pipe_fd!=-1)
     {
      while (bytes<10)
      {
       ret=write(pipe_fd,&stus[bytes],sizeof(SNode));
       if (ret==-1)
       {
        fprintf(stderr,"write error on pipe\n");
        exit(EXIT_FAILURE);
       }
       bytes++;
      }
      close(pipe_fd);
     }
     else
     {
      exit(EXIT_FAILURE);

     }
     printf("Process %d finish \n",getpid());
     exit(EXIT_FAILURE);
     return 0;
    }

     B.cpp

    #include "public.h"

    #define FIFO_NAME "/temp/Linux/my_fifo"
    #define BUFFER_SZIE PIPE_BUF
    typedef struct students
    {
     int no;
     char username[100];
    }*StuNode,SNode;


    int main()
    {
     int pipe_fd;
     int ret;
     int open_mode=O_RDONLY;
     char buffer[BUFFER_SZIE+1];
     int bytes=0;
     SNode stus[10];
     memset(buffer,'\0',sizeof(buffer));
     printf("Process %d opening FIFO O_RDONLY\n",getpid());
     pipe_fd=open(FIFO_NAME,open_mode);
     if (pipe_fd!=-1)
     {
      do
      {
       ret=read(pipe_fd,&stus[bytes],sizeof(SNode));
       printf("no = %d username = %s \n",stus[bytes].no,stus[bytes].username);
       bytes++;
      } while (ret>0);
      close(pipe_fd);
     }
     else
      exit(EXIT_FAILURE);
     printf("Process %d finished,%d bytes read\n",getpid(),bytes);
     exit(EXIT_FAILURE);
     return 0;
    }

  • 相关阅读:
    IIS7最大上传附件大小配置
    数据表的identity
    服务器×××上的MSDTC不可用解决办法——Orchard(转)
    IOS项目中使用全局变量
    九宫格解锁的实现
    UINavigationControlle类详解
    jar仓库
    django安装
    django入门
    值得阅读的pyhon文章
  • 原文地址:https://www.cnblogs.com/newlist/p/2261642.html
Copyright © 2020-2023  润新知