[Linux系统编程]_进程的通信(三)

嵌入式之路,贵在日常点滴

                                                                ---阿杰在线送代码

目录

进程间通信(IPC)介绍 

一、管道

二、FIFO

三、消息队列

ftok 

四、 共享内存

五、信号

信号概述

信号处理函数的注册

信号处理发送函数

六、信号量 (管理临界资源)

临界资源

七、通信方式总结


进程间通信(IPC)介绍 

进程间通信(IPC,InterProcess Communication)是指在不同进程之间传播或交换信息。

IPC的方式通常有管道(包括无名管道和命名管道)、消息队列、信号量、共享存储、Socket、Streams等。其中 Socket和Streams支持不同主机上的两个进程IPC。--》面试可能会问

以Linux中的C语言编程为例。

一、管道

管道,通常指无名管道,是 UNIX 系统IPC最古老的形式。

1、特点:

  1. 它是半双工的(即数据只能在一个方向上流动),具有固定的读端和写端。

  2. 它只能用于具有亲缘关系的进程之间的通信(也是父子进程或者兄弟进程之间)。

  3. 它可以看成是一种特殊的文件,对于它的读写也可以使用普通的read、write 等函数。但是它不是普通的文件,并不属于其他任何文件系统,并且只存在于内存中。

2、原型: 

#include <unistd.h>
int pipe(int fd[2]);    // 返回值:若成功返回0,失败返回-1

当一个管道建立时,它会创建两个文件描述符:fd[0]为读而打开,fd[1]为写而打开。如下图:

要关闭管道只需将这两个文件描述符关闭即可。 

3、例子

单个进程中的管道几乎没有任何用处。所以,通常调用 pipe 的进程接着调用 fork,这样就创建了父进程与子进程之间的 IPC 通道。如下图所示:

若要数据流从父进程流向子进程,则关闭父进程的读端(fd[0])与子进程的写端(fd[1]);反之,则可以使数据流从子进程流向父进程。

#include "stdio.h"
#include <unistd.h>
#include "string.h"
#include <stdlib.h>

int main()
{
        int fd[2];
        int pid;
        char buf[128];

//int pipe(int pipefd[2]);
        if(pipe(fd) == -1){
                printf("creat pipe failed\n");
        }

        pid = fork();//创建子进程

        if(pid<0){
                printf("creat child failed\n");
        }
        else if(pid > 0){
                printf("this is father\n");
                close(fd[0]);
                write(fd[1],"hello from father",strlen("hello from father"));
                wait(NULL);//不能让父进程先结束,让父进程在这边等着
        }
        else{
                printf("this is child\n");
                close(fd[1]);
                read(fd[0],buf,128);//把fd[0]端的数据读到buf里面,如果fd[0]里面没数据,read就会阻塞
                printf("read from father:%s\n",buf);
                exit(0);
        }

        return 0;
}

运行结果:

当程序运行到子进程处,readbuf里面数据为空,这时read堵塞,父进程往fd[1]里面写数据,写入之后,父进程先不结束而是等待子进程运行之后再结束。 如何体现呢,我们把上面的代码进行个微改,在父进程加个睡眠3s

#include "stdio.h"
#include <unistd.h>
#include "string.h"
#include <stdlib.h>

int main()
{
        int fd[2];
        int pid;
        char buf[128];

//int pipe(int pipefd[2]);
        if(pipe(fd) == -1){
                printf("creat pipe failed\n");
        }

        pid = fork();

        if(pid<0){
                printf("creat child failed\n");
        }
        else if(pid > 0){
                sleep(3);
                printf("this is father\n");
                close(fd[0]);
                write(fd[1],"hello from father",strlen("hello from father"));
                wait(NULL);
        }
        else{
                printf("this is child\n");
                close(fd[1]);
                read(fd[0],buf,128);
                printf("read from father:%s\n",buf);
                exit(0);
        }

        return 0;
}

运行结果: 

二、FIFO

FIFO,也称为命名管道,它是一种文件类型

1、特点

  1. FIFO可以在无关的进程之间交换数据,与无名管道不同。

  2. FIFO有路径名与之相关联,它以一种特殊设备文件形式存在于文件系统中。

2、原型

#include <sys/stat.h>
返回值:成功返回0,出错返回-1

int mkfifo(const char *pathname, mode_t mode);
char *pathname:管道名字,mode:管道权限,0600可读可写 

其中的 mode 参数与open函数中的 mode 相同。一旦创建了一个 FIFO,就可以用一般的文件I/O函数操作它。

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

int main()
{
        if((mkfifo("./file",0600) == -1) && errno != EEXIST)//如果管道创建失败且失败的原因不是已存在
        {
                printf("make fifo failed\n");
                perror("why");
        }

        return 0;
}

  

当 open 一个FIFO时,是否设置非阻塞标志(O_NONBLOCK)的区别:

  • 若没有指定O_NONBLOCK(默认),只读 open 要阻塞到某个其他进程为写而打开此 FIFO。类似的,只写 open 要阻塞到某个其他进程为读而打开它。

  • 若指定了O_NONBLOCK,则只读 open 立即返回。而只写 open 将出错返回 -1 如果没有进程已经为读而打开该 FIFO,其errno置ENXIO。

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

int main()
{
        char buf[30] = {0};
        int nread = 0;

        //如果管道创建失败且失败的原因不是已存在
        if((mkfifo("./file",0600) == -1) && errno != EEXIST)
        {
                printf("make fifo failed\n");
                perror("why");
        }

        int fd = open("./file",O_RDONLY);
        printf("open success\n");

        while(1){
                nread = read(fd,buf,30);

                printf("read %d byte from fifo,context:%s\n",nread,buf);
        }

        return 0;
}

若没有指定(默认),只读open到阻塞到其他进程为写而打开次fifo,
只写open要阻塞到其他进程为读而打开次fifo。

#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值