Linux--进程间通信(管道及有名管道FIFO)


一. 管道:

   1.只能用于具有亲缘关系的进程之间的通信  

   2.半双工通信模式

   3.一种特殊的文件,是一种只存在于内核中的读写函数

 

管道基于文件描述符,管道建立时,有两个文件描述符:

a. fd[0]: 固定用于读管道

b. fd[1]: 固定用于写管道

 

创建管道:pipe()

     一般步骤:1. pipe()创建管道  2. fork()创建子进程  3. 子进程会继承父进程的管道

 

关闭管道:1. 逐个关闭文件描述符  2. close()

 

eg. 父子进程间的管道通信:父子进程对管道分别有自己的读写通道,把无关的读端或写段关闭。

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

#define MAX_DATA_LEN 256
#define DELAY_TIME 1

int main() {
    pid_t pid;
    char buf[MAX_DATA_LEN];
    const char *data="Pipe Test program";
    int real_read,real_write;
    int pipe_fd[2];

    memset((void*)buf,0,sizeof(buf));

    if(pipe(pipe_fd)<0){
        perror("Pipe create error!\n");
        exit(1);
    }

    if ((pid=fork())<0) {
        perror("Fork error!\n");
        exit(1);
    } else if (pid==0) {
        close(pipe_fd[1]);
        sleep(DELAY_TIME*3);

        if ((real_read=read(pipe_fd[0],buf,MAX_DATA_LEN))>0) {
            printf("Child receive %d bytes from pipe: '%s'.\n",real_read,buf);
        }

        close(pipe_fd[0]);
        exit(0);
    } else {
        close(pipe_fd[0]);
        sleep(DELAY_TIME);

        if ((real_write=write(pipe_fd[1],data,strlen(data)))>0) {
            printf("Parent write %d bytes into pipe: '%s'.\n",real_write,data);
        }

        close(pipe_fd[1]);
        waitpid(pid,NULL,0);
        exit(0);
    }

}

 

 

二. 有名管道FIFO

  1. 使不相关的两个进程彼此通信:a. 通过路径名指出,在文件系统中可见  

                  b. 管道建立后,两进程可按普通文件一样对其操作

  2. FIFO遵循先进先出规则:a. 对管道读从开始处返回数据

                b. 对管道写则把数据添加到末尾

                c. 不支持如lseek()等文件定位操作

 

  创建有名管道:mkfifo()

 

创建管道成功后,可使用open()、read()和write()等函数。
  为读而打开的管道可在open()中设置O_RDONLY
  为写而打开的管道可在open()中设置O_WRONLY
 
与普通文件不同的是阻塞问题
  •普通文件的读写时不会出现阻塞问题
  •在管道的读写中却有阻塞的可能,
  •非阻塞标志:在open()函数中设定为O_NONBLOCK
 
l 阻塞打开与非阻塞打开
对于读进程
  •若该管道是阻塞打开,且当前FIFO内没有数据,则对读进程而言将一直阻塞到有数据写入
  •若该管道是非阻塞打开,则不论FIFO内是否有数据,读进程都会立即执行读操作。即如果FIFO内没有数据,则读函数将立刻返回0
对于写进程
  •若该管道是阻塞打开,则写操作将一直阻塞到数据可以被写入
  •若该管道是非阻塞打开而不能写入全部数据,则读操作进行部分写入或者调用失败
 
 
eg. 写FIFO与读FIFO
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>

#define FIFO "myfifo"
#define BUFF_SIZE 1024

int main(int argc,char* argv[]) {
    char buff[BUFF_SIZE];
    int real_write;
    int fd;

    if(argc<=1){
        printf("Usage: ./fifo_write string\n");
        exit(1);
    }

    sscanf(argv[1],"%s",buff);

% 测试FIFO是否存在,若不存在,mkfifo一个FIFO
    if(access(FIFO,F_OK)==-1){
        if((mkfifo(FIFO,0666)<0)&&(errno!=EEXIST)){
            printf("Can NOT create fifo file!\n");
            exit(1);
        }
    }

% 调用open以只写方式打开FIFO,返回文件描述符fd    
    if((fd=open(FIFO,O_WRONLY))==-1){
        printf("Open fifo error!\n");
        exit(1);
    }

% 调用write将buff写到文件描述符fd指向的FIFO中
    if ((real_write=write(fd,buff,BUFF_SIZE))>0) {
        printf("Write into pipe: '%s'.\n",buff);
        exit(1);
    }
    
    close(fd);
    exit(0);

}



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

#define FIFO "myfifo"
#define BUFF_SIZE 1024

int main() {
    char buff[BUFF_SIZE];
    int real_read;
    int fd;

%access确定文件或文件夹的访问权限。即,检查某个文件的存取方式
%如果指定的存取方式有效,则函数返回0,否则函数返回-1
%若不存在FIFO,则创建一个
    if(access(FIFO,F_OK)==-1){
        if((mkfifo(FIFO,0666)<0)&&(errno!=EEXIST)){
            printf("Can NOT create fifo file!\n");
            exit(1);
        }
    }

%以只读方式打开FIFO,返回文件描述符fd    
    if((fd=open(FIFO,O_RDONLY))==-1){
        printf("Open fifo error!\n");
        exit(1);
    }

% 调用read将fd指向的FIFO的内容,读到buff中,并打印
    while(1){
        memset(buff,0,BUFF_SIZE);
        if ((real_read=read(fd,buff,BUFF_SIZE))>0) {
            printf("Read from pipe: '%s'.\n",buff);
        }
    }
    
    close(fd);
    exit(0);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值