7

I had a file opened using vim editor. It was in a ssh session and since I had been idle for a long time, the session got closed and I had to establish the connection again. I opened the file again using vim editor and it reported me saying,

E325: ATTENTION
Found a swap file by the name ".latest2.sh.swp"
          owned by: ramesh   dated: Sun May 11 11:54:08 2014
         file name: ~ramesh/latest2.sh
          modified: no
         user name: ramesh   host name: xxx.xxx.edu
        process ID: 1466 (still running)
While opening file "latest2.sh"

Since I did not change much in the file, I did,

kill -9 1466

I checked if the process is gone using,

ps -aef | grep ramesh

and PID 1466 was not there. Now, when I opened the file again, it gave me the same message,

E325: ATTENTION
Found a swap file by the name ".latest2.sh.swp"
          owned by: ramesh   dated: Sun May 11 11:54:08 2014
         file name: ~ramesh/latest2.sh
          modified: no
         user name: ramesh   host name: xxx.xxx.edu
        process ID: 1466
While opening file "latest2.sh"

However the process was not in still running state.

Now, I have a question regarding the PID usage. As per the wikipedia entry,

Under Unix, process IDs are usually allocated on a sequential basis, beginning at 0 and rising to a maximum value which varies from system to system. Once this limit is reached, allocation restarts at zero and again increases. However, for this and subsequent passes any PIDs still assigned to processes are skipped.

Now, assuming I have used up all the available PIDs, will the PID 1466 be used or skipped?

Since, I already killed it, I assume it should be used. However, in my second attempt to open the file I still see the PID 1466.

What will happen in this case?

Ramesh
  • 39,297

1 Answers1

11

Yes, PIDs can be reused at any time.

What you're seeing in that output is that the process that created that .swp file was 1466. It doesn't necessarily mean that process is still around.
Remember that the file is static, it doesn't change just because the process which had it open died. So if 1466 is killed, the file still contains the information that said "I was being edited by PID 1466". VIM checks to see if this process is still alive and will indicate it as (still running).

As mentioned, it is possible for another process to get that exact same PID. When reporing as (still running), VIM doesn't actually check that that PID is a VIM process.

E325: ATTENTION
Found a swap file by the name ".test.swp"
          owned by: root   dated: Sun May 11 17:04:36 2014
         file name: /tmp/test
          modified: no
         user name: root   host name: whistler
        process ID: 21824 (still running)
While opening file "test"
             dated: Sun May 11 17:04:36 2014

In this case, PID 21824 is a shell I launched.  

phemmer  21824  19   0  0.0  0.0 S+         00:53 bash -c [[ "$$" == 21824 ]] && echo MATCH && sleep 999999
phemmer
  • 71,831
  • Did you check vim the source code ? It is easy to check PID and the process name. – Emmanuel May 12 '14 at 07:53
  • I just checked, vim didn't check the process name – Emmanuel May 12 '14 at 08:08
  • @Patrick I'm actually more curious as to how you got your test output... did you have to repeatedly (either manually or through a script) pass the line [[ "$$" == 21824 ]] && echo MATCH && sleep 999999 to bash until you hit bingo (i.e. saw MATCH on the console) before generating VIM's output above? – h.j.k. May 12 '14 at 09:31
  • 2
    @h.j.k. I just did while true; do bash -c '[[ "$$" == 21824 ]] && echo MATCH && sleep 999999'; done and then launched vim after it got the pid. – phemmer May 12 '14 at 12:32