1

Environment: OS --debian + python3.
All the output info below ommit unimportant.
Get my computer's cpu info with cat /proc/cpuinfo :

cat /proc/cpuinfo
processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model name  : Intel(R) Celeron(R) CPU G1840 @ 2.80GHz
physical id : 0
siblings    : 2
core id     : 0
cpu cores   : 2

processor   : 1
vendor_id   : GenuineIntel
cpu family  : 6
model name  : Intel(R) Celeron(R) CPU G1840 @ 2.80GHz
physical id : 0
siblings    : 2
core id     : 1
cpu cores   : 2

Here is mthreads.py to be tested.

import os
import threading
print(os.getpid())
def dead_loop():
    while True:
        pass

t = threading.Thread(target=dead_loop)
t.start()


dead_loop()

t.join()

Run it in a terminal with python3 mthreads.py,get the output 3455 which is the process id of python3 mthreads.py.

cat  /proc/3455/status
Name:   python3
Umask:  0022
State:  S (sleeping)
Tgid:   3455
Ngid:   0
Pid:    3455
PPid:   2205
Threads:    2
Cpus_allowed:   3
Cpus_allowed_list:  0-1

Run it in terminal.

python3 mthreads.py
3455

1.There are 2 cpu in my pc,why the Cpus_allowed is 3 ,more than my cpu?

pstree  3455 -p
python3(3455)───{python3}(3456)

2.There aer 2 threads running now, 3455 is the process id ,3456 is the thread id , which is the other thread id? How to get the second thread id number?

3.I want to know which process id is running on which cpu (cpu0 ,cpu1 )?

enter image description here

  • A process has, by default, at least 1 thread, the process itself. Your process has two threads, the main process thread and a new thread you started, so there are only two thread ids. 3455 is one (and it's the process itself), 3456 is the other. – Martijn Pieters Jan 27 '18 at 18:42
  • None of this is Python specific; your questions are basic Linux process questions. – Martijn Pieters Jan 27 '18 at 18:43
0
+50

Sometimes process id=thread id.
Show my code.

python3 mthreads.py
7761
cat /proc/7761/status|grep Threads
Threads:    2

pstree -p  7761
python3(7761)───{python3}(7762)

LWP means light weight process (thread) ID of the dispatchable entity (alias spid, tid) ,NLWP means number of lwps (threads) in the process in man ps page.

ps -p 7761   -f -L
UID        PID  PPID   LWP  C NLWP STIME TTY          TIME CMD
user   7761  2305  7761 48    2 19:28 pts/1    00:00:09 python3 mthreads.py
user   7761  2305  7762 51    2 19:28 pts/1    00:00:09 python3 mthreads.py

prcess id--7761 contains two threads,one thread id is 7761 same value as process id,other thread id is 7762.

| improve this answer | |
1
  1. http://man7.org/linux/man-pages/man5/proc.5.html
  • Cpus_allowed: Mask of CPUs on which this process may run
    (since Linux 2.6.24, see cpuset(7)).

This is not the number of CPUs, but simply telling you which CPU your programs threads are executing on

  1. PPID stands for Parent Process ID. It's the parent process of the process you are inspecting.

  2. http://linuxcommand.org/lc3_man_pages/ps1.html

psr PSR processor that process is currently assigned to.

The example straight from the man page for reference: ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm

Most of these this type of info can be found in the man pages, but I understand they contain a lot of information and it can be kind of hard to track down.

| improve this answer | |
  • The ppid is 2205 in my case,i am not inpecting 2205,your are wrong. – showkey Jan 25 '18 at 3:01
  • @it_is_a_literature: you misunderstand what Michael is saying. No, you are not inspecting 2205. You are inspecting a child process of the 2205 process, so the information on that child process lists 2205 as the parent process. – Martijn Pieters Jan 27 '18 at 18:40
  • which is the other thread id? – showkey Jan 29 '18 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.