zoukankan      html  css  js  c++  java
  • 代码示例_IPC_有名管道

    有名管道


    1.client

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #include <unistd.h>
     5 #include <strings.h>
     6 #include <sys/stat.h>
     7 #include <sys/types.h>
     8 #include <fcntl.h>
     9 
    10 int main(void)
    11 {
    12     if(mkfifo("./fifo",0666)<0){       // 创建有名管道
    13         perror("mkfifo");
    14         exit(1);
    15     }
    16 
    17     int fd = open("./fifo",O_WRONLY);  // 打开有名管道
    18     if(fd<0){
    19         perror("open failed");
    20         exit(1);
    21     }
    22 
    23     char buf[1024];
    24 
    25     while(1){            //
    26     bzero (buf,1024);
    27     fgets(buf,1024,stdin);
    28     write(fd,buf,1024);
    29         if(strncmp("quit",buf,4)==0)
    30             break;
    31     }
    32 
    33     close(fd);
    34 
    35     return 0 ;
    36 }

     2.server

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #include <unistd.h>
     5 #include <strings.h>
     6 #include <sys/stat.h>
     7 #include <sys/types.h>
     8 #include <fcntl.h>
     9 
    10 int main(void)
    11 {
    12 
    13     int fd = open("./fifo",O_RDWR);    // 打开有名管道
    14     if(fd<0){
    15         perror("open failed");
    16         exit(1);
    17     }
    18 
    19     char buf[1024];
    20 
    21     while(1){            //
    22        bzero (buf,1024);     
    23     read(fd,buf,1024);
    24     printf("read :%s
    ",buf);
    25     if(strncmp(buf,"quit",4)==0)
    26          break;
    27     }
    28 
    29     close(fd);
    30 
    31     return 0;
    32 }

    success !

    Stay hungry, stay foolish 待续。。。
  • 相关阅读:
    QString::toStdString() crashes
    Consolas 字体
    Mesh BRep Shapes
    PyOpenCL库安装
    全国精确到乡镇的行政边界、路网水系建筑poi等矢量shp免费下载
    DEM数据(ASTER GDEM|SRTM|GLS2005|ALOS DEM)下载
    IDL基础
    辐射定标与FLAASH大气校正
    Circos绘图—基础
    R-散点密度图
  • 原文地址:https://www.cnblogs.com/panda-w/p/11049430.html
Copyright © 2011-2022 走看看