Thread-sharing resources between the same processes

Source: Internet
Author: User

Thread-shared environments include:

1. Process Code Snippet

2. The public data of the process (using these shared data, threads can easily communicate with each other)

3. The process opens the file descriptor, the processor of the signal, the current directory of the process and the process user ID and 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
Each thread has its own thread ID, which is unique in this process.    The process uses this to identify the thread. 2. Value of the Register group
Because the threads are running concurrently, each thread has its own different running threads, and when switching from one thread to another, the state of the Register collection of the original thread must be saved so that the thread can be restored in the future when it is re-switched to. 3. The stack of threads
The stack is necessary to ensure that the thread runs independently.
A thread function can call a function, and the called function can be nested in layers, so the thread must have its own stack of functions so that the function call can execute normally, not affected by other threads.
4. Error return code
Since many threads in the same process are running concurrently, it is possible for a thread to set the errno value after a system call, while the thread has not handled the error and another thread is running at this point by the scheduler, so that the error value can be modified.
Therefore, different threads should have their own error return code variable.
5. Thread's Signal Shield code
Because each thread is interested in a different signal, the thread's signal masking code should be managed by the thread itself. But all the threads share the same signal processor.
6. Priority of Threads
Because the thread needs to be dispatched like a process, there must be a parameter available for dispatch, which is the thread's priority. The difference between a thread and a process

The ① process is an independent unit of the system for resource allocation and scheduling. A thread is an entity of a process that is the basic unit of CPU dispatch and dispatch. The thread itself basically does not own system resources, but it can share all the resources owned by the process with other threads that belong to one process. Because the thread is smaller than the process and basically does not have system resources, the thread context switch is much faster than the process context switch, so the overhead of scheduling it is much smaller, thus significantly improving the utilization and throughput of system resources.
② a program has at least one process, and a process has at least one thread. While the process has an independent memory unit address space during execution, multiple threads share memory, which greatly improves the efficiency of the program.
③ interprocess communication IPC requires a special method between threads that can directly read and write process data segments (such as global variables) to communicate.

The process scheduling cost is large, the creation of multiple processes also reduces the response time of the system, the Web server needs to respond to multiple user requests at the same time, so introduced threads to further improve the concurrency of the system.

Each thread has its own stack and program counter for its execution context.

Thread-sharing resources between the same 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.