zoukankan      html  css  js  c++  java
  • linux 管道通信

      下面举linux下有名管道通信的代码。

    -----------------------------------------

    fifo_read.c

    ===========

    #include<errno.h>
    #include<stdio.h>
    #include<memory.h>
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<fcntl.h>
    #define FIFO "myfifo"

    int main(void)
    {
    int fd;
    int num;
    char buff[100];
    if(access(FIFO,F_OK) == -1)
    {
    mkfifo(FIFO,0777);
    }
    fd = open(FIFO,O_RDONLY|O_NONBLOCK);
    if(fd < 0)
    {
    printf("open fifo error:%s ",strerror(errno));
    }
    printf("open fifo fd=%d ",fd);
    while(1)
    {
    num=0;
    memset(buff,'',100);
    num=read(fd,buff,100);
    if(num<0)
    {
    printf("read fifo:%s ",strerror(errno));
    }
    else if(num==0)
    {
    printf("no data to read. ");
    }
    else
    {
    printf("read [%d] bytes in fifo,it is [%s]. ",num,buff);
    }
    sleep(1);
    }
    return 0;
    }

    ----------------------

    fifo_write.c

    ==========

    #include<stdio.h>
    #include<fcntl.h>
    #include<memory.h>
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<fcntl.h>
    #define FIFO "myfifo"

    int main(void)
    {
    int fd;
    char buff[100];
    int num =0;
    if(access(FIFO,F_OK)== -1)
    {
    mkfifo(FIFO,0777);
    }
    fd = open(FIFO,O_WRONLY);
    if(fd < 0)
    {
    printf("open fifo error! ");
    }
    printf("open file fd=%d ",fd);
    while(1)
    {
    memset(buff,'',100);
    num=0;
    printf("please write less than 100 letters: ");
    scanf("%s",buff);
    num= write(fd,buff,strlen(buff));
    printf("[%d] letters is written,it is [%s] ",num,buff);
    sleep(1);
    }
    return 0;
    }

  • 相关阅读:
    android中src和background区别
    html标签大全
    android横竖屏切换时activity的生命周期
    onInterceptTouchEvent和onTouchEvent调用时序
    Android Activity单例
    Android dispatchTouchEvent
    android 2D API相关
    杭电 1085 Holding BinLaden Captive!
    杭电 1715 大菲波数
    杭电 1492 The number of divisors(约数) about Humble Numbers
  • 原文地址:https://www.cnblogs.com/sunzl1987/p/4571775.html
Copyright © 2011-2022 走看看