fifo管道

fifo管道学习. 进程间通过fifo收发数据

例1: read.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>

#define ERROR(flag)             \
if(flag)                        \
{                                \
    printf("%d: ",__LINE__);    \
    fflush(stdout);                \
    perror("error");            \
    exit(errno);                \
}

#define MYFIFO "/tmp/myfifo"

int main(int argc, char *argv[])
{
    int fd;
    char buf[100];
    bzero(buf,100);

    fd = open(MYFIFO,O_RDONLY);
    ERROR(fd == -1);

    read(fd,buf,100);
    puts(buf);

    close(fd);
    return 0;
}

write.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>


#define ERROR(flag)             \
if(flag)                        \
{                                \
    printf("%d: ",__LINE__);    \
    fflush(stdout);                \
    perror("error");            \
    exit(errno);                \
}

#define MYFIFO "/tmp/myfifo"

int main(int argc, char *argv[])
{
    int ret;

    umask(0);
    ret = mkfifo(MYFIFO,0600);
    ERROR(ret == -1);

    int fd = open(MYFIFO,O_WRONLY);
    ERROR(fd == -1);

    char buf[] = "hello read process, i'm write process!!!";

    write(fd,buf,sizeof(buf));
    close(fd);
    unlink(MYFIFO);

    return 0;
}

编译链接执行以上代码. 先执行write, 再在另一个终端执行read, 结果如下:

上图中错误是因为先执行read而产生的.

例2:

server.c

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <termios.h>
#include <signal.h>

#define ERROR(flag,msg)                 \
     if(flag)                \
    {                    \
        printf("%d: ",__LINE__);    \
        fflush(stdout);            \
        perror(msg);            \
        exit(errno);            \
    }

#define FIFO_SERVER "/tmp/server"
#define FIFO_CLIENT "/tmp/client"

int rfd,wfd;

void handler(int signum)
{
    puts("client is exit...");
    close(rfd);
    close(wfd);
    unlink(FIFO_SERVER);
    exit(0);
}

int main(int argc,char *argv[])
{
    int ret;
    char buf[100];

    umask(0);
    ret = mkfifo(FIFO_SERVER,0666);
    ERROR(ret == -1,"mkfifo");

    wfd = open(FIFO_SERVER,O_WRONLY);
    ERROR(wfd == -1,"open");

    rfd = open(FIFO_CLIENT,O_RDONLY);
    ERROR(rfd == -1,"open");

    signal(SIGPIPE,handler);

    char msg[] = "please input msg:     ";

    while(1)
    {
        write(STDOUT_FILENO,msg,sizeof(msg));
        
        ret = read(STDIN_FILENO,buf,100);
        if(strncmp(buf,"q",1) == 0)
            break;
        write(wfd,buf,ret);
        
        ret = read(rfd,buf,100);
        buf[ret - 1] = 0;
        printf("receive from client:  %s\n",buf);
    }

    close(rfd);
    close(wfd);
    unlink(FIFO_SERVER);

    return 0;
}

client.c

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <termios.h>
#include <signal.h>

#define ERROR(flag,msg)                 \
     if(flag)                \
    {                    \
        printf("%d: ",__LINE__);    \
        fflush(stdout);            \
        perror(msg);            \
        exit(errno);            \
    }

#define FIFO_CLIENT "/tmp/client"
#define FIFO_SERVER "/tmp/server"

int rfd,wfd;

void handler(int signum)
{
    puts("server is exit...");
    close(rfd);
    close(wfd);
    unlink(FIFO_CLIENT);
    exit(0);
}

int main(int argc,char *argv[])
{
    int ret;
    char buf[100];

    umask(0);

    ret = mkfifo(FIFO_CLIENT,0666);
    ERROR(ret == -1,"mkfifo");

    while((rfd = open(FIFO_SERVER,O_RDONLY)) == -1)
        sleep(1);
    
    wfd = open(FIFO_CLIENT,O_WRONLY);
    ERROR(wfd == -1,"open");

    signal(SIGPIPE,handler);

    char msg[] = "please input msg:     ";

    while(1)
    {
        ret = read(rfd,buf,100);
        buf[ret - 1] = 0;
        printf("receive from server:  %s\n",buf);
        
        write(STDOUT_FILENO,msg,sizeof(msg));
        ret = read(STDIN_FILENO,buf,100);

        if(strncmp(buf,"q",1) == 0)
            break;
        write(wfd,buf,ret);
    }

    close(rfd);
    close(wfd);
    unlink(FIFO_CLIENT);

    return 0;
}

编译链接执行,结果如下:

 

相比pipe管道只能在相关(父子/兄弟)进程之间传输数据. fifo可以在不相关的进程之间交换数据.

posted @ 2015-12-29 19:46  zhanglong71  阅读(245)  评论(0)    收藏  举报