0

I have made an application in c which creates 4 threads. How can I check which thread is running on which core through terminal and through code.

Barinder Grewal
  • 49
  • 1
  • 1
  • 6
  • Have you tried `pthread_getaffinity_np()` ? http://man7.org/linux/man-pages/man3/pthread_setaffinity_np.3.html – Sergei Kulik Dec 29 '15 at 11:19
  • 2
    Possible duplicate of http://stackoverflow.com/questions/16381138/check-which-thread-is-running-on-which-cpu-in-linux – roottraveller Dec 29 '15 at 11:21
  • Normally, the OS distributes threads to the different CPUs, so any CPU that it's running on right now may or may not be the same CPU it's running on in a millisecond. However, you can control thread affinity with POSIX threads, which Linux supports, or you can use e.g. a tool like `htop` to assign threads/processes to CPUs. – Ulrich Eckhardt Dec 29 '15 at 11:54
  • @SergeiKulik pthread_getaffinity_np() is always returning 0, even though I am setting affinity using pthread_setaffinity_np(). – Barinder Grewal Dec 30 '15 at 05:39
  • What does the manpage have to say about that particular returnvalue? – Ulrich Eckhardt Dec 30 '15 at 08:30

1 Answers1

1

I found the answer about how to check it using terminal.
/home# for i in $(pgrep applicationName); do ps -mo pid,tid,fname,user,psr -p $i;done

Barinder Grewal
  • 49
  • 1
  • 1
  • 6