Process Management[2] – Process Identifier

The previous post helped us to understand about Process Overview. This post I will show you about the way processes managed in Unix System.

1. What are Process Identifiers?

Process Identifiers are the unique non-negative integer numbers, used to guarantee uniqueness of the process.

Now check the processes running on your BBB, by using ps command command:

debian@beaglebone:~$ ps -aux

Then you can see PID (Process Identifier) of each process:

debian@beaglebone:~$ ps -aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  1.0  1.0  25540  5096 ?        Ss   01:55   0:03 /sbin/init
root         2  0.0  0.0      0     0 ?        S    01:55   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        I    01:55   0:00 [kworker/0:0]
root         4  0.0  0.0      0     0 ?        I<   01:55   0:00 [kworker/0:0H]
root         6  0.0  0.0      0     0 ?        I<   01:55   0:00 [mm_percpu_wq]
...
debian    2158  0.0  0.5  27184  2720 ?        S    01:56   0:00 (sd-pam)
debian    2162  0.1  0.7   5456  3532 ttyS0    S    01:56   0:00 -bash
debian    2182  0.0  0.4   6728  2376 ttyS0    R+   02:00   0:00 ps -aux

There are some special processes:

  • PID 0: scheduler process, often known as the swapper. No program on disk corresponds to this process, which is part of the kernel and is known as system process.
  • PID 1: the init process and is invoked by the kernel at the end of the bootstrap procedure.
  • PID 2: the kthreadd is the kernel thread daemon. All kthreads are forked off this thread.

By using Linux provided function, we can check the PID of running process by using function list:

FunctionResult
pid_t getpid(void);process ID of calling process
pid_t getppid(void);parent process ID of calling process
uid_t getuid(void);real user ID of calling process
uid_t geteuid(void);effective user ID of calling process
gid_t getgid(void);real group ID of calling process
gid_t getegid(void);effective group ID of calling process

2. Get PID example with BBB

Now let’s build a program and run on BBB, then check all its PID information.

First, open your Linux host machine and create file name 01_PID_checking.c:

#include <unistd.h>   /* Header for pid_t type and check PID functions */
#include <stdio.h>    /* For printf function */   

int main(void)
{
	printf("Process ID of calling process is: %d\n", (pid_t) getpid());
	printf("Parent process ID of calling process is: %d\n", (pid_t) getppid());
	printf("Real user ID of calling process is: %d\n", (pid_t) getuid());
	printf("Effective user ID of calling process is: %d\n", (pid_t) geteuid());
	printf("Real group ID of calling process is: %d\n", (pid_t) getgid());
	printf("Effective group ID of calling process is: %d\n", (pid_t) getegid());

	return 0;
}

Build the program using cross compile method I showed you:

arm-linux-gnueabihf-gcc -o 01_PID_checking 01_PID_checking.c

Copy output file from Host machine to your BBB using scp:

sudo scp 01_PID_checking debian@192.168.1.10:/home/debian

Open Teraterm and set 2 connection through SSH, one to run application, the other one for result checking

On BBB, first run your program. This time, your process is created:

debian@beaglebone:~$ ./01_PID_checking
Process ID of calling process is: 2349
Parent process ID of calling process is: 2326
Real user ID of calling process is: 1000
Effective user ID of calling process is: 1000
Real group ID of calling process is: 1000
Effective group ID of calling process is: 1000

Then check the result by ps -aux command. You will see your program running

debian@beaglebone:~$ ps -aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  1.0  25540  5096 ?        Ss   09:08   0:05 /sbin/init
...
debian    2158  0.0  0.5  27184  2720 ?        S    09:09   0:00 (sd-pam)
debian    2162  0.0  0.7   5456  3532 ttyS0    S+   09:09   0:00 -bash
root      2199  0.0  0.0      0     0 ?        I    10:11   0:00 [kworker/0:2]
root      2308  0.0  0.0      0     0 ?        I    10:19   0:00 [kworker/0:0]
root      2311  0.0  0.0      0     0 ?        I    10:24   0:00 [kworker/0:1]
root      2321  0.3  0.9   9908  4800 ?        Ss   10:25   0:00 sshd: debian [priv]
debian    2325  0.0  0.7   9908  3724 ?        S    10:26   0:00 sshd: debian@pts/0
debian    2326  0.6  0.7   5572  3644 pts/0    Ss   10:26   0:00 -bash
root      2335  0.8  0.9   9908  4856 ?        Ss   10:26   0:00 sshd: debian [priv]
debian    2339  0.3  0.7   9908  3740 ?        S    10:26   0:00 sshd: debian@pts/1
debian    2340  1.9  0.7   5572  3644 pts/1    Ss   10:26   0:00 -bash
debian    2349  106  0.1   1352   868 pts/0    R+   10:26   0:05 ./01_PID_checking
debian    2350  0.0  0.4   6728  2380 pts/1    R+   10:26   0:00 ps -aux

Let’s check these number:

  • Process ID is 2349, as same as C program and ps command
  • Parent PID is 2362, which is process of bash program, reason we call ./01_PID_checking and bash is the default program to run it.
  • User ID and Group ID is 1000. Reason is Linux starts creating normal users from UID 1000. Root user will have UID 0. You can check it by yourself by:
debian@beaglebone:~$ sudo su
[sudo] password for debian:
root@beaglebone:/home/debian# ./01_PID_checking
Process ID of calling process is: 2359
Parent process ID of calling process is: 2358
Real user ID of calling process is: 0
Effective user ID of calling process is: 0
Real group ID of calling process is: 0
Effective group ID of calling process is: 0

3. Process termination

Now we will terminate running process by using “kill“:

debian@beaglebone:~$ sudo kill -9 2359

This action will “kill” your running process, then ps -aux, your process is stopped.

4. What should we do next?

In this post, we do an example for process life cycle, and how the Process is managed by PID.

The question is what we can do to create and terminate new process by running process. I will show you the answer in next post.

Leave a Reply

Your email address will not be published. Required fields are marked *