Process, thread, and memory management are the most basic services of the kernel and the main components of the kernel. These aspects are the basic knowledge that a software developer must possess. Although a person does not understand this knowledge, he can also write simple Program But such a program can only be regarded as a fur. With the knowledge of processes, threads, and memory management, you can make full use of the services provided by the operating system kernel to improve the execution efficiency, save resources, and improve robustness of the software you write. By the way, you can run visual studio.net under Windows ce.net. NET platform, but such software is the top software, too far away from the operating system kernel. Not only is the execution efficiency relatively low, but also the. NET Framework must be added to the kernel. So in most cases, EVC is still the first choice.
Figure 1 ce kernel structure
I. Processes and threads
1. concept:
Windows CE. Net is a multi-task preemption operating system, which is also called scheduling. During the scheduling process, the scheduling system of the kernel contains a list of priorities of all threads in the current process, and all threads are sorted by priority. When an interruption occurs, the scheduling system reschedule the arrangement of all threads.
A process is an instance of a running application. It consists of two parts: one is the kernel object used by the operating system to manage the process. The other is the address space owned by the process. This address space contains the application'sCodeSegment, static data segment, heap, stack, non-xip (execute in place) DLL. From the execution perspective, a process is composed of one or more threads. A thread is an execution unit that controls a segment of code in the CPU execution process. A thread can access all the address spaces and resources in the process. A process contains at least one thread to execute code. This thread is also called the main thread.
2. Process:
Windows CE. Net supports a maximum of 32 processes running simultaneously. This is determined by the total address space allocated by the entire system to all processes. Earlier than Windows CE 4.0 (that is, earlier. the total process space in the CE operating system is from 0x0000 0000 to 0x4200 0000. Every 32 MB of address space is a slot (slot) with 33 slots in total. When a process starts, the kernel selects an unused slot as the address space of the process. The slot 0x0000 0000 to 0x01ff FFFF is called slot 0. Each process maps the entire address to slot 0 when it is about to obtain CPU control. This process is called the current running process (currently running process) in the help document ). After a slot is allocated, the kernel allocates sufficient address space for code segments and static data segments in the order from low addresses to high addresses in this slot, and then heap and stack, the space after the stack is retained for all DLL files, including xip and non-xip DLL files. Note that 64kb at the bottom of slot 0 is always retained. From slot 1 to slot32 is used by the process. The first few slots are generally used by system programs. For example, filesys.exe?device.exe=gwes.exe.
Windows CE. NET and earlier operating systems are slightly different. This is found in the Article of "technical articles" and "Knowledge Base" in msdn, this really made me a lot of effort. In Windows CE. NET Help files can only find the same statement as earlier versions, while several articles in "technical articles" and "Knowledge Base" clearly illustrate Windows CE.. NET and earlier operating systems. In earlier versions of the operating system, as mentioned above, there are 33 slots. slot 0 is used for the current running process and supports 32 processes to run simultaneously. In addition, all DLL files are loaded to the address space of the process. However, slot 1 in Windows CE. NET is also used in the current process (slot 1 is used only to load all xip DLL ). A process does not occupy 32 MB of address space, but 64 MB. I will explain the memory management in detail.
the API function for creating a process is as follows:
Bool CreateProcess (maid, maid directory, lpstartupinfo, lpprocess_information lpprocessinformation );
Windows CE. Net does not support security, current directory, and inheritance. Therefore, many parameters of this function must be set to 0 or false. Set 3rd, 4, 7, 8, and 9 to 0, and 5th to false. 1st the parameter is the application name. This parameter cannot be null. If you only pass the application name without specifying the path, the system will first search for the \ Windows directory and then the search path specified by the OEM. The 2nd parameter is used to pass the startup parameter and must be a unicode code. 6th the parameter is the creation flag. It can be 0 (create a regular process), create_suincluded (suspended after startup), debug_process (used to create the parent process for debugging), debug_only_this_process (do not debug the child process), create_new_console (console process ). 10th the parameter is passed to it the address of the process_information structure variable. Return the handle and ID of the process and main thread.
It is best to terminate a process that is returned by the winmain function. You can also call the exitthread function in the main thread. Use the terminateprocess function when the current process terminates another process. The terminateprocess function under CE is more powerful than the terminateprocess function in other Windows systems. When the terminateprocess function in CE causes the process to exit, it notifies each loaded DLL and makes all the processing work that should be done when the process exits.
3. Thread:
In addition to being able to access process resources, each thread also has its own stack. The stack size can be adjusted. The minimum size is 1 kb or 4 kb (that is, a memory page. Memory Page size depends on the CPU). Generally, the default value is 64 KB, but the top of the stack is always 2 kb to prevent overflow. If you want to change the initial size of the stack, specify the size in the parameter after the EVC "project"-"Settings"-"Link" link option "/stack. Parameter 1 is the default size, and parameter 2 is the memory page size, expressed in hexadecimal format. If the initial value of the stack is set too small, it is easy to cause illegal access to the system and terminate the process immediately.
The thread is in the running, suspended, sleep, blocked, and terminated state. When all threads are in the blocking status and the kernel is in the idle mode, the CPU power supply will be reduced.
The API function for creating a thread is as follows:
Handle createthread (lpsecurity_attributes lpthreadattributes, DWORD dwstacksize, lpthread_start_routine lpstartaddress, lpvoid lpparameter, DWORD dwcreationflags, lpdword lpthreadid );
Windows CE. Net does not support security. Therefore, parameter 1 must be set to 0. If parameter 5 is stack_size_param_is_a_reservation, parameter 2 can specify the stack size. The kernel retains the address space for the stack owned by this thread based on the value of parameter 2. If parameter 5 is not stack_size_param_is_a_reservation, parameter 2 must be set to 0. Parameter 3 is the first address of the execution path, that is, the address of the function. Parameter 4 is used to pass a parameter to the thread. In addition to the preceding descriptions, parameter 5 can also be 0 or create_suincluded. Create_suincluded indicates that the thread has been suspended until it is restored using the resumethread function. The last parameter stores the ID of the created thread returned by the function.
there are similar methods to exit a thread and exit a process. It is best to return the result by a function. You can also call the exitthead function in the thread. Terminate another thread in the current thread and use the terminatethread function. When this function causes a thread to exit, it will notify all DLL loaded by this thread. In this way, the DLL can end the work.
unlike other Windows operating systems, Windows CE. Net classifies processes into different priority classes. Windows CE. Net only classifies threads into 256 priority classes. The highest priority is 0, and the lowest priority is 255. The priority from 0 to 248 is the real-time priority. Priority from 0 to 247 is generally assigned to real-time applications, drivers, and system programs. Among the 249-255 priorities, the 251 priority (thread_priority_normal) is the normal priority. 255 priority (thread_priority_idle) is the idle priority. 249 priority (thread_priority_highest) is a high priority. Priority from 248 to 255 is generally assigned to common application threads. The specific sections are shown in the following table:
priority range |
assign objects |
0-96 |
Programs higher than the driver |
97-152 |
Windows CE-based Drivers |
153-247 |
Programs lower than the driver |
248-255 |
common applications |
the Windows CE. Net Operating System is real-time. Therefore, the scheduling system must ensure that high-priority threads run first. low-priority threads can obtain CPU time slices after high-priority threads are terminated or blocked. In addition, in the event of interruption, the kernel will pause the running of low-priority threads, so that high-priority threads can continue to run until they are terminated or blocked. Threads with the same priority occupy the CPU time slice on average. When a thread uses the CPU time slice or blocks or sleeps in the time slice, other threads with the same priority occupy the time slice. The CPU time slice mentioned here refers to the time when the kernel limits the CPU occupation by the thread. The default value is 100 ms. The OEM can change this value or even set it to 0. If it is 0, the current thread will occupy the CPU until the higher-priority thread requires the CPU. This scheduling algorithm seems to be very effective and perfect, but there is a situation where the program will be deadlocked when such a situation occurs. For example, an application contains two threads, thread 1 is of high priority, and thread 2 is of low priority. When thread 1 is blocked during running, thread 2 obtains the time slice, thread 2 enters a critical section this time. We all know that resources in the critical section will not be accessed by other threads. When thread 2 is running, thread 1 has changed from blocking to running, thread 1 needs to access the resource of thread 2, but this resource is locked by the critical section, so thread 1 can only wait, waiting for the exclusive permission of thread 2 to run from the critical section and release the resource. But thread 2 never gets a time slice, because Ce ensures that high-priority threads will run first. Then the program will be in a deadlock state. Of course, the system will not be deadlocked, because there are more priority threads and drivers running. In this case, Ce adopts the priority conversion method. In this case, the kernel increases the priority of thread 2 to the priority level of thread 1. In this way, thread 2 can execute the code in the critical section, and thread 1 can access resources. Then the kernel resumes the original priority of thread 2.
suspend a thread and use the suspendthread function. There is only one parameter-the thread handle. It should be noted that if the thread to be suspended is calling a kernel function, executing this function may fail. You need to call this function multiple times until the return value of the function is not 0 xffffffff. This indicates that the function is suspended successfully. The restoration thread uses the resumethread function. There is only one parameter-the thread handle.
the local thread memory and fiber path are rarely used. For more information, see Windows core programming.