The relationship and difference between threads and processes

Source: Internet
Author: User

Thread-Shared The environment includes: The process code snippet, the process's public data (which makes it easy for threads to communicate with each other), the file descriptor that the process opens, the current directory of the processor process for the signal, and the process user ID with the process group ID.


The process has its own personality as well as its many commonalities. With these personalities, threads can achieve concurrency. These personalities include:1. Thread ID. 2. The value of the Register group. 3. The stack of threads (the stack is required to keep threads running independently). 4. The return value of the error code. 5. The thread's signal mask (but all the threads share the same signal processor). 6. Priority of Threads
threads in a process share the heap area, while threads in the process maintain their own stacks. in On platforms such as Windows, different threads use the same heap by default, so the use of synchronous protection is used when allocating memory with malloc (or Windows GlobalAlloc) in C. Without synchronous protection, a race condition occurs when two threads perform simultaneous memory operations, which can lead to confusion in memory management within the heap. For example, two threads are allocated a unified block memory address, an idle list pointer error, and so on.  

Symbian threads generally use separate heap space. This allows each thread to be allocated and freed directly in its own heap, reducing the overhead that synchronization introduces. When the thread exits, the system reclaims the thread's heap space directly, and the memory space within the thread does not cause a memory leak within the process.  

But when two threads use a common heap, they have to use critical section or mutex for synchronous protection. Otherwise, the program crashes sooner or later. If your thread needs to allocate and release any number and type of objects on the shared heap without rules, you can customize a allcator to use synchronous protection within allocator. The thread uses this allocator to allocate memory directly. This is equivalent to implementing your own malloc,free. However, it is recommended that you review your system again, because most of this is unnecessary. With good design, the local heap of threads should be able to meet the needs of most objects. If you have a class of objects that need to be created and shared on a shared heap, this is a reasonable requirement, and you can implement shared protection on new and delete on this class.  
A process is the basic unit of resource allocation, and a thread is the basic unit of system dispatch. usually we write the program is run as a thread, the process can be seen as including a series of threads and resources collectively; a process that includes at least onethreads (the main thread, which is generated when entering the main function), where other threads can be created, or not created. What resources are shared between threads in the same process, and what resources are they exclusive to each other? the shared resources areA. Heap because the heap is opened up in the process space, it is rightfully shared; so new is shared (16-bit platform on the global heap and local heap, local heap is exclusive)B. Global variables it is independent of a specific function and therefore is not related to a particular thread;c. Static variables although for local variables, it is in the code "put" in a function, but its location and the global variable, stored in the heap opened in the. BSS and. Data segments, are sharedD. Common resources such as files This is shared, and threads that use these common resources must be synchronized. WIN32 provides several ways to synchronize resources, including signals, critical sections, events, and mutexes. the exclusive resources areA. Stack is exclusiveB. Register this may be misunderstood, because the computer registers are physical, each thread to fetch value is not the same? In fact, the thread is stored in a copy, including the program counter PC
three states and transitions of the process

The process constantly changes its running state while it is running. Typically, a running process must have the following three basic states.

Ready status

When a process is assigned to all the necessary resources except the CPU, the process state is called the ready state as long as the processor is available for immediate execution.

Execution (Running) status
When a process has been acquired by a processor, its program is executing on the processing machine, at which point the process state is called the execution state.

blocking (Blocked) state
A process being executed that is blocked by a processor while waiting for an event to occur cannot be executed. There can be a number of events that cause a process to block, such as waiting for I/O to complete, requesting a buffer not satisfying, waiting for a letter (signal), etc.

                                                  1: Ready to execute, the current running process is blocked, the scheduler selected one of the highest priority process possession processor;
2: Execute, ready, the current running process time slices run out;
3: Execute, block, the current running process waiting for keyboard input, entered the sleep state.
4: Blocking-ready, I/O operation complete, wake-up by interrupt handler.


The execution state of the process is divided into: nuclear mentality and user state, the main difference between the two is whether the process can acquire all the resources of the computer (the kernel mentality can be, the user state is limited). Everything involved in the fundamental operation of the computer should be executed in the kernel state, and the interruption, clock date, storage image diagram are system-level resources, the modification of these resources must be in the nuclear mentality, but read there is no mandatory requirements.
process consists of a process with a PCB (Process control block), a program segment, and a set of data structures that the program segment operates on.
required steps to create a process:1. Application blank PCB (Process Control block)2. Assigning resources to new processes3. Initializing the PCB4. Insert a new process into the ready queue

the main differences between processes on threads: The main difference between processes and threads is that they are different ways to manage operating system resources. The process has a separate address space, and after a process crashes, it does not affect other processes in protected mode, and the thread is just a different execution path in a process. Thread has its own stack and local variables, but there is no separate address space between the threads, a thread dead is equal to the entire process dead, so the multi-process program is more robust than multithreaded programs, but in the process of switching, the cost of large resources, efficiency is worse. But for some concurrent operations that require simultaneous and shared variables, only threads can be used, and processes cannot be used.

the differences between threads and processes are linked:1, Process: A child process is a replica of the parent process. The child process obtains a copy of the parent process data space, heap, and stack. 2, Thread: Relative to the process, a thread is a concept that is closer to the execution body, which can share data with other threads of the process, but has its own stack space and has a separate execution sequence.
Both can improve the concurrency of the program, improve the efficiency of program operation and response time.
threads and processes have advantages and disadvantages in use: the overhead of thread execution is small, but not for resource management and protection, while the process is the opposite. At the same time, threads are suitable for running on SMP machines, while processes can be migrated across machines. The fundamental difference is one thing: with multiple processes each process has its own address space, and the thread shares the address space. All the other differences come from this:
1. Speed: threads produce faster, faster communication between threads, faster switching, and so on, because they are in the same address space.
2. Resource utilization: The resource utilization of threads is better because they are in the same address space.
3. Synchronization issues: Whether threads need to use a synchronization mechanism when using public variables/memory or because they are within the same address space 。

The relationship and difference between threads and processes

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.