Linux – ls -l /proc/self | increamental PID

command linelinuxprocprocess

Each time, when the commandls -l /proc/self is executed, the link points to process who's PID keeps increasing. Why is this so ? Is it the PID of the ls command ?

Best Answer

Yes, that's the PID of ls.

POSIX defined ls as an external command, so anytime you run ls, the shell must create new process and run ls in that process.

To do that, the shell will call execve() system call:

$ strace ls -l /proc/self
execve("/bin/ls", ["ls", "-l", "/proc/self"], [/* 76 vars */]) = 0

You can see, after new process was created, /proc/self belongs to context of that process, so it was expanded to the PID of ls.

Related Question