Ways to communicate between processes

Source: Internet
Author: User
Tags message queue semaphore sigint signal signal handler stdin

Multi-process:

First, let's talk about what happened after the fork.

A new process created by Fork is called a subprocess (child process). The function is called once, but returns two times. The difference of two returnsis that the return value of the child process is 0, and the return value of the parent process is the process ID of the new process (child process). The reason for returning the child process ID to the parent process is that because there can be more than one child process for a process, there is no function that enables a process to obtain the process ID of all its child processes. For a child process, fork returns 0 to it because it can call getpid () to get its own PID at any time, or call Getppid () to get the ID of the parent process. (Process ID 0 is always used by the interchange process, so the process ID of a child process cannot be 0).

After fork, the operating system replicates a child process that is exactly the same as the parent process, although it is a parent-child relationship, but in the operating system it appears that they are more like siblings, that the 2 processes share the code space, but that the data space is independent and that the content in the child process data space is a complete copy of the parent process. The instruction pointer is also exactly the same, the child process has the parent process currently running to the location (the two process of the program counter PC value is the same, that is, the child process starts from the fork to execute ), but a little different, if the fork is successful, the return value of fork in the child process is 0 , the return value of fork in the parent process is the process number of the child process, and if the fork is unsuccessful, the parent process returns an error.
As you can imagine, 2 processes are running at the same time, and Unison, after the fork, they do different jobs, that is, bifurcation. That's why fork is called fork.

As for the first run, it may be related to the operating system (scheduling algorithm), and this problem is not important in the practical application, if the parent-child process should be coordinated, can be solved by the primitive language method.

Common means of communication:

1 pipe: Pipe is a half-duplex communication mode, the data can only flow in one direction, and can only be used between the process of affinity. A process's affinity usually refers to a parent-child process relationship.
2. Named pipe FIFO: A well-known pipe is also a half-duplex mode of communication, but it allows communication between unrelated processes.
4. Message Queuing MessageQueue: Message Queuing is a linked list of messages, stored in the kernel and identified by message queue identifiers. Message Queuing overcomes the disadvantages of less signal transmission information, only unformatted byte stream, and limited buffer size.
5. Shared storage sharedmemory: Shared memory is the mapping of memory that can be accessed by other processes, which is created by a process, but can be accessed by multiple processes. Shared memory is the fastest IPC approach and is specifically designed for low-efficiency operation of other interprocess communication modes. It is often used with other communication mechanisms, such as signal two, to achieve synchronization and communication between processes.
6. Semaphore Semaphore: Semaphore is a counter that can be used to control the access of multiple processes to shared resources. It is often used as a locking mechanism to prevent a process from accessing the shared resource while other processes are accessing the resource. Therefore, it is primarily used as a means of synchronization between processes and between different threads within the same process.
7. Socket SOCKET: The socket is also an inter-process communication mechanism, which differs from other communication mechanisms in that it can be used for different processes and communication between them.
8. Signal (sinal): signal is a more complex mode of communication for notifying the receiving process that an event has occurred.

Signal:

A signal is a mechanism used in a Linux system to communicate or operate between processes, and signals can be sent to a process at any time without knowing the status of the process. If the process is not in the execution state, the signal is saved by the kernel, knowing that the process resumes execution and is passed to him. If a signal is set by the process to block, the transmission of the signal is delayed until its blocking is canceled before it is passed to the process.

Linux offers dozens of kinds of signals, each representing different meanings. The signals are differentiated by their values, but usually the name of the signal is used in the program to represent a signal. In Linux systems, these signals and constants named after their names are defined in the/usr/includebitssignum.h file. It is usually good to include <signal.h> directly in the program.

Signal is a kind of simulation of interrupt mechanism at software level, it is an asynchronous communication mode, and the signal can interact directly between the user space process and the kernel. The kernel can also use signals to inform user space of the process to inform the user of what system events have occurred in the space. There are two sources of signal events:

1) hardware sources, such as cltr+c, usually generate interrupt signals SIGINT

2) The source of the software, such as using a system call or command to send a signal. The most common system function for sending signals is the Kill,raise,setitimer,sigation,sigqueue function. The software source also includes some operations such as illegal operations.

Once a signal is generated, the user process has three different ways of generating the signal:

1) Perform the default action, and Linux specifies the default action for each signal.

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

3) Ignoring the signal, when you do not want the received signal to affect the execution of the process, and let the process continue to execute, you can ignore the signal, that is, no processing of the signal process.

There are two signals that the application process cannot capture and ignore, that is, Sigkill and segstop, so that the system administrator can interrupt or end a particular process at any time.

Represents a common command in Linux

1. Signal Sending:

The key to sending signals is that the system knows which process to send signals to and what signals to send. The following functions are commonly used in signal operations:

Example: To create a child process, in order to end the child process before the parent process signaled, the child process uses the Raise function to send the sigstop signal to suspend itself, the parent process sends a sigkill signal to the child process using the kill function of the signal operation, the child process receives this signal, and ends the child process.

2. Signal Processing

When a signal is sent to a running process, the process registers the corresponding signal handler function for the next specific signal to complete the required processing. The signal processing mode is the signal function, before the normal end of the program, the application of the signal function to restore the system to the signal

The default processing mode.

3. Signal Blocking

Sometimes it is not desirable for a process to interrupt the execution of a process when it receives a signal, and it does not want the signal to be completely ignored, but rather to delay the call to the signal processing function for a period of time, which requires a signal block to complete.

Example: The main program blocks the CLTR+C SIGINT signal. Using Sigpromask will SIGINT if the signal set is blocked.

Pipeline:

Pipelines allow the transfer of data between processes in a first-in, out-of-process manner, which is a common way of interprocess communication.

The pipeline is one of the original UNIX IPC forms supported by Linux and has the following features:

1) The pipe is half-duplex , the data can only flow in one direction, need to establish two pipelines when the two sides communicate ;

2) Anonymous pipelines can only be used between parent-child processes or sibling processes (affinity processes);

3) Separate form a separate file system: The pipeline is a file for the process at both ends of the pipe, but it is not an ordinary file, it does not belong to a file system, but is a separate file system, and only exists in memory.

Pipeline is divided into pipe (nameless pipe) and FIFO (named pipe) two, in addition to establish, open, delete the way, the two pipelines are almost the same. They all implement data transfer through the kernel buffers.

    • Pipe is used for communication between related processes, such as parent and child processes, which are created and opened through the pipe () system, and the pipe is automatically revoked when the last process that uses it closes a reference to him.
    • FIFO is named pipe, there is a corresponding node on disk, but no data block-in other words, just have a name and corresponding access rights, through the Mknode () system call or Mkfifo () function to build. Once established, any process can be opened and read and written by the file name, not limited to the parent-child process, provided that the process has proper access to the FIFO. When it is no longer used by the process, the FIFO is freed in memory, but the disk node still exists.

The essence of a pipeline is a kernel buffer, in which the process accesses data from a buffer in a first-in, out-of-order way: The process at one end of the pipeline writes the process data to the buffer sequentially, and the process in the other end reads the data sequentially, which can be seen as a circular queue, where the read and write locations are automatically incremented The buffers are no longer present when they are read out. When the buffer is read empty or full, there is a certain rule to control the corresponding read process or write process to enter the waiting queue, the empty buffer has new data to write or slow buffer has the data read out, the wake-up waiting queue process continues to read and write.

Nameless Pipe:

Example of pipe: The parent process creates the pipe and writes the data in the pipeline, and the child process reads the data from the pipe

Named pipes:

The main difference from the nameless pipe is that the named pipe has a name that corresponds to a disk index node with the name of the file, and any process has the appropriate permissions to access it.

The nameless pipes, however, are different, and the process can only access pipelines created by itself or by ancestors, without having to visit any existing pipelines-because there is no name.

Linux creates a named pipe through the system call Mknod () or Makefifo (). The simplest way is by using the shell directly

Mkfifo Myfifo

Equivalent to

Mknod Myfifo P

The above command creates a named pipe in the current directory named Myfifo. When you use the Ls-p command to view the type of a file, you can see that the named pipe has a vertical line "|" after its corresponding file name, which means that the file is not a normal file but a named pipe.

Use the open () function to open a named pipe that has already been created by using the file name, and the unnamed pipe cannot be opened by open. When a named pipe is no longer opened by any process, it does not disappear and can be opened again, just like opening a disk file.

You can delete the normal file by deleting it, the actual deletion of the corresponding node information on the disk.

Example: Using Named Pipes to implement the chat program, a Zhang San end, a Li four-terminal. Both programs establish two named pipes, Fifo1,fifo2, Zhang San write fifo1, John Doe read FIFO1, John Doe write Fifo2, Zhang San read fifo2.

With SELECT, the pipe descriptor and stdin, if set, block with SELECT, and if there is an I/O, the process is awakened. (The pink part is the Select section, the yellow part is the named pipe part)

In the Linux system, in addition to pipe system calls to establish pipelines, you can also use the C function library in the pipeline function Popen function to establish a pipeline, using Pclose to close the pipeline.

Example: Designing a program to create a pipeline with Popen to implement Ls-l |grep MAIN.C functionality

Analysis: First use the Popen function to create a read pipeline, call the Fread function to save the results of ls-l BUF variable, with the printf function output content, with Pclose to shut down the read pipeline;

The Popen function is then used to create a write pipeline that calls the fprintf function to write the contents of the BUF to the pipeline and run the grep command.

Function prototypes for Popen:

file* popen (const char* command,const char* type);

Parameter description: command is a child process to execute the commands, type represents the types of pipes, R represents the read pipeline, and W represents the write pipeline. If a pointer to the pipe file is returned successfully, NULL is returned.

Using the Popen function to read and write the pipeline, it is actually called the pipe function call to establish a pipeline, and then call the fork function to establish a child process, then create a shell environment, and in this shell environment to execute the parameters specified by the process.

Message Queuing:

Message Queuing, a linked list of messages, is a series of messages stored in the kernel. User processes can add messages to message queues, or they can read messages to Message Queuing.

Message Queuing has the advantage of specifying a specific message type for each message when it is received without having to follow the queue order, but rather to receive a specific type of message based on a custom condition.

You can think of a message as a record with a specific format and a specific priority. A process that has write access to Message Queuing can add new messages to a message queue in accordance with certain rules, and a process that has read access to Message Queuing can read messages from the message queue.

The common functions of Message Queuing are the following table:

Inter-process communication through Message Queuing, mainly: creating or opening message queues, adding messages, reading messages, and controlling Message Queuing.

Example: Create a message queue with the function Msget, call the MSGSND function, add the input string to the message queue, then call the MSGRCV function, read the message in the message queue and print the output, and finally call the Msgctl function to delete the message queue in the system kernel. (The yellow part is the key code related to Message Queuing, the pink part is the key code to read stdin)

Shared Memory:

Shared memory allows two or more processes to share a given store, which can be mapped to its own address space by two or more processes, a process that writes to the shared memory, and can be read by other processes that use this shared memory, through a simple memory read error. Thus, the communication between processes is realized.

One of the main benefits of using shared memory for communication is high efficiency, because processes can read and write directly to memory without requiring any copy of the data, and for communication methods like pipelines and messaging teams, four copies of the data are required for the kernel and user space, Shared memory is only copied two times: once from the input file to the shared memory area, and the other from shared memory to the output file.

In general, when sharing memory between processes, it is not always clear to read and write a small amount of data after the map, there is new communication when the shared memory area is re-established, but to maintain the shared area until the completion of the communication, so that the data content is kept in shared memory, and did not write back to the file. The content in shared memory is often written back to the file when it is de-mapped, so it is very efficient to use shared memory for communication.

Shared memory is implemented in two ways: 1, Memory mapping 2, shared memory mechanism

1. Memory Mapping

Memory-mapped memories map mechanism enables shared memory between processes by mapping the same common file, implemented through MMAP () system calls. After the normal file is mapped to the process address space, the process can

Access to files like normal memory eliminates the need to call file manipulation functions such as read/write.

Example: Create a child process where the parent-child process implements shared memory through anonymous mappings.

Analysis: The main program calls Mmap mapped memory before calling the fork function to create the process. Then, after calling the fork function, the child process inherits the address space of the parent process after the anonymous mapping, and also inherits the return address of the MMAP function, so that the parent-child process can communicate through the mapped area.

2. UNIX System V Shared memory mechanism

The IPC shared memory refers to the sharing of all the shared data in the shared memory region (IPC), any process that wants to access that data must have a new memory area in the address space of the process to map the physical memory pages that hold the shared data.

Unlike the previous MMAP system call, which implements shared memory by mapping a plain file, UNIX System V shared memory is a shared memory communication between processes by mapping files in the special file system SHM.

Example: Design two programs, through the UNIX System V shared memory mechanism, one program to write to the shared area, and another to read the shared area.

Analysis: A program calls the FOTK function to produce the standard key, then calls the Shmget function, gets the ID of the shared memory area, calls the Shmat function, maps the memory, loops the age, and the other program reads the shared memory.

(The FOTK function has been used in the Message Queue section,

The Ftok function generates a unique key value for the IPC object, based on the file (or directory) name specified by pathname, and the number specified by the Proj parameter. )

key_t Ftok (char* Pathname,char proj)

How interprocess communication is (RPM)

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.