当前位置:网站首页>IPC mechanism
IPC mechanism
2022-06-25 01:23:00 【Huangyuanbao】
List of articles
Preface
IPC - Interprocess communication , It is often used in multiprocess programming to share data , System learning , Conceptual things , It is often used in past operations socket Conduct IPC, But other things need to be understood , This article briefly summarizes
communication mode
1. The Conduit
1.1 Anonymous pipeline
scene
cat xx.txt|grep a
Knock over linux Those who command can understand this command , This is the use of anonymous pipes , Anonymous pipes can only pass through fork The way to create a process under the communication , So the information flow is unidirectional
principle
Kernel space unformatted byte stream data ,
Dependent file system , The process ends and is automatically destroyed
One-way communication , Upstream and downstream processes default to Sync
fork The process of will inherit the opened by the parent process fd therefore , Applicable to parent-child process or kinship process
summary
Anonymous pipeline , Its communication range can only exist between father and son / The process of kinship
1.2 name pipes FIFO
scene
Two way information flow
principle
Kernel space unformatted byte stream data
Device file
Create a pipe file in advance , Use mkfifo fifo Create named pipes
summary
Can be used for any interprocess communication
Whether anonymous or named , It is rarely used in actual scenarios , But some specific scenarios may use it more efficiently than others , For example, the transmission of large files on this computer
2. Message queue
principle
Kernel space message linked list
The message body is user-defined , Each message body is stored in a fixed size
problem
Kernel mode user mode switching and data copying
Release... Must be displayed , Otherwise, the memory will not be released as the process ends , Unless the operating system restarts
summary
3. Shared memory
principle
Virtual memory maps to the same physical address , Avoid kernel mode user mode switching and data copying
problem
Concurrency issues , Non concurrent secure , Overwriting may occur when multiple processes write data to the same shared memory , How to solve ? Lock it , namely Semaphore mechanism
4. Semaphore
scene
Used for mutual exclusion and synchronization between processes
principle
The kernel directly manages , Is an integer counter , Realize mutual exclusion and synchronization between processes , Not used to cache communication data between processes , There are two types of : Binary semaphore , Count the semaphore , Binary semaphores are 1/0 Used to realize mutual exclusion / Sync , The counting semaphore is used for program counting
Two atomic operations
- P operation
Semaphore -1, After subtraction, if the semaphore <0, Indicates that the resource has been occupied , The process needs to block and wait ; After subtraction, if the semaphore >=0, It means that there are still resources available , The process can continue normally . - V operation
Semaphore +1, After addition, if the semaphore <=0, Indicates that there is a blocking process , The process will then wake up and run ; After addition, if the semaphore >0, Indicates that there is no blocking process .
- P operation
example
Mutex semaphore: The initial value of semaphore is 1, This mechanism ensures that only one process can access the memory at any time .You can see that mutual exclusion requires two factors , ① Semaphore =1 ② The same process executes PV operation
Synchronous semaphore: The initial value of semaphore is 0, This mechanism guarantees mutual exclusion , Also guaranteedSyncThe order
The figure above shows two processes , The process 1 In the process 2 From the previous execution, we can see that synchronization requires two factors , ① Semaphore =0 ②PV The operation is not in the same process
The above two figures mentioned
A critical regionThere is a correspondingCritical resources, How to understand ?
Critical resources : At the same time , A resource that only one process or thread is allowed to access
A critical region : Code snippet for accessing critical resources , The process in this code / Threads will access shared resources , When another process / When the thread is already running in this code . This process / Threads cannot execute in this code , To control access to critical resources is to control the program to enter the critical area
5. The signal
asynchronous communication , A number , Signal is the only asynchronous communication mechanism among inter process communication mechanisms
- scene
kill -9 xx:SIGKILL The signal , End the process immediatelyctrl+c: SIGINT The signal , To force the execution of a program to be interrupted , Kill the process of the programctrl+z: SIGTSTP The signal , Interrupt tasks , Suspend process , fg Re execution ,bg Directly kill , vim A file can be tested
6. Socket
Cross network and different hosts need to use socket We're communicating , The above 5 A mechanism that is all on the same host
Generally, this kind of is through TCP/UDP agreement
边栏推荐
- 归并排序模板 & 理解
- ‘distutils‘ has no attribute ‘version
- 天书夜读笔记——内存分页机制
- Hands on data analysis data modeling and model evaluation
- Bi-sql between
- Why does Dell always refuse to push the ultra-thin commercial notebook to the extreme?
- Ecological escort cloud service providers wave "Intel flag"
- The optimism about technology is making Dell achieve more than expected
- Reverse ordinal number by merge sort
- 腾讯云WeCity解决方案
猜你喜欢

void* 指针

修身励学篇

Deploy a production cluster using Loki microservice pattern
The latest QQ wechat domain name anti red PHP program source code + forced jump to open

Bi-sql Union

"One good programmer is worth five ordinary programmers!"

Bi SQL alias

Abnova 5-methylcytosine polyclonal antibody

4年工作經驗,多線程間的5種通信方式都說不出來,你敢信?

JVM指令
随机推荐
海河实验室创新联合体成立 GBASE成为首批创新联合体(信创)成员单位
归并排序模板 & 理解
屡获大奖的界面控件开发包DevExpress v22.1官宣发布
[live review] 2022 Tencent cloud future community city operator recruitment conference and SaaS 2.0 new product launch!
Cobalt strike installation tutorial
The innovation consortium of Haihe laboratory established gbase and became one of the first member units of the innovation Consortium (Xinchuang)
Tencent cloud wecity solution
汇编语言(2)基础知识-debug
Golang example renewal lock: redis+channel+sync Mutex
Bi SQL alias
Abnova丨A4GNT多克隆抗体中英文说明
yasea apk 下载 镜像
Tencent cloud wecity Hello 2022!
明日考试 最后一天如何备考?二造考点攻略全整理
Deep learning LSTM model for stock analysis and prediction
2种常见的设备稼动率OEE监测方法
Bi-sql delete
木瓜蛋白酶中英文说明书
Bi-sql between
Excel Chinese character to pinyin "suggestions collection"