In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
How many ways does linux implement interprocess communication? In view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
Process concept:
Process is the concept of the operating system, whenever we execute a program, for the operating system to create a process, in this process, accompanied by the allocation and release of resources. It can be considered that a process is an execution of a program.
Process communication concepts:
The process user space is independent of each other and generally speaking cannot access each other. However, in many cases, processes need to communicate with each other to complete some function of the system. Processes coordinate their behavior by communicating with the kernel and other processes.
The way linux communicates between processes:
The basic communication modes between Linux processes are: pipe (including anonymous and named pipes), signal, message queue (queue), shared memory, semaphores and sockets.
Here are some of these ways:
1. Pipeline
The essence of the pipeline is a kernel buffer, and the function of the pipeline is just like its name. The two processes that need to communicate are at both ends of the pipeline, and the process uses the pipeline to transmit information. A pipe is a file for processes at both ends of the pipe, but this file is special because it does not belong to the file system and exists only in memory.
2. Signal
The signal is a simulation of the interrupt mechanism at the software level, and it is a way of asynchronous communication, and the process does not have to wait for the signal to arrive through any operation. Signals can interact directly between the user-space process and the kernel, and the kernel can use the signal to inform the user-space process of what system events have occurred.
3. Message queue
A message queue is a linked list of messages with a specific format, stored in memory and identified by a message queue identifier, and allows one or more processes to write and read messages to it.
4. Shared memory
So that multiple processes can directly read and write the same memory space, which is designed for the low efficiency of other communication mechanisms.
In order to exchange information between multiple processes, the kernel sets aside a memory area that can be mapped to its own private address space by the processes that need to be accessed. The process can read and write this piece of memory directly without copying the data, thus greatly improving the efficiency.
Note: shared memory does not provide a synchronization mechanism, and there is no automatic mechanism to prevent the other two processes from starting to read it until one process ends writing to it. Therefore, we usually need to use other mechanisms to synchronize access to shared memory.
5. Semaphore
A semaphore is essentially a counter that identifies the amount of resources available, and its value is always a non-negative integer. Semaphores with values of 0 and 1 are called binary semaphores (or binary semaphores) and can be used to identify whether a resource is available.
6. Sockets
Sockets are a more basic mechanism for interprocess communication, and unlike other ways, sockets can be used for interprocess communication between different machines.
There are two types of sockets: file-based and network-oriented.
(1) Unix sockets are file-based and have a "family name"-AF_UNIX, which represents the address family (address family): UNIX.
(2) the second type of socket is web-based, and it also has its own family name-AF_INET, which represents the address family (address family): INTERNET
Regardless of the address family, there are two different types of socket connections: connection-oriented and connectionless.
(1) connection-oriented sockets (SOCK_STREAM)
A connection must be established before communication, and connection-oriented communication provides serialized, reliable, and non-repetitive data delivery without recording boundaries.
This means that each piece of information can be split into multiple pieces, and each fragment ensures that it reaches the destination, and then splices the information at the destination.
The main protocol that implements this type of connection is the Transmission Control Protocol (TCP).
(2) connectionless socket (SOCK_DGRAM)
There is no need to establish a connection before the communication begins, and its sequence, reliability or repeatability cannot be guaranteed in the process of data transmission.
However, the Datagram does preserve the record boundary, which means that the message is sent as a whole, rather than divided into multiple fragments first.
Because of the guarantees provided by connection-oriented sockets, their setup and maintenance of virtual circuit connections require a lot of overhead. However, the Datagram does not need these expenses, that is, it is more "cheap".
The main protocol that implements this type of connection is the user Datagram Protocol (UDP).
Application scenarios:
Data transfer: one process needs to send its data to another process, the amount of data sent is between one byte and several megabytes.
Shared data: multiple processes want to manipulate shared data, and changes made by one process to shared data should be seen by other processes immediately.
Notification event: one process needs to send a message to another or group of processes informing it (they) that something has happened (such as notifying the parent process when the process terminates).
Resource sharing: sharing the same resources among multiple processes. To do this, the kernel is required to provide locking and synchronization mechanisms.
Process control: some processes want to have complete control over the execution of another process (such as the Debug process). At this time, the control process wants to be able to intercept all traps and exceptions of another process, and be able to know its state change in time.
This is the answer to the question about several ways to achieve inter-process communication in linux. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.