IPC进程间通信(管道)

基本概念

管道是Unix中最古老的进程间通信的形式,我们把从一个进程连接到另一个进程的一个数据流称为一个“管道”。其本质是内核中固定大小的缓冲区。管道是半双工的,数据只能向一个方向流动;需要双方通信时,需要建立起两个管道。

匿名管道pipe

#include <unistd.h>
int pipe(int pipefd[2]);

返回值:成功返回0,失败返回错误代码



管道只能用于具有共同祖先的进程(具有亲缘关系的进程)之间进行通信;通常,一个管道由一个进程创建,然后该进程调用fork,此后父、子进程之间就可应用该管道


#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    int fd[2],status;
    pid_t pid;
    if(pipe(fd)==-1)
    {
        printf("create pipe failed!\n");
        exit(0);
    }
    pid=fork();
    if(pid==0)
    {
        printf("child process running\n");
        close(fd[0]);
        write(fd[1],"helloworld",12);
        close(fd[1]);
    }
    else
    {
        char buf[20];
        waitpid(pid,&status,NULL);
        close(fd[1]);
        read(fd[0],buf,sizeof(buf));
        printf("parent process recvbuf is %s\n",buf);
    }
    return 0;
}

管道读写规则

当没有数据可读时

O_NONBLOCK disable:read调用阻塞,即进程暂停执行,一直等到有数据来到为止。

O_NONBLOCK enable:read调用返回-1,errno值为EAGAIN。

当管道满的时候

O_NONBLOCK disable: write调用阻塞,直到有进程读走数据

O_NONBLOCK enable:调用返回-1,errno值为EAGAIN

当管道被写满的时候

O_NONBLOCK disable: write调用阻塞

O_NONBLOCK enable:调用返回-1,errno值为EAGAIN

如果所有管道写端对应的文件描述符被关闭,则read返回0

如果所有管道读端对应的文件描述符被关闭,则write操作会产生SIGPIPE信号

#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
void handler(int signo)
{
    printf("signal status num is %d\n",signo);
}
int main()
{
    signal(SIGPIPE,handler);
    pid_t pid;
    int fd[2],cnt=0,ret=0;
    ret=pipe(fd);
    if(ret==-1)
    {
        printf("pipe error!\n");
    }
    pid=fork();
    if(pid==0)
    {
        int flag;
        close(fd[0]);
        flag=fcntl(fd[1],F_GETFL);
        flag=flag|O_NONBLOCK;
        ret=fcntl(fd[1],F_SETFL,flag);
        if(ret==-1)
        {
            printf("pipe fd error!\n");
        }
        for(;;)
        {
            ret=write(fd[1],"a",1);
            if(ret==-1)
                break;
            cnt++;
        }
        printf("write pipe capcity is %d\n",cnt);
        close(fd[1]);
        exit(0);
    }
    else
    {
        wait(NULL);
        close(fd[0]);
        close(fd[1]);
        exit(0);
    }
    return 0;
}


当要写入的数据量不大于PIPE_BUF时,linux将保证写入的原子性。

当要写入的数据量大于PIPE_BUF时,linux将不再保证写入的原子性。

有名管道FIFO

管道应用的一个限制就是只能在具有共同祖先(具有亲缘关系)的进程间通信。如果我们想在不相关的进程之间交换数据,可以使用FIFO文件来做这项工作,它经常被称为命名管道。命名管道是一种特殊类型的文件。

与匿名管道区别

匿名管道由pipe函数创建并打开。命名管道由mkfifo函数创建,打开用open,FIFO(命名管道)与pipe(匿名管道)之间唯一的区别在它们创建与打开的方式不同,一但这些工作完成之后,它们具有相同的语义。

#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>
int main(int argc, char *argv[])
{	
	int outfd;
	outfd = open("./2.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644); //O_TRUNC 打开文件清空
	if (outfd == -1)
	{
		printf("open()  2.txt err..\n");
		exit(0);
	}
	int infd;
	infd = open("fifop", O_RDONLY);
	if (infd == -1)
	{
		printf("open err ... \n");
		exit(0);
	}
	char buf[1024];
	int n = 0;
	while ( (n = read(infd, buf, 1024)) > 0)
	{
		write(outfd, buf, n);	
	}
	printf("读管道文件是否失败。。。。\n");
	close (infd);
	close (outfd);
	unlink("fifop");
	printf("fifor 读管道文件 success\n");
	return 0;
}
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>
int main(int argc, char *argv[])
{
	mkfifo("fifop", 0644);
	int infd;
	infd = open("./1.txt", O_RDONLY);
	if (infd == -1)
	{
		printf("infd open() err..\n");
		exit(0);
	}
	int outfd;
	outfd = open("fifop", O_WRONLY ); //阻塞模式
	if (outfd == -1)
	{
		printf("infd open() err..\n");
		exit(0);
	}
	char buf[1024];
	int n = 0;
	while ( (n = read(infd, buf, 1024)) > 0)
	{
		write(outfd, buf, n);	
	}
	close (infd);
	close (outfd);
	printf("fifow 写管道文件 success\n");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_33506160/article/details/80501463