Inter-process communication IPC

Source: Internet
Author: User
Tags message queue semaphore
A large application system often requires many processes to collaborate, and the importance of interprocess communication is obvious.

First, interprocess communication can be implemented at least by transferring open files, and different processes pass information through one or more files, in fact, this approach is used in many application systems. But in general, interprocess communication (ipc:interprocess communication) does not include this seemingly low-level communication method. There are many ways to implement interprocess communication in a UNIX system, and unfortunately, there are few ways to migrate across all UNIX systems (the only Half-duplex pipeline is the most primitive form of communication). Linux, as a new operating system, supports almost all common interprocess communication methods under UNIX: Pipelines, signals, message queues, shared memory, semaphores, socket interfaces, and so on.
Piping: 1. The pipeline communication is one-way, has the fixed read end and the writing end. 2. When the data is read from the pipeline by the process, the data does not exist in the pipeline. 3. When the process goes to read the empty pipe, the process blocks. 4. When a process writes data to a full pipe, the process blocks. 5. Pipeline capacity for the 64KB pipeline is commonly used in two areas: (1) Pipelines are often used in the shell (as a redirection of input input), in which the creation of the pipeline is transparent to the user, (2) for relational interprocess communication, the user creates the pipe himself, and completes the read and write operation.Nameless Pipe:When a pipe is established, two file descriptors fd[0], fd[1, are created. Where fd[0] is fixed for the read pipe, and fd[1] is fixed for the write pipe. Fd[0] fd[1] As a file, general file I/O functions can be used in pipelines, such as close, read, write, and so on. It does not belong to some kind of file system, but rather, it makes a single file system, and only exists in memory. Can only be used for communication between relational processes. Half-duplex communication mode with fixed read-end and write-end.                        Read End fixed: 1, write end exists: If no input data, blocking, if there is input data input how much output 2, write end does not exist: if the tube has data how much to read, if there is no data in the pipeline immediately returned 0 write-end fixed: 1, read-side existence: If the pipeline space is large enough, you can always write to the pipe content. If there is not enough pipe space, you can only enter some of the previous characters that meet the size of the pipe space, and the rest is lost
Write-End fixed: 2, read-side does not exist: the system defaults to send signal sigpipe end the current process. The process end pipeline will not exist. When writing data to a pipe, at least one process should exist in which the pipe read end is not closed, or a pipe break occurs, the process receives a sigpipe signal, and the default action is the process terminates.
A well-known pipe: a well-known pipe can allow two of unrelated processes to communicate with each other, and can be indicated by path names and visible in the file system. The famous pipeline follows the advanced first out rule. The process operates a well-known pipeline through file IO. General file I/O functions can be used in FIFO, such as close, read, write, and so on. The FIFO space is not enough to write process blocking, there is no data in FIFO, read process is blocked.
Running either the read-side or the write-end will block, and which process will create the pipeline in which process to run first.
The reading of pipelines and FIFO always returns data from the beginning, and writing to them adds the data to the end. They do not support file positioning operations such as Lseek ().
Shared Memory:Shared memory is one of the most efficient ways of interprocess communication in which processes can read and write directly to memory without having to copy any data. The kernel leaves a single memory area, which can be mapped to its own private address space by the process that needs to be accessed because multiple processes share a memory, you also need to rely on some sort of synchronization mechanism, such as mutexes and semaphores. read out the shared memory after reading the value is also written in shared memory after the original value is overwritten, with fgets write will automatically add ' "after the string. So the next time the output does not output the characters after ' the '.
Message Queuing

Message Queuing is a linked list of messages. You can think of a message as a record, with a specific format and a specific priority.

The sender and receiver of the message do not need to cross with the message queue at the same time. The message is saved in the queue until the recipient retrieves it. Message queues can perform read or write operations individually, and the values in message queues are cleared after they are read out.

Message Queuing allows the receiver to retrieve messages long after the message is sent, but this feature of Message Queuing also creates the disadvantage that the receiver must poll the message queue to receive the most recent message.

Message Queuing overcomes the lack of information load, the pipeline can only host unformatted byte streams and buffer size limitations. However, Message Queuing still has a size limit.

Summary: Message Queuing has more flexibility than pipelines and well-known pipelines, first, it provides formatted (char) byte streams to reduce the developer's workload, and secondly, messages have types that can be used as a priority in real-world applications. These two points are not comparable to pipes and famous pipes. Similarly, Message Queuing can be reused among several processes, regardless of whether the processes are related or not, which is similar to a well-known pipe, but Message Queuing is continuous with the kernel and is more resilient and more space-capable than a known pipeline (which continues with the process).
Message Queuing is a linked list of messages. The different type types add messages to the end of the message queue that is open, at the end of the list. The same type will store the message in a FIFO mechanism under this type. Message queues can perform read or write operations individually, and the values in message queues are cleared after they are read out.


Signal:The signal is a simulation of the interrupt mechanism at the software level, and it is a kind of communication mode of asynchronous (uncertain time of event). The signal can interact directly between the user space process and the kernel process. If a signal is set to block by a process, the signal is passed to the process when it is delayed knowing that its blocking is canceled. SIGKILL Sigstop cannot be ignored 2 SIGINT Ctry + C sends int signal (SIGINT), which causes the process to terminate by default. 3 Sigquit ctry + send quit signal (sigquit); The default causes the process to terminate and dumps in-memory information to the hard disk (the core dump).            9 SIGKILL The signal is used to immediately end the program's operation and cannot be blocked or ignored. SIGUSR1 custom Signal. The signal () function is used to determine the function of a signal SIGUSR2 a custom signal. The process of writing data to a pipe receives the sigpipe signal from the kernel when the sigpipe pipe read end does not exist. SIGALRM alarm to the process to set a timer, timed to the kernel to send a sigalarm signal to the process, the default to terminate this process sigterm use kill when there is no signal by default is (Sigterm) terminate a process kill process Number SIGCHLD The parent process will receive this signal when the child process changes state. Sigcont the process of a T (paused) state to run (run in the background).            SIGSTOP This signal is used to suspend a process and cannot be blocked or ignored. The SIGTSTP ctry + Z sends the SIGTSTP signal, which causes the process to hang by default.

Signal Light:Also called semaphore, it is the mechanism of synchronization and mutual exclusion between different threads within a given process or process. Binary signal Light: The value is 0 or 1, the resource is available at 1, and the unavailable value is 0. Count Semaphore: The value is between 0 and N, which is used to count resources, whose values represent the number of available resources waiting for the operation to wait for the semaphore to become greater than 0, and then subtract 1, and the release action, instead, is used to wake the process or thread that is waiting for the resource.


inter-Process communication method comparison:Pipe: A relationship between processes, a single worker, and data in memory. (pipe) FIFO: Available for any process, duplex, file name, data in memory.                        (Named pipes) signal: The only way to communicate asynchronously.   (signal) SHM: Highest efficiency, direct access to memory, need synchronization, mutual exclusion mechanism.   (Shared memory) msg: Commonly used in CS mode, accessed by message type, can have priority.         (Message Queuing) SEM: Used in conjunction with shared memory to achieve synchronization and mutual exclusion. Lights


Pipelines are typically used for data transmission, signals for event notifications, and semaphores for resource sharing 1. Follow the process: The IPC persists until the last process on which the IPC object is opened closes the object. such as pipelines and well-known pipelines; 2. With the kernel persistent: the IPC persists until the kernel is rebooted or the object is deleted. such as message queues, traffic lights, and shared memory; 3. As the file system continues: The IPC persists until the object is displayed for deletion.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.