How to get the running process' pid?

How to get the running process' process ID (pid)?

asked Apr 1 by zma (2,200 points)

4 Answers

In Bash, you can get the process ID from the variable $$.

Note that in a subshell invoked by ‘(...)‘, $$ is actually the parent process' pid.

In Bash 4, you can also use $BASHPID to get the process pid.

answered Apr 3 by zma (2,200 points)

In Python, you can get the pid of the current process by

import os


From the official doc:


Return the current process id.
answered Apr 4 by zma (2,200 points)

In Go, you may call the os.Getpid() func from the os package to get the parent process' pid.

func Getpid() int

Getppid returns the process id of the caller.

One example is as follows.

$ gore
gore version 0.2.6  :help for help
gore> :import "fmt"
gore> :import "os"
gore> fmt.Printf("pid: %d\n", os.Getpid())
pid: 6756
answered Apr 18 by zma (2,200 points)

In C and C++, you can call the getpid() library function which is a function from the POSIX library.

#include <sys/types.h>
#include <unistd.h>

pid_t getpid(void);

getppid() returns the process ID of the calling process.

Example usage:


#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
  pid_t pid = getpid();

  printf("pid: %lu\n", pid);

Build and run it:

$ gcc getpid.c -o s && ./s
ppid: 7108
answered Apr 18 by zma (2,200 points)
edited Apr 22 by zma

Please log in or register to answer this question.

Related questions

5 answers
7 answers
asked Mar 31 by zma (2,200 points)
5 answers
asked Mar 31 by zma (2,200 points)
0 answers
Welcome to Do This In Various Langs (dtivl), where you can ask questions and receive solutions in various programming languages.
Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost