进程间的通信

进程间的通信:两个进程之间的数据交换

无名管道 有名管道 信号通信 共享内存 消息队列 信号灯 socket

通信领域的几个概念:

单工通信:数据的传送方向为单向

半双工通信:数据的传送方向为双向,但不同时

全双工通信:数据的传送方向为双向且同时

1、无名管道

无名管道是用pipe创建的一个内核文件,用于具有亲缘关系的进程间的通信。

 int pipe(int pipefd[2]);

pipefd【2】:数组,fd【0】用来标识读管道文件

                                  fd【1】用来标识写管道文件

返回值:0 成功; -1 失败

特点:1、只能用于具有亲缘关系的进程间的通信

           2、是一种单工通信模式(理论上是双工的,但是通信双方的收发顺序不好控制,通常用作单工。)

           3、无名管道的数据存放在内存中,管道中的数据读走后就不存在了,无名管道只存在于内核中,在文件系统中不可见。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define BUF_SIZE 20

int main()
{
        //1  pipe
        int fd[2] = {0};
        if(0 != pipe(fd))
        {
                printf("create pipe error!\r\n");
                return -1;
        }
        printf("1:create pipe ok!\r\n");
        //2 fork
        pid_t pid = -1;
        pid = fork();
        if(-1 == pid)
        {
                return -1;
        }
        char buf[BUF_SIZE] = {0};
        if(0 == pid)
        {
                //child write
                close(fd[0]);
                printf("child process\r\n");
                strcpy(buf, "child to parent");
                write(fd[1], buf, BUF_SIZE);
        }
        else
        {
                //parent read
                close(fd[1]);
                printf("parent process!\r\n");
                read(fd[0], buf, BUF_SIZE);
                printf("parent read:%s\r\n", buf);

        }
        return 0;
}
                                                                                                                                 1,9          顶端


2、有名管道

有名管道是用mkfifo创建的一个内核文件,用于任意两个进程之间的通信,管道 文件在文件系统中可见。

int mkfifo(const char *pathname, mode_t mode);

作用:创建一个有名管道文件

*pathname:有名管道文件的名称

mode:文件权限

返回值:0 成功  -1  失败

A:1  mkfifo------->有名称

      2  open

      3   write 

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#define BUF_SIZE 20
int main()
{
        //1 mkfifo//创建a进程
        int ret = -1;

        ret = mkfifo("fifo", 0666);
        if(ret < 0 && EEXIST != errno)//创建文件失败且该文件不存在
        {
                return -1;//创建文件失败
        }
        创建文件失败但是该文件存在----》创建文件成功
        printf("1.create fifo ok!------\r\n");
        //2 open
        int fd = open("fifo", O_WRONLY, 0666);//内核函数用open打开
        if(-1 == fd)
        {
                return -1;
        }
        printf("2: open fifo ok!---------\r\n");
        //3 write
        char buf[BUF_SIZE] = {0};
        strcpy(buf, "hello 22101");
        write(fd, buf, BUF_SIZE);
        //4 close
        close(fd);
        return 0;
}

B:   1  mkfifo--------找到有名管道文件

       2  open

       3   read

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

#define BUF_SIZE 20
int main()
{
        //1 mkfifo
        int ret = -1;
        ret = mkfifo("fifo", 0666);
        if(ret < 0 && EEXIST != errno)
        {
                return -1;
        }
        printf("create fifo ok!------\r\n");
        //2 open
        int fd = open("fifo", O_RDONLY, 0666);
   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值