Selbst wenn Sie unter Linux suchen, ist nur die Methode in Bash verfügbar, daher eine Zusammenfassung der Methode 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;
}
Ausführungsergebnis
$ 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
Recommended Posts