← 返回首页
Linux高级程序设计(五十五)
发表时间:2021-12-09 00:44:15
命名管道实现进程通信

由于命名管道在本地创建管道文件,可以实现不相关进程间的通信。

实例: sender和receiver进程之间实现通信。

sender.c

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

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

    if (mkfifo("myfifo1", 0664) == -1)
    { // 出错
        if (errno != EEXIST)
        {
            perror("erro to mkfifo:");
            exit(1);
        }
    }

    if (mkfifo("myfifo2", 0664) == -1)
    { // 出错
        if (errno != EEXIST)
        {
            perror("erro to mkfifo:");
            exit(1);
        }
    }

    int fd_w,fd_r;

    if((fd_w = open("myfifo1", O_RDWR)) == -1)
    {
        perror("error to open:");
        exit(1);
    }

    if((fd_r= open("myfifo2", O_RDWR)) == -1)
    {
        perror("error to open:");
        exit(1);
    }

    char buff[128]="";
    ssize_t bytes;

    while(1) {

        fgets(buff,sizeof(buff),stdin);
        buff[strlen(buff)-1]='\0';
        //sender进程负责将数据写入fifo1,从fifo2读取数据 
        if((bytes=write(fd_w,buff,sizeof(buff))==-1)){
             perror("error to write:");
             exit(1);
        }
        if((bytes=read(fd_r,buff,sizeof(buff))==-1)){
             perror("error to read:");
             exit(1);
        }
        printf("from receiver:%s\n",buff);
    }

    return 0;
}

receiver.c

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

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

    if (mkfifo("myfifo1", 0664) != 0)
    { // 出错
        if (errno != EEXIST)
        {
            perror("erro to mkfifo:");
            exit(1);
        }
    }

    if (mkfifo("myfifo2", 0664) != 0)
    { // 出错
        if (errno != EEXIST)
        {
            perror("erro to mkfifo:");
            exit(1);
        }
    }

    int fd_r,fd_w;
    fd_w = open("myfifo2", O_RDWR);
    if (fd_w == -1)
    {
        perror("error to open:");
        exit(1);
    }

    fd_r = open("myfifo1", O_RDWR);
    if (fd_r == -1)
    {
        perror("error to open:");
        exit(1);
    }

    char buff[128]="";
    ssize_t bytes;

    while(1){

        if((bytes=read(fd_r,buff,sizeof(buff))==-1)){
             perror("error to read:");
             exit(1);
        }

        printf("from sender:%s\n",buff);

        fgets(buff,sizeof(buff),stdin);
        buff[strlen(buff)-1]='\0';

        if((bytes=write(fd_w,buff,sizeof(buff))==-1)){
             perror("error to write:");
             exit(1);
        }

    }
    return 0;
}

先执行sender进程再执行receiver进程。

[root@iz2zefozq9h39txdb8s7npz shelldemo]# gcc sender.c -o sender
[root@iz2zefozq9h39txdb8s7npz shelldemo]# gcc receiver.c -o receiver
[root@iz2zefozq9h39txdb8s7npz shelldemo]# ./sender
[root@iz2zefozq9h39txdb8s7npz shelldemo]# ./receiver

运行结果:

[root@iz2zefozq9h39txdb8s7npz shelldemo]# ./sender
I am sender
from receiver:I am receiver
how are you
from receiver:I am fine
haha
from receiver:xixi
^Z
[root@iz2zefozq9h39txdb8s7npz shelldemo]# ./receiver
from sender:I am sender
I am receiver
from sender:how are you
I am fine
from sender:haha
xixi
^Z