IPC 进程间通信

IPC

InterProcess Communication

The concept of IPC

Each process has a differnt user addess space,and local variables 各自看不见,so 进程间通信 need kernel(内核), so a buffer is opened in the kernel,process 1 copies data from user space to this buffer,and process 2 reads the data from the kernel buffer,this mechanism 机制 calls IPC,InterProcess Communication
在这里插入图片描述

7 ways of IPC

Pipe

也叫做匿名管道

管道,traditional Unix communication mechanism

  • The pipes are half-duplex 半双工,data can only flow in one direction,when two sides need to communicate,we should create 2 pipes
  • this way can only use between parente-child(父子) or sibling(兄弟) processes(具有血缘关系的进程)
  • 管道单独构成一个独立的文件系统,对于管道两头来说,它可以看作是一个文件系统,专门来传输东西的,并且pipes only exists in memory
  • 数据的读写,we know, pipes are flow in one direction,so a process writes to the pipeline, and the other process reads from the other sides,one wirtes,one read,in one direction

管道的实质

  • The essence of a pipeline is kernel buffer,一个内核缓冲区,进程是先入先出来存数据进去,管道一端顺序写入,other side,顺序read
  • The buffer can be seen as a circular queue,the read and write positions are automatically grown,after reading it,it no longer exists in buffer.
  • when a buffer is empty or full,there are some rules to controll the relevant read process or write process,当有新数据写入,or 旧数据读出,就会唤醒在waiting queue里边的process

LImitations of the pipeline

管道的主要的局限性正在于它的特点上

  • only one-way data streams are supported
  • only use between process that are related to each other 为什么只能用于亲缘关系啊?
  • no name 为什么没有名字属于是局限性呢?
  • The buffor of the pipeline is limited,the buffer exists in the memory, so it’s limited
  • The pipeline transmit an unformatted byte stream,无格式字节流,也就是说,管道的输入输出,必须在读写之前规定好数据的格式

这里需要知道的是,为什么说没名字,就只能用于有亲缘关系的进程呢,因为,如果没名字的化,we get two processes, process 1 have pipes, but process 2 don’t know the address of the kernel buffer,so process 2 can’t communicate with process 1. 像父子进程啊,一般都是子process 会copy all parent resouces,including the file decriptor,文件描述符,so they use same pipeline;

Named Pipeline

有名管道 FIFO,就是因为匿名管道,只用于亲缘关系之间,to overcome it,the Named Pipeline was proposed.提出

两者不同之处 在于 named pipeline provides a pathname 与之关联,以有名管道的形式存在于file system中,so two processes 没亲缘关系,也可以互相通信,他们只要访问这个路径就ok了,named pipeline exist in file system,the content exists in the memory

Summary

  • 管道是特殊的文件,并且满足在先入先出的原则进行读写,但是不能进行定位读写
  • 匿名管道是单向的,只能在亲缘关系的process之间通信,有名管道不用,都可以通信
  • 无名管道的blocking 问题,读写的时候,需要确认对方存在,不存在, exit,当前线程向无名管道writre data,必须other sides 有process,不然exit,写入的data,超过max data,blocking. 没数据,blocking. If the pipe lines finds the other 断开, 自动exit
  • 有名管道的blocking 问题,named pipelines must confirm 对方存在,否则blocking,也就是说,当我们读管道的时候,必须在此之前,有一个process 在写管道,否则blocking 此外,我们通过读写模式(O_RDWR)打开有名管道,也就是当前进程读,当前进程写,不会block

Signal

信号

  • Signaling is a mechanism(机制) usedi in Linux to communicate or maniplulate(操作) between processes,signals 任何时候都能发给某个process,and 不用知道它的状态如何
  • If the process is not currently executing, the kernel will save the signal until the process reples to execution and passes it on. 没执行的化,信号会被保存起来,然后等他恢复之后,再传回来
  • If the process is blocking,信号就延迟传递,until its blocking canceled ,然后再发给他.

Common singals in LInux.

(1) SIGHUP: 用户从终端logs out,all 启动的process 都收到
(2) SIGINT: 程序终止信号,while the program is running , press CTRL + C key will传递改sigal
(3) SIGQUIT: 程序退出,运行的时候,按 CTRL + \
etc

信号来源

信号是再软件上对终端机制的一种模拟,是一种asynchronous通信方式,signal can 直接在user space 和 kernel space直接进行交互,kernel can use the signal to inform the user space process of which system events have occured,and there are 2 main sources of signal events

  • 硬件来源: 用户press Ctrl + C,or hardware exception
  • 软件终止,终止进程信号,其他进程调用kill 函数,软件异常发出信号

信号的lifecycle and 处理流程

  • The signal is generated by a certain process, and 设置传递对象,然后传给OS
  • OS 通过接收process的设置选择性的发送给接收者,这个设置就是是否阻塞,也就是说,如果接收者对该信号是阻塞的,then OS will save this signal,不传递,until 它解除了对这个signal的block,If 接收者没blocking,那么OS will pass the signal
  • 目标进程,接收到之后,会根据当前线程对此信号设置的预处理方式,暂时终止当前代码执行,save context(主要包括 临时寄存器的data,当前program的position,以及cpu的status),然后去执行中断服务程序,执行完之后,回到中断位置.

Message Queue

消息队列

  • Message Queue is a linked list of messages stored in the kernel,每个Message Queue 用消息队列标识符表示
  • 消息队列存在于内核中,只有说内核重启或者显示的删除消息队列,才会删除消息队列
  • 和管道不同的是,消息队列在某个进程在写如消息的时候,不需要另外一个线程在那等,也就是说,消息队列是异步的

Summary of message queue features:

  • Message queue is a linked list ,有special format,stored in the memory,通过message queue标识符标识
  • allow multiple process to read or write
  • pipeline and message queue are on a first in first out basics
  • message queue可以实现message random 读取,message不一定是顺序读取,可以按照消息的type,进行分类读取,更有优势
  • The message queue overcomes the shortcomings of the signal carryin the small amout of information and the pipeline can only carry unformatted words,and the limits size of the buffer
  • Now,2 main type of message queue,POSIX message queues and System V message queues,System V message queues are widely used. Sysrtem V 生命周期和kernel 是一样的,只有说kernel重启,或者人工删除queues才会被删除

Share Memory

共享内存

  • Share memory allows multiple processes to read and write to the same memory space,是最快的IPC形式,就是为了针对其他IPC效率低设计的
  • 为了多个进程之间交换信息,kernel 专门流出一块内存区,可以由需要访问的进程将它映射到自己的私有地址space,process 可以直接写这一块区域,并且不用进行copy,从而大大提高效率
  • 因为是share momory,必须depend on a certain synchronization machanism ,like semaphores 信号量,来达到互斥

Semaphore

信号量是一个计数器,为了多进程对share data 的访问,就是进程间的同步

为了获取共享资源,需要干以下的事

  • create a semaphore,要求调用者specify an initial value,usually 1 or 0,对于2值信号量来说
  • wait for a semaphore,该操作会测试信号量的值,if < 0,block,也就是 P操作
  • 挂出一个信号量,该操作,会 将信号量 + 1,也叫做V操作

为了实现semaphore correctly,信号量的测试 + 减一 should be an atomic operation,so 信号量一般在kernel中实现
linux环境中,有三种类型,
Posix 可移植性操作系统接口,有名信号量
Posix 基于内存的信号量
System V信号量

信号量和普通整形变量的区别

  • 信号量事非负整型,除了初始化之后,它只能通过两个atomic operation, wait and singal来进行访问
  • 操作也叫做PV原语,对普通变量来说,什么语句块都能访问

互斥量和信号量之间的区别

互斥量会为了thread之间的互斥,信号量用于线程的同步,他们两的区别就是互斥和同步的区别

  • 互斥,也就是对于某个资源,在同一时间内,只allow 一个user进行访问,但是不能保证对资源的访问顺序,也就是互斥的访问是out of order
  • 同步,在互斥的基础上,通过其他机制,对资源进行有序的访问
  • 互斥量的加锁 + 解锁必须是同一个线程分别使用,信号量可以有一个线程释放,另外一个线程得到

Socket

套接字是一种通信机制,凭借这种机制,客户/服务器系统的开发,既可以在本地单机上运行,也可以跨网络运行,换句话说,它可以通过不再统一计算机上通过connect network 进行通信

套接字是支持TCP/IO的网络通信的基本操作单元,可以看作是2个不同之间之间进程进行双向通信的断电,简单来说,就是一种约定,用套接字种的相关函数完成通信

Socket features

Socket are determined by three attributes, domain ,port number and protocal type
域,端口号,协议类型

  • domain指定socket 通信的网络介质,2 common domains ,One is AF_INET,指的是Internet网络,当客户使用socket来跨网络连接的时候,需要用到服务器computer的ip地址and port来指定一台联网机器上的特定服务,使用socket作为通信的终点,服务器应用程序在通信之前,需要绑定一个port,在指定的port下wait user connect. 另外一个domain AF_UNIX,标识Unix文件系统,他就是文件的输入输出,而他的地址是文件名
  • port number: 每一个基于tcp/ip网络通讯的程序或进程都被赋予了唯一的端口和端口号,端口是一个信息缓冲区,用户保留Socket中的输入输出信息,端口号是一个16位无符号整数,范围是0 - 65535,用来区别主机的不同程序,低于256的端口号被用来给标准应用程序,比如pop3的port number是110,每一个socket都组合进了ip地址,port,这样就可以新参整体,以来区分每一个socket
  • socket protocol type: internet provides 3中通信机制,第一种是流套接字,流套接字在domain中通过tcp/ip连接实现,同时还是AF_UNIX中常用的套接字类型,流套接字提供的是一个有序,可靠,双向字节流的连接,因此发送的数据确保不丢失,重复,乱序到达,而且还有一定的出错后的重新发送机制 第二个是数据报套接字,通过udp/ip实现,它对发送的数据有限制,数据报作为一个单独的网络信息被传输,有可能丢失复制或者错乱到达,所以udp不是一个可靠的协议,但是他快 三是原始套接字原始套接字允许对较低层次的协议直接访问,比如IP、 ICMP协议
  • 28
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

憨憨小江

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值