Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is Linux process communication

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly talks about "what is Linux process communication". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is Linux process communication"?

The purpose of interprocess communication

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.

Process communication mode

Several main ways of inter-process communication under linux:

(1) pipe and FIFO

(2) signal (signal)

(3) message queue

(4) shared memory (shared memory)

(5) semaphore (semaphore)

(6) socket (socket)

Pipeline

Pipe and named pipe: pipes can be used for communication between consanguineous processes. Named pipelines overcome the limitation that pipes do not have names. Therefore, in addition to the functions of pipes, they also allow communication between unrelated processes.

A pipeline is an one-way, first-in, first-out, unstructured, fixed-size byte stream that connects the standard output of one process to the standard input of another process. The write process writes data at the end of the pipe, and the read process reads the data at the first end of the pipe. After the data is read out, it will be removed from the pipeline and can no longer be read by other reading processes. The pipeline provides a simple flow control mechanism. When a process attempts to read an empty pipe, the process will block until data is written to the pipe. Similarly, when the pipe is full, the process tries to write the pipe again, and the write process will block until other processes remove data from the pipe. There is usually a limitation: one is half-duplex, which can only be transmitted in one way; the other is that it can only be used between parent and child processes.

Named pipeline (also known as FIFO, because the pipeline works on a first-in-first-out basis, and the first data written to the pipe is also the first to be read). Unlike pipes, FIFO are not temporary objects; they are real entities in the file system and can be created with the mkfifo command. Processes can use FIFO as long as they have the appropriate access rights. The way FIFO opens is slightly different from that of pipes. A pipe (its two file data structures, VFS I nodes, and shared data pages) is created at once, while FIFO already exists and can be opened and closed by its users. The Linux must handle the opening of the FIFO by the read process before the write process opens it, and the read of the pipe by the read process before the write process writes the data. Apart from that, FIFO handles almost exactly the same as pipes, and they use the same data structures and operations.

Signal

Signal (signal): a signal is a complex mode of communication that notifies the receiving process that an event has occurred. In addition to being used for inter-process communication, the process can also send signals to the process itself. Linux not only supports the early signal semantic function sigal of Unix, but also supports the signal function sigaction which semantically conforms to the Posix.1 standard (in fact, this function is based on BSD. In order to realize the reliable signal mechanism and unify the external interface, BSD uses the sigaction function to re-implement the signal function.

Signal is a kind of simulation of interrupt mechanism at the software level, and it is a way of asynchronous communication.

The signal can directly interact between the user space process and the kernel process, and the kernel process can also use it to inform the user space process of what system events have occurred. It can be sent to a process at any time without knowing the status of the process.

If the process is not currently in the execution state, the signal is saved by the kernel until the process resumes execution and is passed to it; if a signal is set to block by the process, the transmission of the signal is delayed. it is not passed to the process until its blocking is cancelled.

The way the process executes the signal:

Ignore the signal, that is, the signal does not do any processing, among them, there are two signals can not be ignored: SIGKILL and SIGSTOP.

Capture the signal, define the signal processing function, and execute the corresponding processing function when the signal occurs.

The default operation is performed, and Linux specifies the default action for each signal.

Message queue

Message queuing: message queuing is a linked list of messages, including Posix message queuing System V message queuing. Processes with sufficient permissions can add messages to the queue, and processes that are given read permission can read away messages from the queue. Message queue overcomes the shortcomings that the signal carries less information, the pipeline can only carry unformatted byte flow and the buffer size is limited.

The implementation of message queuing includes creating or opening message queues, adding messages, reading messages, and controlling message queues.

The function used to create or open message queues is msgget, and the number of message queues created here is limited by the number of message queues in the system.

The function used to add a message is the msgsnd function, which adds the message to the end of the open message queue.

The function used to read the message is msgrcv, which takes the message from the message queue, unlike FIFO, where you can specify a message to be fetched.

The function used to control message queuing is msgctl, which can perform several functions.

Semaphore / semaphore

Semaphores (semaphore): mainly used as a means of synchronization between processes and between different threads of the same process. Semaphore is an inter-process communication mechanism used to solve the problem of synchronization and mutual exclusion between processes, including a variable called semaphore and a process waiting queue waiting for resources under the semaphore, as well as two atomic operations (PV operations) on semaphores. The semaphore corresponds to a certain resource and takes a non-negative integer value. The signal value refers to the number of resources currently available, and a value equal to 0 means that there are no resources currently available.

P operation: if a resource is available (signal value > 0), a resource is occupied (subtract one from the signal value and enter the critical area code). If no resource is available (the semaphore value is equal to 0), it is blocked until the system allocates the resource to the process (enters the waiting queue until it is the process's turn).

V operation: if there is a process waiting for resources in the waiting queue of the semaphore, wake up a blocking process. If there is no process waiting for it, release a resource (add one to the semaphore value).

Shared memory

Shared memory (shared memory) is arguably the most useful form of interprocess communication and the fastest form of IPC. The shared memory of two different processes An and B means that the same piece of physical memory is mapped to the respective process address space of processes An and B. Process A can instantly see process B updating data in shared memory, and vice versa. Because multiple processes share the same memory area, it is necessary to have some kind of synchronization mechanism, both mutexes and semaphores.

One of the obvious benefits of using shared memory communication is efficiency, because processes can read and write memory directly without requiring any copies of data. For communication methods such as pipes and message queues, four copies of the data are required in the kernel and user space, while shared memory copies the data only twice: once from the input file to the shared memory area and once from the shared memory area to the output file. In fact, when sharing memory between processes, it is not always unmapped after reading and writing a small amount of data, and the shared memory area is re-established when there is new communication. Instead, the shared area is maintained until the communication is complete, so that the data content is saved in shared memory and is not written back to the file. The contents of shared memory are often written back to the file when the mapping is unmapped. Therefore, the communication mode of shared memory is very efficient.

Steps for shared memory implementation:

1. To create shared memory, the function used here is shmget, that is, to get a shared memory area from memory.

two。 Map shared memory, that is, map the created shared memory to a specific process space. The function used here is shmat.

3. Use the unbuffered Istroke O read and write command to operate on it.

4. Undo the operation of the mapping, whose function is shmdt.

Socket interface

Socket (socket): a more general mechanism for inter-process communication that can be used for inter-process communication between different machines. Originally developed by the BSD branch of the Unix system, it is now generally portable to other types of Unix systems: both Linux and System V variants support sockets.

At this point, I believe you have a deeper understanding of "what is Linux 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report