Summary of knowledge [reprint] Linux system processes, disputes between processes and threads ...

Knowledge summarize Linux system processes, disputes between processes and threads ...

https://cloud.tencent.com/developer/article/1500509

 

When a program starts executing, at the beginning of the implementation of the finished quit this time, it is part of the memory is called is called a process.

Linux is a multitasking operating system, that is, at the same time, you can have multiple processes simultaneously. We used single-CPU computer is actually within a time slice can only execute one instruction. So Linux is to perform multiple processes at the same time how to achieve it? The original Linux using a means called a "process scheduling", first of all, to assign a certain running time for each process, this time is usually very short, as short as milliseconds and follow certain rules, from a number of processes in a pick put into operation, wait for other processes temporarily, when that time is running process is exhausted, finished or quit, or pause for any reason, Linux will be rescheduled, the selection of a process put into operation, because each process takes the time is very short clip, from the user's point of view, if multiple processes running simultaneously the same.

In Linux, each process is assigned a data structure is created at the time, called a process control block (PCB). PCB contains a lot of important information for the system operator the ability to perform the process and used, the most important is the ID process ID, the process is also known as the process identifier is a non-negative integer, the operability Linux the only sign of a system process. On I386 architecture most commonly used, a non-negative integer in the range of 0 to 32767, and this is what we might take to process ID, it is the process ID number.

Produce zombie process

Zombie process that has ended the process but has not been removed from the process table. Too many zombie process will lead to full entry process table inside, leading to a system crash, it touches not take up system resources.

In the state of the process, the zombie process is a very special one, it has given up almost all of the memory space, there is no executable code, can not be scheduled, retained only in the process list as a location, description of the process exit status and other information for other processes to collect, in addition to zombie processes not take up any memory space, it needs its parent to give it to remove it, if the parent is not installed SIGCHLD signal processing function calls wait or waitpid () waits son end of the process, and no display of the signal is ignored, then it has been in a zombie state. If the parent process is over, then the init process will automatically take over the child process for it to remove it, he was able to be cleared. However, if the parent process is a cycle, not the end, then the child has been in a zombie state.

The reason zombie process produced:

Each Linux process in the process table has an entry point (Entry), the core of the program to use in the implementation of the process all the information is stored in the entry point. When using the ps command to view the system processes information, see is the process table data. When the fork system call creates a new process, core process will be in the process table for the process to allocate a new entry point, then the process table information stored in the corresponding entry point, this information is in there is a ID of the parent process. When this process has completed its life cycle, it will execute exit () system call, then the original data in the process table will be replaced by the exit code of the process, the implementation of the use of CPU time and other data that will the system has been retained to pass it up to its parent process. Thus, after the time of occurrence zombie really subroutine is terminated, but the parent process has not yet before the data is read.

How to avoid zombies

1, the parent process by wait and wait waitpid functions such as child process ends, which causes the parent process hangs 2, if the parent is busy, you can install a signal handler function for the SIGCHLD, because after the end of the child, the parent will receive the signal to be recovered in the handler call wait. 3, if the parent does not care about the child when to stop, you can use notice "singal (SIGCHLD), SIG_IGN" core, end their child process is not interested, then the child after the end of the process, the kernel will recover, does not give parent process to send a signal. 4, there are some tips that fork () twice, the parent process fork a child process, and then continue to work, fork child process exits after a grandchild, the grandchild is init takeover, after the end of the grandchild, init will recover, but even their own sub-process recycling.

PK thread process

Let's analogy, multi-threaded multi-threaded is a crossroads plane traffic system, low cost, but the traffic lights, traffic jams and old, and is a multi-process is the overpass, although the high cost, fuel consumption and more on the downhill, but no traffic jams. This is an abstract concept. I believe we will be watching this feeling.

Processes and threads are two opposing concepts, generally speaking, a process can be defined in a program instance (Instance). In Win32, the process does not execute anything, it just occupies the address space used by the application. In order for the process to complete certain work process must occupy at least one thread, it is this thread that contains the code responsible for the process address space. In fact, a process can contain several threads that can execute code in the process's address space at the same time. To do this, each thread has its own set of CPU registers and stack. Each process has at least one thread of code execution in its address space. If no threads executing code in the process address space, there is no reason to continue the process, the system will automatically clear the process and its address space.

The principle of multi-threaded

When you create a process that is known as the first thread main thread (Primary thread), automatically generated by the system. You can then thread to create additional threads from the main line, and these threads, and can generate more threads. When you run a multithreaded program, on the surface, these threads seem to run concurrently. And is not the case, in order to run all of these threads, the operating system to arrange some CPU time for each individual thread. Single-CPU operating systems provide a round-robin manner to the thread time slice (the Quantum), each thread After surrender control time slice, then the system CPU time slice assigned to the next thread. Because each time slice is short enough, so giving a false impression, as if these threads to run simultaneously. The sole purpose of creating additional thread is the use of CPU time as possible.

Multithreading problem

Multi-threaded programming can bring a great deal of flexibility to the programmer, but also make the problem requires complex skills in order to solve the original becomes easier. However, it should not be artificially divided into a number of programs written in fragments, so that these pieces executed in its own thread, this is not the right way to develop applications. Thread is useful, but when using threads, may cause new problems while solving old problems. For example, to develop a word-processing program, and want to print their own function as a separate thread execution. That sounds like a good idea because at the time of printing, the user can return immediately begin editing the document. But in that case, the data in the document is likely to be modified when the document is printed, the printed results are no longer desired content. Perhaps it is best not to print function in a separate thread, but if we use multithreading, you can also consider using the following solutions: The first method is to lock the document is printing, allowing users to edit a document, so before the end of print, the document does not make any changes; another approach might be more effective, that the document can be copied to a temporary file, print the contents of the temporary file, while allowing the user to make changes to the original document . When the temporary file containing the document printing is complete, then delete the temporary file. As can be seen by the above analysis, multi-threading may also bring new problems to help solve problems at the same time. It is necessary to figure out what time you need to create multi-threaded, multi-threaded when they are not. In general, multi-threading is often used where the need for further logic to determine or calculate the background while the foreground operation.

Classification threads

In MFC, the thread is divided into two categories, namely worker threads and user interface thread. If a thread is only complete background calculation, and does not require user interaction, you can use worker threads; if you need to create a thread to handle user interface, you should use the user interface thread. The main difference between the two is that, the MFC will UI frame a message thread loop increases, so the user interface thread can process its own message queue for messages. So, if you need the background to make some simple calculations (such as a spreadsheet recalculation), you should first consider the use of worker threads, and when the background thread needs to handle more complex tasks, to be precise, when the background thread the implementation process will vary with the actual situation of different, you should use the user interface thread, in order to be able to respond to different messages.

Thread priority

When the system needs to perform multiple processes or multiple threads simultaneously, sometimes it takes the priority of the specified thread. Priority thread group generally refers to the priority of the thread, i.e., binding thread priority relative priority with respect to the process and containing the thread of the present process. Operating system in order to arrange for the priority basis for all active threads, each thread in the system is assigned a priority, the priority range from 0-31. Run, the system simply assign priority to the first thread 31 of the CPU time, after the end of the thread time slice, the system to the next priority thread 31 of the CPU time allocation. When there is no priority thread 31, the system will begin to assign priority thread 30 of CPU time, and so was. In addition the programmer to change the priority of the thread in the program, the program may also automatically dynamically change the priority of a thread during execution, which is to ensure highly responsive to end-user system. For example, when the user presses a key on the keyboard, the system will temporarily WM_KEYDOWN message processing thread priority to improve 2-3. CPU by a complete execution thread time slice, the time slice when executed, the thread priority system minus 1.

Thread synchronization

When using multi-threaded programming, there is a very important issue is the thread synchronization. The so-called thread synchronization between threads refers to the ability to avoid damage when the respective data communicate with each other. Synchronization problems are caused by the allocation of CPU time slice said before Win32 systems. Although at a time, only one thread occupy CPU (single CPU) time, but there is no way to know at what time, what place is interrupted thread, so how to ensure that does not destroy each other's data between threads is particularly important. In MFC, you can use four synchronization objects to ensure multiple threads to run simultaneously. They are a critical section object (CCriticalSection), mutex objects (CMutex), semaphore object (CS emaphore) objects and events (CEvent). In these subjects, a critical section object easiest to use, it's only drawback is synchronized with a thread in a process. Further, there is a basic approach, linearization method referred to herein, i.e. a certain data write operation are completed in a thread during programming. Thus, since the same thread code is always executed in sequence, it is impossible to rewrite the data at the same time the situation occurred.

to sum up:

In the thread (as opposed to in terms of the process), the thread is a concept closer to the implementation body, it can share data with other threads in the same process, but has its own stack space, an independent execution sequence. Both of which can improve concurrency procedures, improve efficiency and response time of the run. Threads and processes have advantages and disadvantages in the use: small thread execution overhead, but not conducive to resource management and conservation; and the process is reversed. The fundamental difference is this: there are multiple processes with each process its own address space, the thread is a shared address space, in terms of speed: fast thread produced fast communication between threads, fast switching, etc., because they are in the same address space Inside. In terms of utilization of resources: Resources of the thread is better is because they are in the same address space. In terms of synchronization: Thread public variables / synchronization mechanism is required when memory, because they are within the same address space of the process: the child process is a copy of the parent process, the child process the parent process to obtain data space, heap and stack copies.

* Disclaimer: push content and images from the network, will be part of the changes, belongs to original author, as the source of information is incorrect or the rights, please contact us, or delete the authorization.

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11645798.html