[Linux] [C / C ++] Summary of how to get pid, ppid, tid

Even if you search by Linux, pid, only the method in Bash is available, so a summary of the method in C / C ++

API to use

pid => getpid() Man page of GETPID

ppid => getppid() Man page of GETPID

tid => syscall(SYS_getid) Man page of GETTID

Implementation example

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

Relation

[Linux] [C / C ++] Get tid (thread id) / Wrap pthread_create to get tid of child thread --Qiita

Recommended Posts

[Linux] [C / C ++] Summary of how to get pid, ppid, tid
[Linux] [C / C ++] Get tid (thread id) / Wrap pthread_create to get tid of child thread
Summary of how to use pandas.DataFrame.loc
Summary of how to use pyenv-virtualenv
Summary of how to use csvkit
[Linux] [C / C ++] How to get the return address value of a function and the function name of the caller
Here's a brief summary of how to get started with Django
[Python] Summary of how to use pandas
How to get rid of long comprehensions
[Python2.7] Summary of how to use unittest
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
How to get started with laravel (Linux)
Summary of how to write AWS Lambda
Summary of how to import files in Python 3
Summary of how to use MNIST in Python
How to get dictionary type elements of Python 2.7
How to get the number of digits in Python
How to use C216 Audio Controller on Arch Linux
[C language] [Linux] Get the value of environment variable
[Python] Summary of how to specify the color of the figure
Summary of how to share state with multiple functions
How to limit the API to be published in the C language shared library of Linux
linux / c> link> Get the execution result of the shell command in the C program> I was taught how to use popen ()
How to get rid of server custom emoji in message.content
[python] Summary of how to retrieve lists and dictionary elements
Summary of Linux distribution types
[Python] Summary of how to use split and join functions
How to get Instruction Pointer (= program counter) in Linux kernel
How to get a list of built-in exceptions in python
Summary of how to write .proto files used in gRPC
A brief summary of Linux
How to get an overview of your data in Pandas
How to get a list of links from a page from wikipedia
How to get a quadratic array of squares in a spiral!
[Linux] How to disable automatic update of /etc/resolv.conf file (Redhat)
How to display a specified column of files in Linux (awk)
[Hugo] Summary of how to add pages to sites built with Learn
How to get the ID of Type2Tag NXP NTAG213 with nfcpy
How to get the printer driver for Oki Mac into Linux
[Python] How to get divisors of natural numbers at high speed
[Python] How to get the first and last days of the month
[Linux] How to disable the automatic update of the /etc/resolv.conf file (AmazonLinux2)
How to get all traffic through VPN with OpenVPN on Linux
How to output the output result of the Linux man command to a file
How to get the vertex coordinates of a feature in ArcPy
[Linux] [C / C ++] backtrace acquisition method summary
How to install wkhtmltopdf (Amazon Linux2)
How to install VMware-Tools on Linux
How to get the Python version
How to install MBDyn (Linux Ubuntu)
How to get started with Scrapy
How to get started with Python
How to get started with Django
How to wrap C in Python
How to build MongoDB C driver
How to check Linux OS version
Summary of how to write if statements (Scala, Java, Rust, C language, C ++, Go language, PHP, Perl, Python, Ruby)
Summary of how to write increment / decrement (Scala, Java, Rust, C, C ++, Go, PHP, Perl, Python, Ruby, JavaScript)
[Linux] Command to get a list of commands executed in the past
Verification of how to periodically execute a script on a Linux server on Windows