LinuxQuestions.org
Visit Jeremy's Blog.
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-14-2009, 07:57 AM   #1
sinu_nayak2001
Member
 
Registered: Oct 2009
Location: India
Distribution: Puppy Linux, Wary 530.
Posts: 31

Rep: Reputation: 20
thread and cpu it is assigned to


Dear All,

Do we have any function, which when called, will return the cpu number on which the thread --calling the function-- is running.

If this is possible, we can visualize the sceduling of threads.

Thanks and regards,
Srinivas
 
Old 10-15-2009, 01:34 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
http://linux.die.net/man/2/sched_getaffinity followed by CPU_ISSET().
 
Old 10-15-2009, 08:06 AM   #3
sinu_nayak2001
Member
 
Registered: Oct 2009
Location: India
Distribution: Puppy Linux, Wary 530.
Posts: 31

Original Poster
Rep: Reputation: 20
thanks chrism01.

Again I searched the internet, after getting clues from chrism01's answer. I reached at the page,

http://help.lockergnome.com/linux/Al...ict486238.html

This is what I was looking for.

Thanks to David Schwartz for posting the example code on the above thread...I am just siting his code here...

Quote:
Interestingly, recent versions of Linux (with glibc-2.6) have
'sched_getcpu'. It's not particularly useful, but it does exist.

Here's some sample code:

#include <sched.h>
#include <stdio.h>
#include <pthread.h>

void *tf(void *t)
{
int i, j, q, cpu;
for(i=0; i<25600; i++)
for(j=0; j<25600; j++)
{ /* burn some CPU */
q>>=2;
q+=i;
q-=j;
}
cpu=sched_getcpu();
for(i=0; i<25600; i++)
for(j=0; j<25600; j++)
{ /* keep burning */
q<<=2;
q+=i;
}
printf("%d Running on CPU %d\n",
(int) t, cpu);
*(volatile int *) &q=q+1; /* don't let compiler optimize q away */
return (void *) cpu;
}

int main(void)
{ /* create 8 CPU burners and see what CPUs they run on */
pthread_t t[8];
int i;
void *r;
for(i=0; i<8; i++)
pthread_create(&t[i], NULL, tf, (void *) (i+1));
for(i=0; i<8; i++)
pthread_join(t[i], &r);
}

On my quad-core, this is typical:

8 Running on CPU 1
1 Running on CPU 1
4 Running on CPU 3
3 Running on CPU 3
5 Running on CPU 2
6 Running on CPU 0
2 Running on CPU 2
7 Running on CPU 0

DS
 
  


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
thread affinity for a cpu core aloktiagi Linux - Newbie 4 08-21-2009 05:53 AM
viewing CPU usage by thread jsfritz Linux - General 3 11-30-2006 04:11 PM
Thread CPU Utilization frakman1 Programming 3 01-20-2006 01:27 PM
how does the kernel decide which thread gets the cpu? Thinking Programming 8 10-19-2005 12:22 PM
How to assign CPU usage with Thread wangru Programming 5 08-25-2004 10:14 AM

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

All times are GMT -5. The time now is 01:56 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