Thread: GetProcessTimes hanging

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    19

    GetProcessTimes hanging

    Hello,

    I'm writing a function that takes in a handle and returns the CPU usage for that function. When I call the GetProcessTimes function, the program continually runs but never stops. I threw in some log messages to a file to let me know where its hanging. I only got into the first call of GetProcessTimes.

    Code:
    if( ! GetProcessTimes( hHandle, &ftCreate, &ftExit, &ftUser, &ftKernel ) ) 
         { 
         printf("error old getprocesstime %d", GetLastError() ); 
         getch(); 
         }
    So I'm wondering if there's something in my code that's causing an infinite loop to occur.

    Code:
    int iFunGetCpuProcUsage( HANDLE hHandle ) 
    { 
    LONG lOldIdle, lOldUser, lNewUser, lOldKernel, lNewKernel, lNewIdle, lProcUsage, lUser, lKernel, lIdle, lSys; 
    DWORD dwOldTime, dwNewTime, dwTime; 
    FILETIME idleTime, kernelTime, userTime, ftCreate, ftExit, ftUser, ftKernel; 
    int iProcUsage; 
    
    dwOldTime = timeGetTime(); 
    LogMessage("Got to This Point1", "", "", "", "");
    if( ! GetProcessTimes( hHandle, &ftCreate, &ftExit, &ftUser, &ftKernel ) ) 
         { 
         printf("error old getprocesstime %d", GetLastError() ); 
         getch(); 
         } 
    LogMessage("Got to This Point2", "", "", "", "");
    lOldUser = iFunGetTime( ftUser ); 
    lOldKernel = iFunGetTime( ftKernel ); 
    LogMessage("Got to This Point3", "", "", "", "");
    Sleep( 1000 ); 
    
    LogMessage("Got to This Point4", "", "", "", "");
    dwNewTime = timeGetTime(); 
    if( ! GetProcessTimes( hHandle, &ftCreate, &ftExit, &ftUser, &ftKernel ) ) 
         { 
         
    	 printf("error new getprocesstime %d", GetLastError() ); 
         getch(); 
         }
    LogMessage("Got to This Point5", "", "", "", "");
    lNewUser = iFunGetTime( ftUser ); 
    lNewKernel = iFunGetTime( ftKernel ); 
    
    LogMessage("Got to This Point7", "", "", "", "");
    lKernel = lNewKernel - lOldKernel; 
    lUser = lNewUser - lOldUser; 
    LogMessage("Got to This Point8", "", "", "", "");
    dwTime = dwNewTime-dwOldTime; 
    LogMessage("Got to This Point9", "", "", "", "");
    
    if( dwTime == 0 ) 
         { 
         Sleep( 100 ); 
         dwNewTime = timeGetTime(); 
         dwTime = dwNewTime-dwOldTime; 
         } 
    LogMessage("Got to This Point10", "", "", "", "");
    iProcUsage =  (((lKernel+lUser)*100 )  / (Number_of_Processes * dwTime) ); 
    LogMessage("Got to This Point11", "", "", "", "");
    return iProcUsage; 
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    My guess would be that you don't see your error message, and it's sitting inside getch().

    Try either putting a fflush(stdout) or use fprintf(stderr, ...), or put a newline on the end of your error message:
    Code:
    if( ! GetProcessTimes( hHandle, &ftCreate, &ftExit, &ftUser, &ftKernel ) ) 
         { 
         fprintf(stderr, "error old getprocesstime %d", GetLastError() ); 
         getch(); 
         }
    I choose the second method, as that is what I would do.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program is hanging
    By mramazing in forum Networking/Device Communication
    Replies: 7
    Last Post: 03-18-2009, 04:13 PM
  2. pthread hanging
    By munna_dude in forum C Programming
    Replies: 7
    Last Post: 05-17-2007, 08:51 AM
  3. pthread hanging
    By munna_dude in forum C Programming
    Replies: 1
    Last Post: 05-17-2007, 05:07 AM
  4. Socket communication hanging
    By zee in forum C Programming
    Replies: 15
    Last Post: 08-05-2004, 11:52 PM
  5. pthreads - hanging process - gdb output
    By rotis23 in forum Linux Programming
    Replies: 1
    Last Post: 03-31-2004, 04:57 PM