Multiple-process and thread-differentiated portions of thread inheritance

Source: Internet
Author: User
Tags session id

1, first to clarify the meaning of the process and thread:

Process is a program with a certain independent function on a data set on a running activity, is the system for resource allocation and scheduling of an independent unit . Compared with a program, a program is just an ordered set of instructions, which itself does not have any meaning to run, but a static entity. A process is the execution of a program on a dataset and is a dynamic entity. It is created because it is scheduled to run, waiting for a resource or event to be in the waiting state, because the completion of the task is undone, reflecting a program on a certain set of data run on the entire dynamic process.

Each program that is running on the system is a process. Each process contains one or more threads. A process can also be a dynamic execution of an entire program or part of a program. A thread is a set of instructions, or a special section of a program that can be executed independently in a program. It can also be understood as the context in which the code runs. So a thread is basically a lightweight process that is responsible for multitasking in a single program. Typically, the operating system is responsible for scheduling and executing multiple threads.

Multithreading is to accomplish many tasks synchronously, not to improve the efficiency of the operation, but to improve the efficiency of the use of resources to improve the efficiency of the system. Threads are implemented at the same time when multiple tasks need to be completed.

The benefits of using threading include the following:

A) The use of threads can put tasks that occupy a long period of time in the background to handle

b) User interface can be more attractive, such as the user clicked a button to trigger the processing of certain events, you can pop up a progress bar to show the progress of processing

c) The program may run faster

d) threads are useful in some waiting tasks such as user input, file read and write, and network sending and receiving data. In this case we can release some precious resources such as memory footprint and so on.

2, next look at the relationship between the thread and the process

Threads are part of a process, threads run in process space, threads generated by the same process share the same memory space, and threads generated by the process are forced to exit and clear when the process exits. Threads can share all the resources owned by a process with other threads that belong to the same process, but they do not inherently have system resources and have only a bit of information that is essential in the run (such as program counters, a set of registers, and stacks).

3. Then we'll look at the comparison between the thread and the process.

The child process inherits the properties of the parent process:

Child threads inherit the properties of the main thread:

actual user ID, actual group ID, valid user ID, valid group ID,

session ID;

control terminal;

set User ID flag and set group ID flag;

signal shielding and arrangement;

environment;

storage map;

Resource limit;

executable program text;

;

 

Differences between parent and child processes:

Sub-line thread:

The return value of the fork (=0 subprocess);

The process ID is different;

Two processes have a different parent process ID;

The Tms_utime,tms_stime,tms_cutime and tms_ustime of the child process are set to 0;

File locks that do not inherit the parent process settings;

The unhandled alarm of the child process is cleared;

The unhandled signal set of the child process is set to the empty sets;

Thread ID;

A set of register values;

Stack

scheduling priorities and policies;

signal shielding word;

errno variable;

Thread-private data;

3, design considerations of the use of skills

1. Try to avoid long-standing memory processes, such as those that are seldom used, or have a long periodicity (more than 10 minutes), and extract their functions into a small application. Pull them up when needed (such as through crontab configuration, or direct system).

2. The combination of designing the target into a sub-functional system can improve ease of use and maintainability of reuse.
The target is divided into different subsystems according to the function, and the subsystem follows the specific protocol (text or XML), which is connected by the communication and accomplishes the goal together.

In other words, we can consider the following when we make the design:

1, the creation of threads and the communication and synchronization between threads are faster than the process. Task splitting on multicore CPUs is for threading, not process.

2, if you do not need frequent creation and destruction of the efficiency of execution is not much, need to create frequently, thread fast.

3, the other is based on your actual situation selected, if there is no data communication or anything, the communication between the threads is more convenient than the process. The most critical point is that multithreading allows different parts of the same program to execute concurrently.

So in the security system, the alarm system and monitoring system can be used in a multi-process to do, for the alarm system can be multi-threaded to achieve if an accident, you can send a message to the user, while the whistle, and if it is a fire, you can open the valve and so on.

1) You need to frequently create priority threads for destruction.
Instance: Web server. To build a thread, break it and destroy the thread. If the process is used, the cost of creating and destroying it is hard to bear.
2) Priority usage threads that require a lot of computation.
The so-called large number of calculations, of course, is to consume a lot of CPU, switching frequently, this situation first thread is the most suitable.
Example: image processing, algorithm processing
3) Strong related processing with threads, if related processing with process.
What is strong correlation and weak correlation? It is difficult to define in theory, and to give a simple example to understand.
The general server needs to do the following tasks: Messaging and message processing. Messaging and message processing are weak related tasks, and message processing may be divided into message decoding, business processing, these two tasks are relatively more relevant. So messaging and message processing can be divided into process design, message decoding and business processing can be divided into threading design.
4) can be extended to multi-machine distributed with processes, multi-core distributed with threads.
5) Meet the needs of the situation, with the most familiar with you, the most adept way.

As for the "data sharing, synchronization", "Programming, debugging," "Reliability," the dimensions of the so-called "complex, simple" should be how to choose, can only say: There is no clear method of choice. There is a general principle of choice: if multiple processes and multithreading can meet the requirements, then choose the one you are most familiar with.

http://kenby.iteye.com/blog/1014039

Multiple-process and thread-differentiated portions of thread inheritance

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.