Even if you search by Linux, pid, only the method in Bash is available, so a summary of the method in C / C ++
pid => getpid() Man page of GETPID
ppid => getppid() Man page of GETPID
tid => syscall(SYS_getid) Man page of GETTID
test.cpp
#include <iostream>
#include <future>
#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>
pid_t gettid(void) {
return syscall(SYS_gettid);
}
int main() {
std::cout << "[0] pid : " << getpid() << std::endl;
std::cout << "[0] ppid : " << getppid() << std::endl;
std::cout << "[0] tid : " << gettid() << std::endl;
auto func = [] {
std::cout << "[1] pid : " << getpid() << std::endl;
std::cout << "[1] ppid : " << getppid() << std::endl;
std::cout << "[1] tid : " << gettid() << std::endl;
};
auto th = std::thread(func);
th.join();
return 0;
}
Execution result
$ g++ -pthread -std=gnu++11 test.cpp && ./a.out
[0] pid : 13762
[0] ppid : 9459
[0] tid : 13762
[1] pid : 13762
[1] ppid : 9459
[1] tid : 13763
[Linux] [C / C ++] Get tid (thread id) / Wrap pthread_create to get tid of child thread --Qiita
Recommended Posts