C 在核心實現執行緒《C 多執行緒程式設計實戰》

2021-09-11 06:44:21 字數 3334 閱讀 2035

整個核心就是乙個程序. 核心有乙個執行緒表,跟蹤該系統中所有的執行緒.

**使用者級執行緒: 如果執行緒管理開銷非常小,沒必要這樣做

要求必須安裝winddk,驅動程式開發工具包

本示例有2個專案

#include #include #define driver_name text( "testdriver.sys" )

#define driver_service_name text(" testdriver ")

#define message(n) messagebox(0, text(n), \

text("test driver info"), 0)

bool startdriver(lptstr szcurrentdriver);

bool stopdriver(void);

int apientry winmain(hinstance hinstance, hinstance hprevinstance,

lpstr szcommandline, int icmdshow)

bool startdriver(lptstr szcurrentdriver)

; sc_handle hservice = ;

service_status servicestatus = ;

tchar szdriverpath[max_path] = ;

getsystemdirectory(szdriverpath, max_path);

tchar szdriver[max_path + 1];

#ifdef _unicode

wsprintf(szdriver, l"\\drivers\\%ws", driver_name);

#else

sprintf(szdriver, "\\drivers\\%s", driver_name);

#endif

_tcscat_s(szdriver, (_tcslen(szdriver) + 1) * sizeof(tchar),

szdriver);

bool bsuccess = copyfile(szcurrentdriver, szdriverpath, false);

if (false == bsuccess)

hscmanager = openscmanager(null, null,

sc_manager_create_service);

if (0 == hscmanager)

hservice = createservice(hscmanager, driver_service_name,

driver_service_name, service_start | delete | service_stop,

service_kernel_driver, service_demand_start, service_error_ignore,

szdriverpath, null, null, null, null, null);

if (0 == hservice)

bool startsuccess = startservice(hservice, 0, null);

if (false == startsuccess)

closehandle(hfile);

return true;

}bool stopdriver(void)

; sc_handle hservice = ;

service_status servicestatus = ;

tchar szdriverpath[max_path] = ;

getsystemdirectory(szdriverpath, max_path);

tchar szdriver[max_path + 1];

#ifdef _unicode

wsprintf(szdriver, l"\\drivers\\%ws", driver_name);

#else

sprintf(szdriver, "\\drivers\\%s", driver_name);

#endif

_tcscat_s(szdriver, (_tcslen(szdriver) + 1) * sizeof(tchar),

szdriver);

hscmanager = openscmanager(null, null,

sc_manager_create_service);

if (0 == hscmanager)

hservice = openservice(hscmanager, driver_service_name,

service_start | delete | service_stop);

if (hservice)

return false;}

//每個驅動程式都必須在此標頭檔案中

#include "ntddk.h"

//宣告2個主例程

driver_initialize driverentry; //驅動程式入口點

driver_unload onunload; //驅動程式解除安裝

void threadstart(pvoid lpstartcontext)

ntstatus driverentry(pdriver_object thedriverobject, punicode_string theregistrypath)

; pethread pthread = 0;

thedriverobject->driverunload = onunload; //設定解除安裝例程

dbgprint("entering kernel mode..");

//初始化物件,在建立核心執行緒之前

initializeobjectattributes(&threadattributes, null, obj_kernel_handle,

null, null);

//核心開發必須訊息謹慎,哪怕一丁點的錯誤都會導致**藍屏宕機**或機器崩潰,所以使用__try - __except塊

__try

else

}__except (exception_execute_handler)

return status_success;

}void onunload(pdriver_object driverobject)

void (__stdcall* kstart_routine)(pvoid startcontext);

C 多執行緒程式設計

一 thread 基礎 程序 當乙個程式開始執行時,它就是乙個程序,程序包括執行中的程式和程式所使用到的記憶體和系統資源。而乙個程序又是由多個執行緒所組成的。執行緒 執行緒是程式中的乙個執行流,每個執行緒都有自己的專有暫存器 棧指標 程式計數器等 但 區是共享的,即不同的執行緒可以執行同樣的函式 方...

C 多執行緒程式設計

乙個程序通常定義為程式的乙個例項。在win32中,程序佔據4gb的位址空間。與它們在ms dos和16位windows作業系統中不同,win32程序是沒有活力的。這就是說,乙個win32程序並不執行什麼指令,它只是佔據著4gb的位址空間,此空間中有應用程式exe檔案的 和資料。exe需要的任意dll...

C 多執行緒程式設計

建立執行緒的函式 handle createthread lpsecurity attributes lpthreadattributes,使用預設安全形態,設為null,表明不可被子執行緒繼承 size t dwstacksize,初始棧大小,預設值0表示使用與呼叫該函式的執行緒相同的棧大小 lp...