Running (zssure): ipc=inter-process Communication, interprocess Communication Learning Note (i)

Source: Internet
Author: User
Tags message queue posix readfile semaphore

Preface:

Blog articles want to organize the logical, easy to follow their own review study and communicate with you. I don't know if you feel the same way? Sometimes busy all day, search for all kinds of information, sometimes ecstatic, sometimes depressed depressed, sometimes into the source of the horn tip, sometimes the tiger to eat the day. But every time you go through this process, when you're about to give up, it suddenly dawned on you, the problem orbug solved. So again and again and again, day after day ... if you don't make a record afterwards, you get only the slightest thrill and joy at the last moment, but it's hard to make progress and grow. So decided to "accumulate" as a daily study of the position of learning, to open up this series for the recording of daily learning points. 2015,running zssure ...

background:

A few recent periods have focused on some of the relevant technologies related to IPC, namely interprocess communication. Online Search Learning the "UNIX Network Programming Volume 2: interprocess communication", ZeroMQ Guide document, ACTIVEMQ and other information, the IPC has a general understanding, this article records an attempt to anonymous pipeline encountered the wonderful problem, only for learning and communication.

Inter-process communication:

Inter-process communication is the dissemination or exchange of information between different processes, in order to realize the exchange of information will necessarily require different processes are accessible public media. However, the user space of the process is independent of each other, generally cannot access each other, the only exception is the shared memory area. In addition, the system space is a "public place", so the kernel is clearly the two sides of the shared media, in addition to the two sides can access the peripherals. In a broad sense, ordinary files on disk, registry, database, etc. can be used to interact with information, also can be counted as the means of inter-process communication. "Excerpt from Baidu Encyclopedia, 1"

Since the system kernel is mentioned above, we have to mention the three major systems of Windows, Unix, and Linux. The basic principle of the three is the same, but specific to the implementation of the method may be slightly different.

Windows Inter-process communication:

The Microsoft Win32 API provides a variety of methods for interprocess communication "2", which mainly includes:

1) file mapping (memory-mapped files)allows the process to treat the file content as a piece of memory in the process address range, so that the process does not have to use file I/O operations, and a simple pointer operation can read and modify the contents of the file. There are three ways for an application to share a file-mapping object, such as inheritance (the parent process creates a file-mapping object, a child process inherits the object handle), a named file mapping (a process that creates a named file-mapping object, and another process that can open a file-mapping object by that name). The first process can pass the file mapping object name to other processes through the IPC mechanism named pipes, mail slots, and handle replication (the first process establishes a file mapping object, and then passes the object handle to the second process through other IPC mechanisms (known as pipelines, mail slots, and so on). The second process copies the handle to gain access to the file-mapped object. Note: File mapping is a very effective way to share data across multiple processes with better security. However, file mappings can only be used between processes on the local machine, not on the network, and developers must also control synchronization between processes.

2) Shared memory, which is actually a special case of file mapping. When a process creates a file-mapped object with 0xFFFFFFFF instead of a file handle (HANDLE), it indicates that the corresponding file-mapping object accesses the memory from the operating system paging file, and the other process opens the file-map object to access the memory block. Because shared memory is implemented with file mappings, it also has better security and can only run between processes on the same computer.

3) Anonymous pipe (pipe) is a communication channel with two endpoints: a process with one end handle can communicate with a process that has a handle to the other end. The pipe can be unidirectional (one end is read-only, the other end is write-only), or bidirectional (both ends of the pipe can be read and writable). An anonymous pipe (Anonymous pipe) is a non-named one-way pipeline that transmits data between a parent process and a child process, or two child processes of the same parent process. The pipeline is typically created by the parent process, which then inherits the read endpoint handle or write endpoint handle of the channel, and then implements communication by the child process to communicate. The parent process can also establish two or more child processes that inherit the read and write handles of an anonymous pipe. These child processes can communicate directly using pipelines and do not need to pass through the parent process. Note: An anonymous pipeline is an effective way to implement standard I/O redirection on a single machine, which cannot be used on the web or between two unrelated processes.

4) Named pipes (Named pipe), similar to anonymous pipes, where a process names a pipeline when it is created, and other processes can open the pipeline by name. Note: Named pipes provide a relatively simple programming interface that makes it more difficult to transmit data over the network than between two processes on the same computer, but it is not enough to communicate with multiple processes at the same time.

5) mail slot (mailslots), providing one-way communication between processes, any process can establish a mail slot to become a mail slot server. Other processes, called mail slots customers, can send messages to the mail slot server process by the name of the mail slot. The incoming message is kept in the mail slot until the server process reads it. A process can be either a mail slot server or a mail slot customer, so multiple message slots can be established to enable two-way communication between processes. Note: A mail slot is similar to a named pipe, but it transmits data through unreliable datagrams (such as UDP packets in the TCP/IP protocol), and once a network error fails to guarantee that the message is received correctly, the named pipe transfer data is based on a reliable connection. However, the mail slot has a simplified programming interface and the ability to broadcast messages to all computers in the specified network area, so the mail slot is another option for the application to send and receive messages.

There are also such things as the Clipboard (clipped Board), Dynamic Data Exchange (DDE), Object-connected embedding (OLE), dynamic-link libraries (all processes that the global data area in the DLL can be called DLLs to share), remote procedure calls (RPCs), NetBIOS functions, Sockets (most common in network programming, often used in PACs systems), wm_copydata messages , and so on.

UNIX inter-process communication:

Similar to the Windows system, the UNIX system has an inter-process communication mode of "3":

1) pipe and named pipe (Named pipe), pipelines can be used for communication between affinity processes, and in addition to having the functions of a pipeline, a well-known pipe allows for inter-process communication without affinity.

2) signal (Signal), the signal is a simulation of the interrupt mechanism at the software level, it is a more complex mode of communication, to notify the process of an event occurred, a process received a signal with the processor received an interrupt request can be said to be consistent.

3) Message queue, message queue is the link table of the message, it overcomes the disadvantages of limited signal in the two modes of communication, the process with Write permission can add new information to message queue according to certain rules A process that has Read permission on a message queue can read information from a message queue. The message buffering communication technology is first put forward by Hansen, and its basic idea is that the information exchange between processes is realized by using the common message buffer in memory according to the "producer-consumer" principle. Several message buffers are opened in memory to hold messages. Whenever a process sends a message to another process, it requests a message buffer, sends the prepared message to the buffer, and then inserts the message buffer into the message queue of the receiving process. Finally notifies the receiving process that the receiving process receives a notification from the sending mileage, extracts the message buffer from the message queue of the process, takes out the required information, and then sends the message buffer to the system irregularly. The system is responsible for managing the public message buffers and the delivery of messages. A process can send a message to several processes, whereas a process can receive messages from different processes, and it is clear that the operation of the message queue in the process is a critical section. When the sending process is adding a message to the receiving process's message queue, the receiving process cannot get the message from the message queue at the same time, and vice versa.

4) Shared memory, similar to the previous section.

5) The semaphore (Semaphore), which is used primarily as a synchronization and mutex between the processes and the different threads of the same process.

6) socket (socket), above section

Linux inter-process communication:

Linux is similar to UNIX in "4", as shown in:

Among them, the original Unix IPC includes: pipeline, FIFO, signal ; System V IPC includes:System V Message queue, System V Semaphore, System V shared memory area ; Posix IPC includes: POSIX Message Queuing, POSIX semaphore, POSIX shared memory area . There are two points to be brief: 1) due to the diversity of UNIX versions, the Institute of Electrical and Electronic Engineering (IEEE) has developed a standalone UNIX standard, a new ANSI UNIX standard called the Portable Operating System interface (Psoix) for the computer environment. Most of the existing UNIX and popular versions follow the POSIX standard, and Linux adheres to the POSIX standard from the start, and 2) BSD is not a non-interprocess communication within a single machine (the socket itself can be used for interprocess communication within a single machine). In fact, many UNIX versions of the stand-alone IPC are left with BSD traces, such as the 4.4BSD supported anonymous memory mapping, the implementation of 4.3+BSD for reliable signal semantics, and so on.

an issue that is encountered by anonymous pipelines:

Today, the anonymous pipeline is tested locally, using a compilation environment of VS2012, which uses the language C + +.

Where the service-side code is as follows:

TestServerforPipe.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <windows.h> #define Create_pipe_failer 1#define get_stdoutput_failed 2#define set_ stdoutput_topipe_failed 3#define create_procress_failed 4int _tmain (int argc, _tchar* argv[]) {//TODO: In this add control notification handler code int errcode;//Create an anonymous pipe handle hread,hwrite; Security_attributes sa; ZeroMemory (&sa,sizeof (SA)); sa.nlength=sizeof (security_attributes); sa.lpsecuritydescriptor=null; Sa.binherithandle=true;if (! CreatePipe (&hread,&hwrite,&sa,0)) {//messagebox ("Create anonymous pipe failed! "); return 0;} Create child process Startupinfo si; Process_information pi;si.cb=sizeof (STARTUPINFO); HANDLE Htemp=getstdhandle (Std_output_handle); Setstdhandle (Std_output_handle,hwrite)) {printf ("Setting the pipe to standard output failed! \ n ");} Getstartupinfo (&AMP;SI); si.hstderror=hwrite;si.hstdoutput=hwrite;si.wshowwindow=sw_hide;si.dwflags=startf_ Usestdhandles;int bret=0;if (!) ( Bret=createprocess (NULL, "TestClientforPipe.exe", Null,null,true,null,null,null,&si,&pi))) {errcode= GetLastError ();//messagebox("The creation of the console process failed!") "); return 0;} Setstdhandle (std_output_handle,htemp); char szreadbuf[100];D word nread;while (ReadFile (hread,szreadbuf,99,& Nread,null) {szreadbuf[nread]= ';p rintf ("%s", Szreadbuf);} return 0;}


The client code is as follows:

TestClientforPipe.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <windows.h> #include <iostream>using namespace std;int _tmain (int argc, _ tchar* argv[]) {while (1) {Sleep (+);p rintf ("This was a Win32 console application for testing the UN named pipe\n");} return 0;}

In the test time has not waited until the output, the service side program is blocked at the pipeline reading function ReadFile. With Procexplorer you can see that both the server and client programs have started successfully.


Problem Solving:

Search for the relevant information, in the blog "5" In comparison with the code, found that the blog post client code is using cout to write data to the anonymous pipeline, and my local client code using printf, is there a problem with printf?

Then try to replace printf to cout, miraculously successful, the server successfully read the data written by the client, as shown in:


Problem Analysis:

is writing data to the standard output stdout, does cout differ so much from printf? What is the specific reason? Although we usually very often use cout and printf, but the principle and mechanism behind it does not know much, through the blog "6" We can know that cout and printf is the same origin, cout is encapsulated, type-safe printf; The reason why the printf function failed at first is because of the "buffer", and Endl in cout is an operator that can act as a flush buffer. The effect of printf on the output to the screen may not be very large, but when the standard output stdout is redirected to a file or the anonymous impact in this article is large, resulting in real-time is not very good and makes the mistake of programming errors, so you can guess if waiting long enough time, The client program that used printf would also write to the anonymous pipeline when the buffer was full, and the service-side breakpoint would also be set. After the actual test can be found, when waiting for about 1 minutes, after the server did not successfully output the client sent over the data, as shown in:


In addition, if you manually force flush buffers after calling the printf function, you can also implement cout and Endl, and manually flush the buffers using fflush (stdout). The effect is as follows:


The seemingly small printf function, but contains such content, want to understand the detailed mechanism of printf can refer to the blog post "7", Bo Master My Brain capacity is limited, temporarily do not delve into, and later have the opportunity to study carefully.

(end of this article)

References:

"1" http://baike.baidu.com/link?url=rv33pHtgI2esWoGXmeWaBoGeIOBdhHnDOST9WB0r2-8Ae7OPZdD_ 5kwhyu8o8z7viqgwa5vqwhpogrglnawv9_, Baidu Encyclopedia "interprocess Communication"

"2" Http://blog.csdn.net/weiwangchao_/article/details/7104940,Windows interprocess communication

"3" Http://cqgw2.blog.163.com/blog/static/2352470201032210542930/,Unix interprocess communication

"4" Http://www.ibm.com/developerworks/cn/linux/l-ipc/,Linux interprocess communication

Use of pipe in "5" http://blog.chinaunix.net/uid-28300131-id-3391466.html,Windows

The difference between "6" http://hi.baidu.com/gbmzzupggpflpvr/item/446e03eab000e82e6cabb85c,cout and printf

"7" http://blog.csdn.net/dog250/article/details/23000909,printf Introduction


[email protected]

time: 2015-01-15

Daily product (Running) month tired (zssure): ipc=inter-process Communication, interprocess Communication Learning Note (i)

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.