LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-30-2009, 05:01 PM   #1
ufmale
Member
 
Registered: Feb 2007
Posts: 386

Rep: Reputation: 30
CPU usage of each threads


I understand that each thread will use a certain CPU time controlled by
scheduler. How do I find out how much each thread use the CPU time?
I was thinking to tick the time at the beginning of each thread, and again at the end, but it is just telling me the time the thread start and the time the thread end, but it does not tell me the CPU time.

Here is the code I modified from my previous post.
http://www.linuxquestions.org/questi...fusion-765421/

Thanks to rupertwh.

Code:
#include <stdio.h>
#include <pthread.h>

int g_stop = 0;

void* mythread(void *parg);

int main(void)
{
        pthread_t tid[2];
        int i;
        void *tret;

        if (pthread_create(&tid[0], NULL, mythread, NULL)) {
                perror("Couldn't create mythread");
                return 1;
        }

        if (pthread_create(&tid[1], NULL, mythread, NULL)) {
                perror("Couldn't create mythread");
                return 1;
        }

        for (i = 0; i < 100; i++) {
                putchar('M');
                fflush(stdout);
        }
        g_stop = 1;

        pthread_join(tid[0], &tret);
        pthread_join(tid[1], &tret);
        printf("\nmain thinks: g_stop=%d\n", g_stop);

        return 0;
}


void* mythread(void *parg)
{
        while (!g_stop) {
                putchar('t');
                fflush(stdout);
        }
        g_stop = 5;

        printf("\nmythread thinks: g_stop=%d\n", g_stop);

        return NULL;
}

Last edited by ufmale; 10-30-2009 at 05:03 PM.
 
Old 10-30-2009, 05:06 PM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
i dont think you can, you can only get processor info as far down as the process.
BTW whats with your username?
----edit----
and you can get the time the thread took by suptracting endd from start

Last edited by smeezekitty; 10-30-2009 at 05:08 PM.
 
Old 10-30-2009, 05:50 PM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Have a look to the getrusage RUSAGE_THREAD kernel patch. It might be implemented in recent kernels.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
CPU usage 100% due to loops in multiple threads Manjunath1847 Linux - General 4 08-10-2009 02:45 AM
processes/CPU Usage/Mem Usage desktop wallpaper ceantuco Linux - Newbie 2 04-13-2009 01:14 PM
getting realtime info on memory usage-cpu and harddrive usage steering Linux - Newbie 5 03-03-2005 08:43 PM
how to determine cpu usage, memory usage, I/O usage by a particular user logged on li rags2k Programming 4 08-21-2004 04:45 AM
Controlling CPU usage & memory usage Saravana babu Linux - Software 0 02-18-2004 05:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration