Processes and threads in Windows CE

Source: Internet
Author: User

(1) Process
A process is an instance of a running application. It consists of two parts: a, the kernel object for managing the operating system of the process, and B, the address space of the process. Each process has an exclusive, protected 32 MB address space. Windows CE allows up to 32 independent processes to be executed simultaneously. When the system starts, at least four default processes are started, namely, drivers (drivers for device loading and management ). The following are a series of functions:

A. Create a process.

Bool creatprocess (lpctstr lpapplicationname,

Lpctstr lpcommandline,

Lpsecurity_attribustes lpprocessattributes,

Lpsecurity_arrtibustes lpthreadattributes,

Bool binherithandles,

DWORD dwcreationflags,

Lpvoid lpenviromment,

Maid directory,

Lpstartupinfo,

Lppprocess_informationLpprocessinformation );

Because Windows CE does not support security, inheritance, and the current directory, many parameters of this API must be set to 0 or false, that is:

Bool creatprocess (lpctstr lpapplicationname, // Application name

Lptstr lpcommandline, // used to pass startup parameters, which can only be UNICODE

Null, null, false,

DWORD dwcreationflags, // Process status flag

Null, null, null,

Lppprocess_information lpprocessinformation); // The variable address that passes an lppprocess_information Structure

The lppprocess_information struct is defined:

Typedef struct _ process_information {

Handle hprocess;

Handle hthread;

Handle dwprocessid;

Handle dwthreadid;

} Process_information

In the structure, the first two parameters hprocess and hthread are the new process handle and the master thread handle in the new process respectively. Dwprocessid and dwthreadid are the ID of the process and the ID of the main thread in the process.

B. Exit the process.
Void exitprocess (uint uexitcode); uexitcode indicates the exit code. This function can completely disable a process, that is, it will notify all the dynamic link libraries (DLL) associated with the process of the message terminated by the process ). You can use the following function to obtain the exit code of the specified process:

Bool getexitcodeprocess (handle hprocess, lpdword lpexitcode );

When a process is running, its exit code is stili_active. When a process is successfully terminated, it may be terminated in the following three States:

1. Exit code specified by the exitprocess or terminateprocess function; 2. Return Value in the mian or winmain function; 3. Code that causes abnormal Process Termination.

If you want to terminate another process in the current process, you can use the following function:

Bool terminateprocess (handle hprocess, DWORD uexitcode); hprocess is the handle; uexitcode is the exit code.

It should be noted that using the terminateprocess function will terminate the relevant process unconditionally, and all threads in the process will also be terminated. At the same time, the message of Process Termination is also sent to the DLL file related to the process.

C. Obtain the process ID.

When terminating a process, we need to know the handle of the corresponding process. In other cases, we also need to know the handle of some processes. The following functions can be used:

Handle OpenProcess (DWORD dwdesiredaccess, // set to 0 in Windows CE

Bool binherithandle, // set to false in Windows CE

DWORD dwprocessid) // indicates the ID of the process to be opened

In Windows CE, we can also use another function to obtain the window handle and obtain the id value of the corresponding process. For example:

Getwindowthreadprocessid (hwnd, lpdword lpdwprocessid );

(2) threads

In addition to some resources (such as registers and stacks) that are essential for running, threads themselves do not occupy system resources. However, a thread can share all resources of a process with another thread of the same process. Like a process, one thread can create or withdraw another thread, and the threads in the same process can also execute concurrently.

Each process contains a main thread. Through certain functions, many threads can be created in a process, the number of threads created in Windows CE is only affected by the size of the memory space and the size of the thread stack space. After Windows CE 3.0, the priority supported by the system increases to 256, with the highest priority of 0 and the lowest priority of 255. Where:

0-96: programs with a higher priority than the driver;

97-152: A Driver Based on Windows CE. net;

153-247: programs with a lower priority than the driver;

248-255: common application.

In Windows CE, threads are generally divided into five states: running status (running), suspended (suspended), sleeping (blocking), and terminated ). When all threads in the system are in the blocking state, the system kernel is in the idle model, and the CPU usage will be reduced. You know, for most Windows CE devices powered by batteries, it is crucial for the amount of power resources occupied. Here are a series of functions:

A. Set the thread priority:

Bool cesetthreadpriority (handle hthread, int npriority );

B. Priority of the query thread:

Int cegetthreadpriority (handle hthread );

(3) inter-process communication

Windows CE does not provide complex communication mechanisms such as messages and ports, but provides several basic communication methods for waiting for queues. Such as event objects, mutex, and critical section. These are all implemented in the form of system objects. System Objects are resource entities managed by the system. The system records and tracks their statuses through the list.

A, semaphores

In Windows CE, when a thread is waiting for a semaphore, the thread is in the blocked state, which continues until the available resources are greater than 0. The maximum value of available resources is specified when the semaphore is created. Therefore, we can use the semaphore to specify the number of threads accessing the resource.

Handle createsemaphore (lpsecurity_attributes l1_maphoreattributes, // null

Long linitialcount, // the initial value of the currently available resource

Long lmaximumcount, // maximum number of available resources

Lptstr lpname); // Object Name

After a thread has accessed available resources, the semaphore is released. The function for releasing semaphores is:

Bool releasesemaphor (handle hsemaphore, // handle of the object

Long lreleasecount, // number of resources to be released

Lplong lppreviouscount); // returns the original number of available resources.

After a thread has accessed available resources, it must use the releasesemaphor function to release the semaphore to increase the number of currently available resources.

B. Mutual Exclusion

Mutex is also one of the methods to ensure thread synchronization. It can ensure that multiple threads access the same shared resource. Only threads with mutex objects have the permission to access resources. At the same time, because there is only one mutex, it is determined that the shared resources will not be accessed by multiple threads at the same time in any case. Create a mutex object:

Handle createmutex (lpsecurity_attributes lpmutexattributes, // set to null

Bool binitialowner, // ture or false

Lptstr lpname); // name of the mutex

After a mutex object is created, if we want to obtain the mutex object, we should first use the getlasterror function to verify whether the mutex resource has been submitted. If it has been submitted by the previous thread that uses it, then we can use the waitforsingleobject function to obtain this mutex object. Release a mutex object:

Bool releasemutex (handle bmutex );

C. Interlock Functions

In Windows CE, the interlock function is used to ensure that when a thread accesses a variable, other threads cannot access this variable to ensure the uniqueness of the variable value, this access method is called atomic access. Windows CE supports the complete function of mutual lock WIN32API.

Long interlockedincrement (lplong lpaddend );

This function increases the number of variables of the long type by 1, and the parameters in it are pointers to the variable.

Long interlockeddncrement (lplong lpaddend );

This function reduces the number of variables of the long type by 1, and the parameters in it are pointers to the variable.

D. Critical Section

The critical section is the most commonly used mutex in Windows CE. It ensures that all accessed resources in the critical section are not accessed by other threads until the current thread executes the code in the critical section. In general, a critical zone object is used to ensure the non-intercept of a piece of program code. The use of a critical zone object is usually limited to a process or dynamic link library. Functions related to the critical section mainly include:

Void initializecriticalsection (lpcritical_section); The initializecriticalsection function initializes the critical section;

Void entercriticalsection (lpcritical_section); the function entercriticalsection is used to enter the critical section;

Void leavecriticalsection (lpcritical_section); the function of leavecriticalsection is to exit the critical section;

Void deletecriticalsection (lpcritical_section); The deletecriticalsection function is used to cancel the critical section;

 

Http://www.cevx.com/bbs/htm/board4/topic14334.htm ()

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.