In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly talks about "what is the way of Linux inter-process communication". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the way of Linux inter-process communication"?
Inter-process communication: a way provided by the operating system for the system to achieve inter-process communication.
It is impossible to communicate directly between processes, because each process has an independent virtual address space and accesses its own virtual address, so the process is independent and cannot communicate directly.
According to different communication scenarios, many different ways of communication are provided.
Types of inter-process communication: pipes, shared memory, message queues, semaphores
Pipeline
Features: half-duplex communication-one-way communication with optional direction
Essence: open up a buffer (a piece of memory in kernel space) in the kernel
Principle: multiple processes communicate by accessing a buffer in the same kernel (copying the operation handle of the buffer)
Classification: anonymous pipes: buffers have no identifiers and can only be used for related inter-process communication
Named pipes: buffers have identifiers that can be used for any inter-process communication on the same host
Everything is a file under linux-everything operates as a file (including pipes), and access to pipes is completed through IO operations.
Anonymous pipeline
Return value: 0 for success;-1 for failure
Features: can only be used for related inter-process communication
The anonymous pipe has no identifier and cannot be found by other processes, so it can only obtain the operation handle by the child process to copy the parent process to achieve communication.
Read-write feature: if there is no data in the pipe, read will block
If the pipe is full of data, the write will block
If the reader of all pipes is closed, continuing write will trigger an exception, causing the process to crash and exit.
If the write end of all pipes is closed, the continued read will return 0 after reading the data and no longer block.
Note: the pipe is a half-duplex communication. When communicating, once the direction is selected, the unused end should be closed.
Named pipe: essentially a buffer in the kernel with an identifier that can be found by other processes, so it can be used for any inter-process communication on the same host
The identifier of a named pipe is a pipe type file that can be seen in the file system.
Multiple processes communicate by opening the same pipe file and accessing the buffer in the same kernel.
Command action: mkfifo filename creates a named pipe file
Function operation: int mkfifo (const char * pathname, mode_t mode)
Pathname: file name; mode: create permission
Return value: 0 for success;-1 for failure
Summary: the nature of pipes: a buffer in kernel space
Principle: multiple processes realize data transmission by accessing the same buffer
Classification: anonymous pipes, named pipes
Anonymous pipes: can only be used for related inter-process communication
Named pipes: can be used for any inter-process communication on the same host
Features: ① half-duplex communication-one-way communication with optional direction
Provide byte streaming service: an orderly, reliable, connection-based streaming service
Connection-based: write exception when all readers are closed; read returns 0 if all writers are closed
② comes with synchronization and mutual exclusion:
Synchronization: access operation security through unique access to critical resources by processes at the same time
Mutual exclusion: make the process's access to critical resources more reasonable and orderly by judging some conditions.
The embodiment of mutual exclusion: if the size of the write operation to the pipe does not exceed the PIPE_BUF-4096 size, the atomicity of the operation is guaranteed.
The embodiment of synchronization: if there is no data in the pipeline, the read is blocked, and if the pipeline data is full, the write is blocked.
The ③ life cycle follows the process: without human intervention, the pipe buffer is released after all processes that open the pipe exit.
Shared memory: used for data sharing between processes
Essence: a piece of physical memory
Principle: open up a physical memory space, multiple processes map the same block to their own virtual address space, access directly through the virtual address, and then realize data sharing
Features: the fastest way to communicate between processes, with the life cycle of the kernel
Shared memory accesses physical memory directly through virtual address to achieve data sharing. Compared with other methods, data needs to be copied to the kernel and copied to user mode when in use, resulting in two fewer data copy operations.
Note: the operation of shared memory needs to pay attention to security issues.
Operation procedure:
① creates or opens shared memory
② maps shared memory to the virtual address space of the process
③ performs various memory operations through mapped virtual addresses
④ unmapping relationship
⑤ deletes shared memory
Int shmget (key_t key, size_t size, int shmflg)
Key: identifier (multiple processes open the same shared memory with the same identifier)
Size: the amount of space opened up when created (in memory pages)
Shmflg: open method + create permission-- IPC_CREAT | IPC_EXCL | 0664
Return value: successfully returns a non-negative integer-operation handle; failure returns-1
Void * shmat (int shmid, const void * shmaddr, int shmflg)
Operation handle returned by shmid:shmget
Shmaddr: mapped address, usually set to NULL
Shmflg: access method after successful mapping; SHM_RDONLY- read-only; 0-read and write
Return value: the mapped first address is returned for success; (void *)-1 is returned for failure.
Int shmdt (const void * shmaddr)
Shmaddr: the first address after mapping
Return value: 0 for success;-1 for failure
Int shmctl (int shmid, int cmd, struct shmid_ds * buf)
Operation handle returned by shmid:shmget
Cmd: operation type-- IPC_RMID marks the shared memory as destroyed
Buf: for IPC_RMID, 0 is returned for success and-1 for failure
Message queue
Essence: a priority queue in the kernel in which multiple processes transfer data blocks between processes by accessing the same queue and adding or acquiring nodes to the queue.
Features: built-in synchronization and mutual exclusion, life cycle with the kernel
Semaphore
Essence: a counter in the kernel + pcb waiting queue
Function: to achieve synchronization and mutual exclusion between processes, and to coordinate process access to critical resources
P operation: counter-1, which determines that if the count is less than 0, the process is blocked.
V operation: counter + 1 to wake up a blocked process
Through its own counter to count resources, through counting to determine whether the process to obtain resources is reasonable, unreasonable blocking. After waiting for a resource to be generated, wake up the blocked process
Implementation of synchronization: count resources through counters and perform P operations before getting resources
Implementation of mutual exclusion: the counter is 1, which means that there is only one resource. The process performs P operation before accessing the resource and V operation after access.
At this point, I believe you have a deeper understanding of "what is the way of Linux inter-process communication". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.