Interactive concurrent processes can achieve mutual exclusion and synchronization of processes through semaphores and PV operations, such as producer-consumer is a set of collaborative processes, they work together through semaphores, and the introduction of bounded buffers to access. This type of low-level communication is inconvenient and very limited. Cheng is used for advanced programming programming, which centralize and manage the critical areas scattered in each process, adopt the blocking/wake + centralized critical zone and a state test strategy, and provide a new choice of synchronization tools. These tools address synchronization and communication issues, but lack the ability to deliver data. Therefore, a process communication tool is designed, which has both communication and synchronization capability. The different forms of process communication tools include:
I. Signaling and communication mechanismsSignals generated by process execution are called synchronous signals, such as being removed by 0, and signals caused by events outside the process are called asynchronous signals, such as keystrokes. The user, kernel and process can generate send signal: (1) The user presses the interrupt combination key "Ctrl" + "C", the terminal driver receives the input character, and calls the signal system, the signaling system sends the SIGINT signal to the Shell,shell to send it to the process, Process received SIGINT signal will be revoked. The user can also request the kernel to generate a signal through other keys assigned to the signal control character by the terminal driver. (2) When the process execution error occurs, the kernel detects the event and sends a signal to the process, such as illegal segment access, floating-point overflow, illegal operation code, through interrupts and abnormal mechanisms. The kernel can also notify a process that a specific event has occurred by signaling. (3) The process can send a sigkil signal to another process via the system call kill to force it to terminate. A process can also communicate with another process through a signal. The signal mechanism is fully implemented by the software, and the process receives a signal similar to that received by the CPU, but the signal is queried at the end of the exception processing or before the clock interrupt processing is finished. The signal also has the shielding facility, can set the signal shielding position, causes the process to ignore the signal which sends. The process that receives the signal responds by performing a default action (such as the default processing of the SIGINT signal is process revocation), executing a preset signal handler, or ignoring this signal. Signal processing programs are often provided by the application, by the signal number of the signal vector table (located in the system space), so as to find and transfer to the corresponding signal processing program, in the user space execution.
second, the pipeline communication mechanismMultiple processes use a shared message buffer (which can be called a pipe, a multiplexer, a socket). Some processes write a stream of characters to the message buffer (send/write); some processes read the character stream (Receive/read) from the message buffer. The information Exchange unit is based on the character stream of any length. Pipeline (pipeline) is the traditional UNIX process communication, which is a special file for connection reading and writing, which allows data to be transmitted in a FIFO manner, and also enables the process to execute synchronously. Pipeline is one-way, send process view pipeline file as output file, in the form of character stream to send large amounts of data into the pipeline; the receiving process sees the pipeline file as the input file and receives the data from the pipeline. Write process and read process can not rely on the file system to synchronize with each other, also must do: (1) process in the read and write pipeline, the file Inode node will test the read and write mutex flag, if the lock process will wait, otherwise the inode locked and read and write operations. The operation ends and then unlocks and wakes up the process waiting for the node to be locked. (2) Both the sender and the recipient must know whether the other party exists. If the other person does not exist, the system will notify the process via sigpipe signal that there is no need to send and receive information. (3) Pipeline files use only the direct address entry of the Inode node, and the pipe length limit has an impact on the write () and read () operations of the process. The write () operation must be paused after a pipeline overflow to prevent the pipeline from spilling until the other process reads the data from the pipeline. Workaround: The data is divided, each time is less than the limit of the number of bytes of the pipeline, after writing this process sleep, until the reading process to take the data in the pipeline and identify the process to wake the other side of the wait, in order to continue to write the next batch of data, when the read process read empty pipeline, read process sleep until the write process In Linux systems, pipelines are implemented using file structure and Inode nodes. When the kernel calls pipe () to create a pipe, the two file structures of the pipeline file are established in the System open files table, respectively, to control the write and read operations of the pipeline. The two file structure points to the same inode, which then points to the physical page in the disk, returns the handle file[0], file[1], and the reader process reads the data from the pipeline via Files[0], and the writer process writes the data to the pipeline via Files[1].
three, shared memory communication mechanismA process first creates a chunk of memory to use as a communication, while the other processes map the memory area to its own virtual storage space. Because more than one process can map shared memory to its own virtual address space, the code snippet that reads and writes the shared memory area is often considered a critical section. Because the virtual address space of the process is large, the defined shared memory area corresponds to an unused virtual address area to avoid conflicts with the process image area. Pages that share memory have page table entry references in each shared Process's page table, but do not need to have the same address for all of the process's virtual storage segments.
Iv. Message delivery communication mechanismProcesses also sometimes need to exchange more information (such as the transfer of data to another process), you can introduce a high-level communication mode messaging mechanism, to achieve inter-process exchange of information between letters, Linux is called Message Queuing. Direct communication: The process of sending or receiving a letter, using the Send (P, e) primitive that sends the letter to process P and the receive (q, letter) primitive that receives the letter from the process Q, to indicate to whom the letter was sent or to whom it was received. Indirect communication: The mailbox is the storage area of the letter, each mailbox can be divided into the mailbox features and two parts of the mailbox: Mailbox features indicate mailbox capacity, letter format, pointers, etc. the mailbox body is used to store letters, the letter box is divided into several zones, and each district can hold a letter multiple processes share a mailbox. Send or receive a letter through a unique identifier of mailbox A, send the piece to mailbox A, send (a, letter) primitive, receive a letter from mailbox a received (a, letter) the primitive language. If the designated mailbox is not full, send the letter to the position indicated by the pointer in the mailbox and release the waiting person waiting for the letter in the mailbox; otherwise, the person who sent the letter is placed waiting for the mailbox. If there is a letter in the designated mailbox, take out a letter and release the waiting person waiting for the mailbox; The person receiving the letter is placed in a state to wait for the letter in the mailbox synchronization: After sending a message, the execution of the send process can be divided into two situations: (1) synchronous (blocked) send () primitive: The sending process waits for the receiving process to answer the message before proceeding. If the sending process attempts to send a message to a process that does not exist, the operating system will not recognize which mailbox is used to cache the message, it will return an error code to the sender, the sender relies on the error code to work (1) asynchronous (non-blocking) send () primitive: After the sending process sends the message to the receiving process's mailbox, The process of querying and processing the receive Side Execution Reveive () can continue until a time when the receiving process is needed to answer the message: (1) The Blocking type reveive () primitive: The receiving process is waiting for the message to complete until the message is delivered, and if there is no message in the mailbox, The receiving process is suspended until a message is put into the mailbox, and if there is a message in the mailbox, a message is received and returned. This method can synchronize the communication between the sending process and the receiving process. (2) Non-blocking reveive () primitive: The receiving process is not required to wait, and when it needs a message, it receives and processes the message. Return control to the calling process immediately after the mailbox is queried, or return a message if there is a message in the mailbox, or a flag code to indicate no message. This method allows the receiving process to poll the mailbox and, if there is no pending message in the mailbox, can do other work. Generally, the non-blocking send () primitive + blocking receive () primitive is used. Suppose a set of concurrent processes shares a mailbox box that can be used by all processes when they are sent and received, theThe mailbox was initialized to an empty state. The system constructs a message to the box, and the process that wants to enter the critical section first attempts to receive the message, and if there is a message, only one process can receive the message and enter the critical section, and the other process gets blocked, and the process then puts the message back into the mailbox. The message is equivalent to the token passed between processes to resolve the process mutex issue:
NULL ); void Pi () { message msg; while (true) { receive (box, msg); /* critical Section */ Seng (box, msg);} } COBEGINPI (); coend;
Solving producer-consumer problems with messaging mechanisms
intcapacity;voidProducervoid) { intitem; Messsage m; while(true) {Item= Produce_item ();//Production MessagesReceive (consumer, &m);//waiting for the consumer to send an empty bufferBuild_message (&m, item); Send (consumer,&m); }}voidConsumervoid) { intitem, I; Message m; for(i=0; i<capacity; i++) Send (producer,&m) while(true) {receive (producer,&m); Item= Extract_item (&m);//receive a message with itemSend (producer, &m);//loopback empty buffers to producersConsumer_item (item); }}
The system creates enough mailboxes for producers and consumers to hold capacity messages, and consumers send capacity empty messages to producer mailboxes (empty buffers), and the producer receives an empty message after each production of a message and sends back a filled message to the consumer. The total number of messages in the system remains the same. The communication mechanism knows what messages the target mailbox holds that have been sent but not yet received by the target process.
v. Remote Procedure call RPCThe client/server compute Mode server process provides a series of processes/services for client processes to invoke the process/service provided by the calling server process to take care of the hardware heterogeneity of the client computer and server computer, The external data representation of XDR is introduced to convert each computer's special data format to a standard data format based on RPC/XDR's Advanced Communication protocol:
Operating system-process (5) Process communication mechanism