What is pid in linux

Linux:- What is Process ID / PID? and How to Find it?

In this article we will learn what is Process ID / PID in Linux. Process ID is a unique identification number which is used to identify a particular process in Linux. PID has 16 bit number’s that are sequentially assigned to different processes on after the other.

After process execution, the process id number released from the process table for reuse.

PID stands for process identifier. It is an identification number that is automatically assigned to each process when it is create on Linux System. It is unique to each and every processes running on the Linux System. PID number start from 1. Process id 1 is always reserved for init process.

Why INIT gets the same PID number each and every time?

Answer is init is the first program that’s run on Linux System. That’s why PID number 1 is given to it and process id always starting from 1 in Linux. As we know INIT is the first process that get’s started and it is also the parent of all other processes in Linux.

On Linux System there is a limit to the maximum number of processes to run. This was started from kernel version 2.5. You can find the maximum number of PID of a Linux system using below command.

# cat /proc/sys/kernel/pid_max 32768

Above you can see that the maximum number of process id’s are 32768. It means only 32768 number of process can be run simultaneously at a time on Linux system.

If you see higher PID number 25000 on the system it does not means 25000 processes are running on Linux system. A process ID number 5000 does not means that it was started before the PID 25000. Because process id number always reused once process released.

You can increase the total number of PID if you have large amount of RAM is available on system. It is good to increase the number of processes that a server can handle.

Follow this article to find process id : How to find process id/pid in Linux?

Follow the below command to increase the maximum number of process ID on Linux system using below command.

Increase PID Number on Linux System

You will need to update sysctl.conf file to increase the max PID in Linux.

# vim /etc/sysctl.conf kernel.pid_max=

64 Bit Linux system can have only 4194303 is the maximum number of PID. So you will need to modify pid_max with higher value such as 4194303 by modifying Linux “/etc/sysctl.conf” file.

Читайте также:  Удалить графические оболочки linux

I hope this article will help to understand the Process ID or PID in Linux. If you have any queries and problem please comment in comment section.

Источник

What is a .pid File in Linux?

On Linux, a “.pid” file is a process identification (PID) file. It is used to store the process ID (PID) of a running process. The PID is a unique number assigned to each process when it is created and is used to identify the process in the operating system. The .pid file is usually located in the /var/run or /var/run/ directory and is named after the process it represents. In this article, we will discuss what .pid files are, how they are used, and how to work with them.

What is a PID file?

A PID file is a simple text file that contains the PID of a running process. The file is created when the process starts and is deleted after the process ends. System administrators, system scripts and other processes use the PID file to identify and interact with the running process. These files are especially useful when it comes to service management, process monitoring, and signals.

For example, a service script can use the PID file to determine if a service is running or stop the service by sending a signal to the process. A system administrator can use the PID file to view information about the process or to terminate the process. This is done using commands such as “pgrep” and “kill”, which we will discuss in detail later in this article.

Creating a .pid file

Creating a .pid file is a simple process and can be done with a simple command. One way to create a “.pid” file in a script is to pipe the output of “$$” to a file (in Bash shell) −

$$ is a Linux variable that returns the PID of the calling process. In this case, it’s the PID of the shell.

Another way to create a .pid file is to use a simple script like the following −

#!/bin/bash # create file pid_file="process.pid" echo $$ > $pid_file count=0 while [ $count -le 10 ] do echo Going $count.. sleep 1 count=$(($count+1)) done

When this script is run it will spawn the process and create the .pid file containing the process ID.

Location of the .pid file

When it comes to the location of “.pid” files, there is no specific rule as to where they should be stored. However, there are some commonly used locations for these files. Typically, our process places files in /var/run. To avoid conflicts with other processes, we could go one step further and create a new directory, /var/run/myScript. However, some systems may have this directory owned by root, in which case it may not be possible to write the .pid file there. A second option would be the home (/home/user) directory.

Читайте также:  Linux what is pae

Kill a process using a .pid file

One of the primary uses of “.pid” files is to kill a process while it’s running. If there is a .pid file, we can get the PID of the file and then use it with xargs and kill. This ensures that we only need to know the name and location of the “.pid” file and not the PID itself.

$ cat process.pid | xargs kill

This command will take the contents of the .pid file, which is the process ID, and pass it as an argument to the kill command. This ensures that we only stop the exact process we want, instead of having to manually search for the process.

Guarantee a single instance of an application

Another use for .pid files is to ensure that only a single instance of an application is running. To do this, we need to remove the .pid file at the end of our run and add a check at the beginning to see if a .pid file exists. This can be done using the following script −

#!/bin/bash pid_file="process.pid" if [ ! -f $pid_file ]; then echo $$ > $pid_file count=0 while [ $count -le 10 ] do echo Going $count.. sleep 1 count=$(($count+1)) done rm $pid_file else echo "Process already running" fi

In this script, we first check if the “.pid” file exists. If it doesn’t exist, we proceed to create the file and run the script. Once the script has finished running, the .pid file is deleted. However, if the .pid file already exists, the script is already running, so the message «The process is already running» is displayed and the script does not run.

This is a simple yet effective way to ensure that only one instance of the script is running at any given time.

Conclusion

In this article, we discuss what .pid files are and how they are used in Linux. We cover creating and locating “.pid” files, as well as tasks that can be performed with .pid files, such as killing a process and ensuring a single instance of an application. .pid files are a convenient way to track running processes and allow system administrators, scripts, and other processes to easily identify and interact with running processes. Understanding how to use .pid files can greatly simplify the process of administering and maintaining Linux systems.

Источник

What does the PID number mean in Linux?

I’m wondering what is the meaning of the process identifier in Linux, is it the order of the process? Is it a code that identifies the nature of the process or simply a number randomly generated to uniquely identify a process? Are different processes with a similar PID related in some way?

2 Answers 2

When the Linux kernel launches a new process it assigns a unique integer number to it starting with 1 sequentially. You may notice that /sbin/init is normally PID 1 because it’s the first launched process.

Are different processes with a similar PID related in some way?

No. If their PIDs are close they might have been launched approximately at the same time. On a 32bit Linux PIDs are limited to 32768, so once the kernel reaches this number, it will start again. On a 64bit Linux PIDs are limited to 2^22 and rarely overflow (unless you have a very long uptime and launch and stop thousands of processes).

Читайте также:  Включить рут права линукс

I started a process and the system wrote PID 26471 on the bash, then I started another process and the system wrote 4582. How it is possible that it passed from 26471 to the end and again to 4582 if PIDs are assigned consecutively?

The counter might have overflown and started from 1. Would you mind editing your post and adding ps axc output? It’s safe to do.

The PID range is determined by /proc/sys/kernel/pid_max . While this may be absurdly large for a 64-bit system, most distributions that I’ve checked still limit this to 32768.

@MégasAléxandros: Practically all OSs have been randomizing PIDs for decades, because of the obvious security implications.

PID is short for ‘process identifier’. That’s exactly what it is, a way to ‘uniquely’ identify a process on the system. Note that I have ‘uniquely’ in quotes here. This is because a PID is only unique for the lifetime of the process it is assigned to.

As far as how a PID is chosen, it varies by system. The original approach is to simply assign the next number that has not been used, up to some maximum value, and once you get to that max you start reusing previously used but currently unused numbers, starting back from the lowest such number again.

Linux takes that original approach, because it’s simple and fast. The downside is that some poorly written software may rely on PIDs in ways that it should not (such as using them to seed an internal random number generator or create a temporary file name), which allows for some potentially nasty local exploits if you’re using such software (but such software is thankfully increasingly rare).

Some systems, such as OpenBSD, instead pick PIDs at random from the currently unused values between 1 and the maximum. This eliminates the local security issues, but in exchange it slows down creation of new processes, opens you up to random users on the internet potentially nasty things (such as the exploit outlined in this security Stack Exchange question), and possibly breaks software that expects PIDs to not be reused quickly.

Others, like FreeBSD, allow you to choose either approach, or alternatively use a middle ground. This allows you to pick which particular set of security issues you want to deal with (hint, it’s probably the local issues, not the remote issues), or even choose a middle ground (which is usually the correct choice).

Источник

Оцените статью
Adblock
detector