Linux inter-process communication pipeline (pipe), named pipeline (FIFO) and Signal (Signal)

Source: Internet
Author: User
Tags bit set

Organized from Network

Unix IPC includes: pipelines, named pipelines (FIFO), and signals (Signal)

 

Pipeline (pipe)

Pipelines can be used for communications between kinship-related processes. Famous pipelines overcome the restriction that pipelines do not have a name. Therefore, in addition to the functions of pipelines, it also allows communication between unrelated processes;

Implementation Mechanism:

The pipeline is a buffer zone managed by the kernel, which is equivalent to a piece of paper we put into the memory. One end of the MPs queue connects to the output of a process. This process will put information into the pipeline. The other end of the MPs queue connects to the input of a process. This process extracts the information that is put into the MPs queue. A buffer zone does not need to be large.Designed as a circular Data StructureSo that pipelines can be recycled. If there is no information in the pipeline, the process read from the pipeline will wait until the process at the other end is put in the information. When the pipeline is filled with information, the process that tries to put the information will wait until the process at the other end retrieves the information. When both processes are terminated, the MPs queue automatically disappears.

In principle, the pipeline is established using the fork mechanism so that two processes can connect to the same PIPE. At the beginning, the two arrows above are connected to the same Process 1 (the two arrows connected to Process 1 ). When fork copies the Process, the two connections are also copied to the new Process (Process 2 ). Then, each Process closes a connection that is not needed (two black arrows are closed; Process 1 closes the input connection from PIPE, and Process 2 closes the connection from output to PIPE ), in this way, the remaining red connections constitute PIPE, for example.

Implementation Details:

In Linux, the implementation of pipelines does not use a special data structure, but uses the file structure of the file system and the inode of the vfs index node. By directing the two file structures to the same temporary VFS index node, the VFS index node points to a physical page. For example

There are two file data structures, but they define different file operation routine addresses, one of which is the routine address for writing data to the pipeline, the other is the routine address for reading data from the pipeline. In this way, the system calls of a user program are still common file operations, but the kernel uses this abstract mechanism to implement the special operations of pipelines.

 

MPs queue read/write

The source code of pipeline implementation is in fs/pipe. c, in pipe. there are many functions in c, two of which are important, namely the pipeline READ function pipe_read () and pipeline write function pipe_wrtie (). The pipeline write function writes data by copying bytes to the physical memory pointed to by the VFS index node, while the pipeline READ function reads data by copying bytes in the physical memory. Of course, the kernel must use a certain mechanism to synchronize access to the pipeline. Therefore, the kernel uses locks, waiting queues, and signals.

When a write process writes data to a pipeline, it uses the standard library function write (). The system can find the file structure of the file based on the file descriptor passed by the library function. The file structure specifies the address of the function used for write operations (that is, the write function). Therefore, the kernel calls this function to complete write operations. Before writing data to the memory, The Write function must first check the information in the VFS index node and perform actual memory replication only when the following conditions are met:

· The memory has enough space to accommodate all the data to be written;

· The memory is not locked by the read program.

If both of the preceding conditions are met, the write function first locks the memory and then copies the data from the address space of the write process to the memory. Otherwise, the write process will sleep in the waiting queue of the VFS index node. Next, the kernel will call the scheduler, And the scheduler will choose other processes to run. The write process is in an interrupted wait state. When the memory has enough space to accommodate the written data or the memory is unlocked, the read process will wake up the write process, the write process receives the signal. When data is written to the memory, the memory is unlocked, and all reading processes that sleep on the index node are awakened.

The pipeline read process is similar to the write process. However, a process can return an error message immediately when there is no data or the memory is locked, rather than blocking the process, depending on the open mode of the file or pipeline. On the other hand, the process can sleep in the waiting queue of the index node and wait for the write process to write data. After all the processes complete the pipeline operation, the indexing node of the pipeline is discarded, and the shared data page is also released.

Linux function prototype

#include <unistd.h>int pipe(int filedes[2]);

Filedes [0] is used to read data. When reading data, you must close the writing end, that is, close (filedes [1]);

Filedes [1] is used to write data. When writing data, you must close the reader, that is, close (filedes [0]).

Program instance:

Int main (void) {int n; int fd [2]; pid_t pid; char line [MAXLINE]; if (pipe (fd) 0) {/* Create an MPS queue to obtain a file descriptor */exit (0);} if (pid = fork () 0) /* the parent process copies the file descriptor to the sub-process */exit (1); else if (pid> 0) {/* parent process write */close (fd [0]);/* close read descriptor */write (fd [1], "\ nhello world \ n ", 14);} else {/* sub-process read */close (fd [1]);/* close the write end */n = read (fd [0], line, MAXLINE); write (STDOUT_FILENO, line, n);} exit (0 );}

 

Named PIPE (named PIPE)

Based on the fork mechanism, pipelines can only be used between parent and child processes, or between two child processes with the same ancestor (with a kinship between processes ). To solve this problem, Linux provides a FIFO method for connecting processes. FIFO is also called a named PIPE (named PIPE ).

FIFO (First in, First out) is a special file type, which has a corresponding path in the file system. When a process opens the file in read (r) mode and another process opens the file in write (w) mode, the kernel will establish a pipeline between the two processes, therefore, FIFO is actually managed by the kernel and does not work with hard disks. The reason is FIFO because the pipeline is essentially a first-in-first-out queue data structure, and the first data to be put is read first to ensure the order of information exchange. FIFO only borrows a file system (named pipe is a special type of file, because everything in Linux is a file and it exists as a file name in the file system .) To name the MPs queue. Write mode processes write data to FIFO files, while read mode processes read data from FIFO files. When a FIFO file is deleted, the Pipeline Connection disappears. The advantage of FIFO is that we can identify pipelines through file paths, so as to establish connections between unrelated processes.

Function prototype:

#include <sys/types.h>#include <sys/stat.h>int mkfifo(const char *filename, mode_t mode);int mknode(const char *filename, mode_t mode | S_IFIFO, (dev_t) 0 );

Pathname is the name of the created file. mode indicates the permission bit set on the file and the file type to be created (S_IFIFO in this case ), dev is a value used to create a special file for a device. Therefore, the value of the first-in-first-out file is 0.

Program instance:

#include <stdio.h>  #include <stdlib.h>  #include <sys/types.h>  #include <sys/stat.h>        int main()  {      int res = mkfifo("/tmp/my_fifo", 0777);      if (res == 0)      {          printf("FIFO created/n");      }       exit(EXIT_SUCCESS);  }  

Compile this program:

Gcc-o mirror o1.c fifo

Run this program:

$./Ikeo1

Run the ls command to view the created MPs queue.

$ Ls-lF/tmp/my_fifo

Prwxr-xr-x 1 root 0 05-08 20:10/tmp/my_fifo |

Note: The first character in the output result of the ls command is p, indicating that this is a pipeline. Last | the symbol is added by the-F option of the ls command. It also indicates that it is a pipeline.

FIFO read/write rules

1. read data from FIFO: Convention: if a process blocks access to FIFO to read data from FIFO, read operations in the process are read operations with a blocking flag

2. writing data from a FIFO: Convention: if a process blocks the opening of a FIFO to write data to a FIFO, the write operation in the process is a write operation with a blocking flag.

See: http://blog.csdn.net/MONKEY_D_MENG/article/details/5570468

 

 

Signal (Signal)

Signals are complex communication methods used to notify the receiving process of an event. In addition to inter-process communication, processes can also send signals to the process itself. In addition to supporting early Unix signals, Linux
In addition to the semantic function sigal, it also supports the signal function sigaction that complies with the Posix.1 standard (in fact, this function is based on BSD, BSD in order to achieve reliable signal mechanism, and
Can unify external interfaces and implement the signal function again using the sigaction function)

Signal type

Each signal type has a corresponding signal processing program (also called a signal operation), as if every interrupt has an interrupt service routine. The default operation of most signals is to end the process of receiving signals. However, a process can usually request the system to take some replacement operations. The replacement operations are as follows:

  • Ignore the signal. With the setting of this option, the process will ignore the appearance of the signal. There are two signals that cannot be ignored: SIGKILL, which will end the process; SIGSTOP, which is part of the job control mechanism and will suspend job execution.
  • Default recovery signal operation.
  • Execute a preset signal processing function. A process can register special signal processing functions. When a process receives a signal, the signal processing function is called like an interrupt service routine. When the signal processing function returns, the control is returned to the main program and continues to run normally.

However, the signal is different from the interrupt. The interrupt response and processing occur in the kernel space, while the signal response occurs in the kernel space, while the execution of the signal processing program occurs in the user space.

So when will the detection and response signal be? This usually happens in two situations:

  • After the current process enters the kernel space due to system calls, interruptions, or exceptions, it is returned from the kernel space to the eve of the user space;
  • When the current process is awakened after it enters sleep in the kernel, it returns to the user space in advance due to the existence of the detected signal.

For more information about function prototype, see: http://www.cnblogs.com/biyeymyhjob/archive/2012/08/04/2622265.html.

Signal nature

A signal is a simulation of the interrupt mechanism at the software level. In principle, a process receives a signal and the processor receives an interrupt request. The signal is asynchronous. A process does not have to wait for the signal to arrive through any operation. In fact, the process does not know when the signal will arrive.

Signals are the only asynchronous communication mechanism in the inter-process communication mechanism. They can be seen as asynchronous notifications to notify the processes that receive signals of what happened. After POSIX real-time expansion, the signal mechanism is more powerful. In addition to the basic notification function, it can also transmit additional information.

Signal Source

There are two sources for the occurrence of signal events: hardware sources (for example, we press the keyboard or other hardware faults); software sources, the most common system function for sending signals is kill, raise, alarm, setimer, and sigqueue functions. The software source also includes some illegal operations.

 

Principles of Signal Processing (kernel)

The kernel sends Soft Interrupt signals to a process by setting the bit corresponding to the signal field in the process's progress table. It should be added that if the signal is sent to a sleeping process, it depends on the priority of the process going to sleep. If the process goes to sleep, otherwise, only the corresponding bit of the signal domain in the process is set, rather than the process. This is important because the time when the process checks whether a signal is received is: when a process is about to return from the kernel state to the user State; or, when a process needs to enter or exit a proper low-scheduling priority sleep state.

The time when the kernel processes the signal received by a process is when a process returns the user State from the kernel state. Therefore, when a process runs in the kernel state, the soft interrupt signal does not take effect immediately and will not be processed until it returns to the user State. The process will return the user State only after processing the signal (in the above example program, in step 5, after blocking is removed, print caught SIGQUIT and then print SIGQUIT unblocked, that is, before the sigprocmask is returned, the signal processing program executes). In the user mode, the process does not have any signal that has not been processed.

The kernel processes the Soft Interrupt signal received by a process in the context of the process. Therefore, the process must be in the running state. If a process receives a signal to capture, the process executes user-defined functions when returning the user State from the kernel state. In addition, the method for Executing User-defined functions is clever. The kernel creates a new layer on the user stack, which sets the return address value to the address of the User-Defined processing function, in this way, the process returns to the user-defined function from the kernel to the top of the pop-up stack. Only when the function returns and the top of the stack pops up will the process return the original entry to the kernel and continue running. The reason for this is that user-defined processing functions cannot and cannot be executed in the kernel state (if user-defined functions are run in the kernel state, the user can obtain any permissions ).

Pay special attention to the signal processing methods.

First, in some systems, when a process processes the interrupt signal and returns the user State, the kernel clears the address of the processing routine for the signal set in the user area, that is, the next process changes the processing method for this signal to the default value, unless it is called again by the signal system before the next signal arrives. This may cause the process to exit after the signal is obtained before calling signal. In BSD, the kernel does not clear this address. However, if this address is not cleared, the process may obtain a signal too quickly, resulting in stack overflow. In order to avoid the above situation. In the BSD system, the kernel simulates the hardware interrupt processing method, that is, when processing an interrupt, it prevents receiving new such interruptions.

Note that if the signal to be captured occurs when the process is being called by a system, and the process is sleep at the priority of interruption (if the system call is not sleep but is running, according to the analysis above, wait until the system call is completed before processing the signal ), at this time, the signal causes the process to perform longjmp, jump out of the sleep state, return to the user State, and execute the signal processing routine. When the process is returned from the signal processing routine, it is like returning from the system call, but an error such as-1 is returned, and errno is set to EINTR, indicates that the system call was interrupted. Note that the inner core of the BSD system can automatically restart the system call, or manually restart the system as described above.

Third, note that if a process is sleep at an interrupted priority, the process is awakened when it receives a signal to be ignored, but longjmp is not performed, generally, it is to continue sleep. However, the user does not feel that the process has been awakened, but does not have this signal. Therefore, the signal returned by functions such as pause and sleep from the suspended state must have a signal processing function. If there is no action, you can leave the processing function blank.

The fourth thing to note: the processing method of the signal of the core sub-process termination (SIGCLD) is different from that of other signals. When a process ends normally or abnormally, the kernel sends a SIGCLD signal to its parent process. By default, the parent process ignores the signal, just as if it has not received the signal, if the parent process wants to obtain the termination status of the child process, use the signal function to set the confidence number processing program for the SIGCLD signal and call wait in the signal processing program.

The SIGCLD signal is used to wake up a sleep process that can be interrupted. If the process captures this signal, it will be transferred to the processing routine like normal signal processing. If the process ignores this signal, nothing will be done. In fact, wait is not necessarily placed in the signal processing function. However, wait will suspend the parent process for sleep before the child process is terminated because you do not know when the child process is terminated.

Signal Lifecycle

References:

Http://www.cnblogs.com/vamei/archive/2012/10/10/2715398.html

Http://bbs.chinaunix.net/thread-1947211-1-1.html

 

 

 

Related Article

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.