Thread-to-process relationships

Source: Internet
Author: User
Tags message queue

Overview

A process is the basic unit of resource allocation, and processes are composed of threads.

Threads are the basic unit of system execution scheduling, and threads are subordinate to processes.

Why threading is introduced

1, the process at the same time can only do one thing, such as when we are in class, often to listen to the ears, notes, brain thinking, if the process of the mechanism, then we can not record the lectures, but also can not think.

2, when the process of an operation is blocked, the entire process will hang, that is, when the teacher needs to think about the time, we also can only lecture, can not think and can not record notes

the difference between a thread and a process

1, the process is the basic unit of resource allocation, thread is the basic unit of system execution dispatch

2, each process has a separate memory address space, independent of each other, each thread share the memory space of the same process

3, when a process crashes, because the address space is independent, so it does not affect other processes, when a thread has a problem, the whole process will be problematic

4, the thread can be regarded as the execution path inside the process, when the path is only one, it is called single-threaded, multi-threaded

5, create a thread faster than the creation process, in the creation and recycling process, the system to allocate and reclaim resources, resulting in significantly higher system overhead than the cost of the thread


inter-process communication

Pipelines and famous pipelines:

Pipelines are used for communication between parent-child processes that have affinity; well-known pipelines allow communication between missed processes in addition to the functionality of pipelines

Signal:

A simulation of the interrupt mechanism at the software level to notify the process that something is going to happen.

Message Queuing:

A linked table of messages in which a process with write permission can write a message to a message queue, and a process with Read permission can read the message from the message queue.

Shared Memory:

Multiple processes access the same chunk of memory, and the data updates can be seen between different processes

Socket:

Network communication between different machines in the network


inter-thread communication

Semaphore (allows multiple threads to access a unified resource at the same time):

We can look at an example: if the car is a thread, the parking lot is memory space. There is a parking lot can only hold 5 cars, so each into a car in the doorman to take a brand, because there are only 5 grades, so when the parking lot to stop 5 cars, the rest of the car can not enter, can only wait outside the queue. Only when a car in the parking lot comes out, the license plate is returned to the doorman, and a car is queued outside. Doing so ensures that there are no conflicts between multiple threads.

Mutually exclusive:

Replace the example of the parking lot with a car in the parking lot. When I stop, you can't stop.

Critical section:

Only one thread is allowed to operate on a shared resource at any time, and other threads will be suspended to access the shared resource, and when the thread in the critical section leaves, the other thread can preempt

The purpose of inter-thread communication is primarily for thread synchronization, so there is no communication mechanism for exchanging data like in the process


differences between multi-threaded synchronizations and mutexes

Synchronization is a restrictive relationship between threads, where one thread's execution relies on another thread's message, and one thread can be awakened only when it gets a message from another thread, otherwise it should wait.

Mutual exclusion is when a thread accesses a shared resource and only one thread is allowed to use it at any one time, and the other thread that wants to use the resource must wait, as long as the thread that occupies the resource frees the resource.


So mutual exclusion can be seen as a special kind of synchronization.

As an example:

A global variable, as long as the main thread modifies the variable, the next can be accessed, at this point need to synchronize two threads;

When a thread accesses a global variable, another thread cannot access the variable, which is mutually exclusive.


Thread Safety

multithreaded programs are predictable, and as with single-threaded running results, it is safe to note that




Reference

What is the difference between a process and a thread?

A simple explanation of processes and threads



Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.

Thread-to-process relationships

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.