thelsdj
thelsdj

Reputation: 9144

Get current process CPU usage in C

On Windows I can do:

HANDLE hProcess = GetCurrentProcess();

FILETIME ftCreation, ftExit, ftKernel, ftUser;

GetProcessTimes(hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser);

SYSTEMTIME stKernel;
FileTimeToSystemTime(&ftKernel, &stKernel);

SYSTEMTIME stUser;
FileTimeToSystemTime(&ftUser, &stUser);

printf("Time in kernel mode = %uh %um %us %ums", stKernel.wHour,
           stKernel.wMinute, stKernel.wSecond, stKernel.wMilliseconds));
printf("Time in user mode = %uh %um %us %ums", stUser.wHour,
           stUser.wMinute, stUser.wSecond, stUser.wMilliseconds));

How can I do the same thing on *nix?

Upvotes: 1

Views: 2429

Answers (1)

Magnus Westin
Magnus Westin

Reputation: 861

Check getrusage, I think that should solve your problem.

Upvotes: 3

Related Questions