Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5839027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:31:53+00:00 2026-05-22T11:31:53+00:00

How can I know in a C program, on which physical processor and core

  • 0

How can I know in a C program, on which physical processor and core my code is running?
I’m using Linux and gcc 4.4.3.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:31:54+00:00Added an answer on May 22, 2026 at 11:31 am

    You may check /proc//stat file system, according to http://www.kernel.org/doc/Documentation/filesystems/proc.txt, you should just check task_cpu flag.

    As example without correct types and error checking:

    struct pstat
    {
      int  pid;       //process id
      char tcomm[256];//filename of the executable
      char state[2];  //state (R is running, S is sleeping, D is sleeping in an
                      //uninterruptible wait, Z is zombie, T is traced or stopped)
      int ppid;//          process id of the parent process
      int pgrp;//          pgrp of the process
      int sid;//           session id
      int tty_nr;//        tty the process uses
      int tty_pgrp;//      pgrp of the tty
      int flags;//         task flags
      int min_flt;//       number of minor faults
      int cmin_flt;//      number of minor faults with child's
      int maj_flt;//       number of major faults
      int cmaj_flt;//      number of major faults with child's
      int utime;//         user mode jiffies
      int stime;//         kernel mode jiffies
      int cutime;//        user mode jiffies with child's
      int cstime;//        kernel mode jiffies with child's
      int priority;//      priority level
      int nice;//          nice level
      int num_threads;//   number of threads
      int it_real_value;//  (obsolete, always 0)
      int start_time;//    time the process started after system boot
      int vsize;//         virtual memory size
      int rss;//           resident set memory size
      int rsslim;//        current limit in bytes on the rss
      int start_code;//    address above which program text can run
      int end_code;//      address below which program text can run
      int start_stack;//   address of the start of the stack
      int esp;//           current value of ESP
      int eip;//           current value of EIP
      int pending;//       bitmap of pending signals
      int blocked;//       bitmap of blocked signals
      int sigign;//        bitmap of ignored signals
      int sigcatch;//      bitmap of catched signals
      int wchan;//         address where process went to sleep
      int i0;//             (place holder)
      int i1;//             (place holder)
      int exit_signal;//   signal to send to parent thread on exit
      int task_cpu;//      which CPU the task is scheduled on
      int rt_priority;//   realtime priority
      int policy;//        scheduling policy (man sched_setscheduler)
      int blkio_ticks;//   time spent waiting for block IO
      int gtime;//         guest time of the task in jiffies
      int cgtime;//        guest time of the task children in jiffies
    } p ;
    
    int main()
    {
        char name[256];
        char state[8];
        FILE* f = fopen("/proc/self/stat", "r");
    
        fscanf(f,  "%d%s%s%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d",
               &p.pid, &p.tcomm, &p.state, &p.ppid, &p.pgrp, &p.sid, &p.tty_nr, &p.tty_pgrp, &p.flags,
               &p.min_flt, &p.cmin_flt, &p.maj_flt, &p.cmaj_flt, &p.utime, &p.stime,  &p.cutime, &p.cstime,
               &p.priority, &p.nice, &p.num_threads, &p.it_real_value, &p.start_time,  &p.vsize, &p.rss,
               &p.rsslim, &p.start_code, &p.end_code, &p.start_stack, &p.esp, &p.eip,  &p.pending, &p.blocked,
               &p.sigign, &p.sigcatch, &p.wchan, &p.i0, &p.i1, &p.exit_signal,  &p.task_cpu, &p.rt_priority, &p.policy,
               &p.blkio_ticks, &p.gtime, &p.cgtime);
    
         printf("CPU %d\n", p.task_cpu);
     return 0;  
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.