When a program begins to execute, the portion of memory that is in memory from the beginning of execution to the completion of execution is called a process.

Linux is a multitasking operating system, that is, at the same time, there can be multiple processes executing at the same time. The single-CPU computer we all commonly use can only execute one instruction in a time segment. So how does Linux implement multi-process execution at the same time? Linux originally used a method called "process scheduling". First, each process was assigned a certain amount of running time. This time was usually very short, as short as milliseconds, and then according to some rules, from many processes. One of them is put into operation, and the others are waiting for a while. When the process is running out of time, or it exits after execution, or it is paused for some reason, Linux will reschedule and pick a process to run because each process is occupied. The time segments are very short, from the perspective of the user, as if multiple processes are running at the same time.

In Linux, each process is assigned a data structure, called a process control block (PCB). The PCB contains a lot of important information for system scheduling and process execution. The most important one is the ID of the process. The ID of the process is also called the process identifier. It is a non-negative integer, and it is operability in Linux. The only flag in the system is a process. In the most commonly used I386 architecture, a non-negative integer value is 0~32767, which is also the process ID we can get. It is the process ID number.

Zombie process

Zombie processes are already finished processes but have not been removed from the process table. Too many zombie processes cause the entries in the process table to become full, which in turn causes the system to crash. It does not occupy system resources.

In the state of the process, the zombie process is very special, it has given up almost all the memory space, there is no executable code, can not be scheduled, just keep a location in the process list, record the process Exit status and other information for other processes to collect, in addition, the zombie process no longer takes up any memory space, it needs its parent process to receive it, if the parent process did not install the SIGCHLD signal handler call wait or waitpid () wait The end of the child process, but did not show that ignore the signal, 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, collect it for him, and he can still be cleared. However, if the parent process is a loop and does not end, then the child process is always in a zombie state.

The reason for the zombie process:

Each Linux process has an entry point in the process table. All information used by the core program to execute the process is stored at the entry point. When you use the ps command to view the process information in the system, you see the relevant data in the process table. After the fork system call establishes a new process, the core process will assign an entry point to the new process in the process table, and then store the relevant information in the process table corresponding to the entry point. One of these items is The parent process ID. When this process finishes its life cycle, it will execute the exit() system call. At this time, the data in the original process table will be replaced by the process exit code, the CPU time used during execution, and other data. These data will be It remains until the system passes it to its parent process. It can be seen that the emergence time of the zombie process is after the subroutine terminates, but before the parent process has read the data.

How to avoid zombie process

1. The parent process waits for the child process to end with wait and waitpid functions, which will cause the parent process to hang. 2. If the parent process is busy, you can use the signal function to install the handler for SIGCHLD. Since the child process ends, the parent process will receive To this signal, call wait recovery in the handler. 3, if the parent process does not care about when the child process ends, then you can use "singal (SIGCHLD), SIG_IGN" to inform the kernel that he is not interested in the end of the child process, then after the end of the child process, the kernel will recover, and will no longer give The parent process sends a signal. 4, there are some skills, is fork () twice, the parent process fork a child process, and then continue to work, the child process fork a grandchild process exit, then the grandchild process is init to take over, the grandson process is over, init will recover, However, the child process should also do its own recovery.

Process PK thread

Let's first make an analogy. Multi-threading is a multi-threaded cross-platform transportation system at a crossroads. The cost is low, but there are many traffic lights and old traffic jams, and multi-processes are overpasses. Although the cost is high and the fuel consumption is high, there is no traffic jam. This is an abstract concept. I believe we will have this feeling after reading.

Processes and threads are two relative concepts. In general, a process can define an instance of a program. In Win32, the process does not perform anything, it just occupies the address space used by the application. In order for a process to perform a certain amount of work, a process must occupy at least one thread. It is this thread that is responsible for the code contained in the address space of the process. In fact, a process can contain several threads that can execute code in the process address space at the same time. To do this, each thread has its own set of CPU registers and stacks. At least one thread in each process is executing code in its address space. If there is no thread executing the code in the process address space, the process will not continue to exist for reasons, the system will automatically clear the process and its address space.

The principle of multi-threaded implementation

When a process is created, its first thread, called the primary thread, is automatically generated by the system. Additional threads can then be generated by this main thread, which in turn can generate more threads. When running a multithreaded program, on the surface, these threads seem to be running at the same time. The actual situation is not the case, in order to run all of these threads, the operating system schedules some CPU time for each individual thread. The single-CPU operating system provides quantums to threads in a time slice rotation manner. Each thread delivers control after the time slice is used, and the system allocates the CPU time slice to the next thread. Since each time slice is short enough, this gives an illusion that these threads are running at the same time. The only purpose of creating extra threads is to use CPU time as much as possible.

Multithreading issues

The use of multi-threaded programming can give programmers a lot of flexibility, but it also makes it easier to solve problems that previously required complicated techniques. However, it should not artificially divide the written program into pieces that will be executed by the respective threads. This is not the right way to develop applications. Threads are useful, but when using threads, new problems may arise while solving old problems. For example, to develop a word processing program and want the printing function to execute itself as a separate thread. This sounds like a good idea, because when printing, the user can immediately return and start editing the document. But in this way, the data in the document may be modified when the document is printed, and the result of the printing is no longer the desired content. Perhaps it's best not to put the print function in a separate thread, but if you must use multi-threaded, you can also consider using the following method to solve: The first method is to lock the document being printed, let the user edit other documents, This way, the document will not be modified before printing is finished; another method may be more effective. That is, you can copy the document into a temporary file, print the contents of this temporary file, and allow the user to modify the original document. . When the temporary file containing the document is printed, delete the temporary file. From the analysis above, it can be seen that multi-threading may also help bring about new problems while helping to solve problems. Therefore, it is necessary to figure out when it is necessary to create multi-threading and when it does not require multi-threading. In general, multi-threading is often used in front-end operations while also requiring background calculations or logical decisions.

Classification of threads

In MFC, threads are divided into two categories, worker threads and user interface threads. If a thread only performs background calculations and does not need to interact with the user, then worker threads can be used; if it is necessary to create a thread that handles the user interface, user interface threads should be used. The main difference between the two is that the MFC framework will add a message loop to the user interface thread so that the user interface thread can process messages in its own message queue. It seems that if you need to do some simple calculations in the background (such as the recalculation of the spreadsheet), you should first consider the use of worker threads, and when the background thread needs to deal with more complex tasks, specifically, when the background thread When the execution process changes with actual conditions, user interface threads should be used to respond to different messages.

The priority of the thread

When the system needs to execute multiple processes or multiple threads at the same time, it sometimes needs to specify the priority of the threads. The priority of a thread generally refers to the base priority of this thread, that is, the combination of the thread's relative priority with respect to this process and the priority of the process containing this thread. The operating system schedules all active threads on a priority basis. Each thread of the system is assigned a priority. The priority ranges from 0 to 31. At runtime, the system simply allocates CPU time to the first priority 31 thread. After the thread's time slice ends, the system allocates CPU time to the next priority 31 thread. When there is no thread with priority 31, the system will begin to allocate CPU time to the thread with priority 30, and so on. In addition to the programmer changing the thread's priority in the program, sometimes the system will automatically change the thread's priority during the execution of the program. This is to ensure that the system is highly responsive to end users. For example, when the user presses a key on the keyboard, the system temporarily increases the priority of the thread that processes the WM_KEYDOWN message by 2 to 3. The CPU executes the thread in a complete time slice. After the time slice is executed, the system decrements the priority of the thread.

Synchronization of threads

When using multithreaded programming, there is also a very important issue is thread synchronization. Thread synchronization refers to the ability of threads to avoid corrupting their own data when communicating with each other. The synchronization problem is caused by the CPU time slice allocation method of the Win32 system mentioned above. Although at a certain moment, only one thread occupies the CPU (single CPU time), there is no way to know when and where the thread is interrupted. It is very important to ensure that the threads do not destroy each other's data. In MFC, you can use 4 synchronization objects to ensure that multiple threads run simultaneously. They are the CCriticalSection, the CMutex, the CS emaphore, and the CEvent. Among these objects, the critical area object is the simplest to use, and its disadvantage is that it can only synchronize threads in the same process. In addition, there is a basic method, this article is called a linearization method, that is, write operations on a certain data in the programming process are completed in a thread. In this way, since the code in the same thread is always executed sequentially, it is impossible to rewrite the data at the same time.

to sum up:

In the thread (relative to the process), the thread is a concept that is closer to the execution body. It can share data with other threads of the same process, but has its own stack space and has an independent execution sequence. Both of these can increase the degree of program concurrency, improve the efficiency of program operation and response time. Threads and processes have their own advantages and disadvantages in use: Threads have low overhead, but they are not conducive to resource management and protection. The process is just the opposite. The fundamental difference is that with multiple processes, each process has its own address space, and the threads share the address space, in terms of speed: the speed of thread generation, communication between threads is fast, and switching is fast because they are in the same address space. Inside. In terms of resource utilization: The thread resource rate is also better because they are in the same address space. In terms of synchronization: When threads use public variables/memory, they need to use the synchronization mechanism because they are in the process in the same address space: the child process is a duplicate of the parent process, and the child process gets the copy of the parent process data space, heap, and stack.

Budget Laptop

Everyone want a budget laptop. There are different level according to application scenarios. 14 inch Budget Laptop For Students for your elementary project, 10.1 inch Low Budget Laptop for kids play or online learning, 15.6inch celeron j4125 Budget Laptop For Programmers, 14inch budget i5 laptop for your business projects, 15.6inch budget i7 laptop for university students, officers who love bigger screen and performance focused, etc. Of course, other type laptops also optional, like Yoga Laptop , 2 In 1 Laptop, android laptop, etc

As a professional manufacture of custom laptop, Android Tablet, Mini PC , All In One PC, we can provide unique and satisfy oem service. What you need to do is kindly share the exact parameters and special points care more, thus we can provide solutions accordingly.

When you have tender, you can contact us and send the parameters list require, then will provide the most matched one for you. More simple way is that you share your budget, design, delivery time , etc. Believe you can always get a right solution here.

Budget Laptop,Budget Laptop For Students,Low Budget Laptop,Top 10 Budget Laptops,Budget Laptop For Programmers

Henan Shuyi Electronics Co., Ltd. , https://www.shuyicustomlaptop.com