Hello and welcome.
In this video session we will discuss process and its ID.
Each process in Linux has a unique identifier called as PID or process ID.
A process is created by its parent process.
In Linux, we have a parent and child process relationship.
In Linux.
The first process that is being created is the swapper process and it has the process id zero.
The swapper process.
In turn, folks, the new process, called as the 'INIT' process and the init process has a process ID
equal to one.
Init process plays an important role in Linux systems.
It creates and monitors a set of other process.
Init process also becomes the parent of any orphan process.
We will discuss more on the orphan process and fork system call in upcoming video sessions.
For more information on init process, we can see the manual pages.
This manual page can be referred for more information on init process.
Now let's see what is process ID?
We can get the ID of a running process using system call.
GetPID().
This is the syntax of getpid.
We need to include Unistd.h in order to use Getpid.
getpid() system call does not take any argument.
And it returns the ID of process from where it is called.
In 32 bit Linux system, we can have up to 32768 maximum process.
We can get the information of maximum supported process in system by reading this file.
In case of 64 bit Linux system, the value is much higher.
Let's see a example for this.
In our case, the Linux system is 64 bit.
Let's read the file content here.
So from this we can see that on a 64 bit Linux system supports 4194304 maximum number of processes.
As a note, the process ID starts from zero.
We can get the list of all the processes running in system by running ps -ef
Let's see this.
ps -ef is the command.
We have got the list of process and its details.
That is running at this moment.
First column is the UID that is user ID.
Who has created the process followed by pid, which is nothing but the process ID.
The third column is the PPID.
This is nothing but parent process ID.
Here, let's take PID 10, 11 and 12.
These processes are created by its parent whose process ID is two.
This means the PPID of process 10 11, 12 is 2.
Similar to getpid() the system call to get the parent process ID is called getppid()
Let's see this with example program.
Here we have a simple example which gives the id of current running process using Getpid.
And we can also get the parent process ID by calling getppid() .
As discussed in Linux, everything is considered as a process.
So when we run this program, a new process is created by its parent process.
We will run this program and examine its output.
Let's compile the code and generate the output file called process_demo.
.
Now let's execute this program.
In other tab, let's run ps -ef command and check the pid and ppid.
Here we can see process_demo and its parent process ID that is ppid.
Let's examine the output of process_demo.
.
We can see that we have got same PID and PPID as we saw by running ps -ef Command.
So here when we run ./process_demo from shell or terminal.
This shell is the parent process.
And it forks a child process and creates process_demo.
program.
Let's run the program again.
Now, if we observe the process ID is different from previous executed process ID.
This means process ID is not fixed and it varies from execution to execution.
But during execution, the process ID is unique.
Which means no other process can have same process ID.
When the process terminates, this process ID is given back to system.
And Linux can reuse this process ID later.
Also, if we observe when we execute the program every time the PID is changing, whereas the PPID is
same because the shell that is executing the program is same.
Now let's execute the same program from other terminal.
We can see that the ppid is now changing.
So with this, we can also infer that each terminal is executing as a unique process ID.
In next video session we will learn more about process states.
Thank you.