进程通信(The process of communication)Interprocess communication has the following objective:
1. data transmission: a process to send data it to another process, the amount of data sent in a byte to a few megabytes.
2.: multiple processes to share data sharing data, a process of sharing data changes, other processes should immediately see
Interprocess communication has the following objective:
3. data transmission: a process to send data it to another process, the amount of data sent in a byte to a few megabytes.
4.: multiple processes to share data sharing data, a process of sharing data changes, other processes should immediately see.
5. notification events: a process needs to be sent to another process or a set of message, notice it (they) the occurrence of certain events (such as the process of termination notice to the parent process).
6.: resource sharing the same resource sharing between multiple processes. In order to do this, need to provide kernel lock and synchronization mechanism.
7. process control: some process to fully control the execution of another process (such as the Debug process, the control process) to intercept all into another process and abnormal, and can timely know its state changes.
UNIX interprocess communication (IPC) including pipeline, FIFO, signal.
The use of more communication in Linux process are mainly the following.
(1) pipeline (Pipe) and (named pipe): the famous pipe pipe can be used for communication with relationship between processes,
In addition to a named pipe, pipe has the function, it also allows communication between processes unrelated.
(2) signal (Signal) signal is a simulation of the interrupt mechanism in the software level, it is more complex communication
The way, for an event notification of acceptance process, a process of receiving a signal processor receives an interrupt request
The effect can be said to be the same.
delete in
(3) message queue: a message queue is a linked list of messages, including Posix message queue systemV message queue. it
To overcome the disadvantages of the information before the two communication modes in limited quantities, the process can have write permissions to the message queue in accordance with certain
The rules add a new message; have read access to the message queuing process can read messages from the message queue.
(4) shared memory: it can be said that this is the most useful interprocess communication. It allows multiple processes to access the same block
The memory space, different processes can be seen on the timely updating of data in shared memory in each process. Need this communication mode
Depending on a synchronization mechanism, such as mutexes and semaphores.
(5): the main signal as the synchronization between processes and means between different threads of the same process.
(6) the socket (Socket): This is a more general inter process communication mechanism, it can be used between different machines
Interprocess communication is widely used.
pipeline communication
The ordinary Linux shell permit and redirect redirection, is the use of the pipeline. For example:
PS grep vsftpd |
The pipeline is one-way, FIFO, no structure, fixed size byte
stream,
It is the standard input and standard output of a process and a process to connect together. At the end of the write process in the pipeline to write data and read data read process in the head end of the pipe. The data read from the pipe will be removed, other processes can be read to read these data. The pipeline provides a simple flow control mechanism. The process of trying to read in the empty pipe, write data pipeline, the process will have been blocked. Similarly, pipeline is full, then the process
of trying to write the pipe shift from the pipeline in other process data before the write process will be blocked.
The pipeline is mainly used for different interprocess communication.
Create a simple pipeline, can use the system call pipe (). It takes one parameter, which is a two integer array. If the system succeeds, this array will include the use of the two pipeline file descriptor. After creating a pipeline, general process will produce a new process.
The system call pipe ();
Prototype: int pipe (int fd[2]);
Return value: if the system succeeds, the return of 0. If the system fails to return - 1:
Errno = EMFILE (free file descriptor)
EMFILE (system filetable full)
EFAULT (FD array invalid)
Note: fd[0] is used to read the pipeline, fd[1] pipeline for writing.
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
Int (main)
{
Int pipe_fd[2];
If (pipe (pipe_fd) 0)
{
Printf ("pipe create error");
Return -1;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。