Process is the concept of the operating system. Whenever we execute a program, a process is created for the operating system. In this process, resources are allocated and released. A process can be thought of as an execution of a program.
Process user spaces are independent of each other and generally cannot access each other. But in many cases, processes need to communicate with each other to complete a certain function of the system. Processes coordinate their behavior by communicating with the kernel and other processes.
Data transfer: One process needs to send its data to another process, and the amount of data sent ranges from one byte to several megabytes.
Shared data: Multiple processes want to operate shared data. If one process modifies the shared data, other processes should see it immediately.
Notification event: A process needs to send a message to another process or a group of processes to notify it (them) that a certain event has occurred (such as notifying the parent process when the process terminates).
Resource sharing: share the same resources between multiple processes. In order to do this, the kernel needs to provide locking and synchronization mechanisms.
Process control: Some processes want to completely control the execution of another process (such as Debug process). At this time, the control process hopes to intercept all traps and exceptions of another process and be able to know its status changes in time.
Pipes are divided into named pipes and unnamed pipes
The unnamed pipe is a half-duplex communication method. Data can only flow in one direction and can only be used between processes that have affinity. The affinity of a process generally refers to the parent-child relationship. Ignorance pipes are generally used for communication between two different processes. When a process creates a pipe and calls fork to create a child process of its own, the parent process closes the reading pipe end and the child process closes the writing pipe end. This provides a way for data to flow between the two processes.
The famous pipe is also a half-duplex communication method, but it allows communication between unrelated processes.
A semaphore is a counter that can be used to control multiple threads' access to shared resources. It is not used to exchange large amounts of data, but is used for synchronization between multiple threads. It is often used as a lock mechanism to prevent a process from When accessing a resource, other processes also access the resource. Therefore, it is mainly used as a means of synchronization between processes and between different threads within the same process.
Linux provides a set of well-designed semaphore interfaces to operate signals. They are not just for binary semaphores. These functions will be introduced below, but please note that these functions are used to operate group signals. Operates on quantitative values. They are declared in the header file sys/sem.h.
semget function
Its function is to create a new semaphore or obtain 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
Signal is a relatively complex communication method used to notify the receiving process that an event has occurred.
메시지 큐는 커널에 저장되고 메시지 큐 식별자로 식별되는 메시지의 연결된 목록입니다. 메시지 큐는 신호 전송 정보가 적다는 특성을 극복하고 파이프는 형식화되지 않은 바이트 스트림만 전달할 수 있으며 버퍼 크기가 제한됩니다. 메시지 대기열은 UNIX에서 서로 다른 프로세스 간에 리소스를 공유하기 위한 메커니즘입니다. UNIX에서는 메시지 대기열에 대한 작업 권한이 있는 프로세스가 형식이 지정된 데이터 스트림을 모든 프로세스에 보낼 수 있도록 msget을 사용하여 메시지 대기열을 완성할 수 있습니다. 운영 제어. 메시지 유형을 사용하면 프로세스가 어떤 순서로든 정보를 읽거나 메시지의 우선 순위를 지정할 수 있습니다.
소켓의 특성은 도메인, 유형, 프로토콜의 3가지 속성에 의해 결정됩니다.
서로 다른 프로세스 간 통신에 사용할 수 있습니다
The above is the detailed content of 6 ways to communicate between Linux processes. For more information, please follow other related articles on the PHP Chinese website!