进程间通信学习笔记(有名管道和无名管道)

进程间通信方式:

  • 无名管道(pipe)
  • 有名管道(fifo)
  • 信号(signal)
  • 共享内存(mmap)
  • 套接字(socket)

 无名管道:

        在内核里面开辟一片内存,进程1和进程2都可以通过这片内存进行通信

无名管道特点:
  • 只能用于具有亲缘关系的进程之间的通信(比如父子进程、兄弟进程)
  • 单工的通信模式,具有固定的读端和写端(只能一个进程写、一个进程读,不能反过来)
  • 无名管道创建时会返回两个文件描述符,分别用于读写管道(一个进程只能用一个描述符,因为是单工通信)
无名管道创建-pipe
#include <unistd.h>
int pipe(int pfd[2]);
  • 成功时返回0,失败时返回EOF
  • pfd包含两个元素的整形数组,用来保存文件描述符
  • pfd[0]用于读管道;pfd[1]用于写管道

示例代码:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main()
{
    int pfd[2];
    int re;
    char buf[20] = {0};
    __pid_t pid;
    re = pipe(pfd);
    if(re < 0)
    {
        perror("pipe");
        return 0;
    }
    printf("%d,%d\n",pfd[0],pfd[1]);
    pid = fork();
    if(pid < 0)
    {
        perror("fork");
        return 0;
    }else if(pid == 0)//子进程
    {
        close(pfd[0]);
        while (1)
        {
            strcpy(buf,"hahahahhahah");
            write(pfd[1],buf,strlen(buf));
            sleep(1);
        }
    }else // 父进程
    {
        close(pfd[1]);
        while (1)
        {
            re = read(pfd[0],buf,20);
            if(re > 0)
            {
                printf("red pipe=%s\n",buf);
            }
        } 
    }
}

 运行结果:

创建两个子线程,两个子线程对管道就行写,一个父进程对管道进行读,示例代码:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main()
{
    int pfd[2];
    int re,i;
    char buf[40] = {0};
    __pid_t pid;
    re = pipe(pfd);
    if(re < 0)
    {
        perror("pipe");
        return 0;
    }
    printf("%d,%d\n",pfd[0],pfd[1]);
    for(i = 0;i < 2;i++)
    {
        pid = fork();
        if(pid < 0)
        {
            perror("fork");
            return 0;
        }
        else if(pid > 0)// 父进程
        {
            
        }
        else //子进程
        {
            break; 
        }
    }
    if (i == 2)//父进程执行到这里
    {
        close(pfd[1]);
        while (1)
        {
            memset(buf,0,40);
            re = read(pfd[0],buf,20);
            if(re > 0)
            {
                printf("%s\n",buf);
            }
        }
        return 0;
    }
    if (i == 1)
    {
        close(pfd[0]);
        while (1)
        {
            strcpy(buf,"this is 2 process");
            write(pfd[1],buf,strlen(buf));
            usleep(930000);
        }
        return 0;
    }
    if (i == 0)
    {
        close(pfd[0]);
        while (1)
        {
            strcpy(buf,"this is 1 process");
            write(pfd[1],buf,strlen(buf));
            sleep(1);
        }
        return 0;
    }
}

 运行结果:

无名管道读写特性:

1.读管道:

(1)管道中有数据,read返回实际读到的字节数

(2)管道中无数据:

  • 管道写端被全部关闭(就是读写全关闭),read返回0(好像读到文件结尾)
  • 写端没有全部被关闭(就是把读关闭,没有关闭写),read阻塞等待(不就的将来可能有数据抵达,此时让出cpu)

2.写管道:

(1)管道读端全部被关闭(就是读写全关闭),进程异常终止(也可以使用SIGPIPE信号,使进程不终止)

(2)管道读端没有全部关闭(就是把写关闭,没有关闭读):

  • 管道已满,write阻塞(管道是64k)
  • 管道未满,write将数据写入,并返回实际写入的字节数

有名管道(fifo)特点:

  • 有名管道可以使非亲缘的两个进程互相通信
  • 通过路径名称来操作,在文件系统中可见,但内容存放在内存中(不是磁盘文件)
  • 文件IO来操作有名管道
  • 遵循先进先出规则
  • 不支持leek操作
  • 单工读写
有名管道创建-mkfifo:
#include <unistd.h>
#include <fcntl.h>
int mkfifo(const char *path,mode_t mode);
  • 成功时返回0,失败时返回EOF
  • path创建的管道文件路径
  • mode管道文件的权限,如666

示例代码:

向文件写:

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
int main()
{
    int re;
    int fd;
    char buf[32];
    re = mkfifo("./myfifo",0666);
    if (re < 0)
    {
        perror("mkfifo");
        // return 0;
    }
    fd = open("./myfifo",O_WRONLY);
    if (fd < 0)
    {
        perror("open");
        return 0;
    }
    while (1)
    {
        fgets(buf,32,stdin);
        write(fd,buf,strlen(buf));
    }
}

 向文件读:

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
int main()
{
    int re;
    int fd;
    char buf[32];
    fd = open("./myfifo",O_RDONLY);
    if (fd < 0)
    {
        perror("open");
        // return 0;
    }
    while (1)
    {
        re = read(fd,buf,32);
        if (re > 0)
        {
            printf("read fifo=%s\n",buf);
        }
        else
        {
            exit(0);
        }
    }
}

运行结果:

open函数四种状态

open(const char *path,O_RDONLY);

open(const char *path,O_RDONLY|O_NONBLOCK);

open(const char *path,O_WRONLY);

open(const char *path,O_WRONLY|O_NONBLOCK); 

 注意事项
  1. 就是程序不能以O_RDWR(读写)模式打开FIFO文件进行读写操作,而其行为也未明确定义,因为如一个管道以读/写方式打开,进程可以读回自己的输出,同时我们通常使用FIFO只是为了单向的传递数据
  2. 第二个参数中的选项O_NONBLOCK,选项O_NONBLOCK表示非阻塞,加上这个选项后,表示open调用是非阻塞的,如果没有这个选项,则表示open调用是阻塞的
  3. 对于以只读方式(O_RDONLY)打开的FIFO文件,如果open调用是阻塞的(即第二个参数为O_RDONLY),除非有一个进程以写方式打开同一个FIFO,否则它不会返回;如果open调用是非阻塞的(即第二个参数为O_RDONLY|O_NONBLOCK),则即使没有其他进程以写方式打开同一个FIFO文件,open调用将成功并立即返回。但如果没有其他进程以只读方式打开同一个FIFO文件,open调用将返回-1,并且FIFO也不会被打开
  4. 数据完整性,如果有多个进程写同一个管道,使用O_WRONLY方式打开管道,如果写入的数据长度小于等于PIPE_BUF(4K),那么或者写入全部字节,或者一个字节都不写入,系统就可以确保数据绝不会交错在一起。
  • 30
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值