Order today is asked about interprocess communication problems, found that they do not know enough, so, to sum up the main task of the operating system is to manage the computer software, hardware resources. The main features of modern operating systems are multi-user and multitasking, which is the parallel execution of programs, and Windows is so Linux. Therefore, the operating system relies on the process to manage the computer's software and hardware resources, supporting multi-tasking parallel execution. Multiple processes and multithreading are required to execute in parallel. Therefore, multi-process and multi-threaded in order to complete a certain task, it is necessary to carry out certain communication. Communication between threads is different from interprocess communication. Because the data space of the process is relatively independent and the threads are shared data space, the communication mechanism is very different.I. Inter-process communicationInter-process communication, the independence of its data space determines that its communication is relatively complex and needs to be through the operating system. Previously, interprocess communication can only be a standalone version, and now the operating system inherits the inter-process communication mechanism based on socket (socket). So the communication between processes is not limited to a single computer, the realization of network communication.
The communication mechanisms of the process are: pipelines, well-known pipelines, message queues, semaphores, shared spaces, signals, sockets.
1. Signal 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 can be said to be the same. The signal is asynchronous, and a process does not have to wait for the signal to arrive by any action, in fact, the process does not know exactly when the signal arrives. The signal is the only asynchronous communication mechanism in the interprocess communication mechanism, which can be regarded as asynchronous notification and what happens in the process of notifying the receiving signal. After POSIX real-time expansion, the signaling mechanism is more powerful and can deliver additional information in addition to the basic notification function. There are two sources of signal events: hardware sources (such as when we press a keyboard or other hardware failure), and the source of the software. The signal is divided into reliable and unreliable signals, real-time signals and non-real-time signals. The process responds to signals in three ways:
- Ignore signal
- Capturing signals
- Perform the default action
2. Semaphore semaphore can also be said to be a counter, commonly used to deal with the problem of process or thread synchronization, especially the critical resource access synchronization problem. Critical resource: A resource that can be manipulated by only one process or thread at a time, when the semaphore value is greater than or equal to 0 o'clock, represents the number of critical resources available to the concurrent process for access, when it is less than 0 o'clock, indicating the number of processes waiting to use the critical resource. More importantly, the value of the semaphore can only be changed by the PV operation.
3. Message Queuing Message Queuing is a list of messages that are stored in the kernel, each message queue is identified by a message queue identifier, and in the case of a pipeline, Message Queuing is stored in the kernel, and only one message queue can be deleted when the kernel is restarted, the kernel restart is the system restart, and the message queue size is also limited.
4. Shared memory sharing memory is the allocation of a piece of memory that can be accessed by other processes. Shared memory can be said to be the most useful inter-process communication and the fastest form of IPC. First, before using the shared memory area, you must attach it to the process's address space by means of a system function or to the process space. Two different processes A, B shared memory means that the same piece of physical memory is mapped to the respective process address space of process A and B. Process A can instantly see that process B updates the data in shared memory, and vice versa. Because multiple processes share the same block of memory, there is a need for some kind of synchronization mechanism, both mutexes and semaphores. One obvious benefit of using shared memory communication is that it is efficient because the process can read and write directly to the memory without requiring any copy of the data. For communication methods such as pipelines and message queues, four copies of the data are required in the kernel and user space, while shared memory copies only two data [1]: One from the input file to the shared memory area, and the other from the shared memory area to the output file. In fact, when you share memory between processes, you do not always have to read and write small amounts of data, and then re-establish the shared memory area when there is new communication. Instead, the shared area is maintained until the communication is complete, so that the data content is kept in shared memory and is not written back to the file. Content in shared memory is often written back to a file when it is de-mapped. Therefore, the use of shared memory communication mode is very efficient.
5. Pipeline pipeline transmission data is unidirectional, can only flow from one side to the other, that is, a half-duplex communication mode; only for inter-process communication, kinship is parent-child process or sibling process; No name and limited size, no format stream is transmitted, So it is necessary to agree on the format of data communication when two processes communicate. Pipeline It is like a special file, but this file exists in memory, when the pipeline is created, the system allocates a page as a data buffer for the pipeline, and the process reads and writes the data buffer to complete the communication. One of the processes can only read one can only write, so called half-duplex communication, why one can only read one can only write? Because the write process is written at the end of the buffer, the read process is read in the head of the buffer, and their respective data structures are different, so the functions are different.
6. Named pipe Named pipes (namedpipe) are one-way or two-way pipelines that communicate between a server process and one or more client processes. Unlike anonymous pipelines, named pipes can be used between unrelated processes and between different computers, and the server assigns a name to it when it establishes a named pipe, and any process can open the other end of the pipeline by that name, communicating with the given permissions and the server process. 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.
Named pipes are different from pipelines that can communicate only between processes that have affinity. It provides a path name associated with it, with its own transmission format. The difference between named pipes and pipes is that a well-known pipe is a device file that is stored in a file system, and that no affinity process can access it, but that it reads the data according to the FIFO principle. is also single-duplex.
7. Socket sockets are also an inter-process communication mechanism, unlike other communication mechanisms, which can be used for process communication between different hosts.second, inter-thread communicationInter-thread communication: Because multithreading shares address space and data space, communication between multiple threads is a thread of data that can be provided directly to other threads instead of through the operating system (that is, the kernel's dispatch).
1. Locking mechanism includes mutex, condition variable, read-write lock, and mutex provides a method to prevent the data structure from being modified concurrently by exclusive means. Use a condition variable to block a process atomically, until a particular condition is true. The test of the condition is performed under the protection of the mutex. A condition variable is always used with a mutex.
Read-write locks allow multiple threads to read shared data at the same time, and write operations are mutually exclusive.
2. Semaphore mechanism (Semaphore) includes nameless thread semaphore and named thread Semaphore 3. Signal mechanism (Signal) signal processing between similar processes
The purpose of communication between threads is primarily for thread synchronization. So threads do not have a communication mechanism for data exchange, as in process communication.third, the difference between interprocess communication and inter-thread communication in Linux
- The process in Linux is created by a fork () system call, and there are separate address spaces between the processes, they cannot communicate directly, and must be accomplished through some IPC process interprocess communication mechanisms. Common IPC are: pipe, named pipe, signal, shared memory and socket, etc.
- Threads in Linux are created by the clone () system call, which is shared memory space between threads in a process, so thread A can access the variables defined in thread B, but it must be aware of concurrency;
- Another: "Thread context" is much smaller than the process context
Iv. process/inter-thread synchronization mechanismCritical area (Critical section), Mutex (mutex), Semaphore (Semaphore), event four ways
1. Critical section access to public resources or a piece of code through serialization of multithreading is fast and suitable for controlling data access. Only one thread is allowed to access the shared resource at any time, and if more than one thread attempts to access the public resource, the other threads that attempt to access the public resource will be suspended after one thread enters, and wait until the thread that enters the critical section leaves and the critical section is freed before other threads can preempt it.
2. Mutex uses mutually exclusive object mechanism. Only the thread that owns the mutex has access to the public resource, because there is only one mutex object, so that the public resources are not accessed by multiple threads at the same time. Mutual exclusion can not only realize the common resources security sharing of the same application, but also can realize the security sharing of common resources of different applications. The mutex is more complex than the critical section. Because using mutexes not only enables the secure sharing of resources in different threads of the same application, but also enables secure sharing of resources between threads of different applications.
3. Semaphore it allows multiple threads to access the same resource at the same time, but it needs to limit the maximum number of threads that access this resource at the same time. Semaphore objects are synchronized to threads in a way that allows multiple threads to use shared resources simultaneously, which is the same as the PV operation in the operating system. It indicates the maximum number of threads concurrently accessing the shared resource. It allows multiple threads to access the same resource at the same time, but needs to limit the maximum number of threads that access this resource at the same time.
The concept of PV operation and signal volume is presented by Dutch scientist E.w.dijkstra. The semaphore S is an integer, s greater than or equals zero represents the number of resource entities available to the concurrent process, but s less than zero indicates the number of processes waiting to use the shared resource.
P Operation Request Resources:
(1) s minus 1;
(2) If s minus 1 is still greater than or equal to zero, then the process continues to execute;
(3) If s minus 1 is less than 0, then the process is blocked into the queue corresponding to the signal, and then transferred to the process scheduling.
V Operations Release Resources:
(1) s plus 1;
(2) If the sum result is greater than 0, the process will continue to execute;
(3) If the sum result is less than or equal to zero, a wait process is awakened from the waiting queue of the signal and then returned to the original process to continue or transfer to the process schedule.
4. Events maintain thread synchronization by notifying the operation, and it is convenient to implement a priority comparison of multiple threads.
5. Summary
- The mutex is very similar to the critical section, but the mutex can be named, which means it can be used across processes. So creating mutexes requires more resources, so using a critical section just to be used within a process can bring speed advantages and reduce resource usage. Because the mutex is a cross-process mutex once created, it can be opened by name.
- Mutexes (mutexes), semaphores (Semaphore), events can be used across processes to synchronize data operations, while other objects are not related to data synchronization operations, but for processes and threads, if the process and thread are running in a state that is not signaled, signaled state after exiting. So you can use WaitForSingleObject to wait for processes and threads to exit.
- The mutex can be used to specify that the resource is exclusive, but if one of the following cases can not be handled by mutual exclusion, for example, now a user buys a three concurrent Access License database system, depending on the number of access licenses purchased by the user to determine how many threads/processes can simultaneously perform database operations, At this time, if the use of mutual exclusion is no way to complete this requirement, the Beacon object can be said to be a resource counter.
Inter-process communication and inter-thread communication