Linux 进程通信(有名管道)

有名管道(FIFO)
有名管道是持久稳定的。
它们存在于文件系统中。
FIFO比无名管道作用更大,因为他们能让无关联的进程之间交换数据。
管道文件一般用于交换数据。
shell命令创建管道
一个shell命令可以建立有名管道
--mkfifo [option] name
--mkfifo创建一个名为name的有名管道
--mkfifo fifo1     创建一个有名管道fifo1
--mkfifo -m 666 fifo2    创建一个带权限的管道文件
--cat < fifo1      通过cat命令从fifo1中读取数据。
--ls > fifo1       将ls命令输出的结果写入fifo1中
shell命令删除管道
--"rm name"
代码创建管道fifo
mkfifo()函数是C语言库函数
int mkfifo(const char * pathname,mode_t mode);
参数pathname是文件名称,
参数mode是文件读写权限(读写权限是一位八进制数,例如0777(0表示八进制)表示当前用户,组用户,其他用户都拥有对该文件的可读,可写,可执行权限)
函数执行成功返回0,否则返回-1,并设置变量errno。
代码删除管道
int unlink(const char *pathname);
unlink()函数是系统函数
函数执行成功返回0,否则返回-1,并设置变量errno。

 

//代码创建有名管道
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h> int main(int arg, char * args[]) { if(arg<2) { printf("请输入一个参数!\n"); return -1; } int no=0; //创建有名管道 no=mkfifo(args[1],0444); if(no==-1) { printf("创建管道失败!\n"); return -1; } return 0; }

 

//代码删除有名管道
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

int main(int arg, char * args[])
{
    if(arg<2)
    {
        printf("请输入一个参数!\n");
        return -1;
    }
    int no=0;
    //删除有名管道
    no=unlink(args[1]);
    if(no==-1)
    {
        printf("删除管道失败!\n");
        return -1;
    }
    return 0;
}

 

打开和关闭FIFO
int open(const char * pathname,int flags);
int close(int fd);
Linux的一切都是文件这一抽象概念的优势,打开和关闭FIFO和打开关闭一个普通文件操作是一样的。
FIFO的两端使用前都必须打开
open中如果参数flags为O_RDONLY将阻塞open调用,一直到另一个进程为写入数据打开FIFO为止。
相同的,O_WRONLY也导致阻塞一直到为读出数据打开FIFO为止。

 

//有名管道读文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int arg, char *args[])
{
    if (arg < 2)
    {
        printf("请输入一个参数!\n");
        return -1;
    }
    int fd = 0;
    char buf[100] = { 0 };
    //打开管道文件
    fd = open(args[1], O_RDONLY);
    if (fd == -1)
    {
        printf("open the file failed ! error message : %s\n", strerror(errno));
        return -1;
    }
    while (read(fd, buf, sizeof(buf)) > 0)
    {
        printf("%s", buf);
        memset(buf, 0, sizeof(buf));
    }
    //close the file stream
    close(fd);
    return 0;
}

 

//管道写文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int arg,char * args[])
{
    if(arg<2)
    {
        printf("请输入一个参数!\n");
        return -1;
    }
    int fd=0;
    char buf[100]={0};
    fd=open(args[1],O_WRONLY);
    if(fd==-1)
    {
        printf("open the file failed ! error message :%s\n",strerror(errno));
        return -1;
    }
    while(1)
    {
        //从键盘上读取数据
        read(STDIN_FILENO,buf,sizeof(buf));
        if(buf[0]=='0')
        {
            break;
        }
        //写入管道文件中
        write(fd,buf,strlen(buf));
        memset(buf,0,sizeof(buf));
    }
    //close the file stream
    close(fd);
    return 0;
}

posted on 2016-09-01 18:08  寒魔影  阅读(2346)  评论(0编辑  收藏  举报

导航