Multi-threaded multi-process simple explanation, System V IPC Introduction (native IPC) [linux] (zz)

Multi-process multi-threaded brief description

Pipe communication involving multi-process and multi-threaded instructions

When using two-way communication named pipe, the pipe is blocked because the read to read, in order not to "read" blocking "write", using his son into the
process to multi-line operation,
1) the parent process this line: Read the pipeline 1
2) the child this line: write pipeline 2

In fact, we back at a later thread, all related to the multi-line operation, the basic use multithreading to achieve , such as
1) the main thread: Read Pipeline 1
2) times thread: 2 pipes write
us via inter-process communication pipe method implementation, there will be a blog thread implementation instructions.
That pipe communication above, we can change the main thread to the parent process, the process was changed to handle twice the thread .

Comparison multi-process and multi-threaded applications used by each

Threads and processes are running concurrently , but each of the threads and processes used in different situations.

Thread

When all related multi-line, we use concurrent threads to achieve, such as the example above, we realize the "named pipe" two-way communication, this multi-line operation theoretically should use multiple threads to achieve.
Because multi-line use threads more provincial computer cpu and memory overhead.

That is the purpose to create a secondary thread to run concurrently, for multi-line operation.

process

A simple criterion is that if you find that your program must go to run a new program, this time must involve multiple processes, because at this time if you do not create a child process, you have no way to perform a new program .

Child and parent of the newly created process must be run concurrently, but the main purpose here is not to run concurrent multi-line operation, but to go alone to implement the new program, implementation of the new program, we can only use multi-process to operate, there is no way to use multiple threads to operate, because the thread is not possible to execute a new program.

More direct say only process threads inside a function, not a new program
that is to create a child process concurrent execution of purpose, in order to implement a new program.

System V IPC (native IPC)

About System V IPC

A comparison between the original process early unnamed pipes and named pipes, all UNIX systems provide communication (IPC) way
early Unix systems design at the beginning there.
Later Unix system upgrade to version 5, but also offers three new IPC communication, namely:
· Message Queue
· semaphore
· Shared Memory

System V is the fifth version of the system of meaning, later Linux also inherited the unix these three means of communication, Unix is ​​very early and very good OS, it also draws on other os three of System V IPC.

System V IPC features

Pipeline (original IPC)

The nature of the pipe is a piece of cache, but is a form of Linux OS kernel files to manage, so we operate the pipeline, whether anonymous pipes, named pipes or, as we are using the file descriptor file to operate.

So when we operate the pipeline, in addition to the pipe and mkfifo these two functions, the other like read, write, open the file io is our function, so in fact when learning the pipeline, it will feel easier.

System V IPC

System V IPC and the pipe is different, it uses a completely different implementation mechanisms, and did not file any relationship that is no longer in the form of the kernel file to manage System V IPC, it can no longer use file approach to the operation .
For System V IPC, OS kernel provides a new API.

When you use System V IPC, saying there is no genetic process

Any communication between processes, you can use System V IPC to communicate.

System V IPC identifier

As we have said, System V IPC no longer exist in the form of documents, there is no file descriptors this thing, but it has a similar "identifier."

You can think of this "identifier" is the replacement file descriptor, but it is dedicated to the use of System V IPC, so we can not use file IO functions to operate "identifier", can only use the System V IPC unique API to operate.

How to get this "identifier"

After calling an API created a "communication structure", API returns a unique "identifier."
For example, after you've created a "message queue", API will return created a unique identification message queue "identifier."

The role of System V IPC identifier?

For example, if you create a message queue, then process the message queue by a unique identifier, you can find the created "message queue", using the message queue, the process will be able to read and write data, and interprocess communication.

Published 163 original articles · won praise 94 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43648751/article/details/104724791
IPC