Process synchronization and communication, the difference between process and thread synchronization, the difference between process and thread communication "go"

Source: Internet
Author: User
Tags message queue mutex semaphore



This article was reproduced from: http://www.cnblogs.com/youngforever/p/3250270.html



These two days to see the process of synchronization and communication, read a few books on the introduction, but also from the Internet search a lot of information, the more confused, the more puzzled by these issues are very tangled.


    1. What is the difference between process synchronization and mutex?
    2. What are the synchronization methods for a process?
    3. What are the communication methods of the process?
    4. What is the difference between process synchronization and communication?
    5. Is there a difference between thread synchronization/communication and process synchronization/communication?


In many textbooks (both domestic and foreign) are not clear these concepts, and now have no accurate answers to each question, the following to write down their own understanding, and then Add.



Resources:



"Operating system Tutorial" Sun Jongxiu Editor-in-chief Fechanglin Bingbin Feng, higher Education Press



"Computer operating System" he yanxiang Li Fei Li Ning University Press (Process Management Section similar to "Operating system tutorial")


The concept of process mutex and synchronization


The concept of process mutex and synchronization is the concept of concurrent process, and with the concurrent process, the competition and cooperation of resources are created, so the competition and cooperation of resources can be solved by mutual exclusion, synchronization and communication.



Here are the concepts of process mutex, synchronization, as described in the operating system tutorial 3.1.4.



In a multi-channel program design system, there may be many processes at the same time, and there are two basic relationships between these processes: competitive relationships and collaborative relationships.



The mutual exclusion, synchronization and communication of the process are based on these two basic relationships, in order to solve the inter-process competition relationship (indirect constraint relationship) and the introduction of process mutex, in order to solve the loose cooperative relationship between processes ( direct constraint relationship ) and introduce process synchronization; In order to resolve inter-process Process communication in a tight collaborative relationship.



The first is a competitive relationship.



Multiple processes in the system are not related to each other, and they are unaware of the existence of other processes and are unaffected by the execution of other processes. For example, multiple user processes are established in a batch system, and multiple terminal processes are established in a time-sharing system. Since these processes share a set of computer system resources, it is inevitable that multiple processes compete for resources. When multiple processes compete to share resources such as hard devices, storage, processors, and files, the operating system must coordinate the process's contention for resources.



There are two control problems in resource competition: one is a deadlock (deadlock) problem, and if a group of processes get some resources and want to get the resources that other processes occupy, eventually all the processes will fall into deadlock. The other is the problem of hunger (starvation), which refers to the fact that a process is indefinitely delayed because other processes always take precedence over it.



The operating system needs to ensure that the process can access the critical resources mutually, both to solve the hunger problem and to solve the deadlock problem.
The mutex of the process (mutual exclusion) is the means to resolve the inter-process competition relationship ( indirect constraint relationship ). Process mutex means that when several processes want to use the same shared resource, a maximum of one process is allowed to be used at any time, and the other processes that use the resource must wait until the resource-owning process releases the resource.



The second is the collaborative relationship



Some processes require a collaborative effort to accomplish the same task, since each process of cooperation is carried out independently and at an unpredictable pace, which requires a collaborative process to coordinate their work at certain focal points. When one of the cooperation processes arrives at the coordination point, it should block itself until a message or signal from its partner process has been received until the other partner process sends a coordinated signal or message to wake up and continue execution. The coordination between such collaborative processes waiting for each other's messages or signals is called process synchronization.



Collaboration between processes can be an indirect collaboration between two parties who do not know the other's name, for example, by sharing access to a buffer for loose collaboration, or by knowing each other's name and collaborating directly with the communication mechanism. Allowing processes to work together facilitates the sharing of information, facilitates faster computation, and facilitates modular programming.



Process Synchronization (synchronization) is a means of resolving inter-process collaboration relationships ( direct constraint relationships ). Process synchronization refers to more than two processes that coordinate their activities based on a condition. The execution of one process relies on another
A message or signal from a collaborative process that waits until a message or signal arrives before a process receives a message or signal from another process.



It is not difficult to see that a process mutex is a special process synchronization relationship that uses mutually exclusive shared resources over and over again, and is a coordination of the use of resource order for processes.


The concept of process communication


The following is the concept of process communication organized according to the instructions in the operating system tutorial 3.5.



The interaction between concurrent processes must meet two basic requirements: synchronization and communication.



In order to implement mutual exclusion when competing resources, mutual exclusion is a special kind of synchronization, which essentially needs to solve the process synchronization problem, process synchronization is a process communication, through modifying the signal volume, the process can establish a link between each other, coordinate operation and work together. However, the signal volume and PV operation can only transmit signals, not the ability to transmit data. In some cases, the amount of information exchanged between processes is small, for example, by exchanging only one state information, but in many cases there is a need to exchange large amounts of data between processes, for example, to send a batch of messages or an entire file, which can be accomplished through a new communication mechanism, The work of exchanging information between processes is called process communication IPC (interprocess communication) (mainly refers to the exchange of large amounts of data). There are many ways to communicate between processes, including:


    1. Signal (signal) communication mechanism;
    2. The shared memory communication mechanism controlled by the semaphore and its primitive operation (PV, read-write lock, enhancement);
    3. The shared file communication mechanism provided by the pipeline (pipeline);
    4. Messaging (message passing) communication mechanism for mailbox and send/Receive primitives.
      The first two modes of communication because of the exchange of less information and inefficient, it is called low -level communication mechanism , corresponding to the signal/signal and PV operations such as low-level communication primitives, only applicable to centralized operating system. The message passing mechanism belongs to the advanced communication mechanism , and the sharing file communication mechanism is a variant of the message passing mechanism, both of which are suitable for both centralized operating system and distributed operating system.
Methods for process synchronization


As mentioned earlier, the process mutex is a special process synchronization relationship, and the following is a common process synchronization method that can actually be used for mutual exclusion of processes (personal understanding).



In He Yanxiang's computer operating system section 3.2, the mechanism of process synchronization is the same as that of the solution process mutex, which clearly points out that mutually exclusive software solution is Dekker algorithm and Peterson algorithm, mutually exclusive hardware solution is interrupt method, and the method of using machine instruction, There are three methods of signal volume, enhancement and message passing.



In practical applications, different systems have different process synchronization methods, there are some discussions in csdn post http://bbs.csdn.net/topics/80156687, the main synchronization and communication mechanism between Linux and Windows as follows:



Under Linux:



The common process synchronization methods under Linux are: SYSVIPC's SEM (semaphore), File Locking/record locking (through the FCNTL settings, record Lock), Futex (Fast User-state mutex based on shared memory). There are also Pthread_mutex and pthread_cond (condition variables) for threads (pthread).

Common process communication methods under Linux are: pipe (pipe), FIFO (named pipe), socket (socket), SYSVIPC SHM (Shared memory), MSG queue (Message queue), mmap (file map). There used to be a STREAM, but now it's more rare (as it seems).



Under Windows:
In Windwos, process synchronization mainly has the following: Mutex, Semaphore, event, can wait for several kinds of technology.



Under Windows, process communication is mainly the following: memory mapping, pipelines, messages, etc., but memory mapping is the most basic, because the other process communication methods are internal memory mapping to complete.


Is there a difference between thread synchronization/communication and process synchronization/communication?


For this question, the textbook does not have a clear answer, the textbook is generally given in the process rather than the thread of synchronization, communication mode. But many of the internet's claims confuse the two. According to the textbook, as well as the online statement, the individual's understanding is:



Synchronization mechanism:



semaphore, enhancement, mutual exclusion is the synchronization mechanism of the process, and the semaphore and mutex can also be used for thread synchronization, but the tube is only used in process synchronization;



Thread synchronization In addition to the signal volume, mutual exclusion, there are critical areas, events, and did not see the textbook on the two methods as a process of synchronization;



Communication mechanism:



Pipeline, FIFO, message queue, semaphore, shared memory is the synchronization mechanism of the process, there is no thread communication mechanism in the textbook, but it is certain that these methods are the process of communication, and the signal volume can be used for both process synchronization and process communication, on the network and can be used for thread synchronization.



Pipelines are different from enhancement, which is the way the process synchronizes, and the pipeline is the way the process communicates.


Synchronization/Communication of processes


The following is a detailed description of how synchronization between threads is common.



(Note: The following transfer from the network, the following synchronization, communication method for the process and the thread is not very clear, about the process or the thread of explanation see above--thread synchronization/communication and process synchronization/communication is different? )



First, process/thread synchronization mechanism.



Four ways of critical, mutex, event, Semaphore
Differences between critical sections (Critical section), mutexes (mutexes), semaphores (Semaphore), Events (event)
1, critical area: Through the serialization of multithreading to access public resources or a piece of code, fast, 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: adopt 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 needs to limit the maximum number of threads that access this resource at the same time.



Semaphore objects synchronize threads differently than in the previous methods, and the signal allows multiple threads to use shared resources at the same time 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, Event: the way to maintain the synchronization of threads by notification operation, it is also convenient to achieve the priority comparison of multiple threads operations.



Summarize:
1. 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.
2. 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.
3. 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.



Second. modalities of communication between processes



Because it is easy to confuse, we have also listed the Interprocess communication method here for comparison.



Process communication is the so-called IPC problem, which mainly refers to the way in which data is exchanged between processes. Process communication includes advanced communication and low-level communication, in which process synchronization and mutual exclusion belong to low-level communication, mainly used for inserting U farmland control signals; advanced communications include three kinds: shared storage systems (some are called shared memory areas), messaging systems (some are called message queues), pipelines.



Semaphores are common methods for process synchronization and mutual exclusion, and can also be used as a low-level process communication method for transmitting control signals.



In short, inter-process communication mainly includes pipelines, FIFO, message queue, semaphore, shared memory.



1. Pipelines, as well as named pipes and non-named pipes (that is, anonymous pipes), non-named pipes (that is, anonymous pipes) can only be used for parent-child process communication, named pipes can be used for non-parent-child processes, named Pipes are FIFO, pipeline is first-out communication mode



2. Message Queuing, which is used for communication between two processes, first creates a message queue in one process, then writes the data to the message queue, and the other process takes the data from that message queue. Note that Message Queuing is created by creating a file, and if one process writes data to a message queue, the other process does not take out the data, even though the process of writing data to the message queue has ended, the data saved in the message queue does not disappear. That is, the next time you read the data from this message queue is the last data!!!!



3. Semaphore, which is the same as the semaphore under Windows, so don't say more.



4. Shared memory, similar to a shared variable in a DLL under Windows, but the shared memory area under Linux does not require anything like a DLL, as long as a shared memory area is created first, other processes can access the data in this shared memory area at a certain step, of course readable and writable



Comparison of the above methods:



1. Pipeline: Slow speed, limited capacity, only the parent-child process can communicate



2.FIFO: can communicate between any processes, but is slow



3. Message Queuing: The capacity is limited by the system, and pay attention to the first reading, to consider the last time you did not read the data of the problem



4. Semaphore: cannot transmit complex messages, can only be used to synchronize



5. Shared memory Area: can easily control capacity, fast, but to maintain synchronization, such as a process in writing, another process to pay attention to read and write problems, the equivalent of thread security threads, of course, the shared memory area can also be used for inter-thread communication, but not necessary, A piece of memory in the same process is already shared between threads



Essentially, a semaphore is a counter that is used to record access to a resource, such as shared memory. In general, in order to get a shared resource, the process needs to do the following:


(1) The Test control the signal volume of the Resource, (2) If the value of this semaphore is positive, allow the use of the resource, the process will be the number of 1, (3) If the semaphore is 0, the resource is currently unavailable, the process goes to sleep until the semaphore value is greater than 0, the process is awakened, into the step (1 (4) When the process no longer uses a semaphore-controlled resource, the semaphore value is added 1, and if there is a process waiting for this semaphore at this time, the process is awakened.
Socket communication is not proprietary to Linux, almost all of the operating systems that provide the TCP/IP stack are provided with sockets, and all such operating systems are almost identical to the socket programming method


Third.Comparison of process/thread synchronization mechanism and inter-process communication mechanism



It's obvious that 2 of them are similar, but the difference is great.



Synchronization is mainly critical area, mutual exclusion, semaphore, event



Interprocess communication is a pipeline, memory share, message queue, semaphore, socket



In common, semaphores and messages (events)


Summary:
      1. Process mutual exclusion, synchronization and communication relationship: the process of competing resources to implement mutual exclusion, mutual exclusion is a special synchronization, in essence, the need to solve the process synchronization problem, process synchronization is a process of communication, it appears that the process of mutual exclusion, synchronization can be regarded as process communication;
      2. Signal volume is a common method of process synchronization and mutual exclusion, and it can be used as a low-level process communication method for transmitting control signals.

      3. The pipeline is different from the enhancement, and the pipe is the way of the process synchronization, and the pipeline is the way of the process communication;


Process synchronization and communication, the difference between process and thread synchronization, the difference between process and thread communication "go"


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.