当前位置:网站首页>Communication mode between processes
Communication mode between processes
2022-07-07 09:27:00 【wrdoct】
List of articles
Preface
Communication between processes .
Communication between processes
Interprocess communication (IPC
): The data transfer 、 Notification event 、 Resource sharing 、 Process control .
The signal ; //Unix
The Conduit ; //Unix
Famous pipeline (FIFO); //Unix
Message queue ;
Shared memory ;
Semaphore ;
Socket Socket .
One 、 The Conduit
Used to have genetic relationship Communication between processes ;
A pipe is a Byte stream ;
The data transmitted through the pipeline is The order Of , The order in which bytes are read from the pipeline is exactly the same as the order in which they are written to the pipeline ( It's like a queue );
The direction of data transmission in the pipeline is A one-way Of , Write at one end and read at the other , yes Half duplex Of ;
Pipeline read data is Disposable Of .
Terminal command :
ls | wc -l //wc—— Number of statistical files
ulimit -a // View pipe buffer size
function :
int pipe(int pipefd[2]);// Create an anonymous pipeline , Used for interprocess communication .
long fpathconf(int fd, int name);// View pipe buffer size
【 notes 】 Why pipes are used for inter process communication with kinship ( Why can the processes of kinship communicate through pipelines )?
Because the parent process fork Come out with a sub process , Will be able to Virtual address space replication One copy , There is one in the virtual address space of the parent process Document descriptor table , Point to the read end and write end , Then the child process also has a file descriptor table , Point to the read end and write end , So it can communicate .
【 notes 】 Why should one close the read side and the other close the write side ?
To avoid the father ( Son ) process Write Then father ( Son ) Process again read .
Two 、 Famous pipeline FIFO
The famous pipeline provides a Pathname Associated with it , therefore FIFO The process created does not have the restriction of kinship , As long as the process can access the path , You can pass FIFO Mutual communication .
Terminal command :
mkfifo name // establish FIFO The Conduit
function :
int mkfifo(const char *pathname, mode_t mode); // establish FIFO The Conduit
Reading and writing characteristics of pipeline :
Read pipeline :
There's data in the pipeline :
read Returns the actual number of bytes read
No data in the pipeline :
The write side is completely closed ,read return 0( Equivalent to reading to the end of the file )
The write side is not completely closed ,read Block waiting
Write the pipeline :
All readers are turned off :
Process aborted ( Process received SIGPIPE The signal )
The reader is not all closed :
The pipe is full ,write Blocking
The pipe is not full ,write Write data to , And return the actual number of bytes written
3、 ... and 、 Memory mapping
(I/O) Disk file Data mapping to Memory , adopt Modify memory to modify files .
The process communication realized by memory mapping is Non blocking Of .
function :
// Map the data of a file or device to memory
void *mmap(void *addr, size_t length, int prot, int flags,int fd, off_t offset);
// Free memory map
int munmap(void *addr, size_t length);
Four 、 Shared memory
Shared memory allows two or more processes Shared physical memory The same area of ( Often referred to as segments ).
No kernel intervention is required .
Terminal command :
ipcs -a // Print all ipc Information
ipcs -m // Print shared memory ipc Information
ipcs -q // Print message queue ipc Information
ipcs -s // Print signal ipc Information
ipcrm -M shmkey // remove shmkey Shared memory segment created
ipcrm -m shmid // remove shmid The shared memory segment identified
ipcrm -Q msgkey // remove msgkey Created message queue
ipcrm -q msgid // remove msgid Identified message queue
ipcrm -S semkey // remove semkey Created signals
ipcrm -s semid // remove semid Signal of identification
function :
// Create a new shared memory segment , Or get the ID of an existing shared memory segment . The data in the newly created memory segment will be initialized to 0
int shmget(key_t key, size_t size, int shmflg);
// Associate with the current process
void *shmat(int shmid, const void *shmaddr, int shmflg);
// signal communication addr
// Release :
// Disassociate the current process from shared memory
int shmdt(const void *shmaddr);
// Delete shared memory , Call it once , All associated processes are disassociated before calling
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
How does the operating system know how many processes are associated with a piece of shared memory ?
answer : Shared memory maintains a structure struct shmid_ds,
There is a member in this structure shm_nattch,
shm_nattach The number of associated processes is recorded
5、 ... and 、 The signal
The signal is Linux One of the oldest ways of interprocess communication , Is the of the process at the time of the event A notification mechanism , Sometimes called Software interrupt , It is a simulation of interrupt mechanism at software level , It's a kind of asynchronous communication The way .
Signals can cause one running process to be interrupted by another running asynchronous process , To deal with an emergency .
Characteristics of signal :
Simple ; Can't carry a lot of information ; Send only when a certain condition is met ; High priority .
Terminal command :
kill -l // Look at all the signals
Common signals :
SIGINT // Terminate the process ——ctrl+C
SIGQUIT // Terminate the process ——ctrl+\
SIGKILL // Kill process
SIGCSTOP // Stop the process
SIGCONT // Continue the process
The signal 5 A default processing action :
Term Terminate the process
Ign The current process ignores this signal
Core Terminate the process , And generate a Core file , Used to save error messages //
Stop Pause the current process
Cont Continue executing the currently suspended process
【 notes 】Core Use :
ulimit -a
ulimit -c unlimited // Change the upper limit of available resources
g++ ./a.out -g
gdb a.out core-file core // see core File error message
The state of the signal : produce 、 outstanding ( The signal is generated and not processed )、 Blocking ( The blocking signal is processed , Do not block signal generation ).
int kill(pid_t pid, int sig);// To any process or process group pid, Send any signal sig
int raise(int sig);// Send a signal to the current process
void abort(void);// send out SIGABRT Signal to the current process , Kill the current process
// Set the timer ( alarm clock ). Function call , Start countdown , When the countdown is 0 When , The function sends a signal to the current process :SIGALARM, Terminate the current process
unsigned int alarm(unsigned int seconds);
alarm(0); // Cancel timer
// Set the timer ( alarm clock ). Can replace alarm function . Precision is subtle us, Periodic timing can be realized ( Do one thing every few seconds )
int setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value);
// Set the capture behavior of a signal
sighandler_t signal(int signum, sighandler_t handler);
// Check or change signal processing . Signal capture
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
【 notes 】 Use SIGCHLD
Signals solve the problem of zombie process .
Multiple signals can use one called Signal set The data structure of .
int sigemptyset(sigset_t *set);// Clear the data in the signal set , Position all the signs in the signal set as 0
int sigfillset(sigset_t *set);// Position all the signs in the signal set as 1
int sigaddset(sigset_t *set, int signum);// Set the flag bit corresponding to a signal in the signal set to 1, Indicates blocking this signal
int sigdelset(sigset_t *set, int signum);// Set the flag bit corresponding to a signal in the signal set to 0, Indicates that the signal is not blocked
int sigismember(const sigset_t *set, int signum);// Determine whether a signal is blocked
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);// Set the data in the custom signal set into the kernel ( Set blocking , unblocked , Replace )
int sigpending(sigset_t *set);// Get the pending semaphore set in the kernel
6、 ... and 、Socket Socket
TCP/IP Protocol family communication socket Introduction and programming
边栏推荐
- Using JWT to realize login function
- 超十万字_超详细SSM整合实践_手动实现权限管理
- Idea development environment installation
- shake数据库中怎么使用Mongo-shake实现MongoDB的双向同步啊?
- Test Engineer Interview Questions 2022
- Jenkins automated email
- 创建一个长度为6的int型数组,要求数组元素的值都在1-30之间,且是随机赋值。同时,要求元素的值各不相同。
- nlohmann json
- Loxodonframework quick start
- C language pointer (Part 2)
猜你喜欢
How long does the PMP usually need to prepare for the exam in advance?
STM32 and motor development (from stand-alone version to Networking)
Jenkins+ant+jmeter use
Network request process
如何使用clipboard.js库实现复制剪切功能
软件建模与分析
Dynamics 365Online ApplicationUser创建方式变更
Locust performance test 2 (interface request)
Sublime Text4 download the view in bower and set the shortcut key
VSCode+mingw64
随机推荐
How does the project manager write the weekly summary and weekly plan?
Binary tree high frequency question type
Vs2013 generate solutions super slow solutions
[SVN] what is SVN? How do you use it?
shake数据库中怎么使用Mongo-shake实现MongoDB的双向同步啊?
Nested (multi-level) childrn routes, query parameters, named routes, replace attribute, props configuration of routes, params parameters of routes
Port multiplexing and re imaging
数据库多表关联查询问题
Register address name mapping
其实特简单,教你轻松实现酷炫的数据可视化大屏
C language pointer (special article)
VSCode+mingw64+cmake
IIS faked death this morning, various troubleshooting, has been solved
Integer or int? How to select data types for entity classes in ORM
Netease cloud wechat applet
网易云微信小程序
消费互联网的产业链其实是很短的,它仅仅承接平台上下游的对接和撮合的角色
Unity shader (learn more about vertex fragment shaders)
华为HCIP-DATACOM-Core_03day
[cloud native] Devops (I): introduction to Devops and use of code tool