CPU usage

This is a discussion on CPU usage within the C Programming forums, part of the General Programming Boards category; i'm new to c coding and stuff.this code i wrote to calculate the cpu utilization. but now i want the ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    2

    CPU usage

    i'm new to c coding and stuff.this code i wrote to calculate the cpu utilization.
    but now i want the cpu utilization of a particular process when i run it based on its pid. suppose a get the pid of particular process from the cmd>tasklist
    now when i use that as input, i should get the cpu utilization.
    getpid() returns the process id but i dont need it.
    someone help me please
    thanks in advance



    here is the code.

    Code:
    #include<time.h>
    #include<unistd.h>
    #include<sys/types.h>
    
    int main(void)
    {
    int n;
    for(;;)
    {
    printf("process pid:&#37;d",getpid());
    printf("\n");
    printf("CPU Usage: %3d%%\r",CPUusage());
    printf("\n");
    sleep(500);
    }
    
    return 0;
    
    }
    
    CPUusage()
    {
    clock_t start, end;
    double elapsed;
    double P;
    int i,j,k,m;
    
    start = clock();
    for( i = 0 ; i < 1000 ; i++)
    for( j = 0 ; j < 100 ; j++)
    for( k = 0 ; k < 50 ; k++)
    for(m=0; m < 5;m++);
    end = clock();
    elapsed = (((double) (end - start)) / CLOCKS_PER_SEC);
    P = elapsed*100;
    return(P);
    }
    Last edited by sandy143; 07-13-2007 at 12:08 AM.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Indenting wouldn't hurt, and since it's platform specific browse around MSDN.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    2
    i'm working on windows XP
    i'm using dev C++ as compiler

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    I think you need to use Win32 API. Windows Vista is better in it. MSDN is where you should go.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reduce CPU usage
    By patrick22 in forum Windows Programming
    Replies: 9
    Last Post: 07-10-2009, 02:13 PM
  2. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 07:44 AM
  3. Net cpu usage of pthreads?!
    By mynickmynick in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2008, 07:59 AM
  4. Calculating CPU Usage
    By vitaliy in forum Linux Programming
    Replies: 3
    Last Post: 08-21-2005, 09:38 AM
  5. CPU Usage so high
    By X PaYnE X in forum Windows Programming
    Replies: 9
    Last Post: 12-21-2003, 02:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21