Deadlock, Process Communication, and process thread differences

Source: Internet
Author: User

I. deadlock

In the operating system, several processes are executed concurrently, and they continuously apply for, use, and release system resources. Although the process coordination and communication mechanisms of the system control them, however, several processes may wait for the other party to release resources to continue running. Otherwise, the operation will be blocked. At this time, no one can release the resource without external factors, and no one can unblock the resource. According to this situation, the deadlock in the operating system is definedSystemTwo or moreProcessIndefinite periodLocation waitConditions that will never happenThe system is in a stopped state, which is a deadlock. 
The cause of the deadlock is as follows:
(1) Insufficient system resources.
(2) The order in which the process is promoted is inappropriate.
(3) improper resource allocation.
If the system resources are sufficient, all process resource requests can be satisfied, and the possibility of deadlock is very low. Otherwise, a deadlock will occur due to competition for limited resources.

Second, the process may run in different order and speed, and may also lead to deadlocks.
Four Conditions for deadlock:
(1)Mutex: A resource can only be used by one process at a time.
(2)Request and persistence Conditions: When a process is blocked by requesting resources, it will not release the obtained resources.
(3)Non-deprivation conditions: Resources obtained by the process cannot be forcibly deprived before they are used.
(4)Cyclic wait Condition: A cyclical wait resource relationship is formed between several processes.

These four conditions are necessary for a deadlock. As long as a deadlock occurs in the system, these conditions must be met. As long as one of the above conditions is not met, no deadlock will occur.

Deadlock relief and prevention:

Understanding the cause of the deadlock, especially the four necessary conditions for the deadlock, can avoid, prevent and remove the deadlock as much as possible. Therefore, in terms of system design and process scheduling, pay attention to how to avoid the establishment of the four necessary conditions and determine the rational allocation of resources.AlgorithmTo prevent processes from occupying system resources permanently. In addition, it is necessary to prevent the process from occupying resources while waiting. Therefore, reasonable planning should be made for resource allocation.

2. Process Communication Methods

(1) pipe : A pipe is a half-duplex communication method. Data flows only one way and can only be used between unrelated processes. The kinship of a process usually refers to the parent-child process relationship.
(2) Named Pipe (Named Pipe) : the named pipe is also a half-duplex communication method, but it allows communication between unrelated processes.
(3) semophore : a semaphore is a counter, it can be used to control access to shared resources by multiple processes. It is often used as a lock mechanism to prevent other processes from accessing a shared resource. Therefore, it is mainly used for synchronization between processes and between different threads in the same process .
(4) Message Queue : A message queue is a linked list of messages stored in the kernel and identified by the Message Queue identifier. The message queue has overcome the disadvantages of few signal transmission information, the pipeline can only carry unformatted byte streams, and the limited buffer size.
(5) signal (Sinal): a signal is a complex communication method used to notify the receiving process that an event has occurred.
(6) shared memory : shared memory is the memory mapped to a segment that can be accessed by other processes. The shared memory is created by one process, but can be accessed by multiple processes. Shared memory is the fastest IPC method. It is specially designed for the low efficiency of communication between other processes. It is often used with other communication mechanisms, such as signal two, to achieve synchronization and communication between processes.
(7) socket : the Interprocess interface is also a communication mechanism between processes. Unlike other communication mechanisms, it can be used for communication between different processes.

Iii. Differences between processes and threads

Both processes and threads are implemented by the operating system.ProgramThe basic unit for running. The system uses this basic unit to implement the system's concurrency for applications. The difference between a process and a thread is:

(1) A program has at least one process and at least one thread.

(2) The division scale of threads is smaller than the process, which makes the concurrency of multi-threaded programs High.

In addition,The process has independent memory units during execution, and multiple threads share the memory, which greatly improves the program running efficiency..
The execution process of a thread is different from that of a process.Each Independent thread has a program running entry, sequence execution sequence, and program exit. But the thread cannot be executed independently. It must exist in the application and the application provides multiple thread execution control.

Logically, multithreading means that multiple execution parts in an application can be executed simultaneously. However, the operating system does not view multiple threads as multiple independent applications to implement process scheduling, management, and resource allocation. This is an important difference between processes and threads.

A process is a running activity of a program with certain independent functions on which a dataset is closed. A process is an independent unit for the system to allocate and schedule resources.

A thread is an entity of a process,It is the basic unit for CPU scheduling and allocation.It is a basic unit that can run independently less than a process.The thread itself basically does not have system resources, and only has a few resources (such as program counters, a set of registers and stacks) that are essential for running ), however, it can share all resources of a process with other threads of the same process.

one thread can create and withdraw another thread. concurrent execution can be performed between multiple threads in the same process .

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: 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.