当前位置:网站首页>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
边栏推荐
猜你喜欢

Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop

Bi skill - judge 0 and null

修身励学篇

1. package your own scaffold 2 Create code module

AutoCAD - two extension modes

带马尔科夫切换的正向随机微分方程数值格式模拟

Abnova丨CSV 磁珠中英文说明

天书夜读笔记——内存分页机制

Install mysql5.6 under linux64bit - the root password cannot be modified

Abnova丨A4GNT多克隆抗体中英文说明
随机推荐
4年工作經驗,多線程間的5種通信方式都說不出來,你敢信?
Transformers 库的基本使用
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
TC对象结构和简称
腾讯云WeCity丨你好 2022!
腾讯云WeCity解决方案
腾讯云WeCity丨产业联合 协同创新 共贺新春!
天书夜读笔记——深入虚函数virtual
Why does Dell always refuse to push the ultra-thin commercial notebook to the extreme?
搜索二维矩阵[二分巧用 + 记录不同于插入二分的解法]
excel 汉字转拼音「建议收藏」
数组中关于sizeof()和strlen
How much commission does CICC wealth securities open an account? Is stock account opening and trading safe and reliable?
Bi-sql top
C language boundary calculation and asymmetric boundary
The innovation consortium of Haihe laboratory established gbase and became one of the first member units of the innovation Consortium (Xinchuang)
Golang example renewal lock: redis+channel+sync Mutex
实验5 8254定时/计数器应用实验【微机原理】【实验】
CCNP的BGP部分笔记
Bi-sql select into