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 ?


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.

| improve this answer | |

Yes, it's the PID of ls:

/proc/self This directory refers to the process accessing the /proc file system, and is identical to the /proc directory named by the process ID of the same process.

(from man 5 proc)

/proc/self is a symbolic link to /proc/PID for the PID of the process that accesses the link, by the magic of the proc filesystem.

In your case, that process is ls, and so each time you run the command you get a different PID (which tends to increase).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.