     

      4 

              ,     
   .  ,   
 ,      ,  
        .  , 
   ,       
.     ,   . 

         

     (thread)      . 
       .  
       (    
  WinMain   ),     , 
 WinMain         
ExitProcess.    ,  . 
     ,   
      . 
    ,         
  .      
 ,         
     .     
          (  
) .  ,    , 
  , . .      
,   .      ,   
   ,   ,   
    (      ). 
     ,    
,  ,  .   
          
    . 
                
          . 
,  16 Windows   Microsoft Word 
  ,     Win32   
    ^~  .  
    ,  ,     
  . 

    52 ______________________________Windows   

             .  
,    ,     . 
     .        
 ,    . ,   
 Explorer     , ,   
 ,    Cancel ().  .  
   ( ). 
     16- Windows      File Copy 
   PeekMessage,        
    .       
    Cancel   :   
  ,    . -   
, ,   -   ,   
            . 
     ,  ,    ,   
 .          PeekMessage  
,    ,  
.  ,   Cancel   . 
           , 
   .   10    
.       ,   
        ,    
    . 
        -    ,  
    . -,     
   (  )   
.   ,   ,   
   ,     
    . ,     2 , 
   8 ,       
 .     1000 ,   
    8 .     
        
,    .    
,     .    
         
     . 
    -,     ,    
,  . ,    , 
     ;    
   . 
    , -,   ,     
 -     . ,   
  ,   ,   ,  
       (    
 ). , ,    
,     ,     
 '.         . 

           

       ,     ,  
,      :      
  ,      !   -, 
         ,  
      . ... 
    1.  , ,      
,     . 

     4.  53 

        ,     . ,  
 ,    . ,   
      ,   
,   .   : ,  
  ,     .   
  : ,         
?  ,      ,   
   . -  ,     
   ,    ?     
    ,  ,    
 ,   ,    . 
 :           , 
       .  
   ,       . 
       ,       
  ,      .  
      () 
   . ,     ,  
    ,    ? 
         ,     
   ,    ,    - 
 ( ).     ,   
, ,       .  
        ,  
,  ,  . 
          . -,  ,     
,      GetMessage. -,  , 
 ,    .      
  .     ,   
  ,    ,     
 . 
      ,  ,    ,    . 
        
  - .       
    . ,   Calculator  
,        ,   
 Paint  ,   .    
 .       
,      Paint.    16- 
Windows,   ?  ,    ,   .  
,    Win32,     
 Calculator      Paint.        11. 
    ,   ,     ,  
Explorer.      Explorer       
  ,   ,    Explorer 
  .      ,    
  ,        
 . 
          GUI    
  (multiple document interface, MDI),   
 MDI   .      
        , 
      MDI-OKHO    
,     .   ,  
 Win32    , 

    54 ______________________________Windows   

     ,  ,       
MDI-OKHO,   WMJUDICREATE  MDIClient: 

    HWND CreateMDIWindow(LPTSTR IpszClassName, LPTSTR IpszWindowName, DWORD 
dwStyle, int x, int y, int nWidth, int nHeight, HWND hwndParent. HINSTANCE 
hinst, LONG IParam); 

       ,  CreateMDIWindow    
MDI-OKHO    . 
     ,     :  
  .      , 
  .      -   
   ,  .       
     ,  . 

        

        ,   .   
   : 

    DWORD WINAPI YourThreadFunc(LPVOID IpvThreadParm); 

      WinMain,      .  
     ,   KERNEL32.DLL ( 
    ).     StartOfThread   
   ,   .   : 

    void StartOfThread(LPTHREAD_START_ROUTINE IpStartAddr. LPVOID 
IpvThreadParm) { 
    __try { 
    DWORD dwThreadExitCode = IpStartAddr(lpvThreadParm); 
    ExitThread(dwThreadExitCode); 
    } 
    except (UnhandledExcept ion Filter (GetExceptionInformationO)) { 
    ExitProcess(GetExceptionCodeO); 
    } 
    ) 


        StartOfThread^ 

    1.         
 (SEH-frame     SEH-),    
            
- ,   .    
  .  16. 
    2.     ,   32-  
IpvThreadParm,     CreateThread. 
    3.     , StartOfThread  ExitThread, 
  ,   .    
   ,    . 
    4.       ,   
SEH,   StartOfThread.     
   - , ,    
    , StartOfThread  ExitProcess    
,     ,    . 

     4.  55 

          ,      
     StartOfThread.     
     ,    WinMain  
 .     '  StartOfThread, 
        ExitProcess. 
        ,   . 

      

            
.         
       ,   
  .   ,    
    ,  ,      
    .     
         
  . 
      ,  ,  ,   
    ,    ;   
      7. 

     CONTEXT 

          ,  
 .     CONTEXT    
     .    
   Win32,    .     
Win32     .   ,   
  ,    WINNT.H       
:  ;>86,  MIPS,  Alpha   PowerPC.   
        ,  
   -  DLL-. 
        ,   
    , ,    
     ,   
 .  ,     ,  
  ,  . 

       

          ,  
   .    ,      
,    , ,   -  
,          
.       ,    
      ,   : 
    DWORD dwStartTime = GetTickCountO; 
    //   -   
    DWORD dwElapsedTime = GetTickCountO - dwStartTime; 
        ,   ,  
    , ,  ,  Win32   : 
    BODL GetThreadTimes(HANDLE hThread, LPFILETIME IpCreationTlme. 
    LPFILETIME IpExltTime, LPFILETIME IpKernelTime, 
    LPFILETIME IpUserTime); 


    56 ______________________________Windows   


    GetThreadTimes  4  : 

                    

      (Creation Time)         . 
      (Exit Time)           .  
   ,     . 
       (Kernel Time) ,     
   . 
      User (U^er Time) ,     
 . 
          ,    
 : 

    __int64 FileTimeToQuadWord(PFILETIME pFileTime) { 
    __int64 qw: 
    qw = pFileTime->dwHighDateTinie; 
    qw = 32: 
    qw ;= pFileTime->dwLowDateTime: 
    return(qw): 
    PFILETIME QuadWordToFileTime(__int64 qw, PFILETIME pFileTime) { 
    pFileTime->dwHighDateTime = (DWORD) (qw  32); 
    pFileTime->dwLowDateTiine  = (DWORD) (qw & OxFFFFFFFF); 
    return(pFileTime); 
    } 
    void Recalc() { 
    FILETIME ftKernelTimeStart, ftKernelTimeEnd; 
    FILETIME ftUser-TimeStart, ftUserTimeEnd: 
    FILETIME ftDummy, ftTotalTimeElapsed; 
    __int64 qwKernelTimeElapsed, qwUserTimeElapsed, 
    qwTotalTimeElapsed; 
    
    //     
    GetThreadTimes(GetCurrentThread(), &rtDurnmy, &rtDummy, 
    &ftKernelTimeStart. &ftUserTimeStart): 
    //     
    
    //     
    GetThreadTimes(GetCurrentThread(), &ftDummy, &ftDummy, 
    SftKernelTimeEnd, &ftUserTimeEnd); 
    
    //   ,      User, 
    //        FILETIME 
    //   ,        
    qwKernelTimeElapsed = FileTimeToQuadWord(&ftKernelTimeEnd) 
    FileTimeToQuadWord(&ftKernelTimeStart): 


    _______________________________________  4.  57 
    

    qwUserTimeElapsed = FlleTimeToQuadWord(&ftUserTimeEnd) 
    FileTimeToQuadWord(&ft(JserTimeStart); 
    
    //   ,      User 
    qwTotalTimeElapsed = qwKernelTi(neElapsed + qwUserTimeElapsed: 
    
    //       FILETIME 
    QuadWordToFileTime(qwTotalTimeElapsed, SftTotalTimeElapsed); 
    
    //     qwTotalTimeElapsed 
    // ftTotalTimeElapsed;     
    } 

      ,     ,  
GetThreadTimes       : 

    BOOL GetProcessTimes(HANDLE hProcess, LPFILETIME IpGreationTime, 
    LPFILETIME IpExitTime, LPFILETIME IpKernelTime, 
    LPFILETIME IpUserTime); 

    GetProcessTimes   ,     
  . ,       
,         . 
    VWINDOWS/  ,  Windows 95   GetThreadTimes  
GetProcessTimes \95/  ,        
FALSE.   GetLastError   120 
(ERROR_CALL_NOT_IMPLEMENTED), . .      Windows NT. 
       Windows 95   ,     
  ,       . 

     CreateThread 

      ,    CreateProcess     
 .    ,     
 ,      CreateThread: 

    HANDLE CreateThread( 
    LPSECURITY_ATTRIBUTES Ipsa: 
    DWORD cbStack: 
    LPTHREAD_START_ROUTINE IpStartAddr: 
    LPVOID IpvThreadPariri; 
    DWORD fdwGreate; 
    LPDWORD IpIDThread); 

         : 
    1.         
 .       , 
   .     , 
  CreateThread. 

    58 ______________________________Windows   

    2.     (    
)  STILL_ACTIVE      
(thread's suspend count) .       . 
    3.      CONTEXT. 
    4.   ,       
,   2        
 PAGE_READWRITE,    (   ) 
   PAGE_GUARD.     . 
 7. 
    5.  IpStartAddr  IpvThreadParm       
,    ,   StartOfThread. 
    6.          
 CONTEXT ,        , 
      StartOfThread. 
     ,        Create Thread.  
     . 

     /psa 

     Ipsa     SECURITY_ATTRIBUTES.  
 ,         , 
    NULL.       
   ,   SECURITY_ATTRIBUTES  
   bInheritHandle  TRUE. 

     cbStack 

      ,       
   .     . 
CreateProcess,  ,   CreateThread,   
   .   CreateProcess   
 cbStack ,     .  
    /STACK : 

    /STACK: [reseri/e] [, commit} 

     reserve    ,   
     (  1 ).  commit 
   ,    
  (  1 ). (     
.  7.)       ,  , 
       .   
  . (   .  16.)  
 ,       
 ( ,     commit).   
        . 
      CreateThread,     cbStack.  
       ,   commit, 
 _.   -.    
  1 .        
 . 
    ,   ,     . 
 ,     ,    . 
 ,     ,      
.         , 
         ,   
    .   

    ______________________________________  4.  59 

     , , -,    
   , -,       
    . - SEHSum   16 
,        . 

     IpStartAddr  IpvThreadParm 

     IpStartAddr    ,    
    .      
  ,        
      . ,   
MDI-,         , 
     .       : 

    DWORD WINAPI ThreadFunc(LPVOID IpvThreadParm) { 
    DWORD dwResult = 0; 
    return(dwResult); 

     IpvThreadParm     IpvThreadParm, 
    Create Thread.     
    ,      
.  ,       
-  .       
32- ,  32      
 . 

     fdwCreate 

        ,   
.      : 0 (   
)  CREATE_SUSPENDED.      , 
  ,     CONTEXT , 
       ,  
   . 
        CreateThread       
,              
 CREATE_SUSPENDED.2        
,  ,    .  : 

    DWORD WINAPI FirstThread(LPVOID IpvThreadParm) { 
    int x = 0: 
    DWORD dwResult = 0, dwThreadId; 
    HANDLE hThread: 
    hThread = CreateThread(MULL, 0, SecondThread, (LPVOID) &x, 0, &dwThreadId); 
    CloseHandle(hThread); 
    return(dwResult); 
    } 

    . . . 

    2.        
,   ,     .  
,         . 

    60 -____________________________Windows   


    DWORD WINAPI Secondrnread(LPVOID IpvTnreadParm) { DWORD dwResult = 0; 
    //   -   
    * ((int *) IpvThreadParm) = 5; 
    return(dwResuit): 
    } 

    He ,     FirstThread     
,  SecondThread   5    FirstThread.  
  , SecondThread  ,  FirstThread   ,  
   -     
 .     :    
     FirstThread.   ?   
  ,        
  ,      (application's data section).  
   .  ,      
     ,      ,    
     . 
       (    )   
  ,       10. 

     /p/DThread 

       CreateThread      DWORD, 
    ,    . 
     Windows 95  NULL     .  Windows 
NT   4       NULL     
      000000000,     
 .  Windows NT 4   NULL   
IpIDThread,      .                       
      Windows 95  Windows NT   
 /  \    . ,    
 /\ ^ g windows NT 4     ,  
   NULL   IpIDThread  CreateThread. 
.       Windows 95,  , ,  
.  :         
Windows 95,    Windows NT      . 

      

      ,     : 
    1.    ExitThread (  ). 
    2.         
TerminateThread (  ). 
    3.  ,   . 

    ______________________________________  4.  61 

             ,  
 ,         . 

     ExitThread 

     ,   ExitThread: 

    VOID ExitThreaddlINT fuExltCode): 

         ,      
 .   fuExitCode     . 
          ,    
      StartOfThread  
 ExitThread,   ,   . 

     TerminateThread 

         : 

    BOOL TerminateThreacKHANDLE hThread, DWORD dwExitCode): 

      ,   hThread,   
   dwExitCode.      ,  
         . 
        ExitThread     .  
    TerminateThread,    ,   
  ,    .   ,  
    ,      
 .       ,  
  . 
      ,      DLL-, 
      .    
TerminateThread   ,      
. , - DLL       
      .       
       TerminateThread,      . 

       

     ExitProcess  TerminateProcess,    3,  
 .    ,     
 ,   . 

         

       : 
    1.     User,  .  
Win32    ,  ,  
   .       User: 
   (hooks).  ,   , , 
   .   ,   
   . 
    2.       . 

    62 ______________________________Windows   

    3.      STILL_ACTIVE  ,   
 ExitThread  TerminateThread. 
    4.         ,   
 . 
    5.         1. 
              
,          . 
      ,          
 . ,    ,   
GetExitCodeThread  ,   ,   
hThread, ,  ,    : 

    BOOL GetExitCodeThread(HANDLE hThread. LPDWORD IpdwExitCode); 

          DWORD,    
IpdwExitCode.        GetExitCodeThread, 
      STILL_ACTIVE (0103).  
    TRUE.     
         10. 

        

     \32-       
 .      ,  
 GetCurren tProcess: 

    HANDLE GetCurrentProcess(VOIO): 

        ,    , 
      .   
CloseHandle     ,     
  . 
         ,   
 . ,       
   HIGH_PRIORITY_CLASS: 

    SetPriorityClass(GetCurrentProcess(),HIGH_PRIORITY_GLASS); 

     Win32 API    ,   
.       ,  GetCurrentProcessId: 

    DWORD GetCurrentProcessId(VOID); 

          . 
       CreateThread     
   ,        ,   
 .      : 

    HANDLE GetCLirrentThread(VOID); 

      GetCurrentProcess,  GetCurrentThread  
,         
 .       ,   
CloseHandle        . 
         : 

    DWORD GetCurrentThreadId(VOID); 


    ______________________________________-  4.  63 

       ,    .  
   ,    
 .    : 

    DWORD WINAPI ParentThread(LPVOID IpvThreadParm) { DWORD IDTI-iread; 
    HANDLE hThreadParent = GetCurrentThread(); 
    CreateThread(NULL, 0, ChildThread, (LPVOID) hThreadParent, 0, SIDThread); 
    //   - ... 
    DWORD WINAPI ChildThread(LPVOID IpvThreadParm) { 
    HANDLE hThreadParent = (HANDLE) IpvThreadParm; 
    SetThreadPriority(hThreadParent, THREAD_PRIORITY_NORMAL); 
    //   - ... } 

     ,     ?    ,   
    .    -,   
 .  ,     
  SetThreadPriority,      
      !  .   
,      ,    . 
         ,   
    DuplicateHandle: 

    BOOL DuplicateHahdle( 
    HANDLE hSourceProcess, 
    HANDLE hSource, 
    HANDLE hTargetProcess, 
    LPHANDLE IphTarget, 
    DWORD fdwAccess, 
    BOOL finhent, 
    DWORD fdwOptions): 

          - 
    ,      
 (.  2).    DuplicateHandle    
          : 
    
    DWORD WINAPI ParentThread(LPVOID IpvThreadParm) { 
    DWORD IDThread: 
    HANDLE hThreadParent; 
    DuplicateHandle( 
    GetCurrentProcess(),       //  ,   
    //   ; 
    GetCurrentThread(),        //   ; 
    GetCurrentProcess(),      //  ,   
    //  , "" 
    //  ; 
    &hThreadParent,           //  , "" , 
    //   ; 
    . . . 


    64 ______________________________Windows   


    ,                         //  - 
    // DUPLICATE_SAME_ACCESS; 
    FALSE,     .               //    ; 
    DUPLICATE_SAME_AGCESS);   //     
    //    , 
    //    
    CreateThread(NULL, 0, ChildThread, (LPVOID) hThreadParent, 0, &IDThread): 
    //   - ... 
    } 
    DWORD WINAPI ChildThread(LPVOID IpvThreadParm) { 
    HANDLE hThreadParent = (HANDLE) IpvThreadParm; 
    SetThreadPriority(hThreadParent, THREAD_PRIORITY_NORMAL); 
    CloseHandle(hThreadParent); 
    //   - ... 
    } 

           
 ,    ,     
CreateThread.     ,   
IpvThreadParm     .    
-         ,   
 . 
     DuplicateHandle     , , 
     ,     
 ;    - (   
 )   ChseHandle.     
SetThreadPriority    CloseHandk,     1 
   .        
,           
.       -     
 , , ,  ChseHandle    
 ,         . 
     ,  DuplicateHandle    
   .    : 

    HANDLE hProcess; 
    DuplicateHandle( 
    GetCurrentProcess(),       //  ,   
    //  ; 
    GetCurrentProcess(),       //  ; 
    GetCurrentProcess(),       //  ,   
    //  , "" 
    // ; 
    &hProcess,                 //  , "" , 
    //  ; 
    ,                         //  - 
    // DUPLICATE_SAME_ACCESS; 
    FALSE,                     //   ; 
    DUPLICATE_SAME_ACCESS);    //     
    //    , 
    //    


    ______________________________________  4.  65 


         

           
   ,       
.    ,   Windows NT  Windows 95. 
    /\ 
            
        Win32.   
,  Microsoft      -  
 .   Microsoft    :  
Windows 95, Windows NT 3.5  Windows NT 4.0   
   .   ,   
 ,      ,  , 
      . 
          ,    
 ,    0 ()  31 ().  
        (zero page 
thread),       , 
    .   ,  ,   
   . 
         ,     
   .  ,    
    31,        
         . 
      31    ,  
        31. :   
            
31,            
     .    
 (starvation).  ,     
   ,     . 
         31      
 30;       ,   
    29  . . 
      ,  ,   ,    
  (   )      
.      :        
. ,       GetMessage, 
  ,     ,    
,          
 ,  .         
  ,          
 .        ,   
      (       
    ). 
        . ,     
 5,    ,      
   .  ?      
           
           (,  
,     ).       
          
,    . 

    66 _______________________________Windows   


        Win32 

          ,   
   .        
,   ,       
    .     , 
  ,    .  
        .         """' 

       

    Win32  4  : idle (), normal 
(), high ()  realtime ( ).   
   ( OR)     
CreateProcess    fdwCreate.      
   : 

               CreateProcess               

    Idle           IDLE_PRIORITY_CLASS            4 
    Normal        NORMAL_PRIORITY_CLASS        8 
    High          HIGH_PRIORITY_CLASS           13 
    Realtime       REALTIME_PRIORITY_CLASS       24 

     ,  ,       
,    8. 
       ,     ,   
     .  CreateProcess, 
      NORMAL_PRIORITY_CLASS, 
     .     
   normal        
 idle (       idle). 
        - ,    
(foreground),      (background). , 
 ,        
 .    ,      
    ,    . 

    \WINDOWS/ \NT/ 
     Windows NT,     ,  
  ,    . ,  , 
    15   , ,   
   ,    45  (,  
,  ,   ,  3 ). 

    VWINDOWS/ \95/ 
     Windows 95,     ,   
      1, . .    \/    
  8  9.      ,   
   1. 
           :  
 ,       .  
   

     4.  67 

      ,      ,  ,  
 ,   ,    
    .   ,    
 ,  ,      - 
.         ,  
   ,    . 
        Windows NT,    
    ,   .  
     System  Control Panel    
Performance.       : 
     Boost ,     , 
  .      None,  
   1;    Maximum    
  3,       2. 
        Windows 95 ,   
      .    
 Windows NT     ,   
  .  ,     Windows NT 
   ,     
   None,        . 
     idle   ,   . 
,   ,      
 .     ,      
? ,       IDLE_PRIORITY_CLASS. 
       ,   idle,   
.        
.         , 
 .        
   , . . idle. 
      high      
. ,     ,  Explorer    
.      ,  
,    

    68 ______________________________Windows   

     -     .   Explorer 
,      ,   
    .    , , 
Ctrl+Esc,     Explorer. (  Ctrl+Esc 
   Start.)        
 ,   ,     
Explorer. Microsoft  Explorer   ,   
           
    .  ,  Explorer  , 
          . 
   ,  Explorer  , 
  ,      . 
         Explorer.   
   ,    .    , 
     ,      
    . 
    , ,        
REALTIME_PRIORITY_CLASS      .     
 - Win32 API      
 ,      
. Realtime    ,    
   ( ,    )   
 ,         . , 
 ,    ,     
   Alt+Ctrl+Del,         
.    , ,    
,      realtime.     
   :     ,  . 
   -      . 
      realtime    ,  
  ,      
,       . 
    VwiNDowsy     realtime  ,  
, \NT/   ,    
Increase Scheduling Prio\/    rity.      
     .    
     ( )   User Manager. 

        

       , ,   , 
     .     
       Explorer.     - 
      .  Explorer   
,            
 .       
       SetPriorityClass: 

    BOOL SetPriorityClass(HANDLE hProcess, DWORD fdwPriority): 

         ,   
hProcess,      fdwPriority.   
   : IDLE_PRIORITY_CLASS, NORMAL_PRIORITY_CLASS, 
HIGH_PRIORITY_CLASS  

    ___________________________________  4.  69 

    REALTIME_PRIORITY_CLASS.      TRUE; 
    FALSE.  SetPriorityClass   
.      ,   
,            
. "   GetPriorityClass     
 : 

    DWORD GetPriorityClass(HANDLE hProcess): 

     ,   ,     . 
            
  . ,    START,   
,    . ,  , 
    ,    
 Calculator     : 

    C:\>START /LOW CALC.EXE 

     START    /NORMAL, /HIGH  /REALTIME, 
       (   
),      . , 
     SetPriorityClass    
  . 

    \wiNDowsy  Windows 95  START    /LOW, /NORMAL, 
/HIGH \95^  /REALTIME.     Windows 95  
>/        . 


        

       ,      
 . ,      
HIGH_PRIORITY_CLASS      13.  
      ,  .    
        . 
             
 SetThreadPriority. 

    BOOL SetThreadPriority(HANDLE hThread, int nPriority); 

      , hThread,    ,    
,  nPriority    ,   : 

                             

    THREAD_PRIORITY_LOWEST                 2 
    . 
    THREAD_PRIORITY_BELOW_NORMAL         1  
   . 
    THREAD_PRIORITY_NORMAL                
  . 
    THREAD_PRIORITY_ABOVE_NORMAL          1  
   . 
    THREAD_PRIORITY_HIGHEST                 2 
    . 

    70 ______________________________Windows   

             
 THREAD_PRIORITY_NORMAL.       
 -       
  .   THREAD_PRIORITY_HIGHEST 
,         
 .         
     . 
        ,   SetThreadPriority  
   () : THREAD_PRIORITY_IDLE  
THREAD_PRIORITY_TIME_CRITICAL.      
 1      idle, normal  high.   
   realtime,     
16.         15   
   idle, normal  high.     
 realtime,     31.    
. 4-1 ,       , 
       . 


       

              Idle      Normal        High        Realtime 

    Time critical (  )      15       15             15       
   31 
    Highest ()                       6        10             15          26 
    Above normal ( )           59              14          25 
    Normal ()                      48              13          24 
    Below normal ( )            37              12          23 
    Lowest ()                        26              11          22 
    Idle ()                    11              1           16 

    . 4-1.         


    / ^ ^^ 
      . 4-1       
      .  Windows NT  
 ,    ,     
,  ,     Windows 95,   
 ,    lowest, below normal, 
normal  above normal  1 ,      idle  | 
time critical. 

     GetThreadPriority,  SetThreadPriority,   
  : 

    int GetThreadPriority(HANDLE hThread); 

          ,   
  THREAD_PRIORITY,ERROR_RETURN. 
            
  .     ,    
SetThreadPriority    . ,   
           
 : 

    ______________________________________  4.  71 


    SetThreadPriority(hThread, THREAD_PRIORITY_LOWEST); 
    SetTh readPriority(hTh read, THREAD_PRIORITY_LOWEST); 

         11,   9. 


    /8\ 
      \32-     , . .  
Win32  ,       ,  ' 
    , , 8.    
. ,  Microsoft       
  .      
   -   .   
     ,    '. 


         

     ,     
    ,    , 
    .     
 .        , 
  / (,     ). 
,     normal,     
  ,    13. 
       - ,    
   WM_KEYDOWN.       
,        .  
        13  15 
(        ).   
      . 
       ,    
      1,   14.   
    ,     
      1.     
      .   
   -    . 
    Microsoft       
    ,     
    . ,    
   ( 16  31)    . 
          
,    . ,  ,   
        
 ( 15). 
     Windows NT 4    ,   
   : 

    BOOL SetProcessPriorityBoost(HANDLE hProcess, BOOL DisablePriorityBoost); 
    BOOL SetThreadPnorityBoost(HANDLE hThread, BOOL DisablePriorityBoost); 

    SetProcessPriority Boost      
      , a 
SetThreadPriorityBoost     .  
   ,  ,    
 : 

    72 ______________________________Windows   

    BOOL GetProcessPriorityBoost(HANDLE hProcess, PBOOL pDisablePrlorityBoost); 
    BOOL GetThreadPriorityBoost(HANDLE hThread, PBOOL pDisablePriorityBoost); 

               
     BOOL,     . 

    VWINDOWS/  Windows 95      .  
  \^/    ,     . 


        

      ,     ,   
CreateProcess  CreateThread  CREATE_SUSPENDED.     
  ,  ,     
  (. .   CONTEXT).   
      1,   ,  
        .  
   ,      ResumeThread  
  ,   CreateThread. ,  
     ,     IppiProdnfo, 
  CreateProcess. 

    DWORD ResumeThread(HANDLE hThread); 

      ResumeThread  ,     
   ;     OxFFFFFFFF. 
          .   
 3 ,            
    .     
       CREATE_SUSPENDED,    SuspendThread: 

    DWORD SuspendThread(HANDLE hThread); 

              
.        (    !), 
     ,     
   .   ResumeThread, SuspendThread  
     .   
    MAXIMUM_SUSPEND_COUNT  (  WINNT.H  
   127). 

        

     ,         
   ,      Visual 
C++  PSTAT.EXE.  ,     Windows 95,    
      PView95.  . 4-2  
,    PSTAT.EXE.      
  ,      .   pid 
   . ,  Explorer 
(EXPLORER.EXE)    074.   pri (   
)         
. ,  Explorer    13,     
 .  HandleCount       
     . 

    ____________________________________  4.  73 

      ,  ,    .  
, Explorer  4 .  tid   , 
a pri    .  cs     
  .      .  Wait 
,       ,  
    .      . 

    Pstat version 0.2: memory: 32176 kb uptime: 0 6:50:37.687 
    PageFile: \??\D:\pagefile.sys 
    Current Size: 44032 kb Total used: 10392 kb   Peak Used: 17888 kb 
    pid: 0 pri: 0 HandleCount:    0 (null) 
    tid: 0 pri:16 cs: 564389 Running 
    tid: 0 pri:16 cs: 510456 Running 
    pid: 2 pri: 8 HandleCount:   89 System 
    tid: 1 pri: 0 cs:   3730 Ready 
    tid: 3 pri:16 cs: 10128 Wait.'EventPairLow 
    tid: 4 pri:16 cs:   8928 Wait:EventPairLow 
    tid: 5 pri:16 cs: 15450 Wait:EventPairLow 
    tid: 6 pri:12 cs: 40614 Wait:EventPairLow 
    tid; 7 pri:12 cs:    313 Wait.'EventPairLow 
    tid: 8 pri:12 cs: 19352 Wait.'EventPairLow 
    tid: 9 pri:15 cs:    607 Wait:EventPairLow 
    tid: a pri:18 cs:   1300 Wait:VirtualMemory 
    tid; b pri: 17 cs:   1113 Wait.'FreePage 
    tid:  pri:16 cs: 26658 Wait:Executive 
    tid: d pri:23 cs:   9986 Wait:Executive 
    tid; e pri: 8 cs:      1 Wait:EventPairLow 
    tid: f pri:14 cs:      8 Wait:LpcReceive 
    tid: 18 pri:17 cs:      1 Wait:VirtualMemory 
    tid: 21 pri:19 cs: 268710 Wait:UserRequest 
    tid: 22 pri:16 cs:   8279 Wait:UserRequest 
    tid: 26 pri:16 cs: 10272 Wait:UserRequest 
    tid: 3d pri: 8 cs:      3 Wait:Executive 
    tid: 56 pri: 6 cs:      3 Wait:UserRequest 
    tid: 5a pri:16 cs:     45 Wait:Executive 
    tid: 58 pri: 9 cs:     78 Wait:EventPairLow 
    tid: 5b pri: 9 cs:      4 Wait:EventPairLow 
    tid: 60 pri: 9 cs:    111 Wait:EventPairLow 
    pid: 11 pri:11 HandleCount:   29 smss.exe 
    tid: 10 pri:14 cs:    819 Wait:UserRequest 
    tid: 12 pri:14 cs:      6 Wait .LpcReceive 
    tid: 13 pri:11 cs:      3 Wait:LpcReceive 
    tid: 19 pri:14 cs:    192 Wait:LpcReceive 
    tid: 1a pri:12 cs:    101 Wait:LpcReceive 
    tid: 1b pri:11 cs:      1 Wait:LpcReceive 
    . . . 

    . 4-2. ,     PSTAT.EXE 


    74 ______________________________Windows   


    . 4-2 () 
    pid: 1d pri:13 HandleCount: 183 csrss.exe 
    tid: 1e pri:13 cs: 39 Wait:UserRequest 
    tid: 14 pri:14 cs: 48245 Wait:LpcReceive 
    tid; 15 pri:14 cs: 115 Wait:LpcReceive 
    tid: 16 pri:14 cs: 4 Wait:LpcReceive 
    tid: 20 pri:15 cs: 48293 Wait:LpcReceive 
    tid: 6f pri:14 cs: 48131 Wait:LpcReceive 
    tid: 8b pri:14 cs: 447 Wait:UserRequest 
    pid: 1f pri:13 HandleCount: 43 winlogon.exe 
    tid: 17 pri:15 cs; 8375 Wait:UserRequest 
    tid: 23 pri:13 cs: 6 Wait:UserRequest 
    pid: 25 pri: 9 HandleCount: 174 services.exe 
    tid: 37 pri:14 cs: 34 Wait:UserRequest 
    tid: 39 pri: 9 cs: 23 Wait:Executive 
    tid: 3c pri: 9 cs: 2 Wait:LpcReceive 
    tid: 45 pri: 9 cs: 213 Wait:UserRequest 
    tid: 47 pri: 9 cs: 18 Wait:UserRequest 
    tid: 6d pri: 9 cs: 4 Wait:Executive 
    tid: 6e pri: 9 cs: 20 Wait:UserRequest 
    tid: 6b pri:11 cs: 445 Wait:UserRequest 
    tid: 6c pri:11 cs: 413 Wait:UserRequest 
    tid: 46 pri:14 cs: 4 Wait:UserRequest 
    tid: 3b pri:15 cs: 909 Wait:UserRequest 
    tid: 72 pri:10 cs: 4 Wait:LpcReceive 
    pid: 28 pri: 9 HandleCount: 87 Isass.exe 
    tid: 2a pri:10 cs; 4 Wait:LpcReceive 
    tid: 2c pri:14 cs: 10 Wait:UserRequest 
    tid: 2d pri:10 cs: 13 Wait:LpcReceive 
    tid: 2e pri:10 cs: 15 Wait:LpcReceive 
    tid: 2f pri:10 cs: 7 Wait:LpcReceive 
    tid: 30 pri:10 cs: 8 Wait:LpcReceive 
    tid: 31 pri:10 cs: 5 Wait;LpcReceive 
    tid: 32 pri:14 cs: 6 Wait:LpcReceive 
    tid: 33 pri: 9 cs: 12 Wait:LpcReceive 
    tid: 34 pri: 9 cs: 9 Wait:Executive 
    tid: 2b pri: 9 cs: 85 Wait:UserRequest 
    pid: 27 pri: 8 HandleCount: 83 SPOOLSS.EXE 
    tid: 3e pri:14 cs: 16 Wait:Executive 
    tid: 3f pri: 8 cs: 2 Wait:UserRequest 
    tid: 41 pri:14 cs: 94 Wait:UserRequest 
    tid; 42 pri: 8 cs: 137 Wait:Executive 
    tid: 55 pri: 8 cs: 7 Wait:UserRequest 
    tid: 57 pri: 8 cs: 2 Wait:UserRequest 
    tid: 76 pri:11 cs: 236 Wait:LpcReceive 
    tid: 71 pri: 8 cs: 134 Wait:Executive 
    . .  


    ;_____________________________________  4.  75 


    . 4-2 () 
    pid: 49 pri: 8 HandleCount: 70 rpcss.exe 
    tid: 48 pri:14 cs: 24 Wait:Executive 
    tid; 4b pri: 8 cs: 421 Wait:DelayExecution 
    tid: 4c pri:14 cs: 232 Wait:LpcReceive 
    tid: 4d pri: 8 cs: 2 Wait:LpcReceive 
    tid: 5d pri:10 cs: 6 Wait.'UserRequest 
    tid: 80 pri:15 cs: 294 Wait:LpcReceive 
    tid: 61 pri: 8 cs: 495 Wait:DelayExecution 
    pid: 3a pri: 8 HandleCount: 74 tapisrv.exe 
    tid: 4e pri:14 cs 18 Wait:Executive 
    tid: 4f pri: 8 cs 13 Wait:UserRequest 
    tid: 50 pri: 9 cs 3 Wait:UserRequest 
    tid: 51 pri: 8 cs 2 Wait:UserRequest 
    tid: 52 pri:14 cs 311 Wait:LpcReceive 
    tid: 5f pri:14 cs 6 Walt;LpcReceive 
    tid: 63 pri: 9 cs 2 Walt:EventPairLow 
    tid: 64 pri: 9 cs 3 Wait:UserRequest 
    tid: 65 pri: 9 cs 3 Wait:EventPairLow 
    tid: 66 pri: 9 cs: 3 Wait:EventPairLow 
    pid: 54 pri: 8 HandleCount: 121 RASMAN.EXE 
    tid; 53 pri: 8 cs 18 Wait:Executive 
    tid: 59 pri:14 cs 65 Wait:UserRequest 
    tid: 5e pri:14 cs 22 Wait:UserRequest 
    tid: 67 pri: 8 cs 2 Wait:UserRequest 
    tid: 68 pri: 9 cs 1053 Wait:UserRequest 
    tid: 69 pri: 8 cs 24 Wait:UserRequest 
    tid: 7c pri: 9 cs 51 Wait:UserRequest 
    tid: 5c pri:14 cs 30 Wait:UserRequest 
    tid: 83 pri: 9 cs 2 Wait:UserRequest 
    tid: 85 pri:14 cs 88 Wait:UserRequest 
    tid: 88 pri: 9 cs 4 Wait:UserRequest 
    tid: 89 pri: 8 cs 3 Wait:UserRequest 
    tid: 8a pri:10 cs 1991 Wait:DelayExecution 
    pid: 29 pri: 8 HandleCount: 16 nddeagnt.exe 
    tid: 6a pri:14 cs: 78 Wait:UserRequest 
    pid: 74 pri:13 HandleCount: 69 explorer.exe 
    tid: 73 pri:15 cs: 230614 Wait.'UserRequest 
    tid: 7e pri:13 cs: 324 Wait:UserRequest 
    tid: 81 pri:13 cs: 1 Walt:LpcReceive 
    tid: 9e pri:13 cs: 29 Wait .LpcReceive 
    pid: 7f pri:13 HandleCount: 31 taskmgr.exe 
    tid: 24 pri:14 cs: 265873 Wait:UserRequest 
    tid: 84 pri:15 cs: 58241 Wait:UserRequest 
    tid: 86 pri:14 cs: 86841 Wait:UserRequest 
    , :. (. ci4'i\ 


    76 ______________________________Windows   


    . 4-2 () 

    pid: 70 pri: 8 HandleCount:   94 WINWORD.EXE 
    tid: 4a pri: 8 cs: 263093 Running 
    tid: 8e pri: 8 cs:      2 Wait:LpcReceive 
    tid: ad pri: 8 cs:      1 Wait:LpcReceive 
    pid: 93 pri: 8 HandleCount:   48 ntvdm.exe 
    tid: 96 pri:14 cs:   1074 Wait:UserRequest 
    tid: 90 pri:15 cs:   1125 Wait:UserRequest 
    pid: a3 pri: 8 HandleCount:   25 rundll32.exe 
    tid: a2 pri:14 cs:    468 Wait:UserRequest 
    pid: 7b pri: 8 HandleCount:   22 cmd.exe 
    tid: aa pri:15 cs:    190 Wait:UserRequest 
    pid: a8 pri: 8 HandleCount:   14 pstat.exe 
    tid: a5 pri:14 cs:      9 Running 
    
    
    ,      
    
    Microsoft    Visual C++ 4.     
( run-time libraries).      : 

            
    
    LIBC.LIB               . 
    LIBCD.LIB               
 . 
    LIBCMT.LIB            . 
    LIBCMTD.LIB            
 . 
    MSVCRT.LIB              
    MSVCRT40.DLL.   -,    . 
    MSVCRTD.LIB          
  
    MSVCRT40.DLL.   -,    . 

    ,    :       
   ? .   
    -  1970       
 .   ,  ,  
  ,    . 
    ,  ,       
.  ,   - ,    
 . ,     : 

    ______________________________________  4.  77 


    BOOL fFailure = (system("NOTEPAD.EXE README.TXT-) == -1); 
    if (fFailure) { 

    switch (errno) { 

    case E2BIG:     //        
    break; 
    
    
    case ENOENT:     //     
    break; 
   
    
    case ENOEXEC:    //     
    break; 
    
    
    case ENOMEM:     //      
    break; 
    
    } 
    1 
    
      ,  ,    ,  
   system    if. ,    
   (   ),      
   ,     -   
  errno. ,  :   
    ,   errno     
,    system.      
       errno. 
        ,       
   .  errno,       
  ,       : 
_doserrno, strtok, _wcstok, strerror, _strerror, tmpnam, tmpfile, asctime, 
_wasctime, gmtime, _ecvt, _fcvt    . 
      /++-,    
,  ,         
  ,      .    
     _beginthreadex,   
\32- CreateThread: 

    unsigned long _beginthreadex(void security, unsigned stack_size, 
    unsigned (*start_address)(void *), void *arglist, 
    unsigned initflag, unsigned *thrdaddr); 

        _beginthreadex    ,    
CreateThread,      .   CreateThread,  
     . ,    
 Windows.H    STRICT,    
,   .   _beginthreadex: 
    1.     ,   
  ,    . ,  
   errno     strtok.   
   2 ,    
start_address  arglist,    _beginthreadex. 
    1.   \32- CreateThread.    .  
CreateThread  : 
    
    hThread = CreateThread(security. stack_size, _threadstart. 
    &PerThreadData, initflag, thrdaddr); 
    3.         0   . 

    78 ______________________________Windows   

         _threadstart,    ,     
_beginthreadex.  _threadstart      : 
    1.      ,  
,      ,    
(     .  13).  _threadstart 
       . 
    2.          
(   ). 
    3.   SEH-    signal   
 . 
    4.            
,      .     
_threadstart         32- . 
    5.      , _threadstart  
        _endthreadex,   
,   . 
       _endthreadex  ,  
_beginthreadex.  _endthreadex  : 
    void _endthreadex(unsigned retval); 
     retval    .  _endthreadex: 
    1.         . 
    2.    ,    . 
    3.   . 
    4.    \32-.. ExitThread,   
   retval. 
     _endthreadex       .  
,          
 _threadstart   _endthreadex. 
       ,       
          
_beginthreadex       ,   
      .  ,    
  ,   _endthreadex      
 . 
             , 
   ,    ,  
           , 
  . 
    ,    ,   ,  
     .    
    : 

    #if defined(_MT) :: defined(_DLL) 
    extern int * __cdecl _errno(void); 
    ftdefine errno   (*_errno()) 
    #else     /* ndef _MT && ndef _DLL / 
    extern int errno; 
    ffendif    /* _MT :: _DLL */ 

    ______________________________________  4.  79 

      ,      
   : / ( )  /MD 
( DLL);     _.  
,   .        _ 
   .        
,    . ,    
,        .    
 ,       : 

    int * = &errno; 
    if (*p == ENOMEM) { 
    } 

        _    ,  
    . 
        ,  ,  
   .      
   malloc,     .   
         
  .        ,    
   malloc,         malloc. 
(        10.) 
    ,          
   .  Microsoft,  
,       
  . 
           
:        DLL-, 
     .     
     .    
  DLL,        -  DLL-,  
   .  ,  Microsoft  
-    ,   ,    , 
    . 
    ,   ,  ,     
 32- CreateThread,      
_begmthreadey?.  ,  ,  CreateThread,   
  ,      
,  : 
    1.       , 
  . 
    2.    NULL,  ,    ,   
  ,       . (  
  .  13.) 
    3.      ,    
    . 
    ,    . -,     signal  
 ,      ,   
SEH-   . -,      
 _endthreadex,         
.   -         
 DLL-  . 

    80 ______________________________Windows   

      , , ,     DLL-,   
    ,  .   
           
     .     
        Wn-132- 
CreateThread/ExitThread,      Jbeginthreadex/_endthreadex. 
     , ,  ,      
         .  
          
 .        
WinMain,       . 
 ,         
,           
   signal. 

      ,     

         : 

    unsigned long _beginthread(void (__cdecl *start_address)(void *), unsigned 
stack_size, void *arglist); 

     

    void _endthread(void); 

         ,     
 _beginthreadex  _endthreadex. ,  ,  _beginthread 
 , , ,       
 _beginthreadex, ,   _beginthread,  
    ,     , 
            
 .   _endthread   :    
,          . 
       _endthread    ,  :  
 ExitThread    CloseHandle     
 .  ,    ,    : 

    DWORD dwExitCode; 
    HANDLE hThread = _beginthread(,.,); 
    GetExitCodeThread(hThread, &dwExitCode); 
    CloseHandle(hThread); 

     ,         , 
      GetExitCodeThread.    
,   hThread     _endthread  
   . , ,  CloseHandle  . 
      _endthreadex    ,   
,  ,    ( , ,  
 _beginthread   _beginthreadex).      : 
     , _beginthreadex  
 _endthreadex, a _beginthread   _endthread. 