In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the example analysis of process communication in Linux. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Each process has a different user address space, and the global variables of any process cannot be seen in the other, so to exchange data between processes must go through the kernel, opening up a buffer in the kernel. Process 1 copies the data from user space to the kernel buffer, and process 2 reads the data from the kernel buffer. This mechanism provided by the kernel is called IPC,InterProcess Communication.
The way the process communicates
1. Pipeline
Pipes are divided into famous pipes and unnamed pipes.
Unnamed pipe is a half-duplex communication mode, in which data can only flow in one direction and can only be used between related processes. The kinship of process generally refers to the relationship between father and son. Unknown pipes are generally used for communication between two different processes. When a process creates a pipe and calls fork to create its own child process, the parent process closes the read pipe end and the child process closes the write pipe end, which provides a way to flow data between the two processes.
A named pipeline is also a half-duplex mode of communication, but it allows communication between unrelated processes.
two。 Semaphore
A semaphore is a counter that can be used to control the access of multiple threads to shared resources. it is not used to exchange large amounts of data, but for synchronization between multiple threads. It is often used as a locking mechanism to prevent a process from accessing a resource when other processes access it. Therefore, it is mainly used as a means of synchronization between processes and between different threads in the same process.
Linux provides a set of well-designed semaphore interfaces to manipulate signals, not just for binary semaphores. These functions are described below, but note that these functions are used to manipulate groups of semaphore values. They are declared in the header file sys/sem.h.
Semget function
Its function is to create a new semaphore or get an existing semaphore
Semop function
Its function is to change the value of the semaphore.
Semctl function
This function is used to directly control semaphore information.
3. Signal
Signal is a complex way of communication, which is used to notify the receiving process that an event has occurred.
4. Message queue
Message queues are linked lists of messages, stored in the kernel and identified by message queue identifiers. Message queue overcomes the characteristics of less signal transmission information, pipeline can only carry unformatted byte flow and limited buffer size. Message queuing is a mechanism for sharing resources among different processes in UNIX. UNIX allows different processes to send formatted data streams to any process in the form of message queues. Processes with permission to operate on the message queue can use msget to control the operation of the message queue. By using message types, processes can read information in any order or prioritize messages.
5. Shared memory
Shared memory is to map a piece of memory that can be accessed by other processes, which is created by one process, but can be accessed by multiple processes. Shared memory is the fastest IPC (inter-process communication) mode, which is specially designed for the low efficiency of other inter-process communication modes. It is often used with other communication mechanisms, such as semaphores, to achieve synchronization and communication between processes.
6. Socket
Socket, or socket, is a communication mechanism. With this mechanism, the development of the client / server (that is, the process to communicate) can be carried out either on the local stand-alone machine or across the network. In other words, it allows processes that are not on the same computer but are connected to the computer through the network to communicate. Because of this, sockets clearly distinguish between the client and the server.
The characteristics of sockets are determined by three properties: domain, type, and protocol.
This is the end of this article on "sample analysis of process communication in Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
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.