unix网络编程卷2:管道和FIFO

管道和FIFO

  1. 管道(Pipe):管道可用于具有亲缘关系进程间的通信,允许一个进程和另一个与它有共同祖先的进程之间进行通信。
  2. 命名管道(named pipe):命名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允许无亲缘关系进程间的通信。命名管道在文件系统中有对应的文件名。命名管道通过命令mkfifo或系统调用mkfifo来创建。
#include <unistd.h>
int pipe(int fd[2])

该函数返回两个文件描述符 fd[[0]和fdp[1],前者用于读,后者用于写。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>

void client(int readfd, int writefd);
void server(int readfd, int writefd);

int main(int argc,char **argv)
{
    int pipe1[2], pipe2[2];
    int childpid;

    pipe(pipe1);
    pipe(pipe2);

    if( (childpid = fork()) == 0)  //child
    {
        close(pipe1[1]);
        close(pipe2[0]);

        server(pipe1[0],pipe2[1]);
        exit(0);
    }

    close(pipe1[0]);
    close(pipe2[1]);

    client(pipe2[0],pipe1[1]);

    waitpid(childpid, NULL, 0);
    exit(0);
}

void server(int readfd, int writefd)
{
    int fd;
    size_t n;
    char buff[4097];

    if( (n = read(readfd, buff, 4096)) == 0)
    {
        std::cout<< "read file error " << std::endl;
        return;
    }

    buff[n] = '\0';
    std::cout << "the server get filename from client:" << buff << std::endl;

    if( (fd = open(buff, O_RDONLY)) < 0)
    {
        std::cout << "the file file can't open file" << std::endl;  
        write(writefd, buff , n);
    }
    else
    {
        while( (n = read(fd, buff, 4096)) > 0)
        {
            std::cout << "the server read file:" << buff;
            write(writefd, buff , n);
        }

        close(fd);
    }
}

void client(int readfd, int writefd)
{
    size_t len;
    size_t n;

    char buff[4097];
    std::cout<< "pls input filename" << std::endl;
    fgets(buff, 4096, stdin);

    len = strlen(buff);
    if( buff[len-1] == '\n')
        len--;
    write(writefd, buff, len);

    while( (n = read(readfd, buff ,4096)) > 0)
    {
        std::cout << "the client read message from server:" <<buff;
    }
}

有一个test文件 文件有一行数据this is a test
这里写图片描述

FIFO

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


#define FIFO1 "/tmp/fifo1"
#define FIFO2 "/tmp/fifo2"

#define FILE_MODE   (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)

void client(int readfd, int writefd);
void server(int readfd, int writefd);

int main(int argc, char**argv)
{
    int readfd, writefd;
    pid_t childpid;

    if ((mkfifo(FIFO1, FILE_MODE) < 0) && (errno != EEXIST))  
        printf("can't create %s", FIFO1);  
    if ((mkfifo(FIFO2, FILE_MODE) < 0) && (errno != EEXIST)) {  
        unlink(FIFO1);  
        printf("can't create %s", FIFO2);  
    }  

    if ( (childpid = fork()) == 0) {        /* child */  
        readfd = open(FIFO1, O_RDONLY, 0);  
        writefd = open(FIFO2, O_WRONLY, 0);  

        server(readfd, writefd);  
        exit(0);  
    }  
        /* 4parent */  
    writefd = open(FIFO1, O_WRONLY, 0);  
    readfd = open(FIFO2, O_RDONLY, 0);  

    client(readfd, writefd);  

    waitpid(childpid, NULL, 0);     /* wait for child to terminate */  

    close(readfd);  
    close(writefd);  

    unlink(FIFO1);  
    unlink(FIFO2);  
    exit(0);  
}

void server(int readfd, int writefd)
{
    int fd;
    size_t n;
    char buff[4097];

    if( (n = read(readfd, buff, 4096)) == 0)
    {
        std::cout<< "read file error " << std::endl;
        return;
    }

    buff[n] = '\0';
    std::cout << "the server get filename from client:" << buff << std::endl;

    if( (fd = open(buff, O_RDONLY)) < 0)
    {
        std::cout << "the file file can't open file" << std::endl;  
        write(writefd, buff , n);
    }
    else
    {
        while( (n = read(fd, buff, 4096)) > 0)
        {
            std::cout << "the server read file:" << buff;
            write(writefd, buff , n);
        }

        close(fd);
    }
}

void client(int readfd, int writefd)
{
    size_t len;
    size_t n;

    char buff[4097];
    std::cout<< "pls input filename" << std::endl;
    fgets(buff, 4096, stdin);

    len = strlen(buff);
    if( buff[len-1] == '\n')
        len--;
    write(writefd, buff, len);

    while( (n = read(readfd, buff ,4096)) > 0)
    {
        std::cout << "the client read message from server:" <<buff;
    }
}
### 回答1: Unix网络编程2:进程间通信PDF是一本非常有用的书籍,它涵盖了关于进程间通信的所有关键知识。 进程间通信是指进程之间交换数据或信息的过程,这对于理解操作系统以及网络编程非常重要。本书不仅讲解了进程通信的基础知识,还深入解释了信号、管道、消息队列、共享内存等高级通信方法。 在本书中,作者详细介绍了如何使用各种系统调用和库函数实现不同类型的进程间通信。读者将学习如何在不同进程之间共享文件句柄,如何创建匿名和命名管道,以及如何使用信号和信号处理程序等。 此外,本书还说明了如何以面向对象的方式编写并发程序。作者展示了C++ STL标准库和Boost库的使用方法,这些工具可以帮助程序员编写更高效的并发程序。 总之,Unix网络编程2:进程间通信PDF是一本非常有用的书籍,可以通过实例和详细解释帮助读者更好地理解进程通信的概念和技术。 ### 回答2: 《Unix网络编程 2:进程间通信》是一本经典的计算机网络编程书籍,主要讲解了在UNIX环境下进程之间如何进行通信,并介绍了常用的进程间通信机制和技术。 该书包含了进程间通信的基本概念和理论知识,从分析进程地址空间、进程控制、信号处理、进程间通信等多个方面详细阐述了进程间通信的各种实现方式,并通过实际的例子和代码提供了丰富的实践经验。同时,该书还包含了大量的参考文献和附录,方便读者深入学习和进一步研究进程间通信技术。 该书涵盖的主要内容包括UNIX进程模型、基本进程管理、进程资源和限制、信号、管道、消息队列、共享内存、信号量、套接字、RPC、XSI IPC等多个进程间通信机制和技术。其中,对于常用的进程间通信方式如管道、消息队列、共享内存、信号量等都进行了详细的介绍。同时,该书还介绍了进程间通信的高级技术,如分布式进程间通信(RPC)和XSI IPC等,帮助读者更好地实现进程间的通信。 总之,《Unix网络编程 2:进程间通信》是一本非常重要的计算机网络编程参考书籍,对于了解UNIX进程模型、深入理解进程间通信技术以及开发UNIX网络应用程序有很大的帮助。该书不仅适合计算机专业的学生和研究人员,也适合从事UNIX网络编程工作的程序员和工程师阅读。 ### 回答3: 《UNIX网络编程 2:进程间通信》是由W.Richard Stevens和Stephen A. Rago合作编写的,是一本关于UNIX如何实现进程间通信的技术指南,是一本精华之作。该书的主要内容包括:管道FIFO、消息队列、信号量、共享内存以及套接字等多种进程间通信方式。同时,书中也介绍了如何在不同的进程间进行信息交换、如何保证进程的同步性和互斥性以及如何利用各种进程间通信工具实现并发编程。 该书在介绍管道FIFO、消息队列等进程间通信机制时,都给出了详细的代码实现,并给出了该技术的优缺点以及适用场景。同时,书中还提供了丰富的案例分析,让读者能通过实践更好地理解和掌握这些技术。此外,书中还介绍了一些进程和线程相关的基础知识,如进程的创建、精灵进程、线程的创建、线程的同步等,这些知识为读者更好地理解进程间通信技术提供了背景和基础。 总的来说,该书是一本深入浅出、全面系统的进程间通信技术指南,它通过具体的代码实现和案例分析,使得读者能在实践中更好地理解和掌握这些技术,并能够开发出高效可靠的应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值