multiple instance on Unix

Andrew Dalke adalke at mindspring.com
Thu Sep 30 03:00:47 EDT 2004


Ville Vainio wrote:
> I'd go for pid file - in Unix at least, process id's are not reused so
> if no process for the pid in pidfile exists, the process has died.

They aren't?  I remember seeing them cycle before.
Granted, pids are now, what 32 bit numbers so it's rare,
but there's nothing in the Unix specs to say they
cannot.

I pulled out my Minix book (much easier to root through,
err, read through than the Linux one, plus I'll need to
boot that machine :)

p. 548, mm/forkexit.c, lines 5739-5749

do {
       t = 0;            /* 't' = 0 means pid still free */
       next_pid = (next_pid < 30000 ? next_pid + 1 : INIT_PROC_NR + 1);
       for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++)
               if (rmp->mp_pid == next_pid) {
                       t = 1;
                       break;
               }
       rmc->mp_pid = next_pid;  /* assign pid to child */
} while (t);

In other words Minix, a Unix (like) OS, cycled pids,
with maxpid == 29999.

Ah-ha, here's experimental proof.  I can log into a
machine named 'xebte' (I have "alias xebte ssh xebte.[...]")
Elipsis used to obscure the hostname

% xebte uname -a
FreeBSD xebte[...] 4.8-STABLE FreeBSD 4.8-STABLE [...]
% xebte uptime
  2:44AM  up 90 days,  4:20, 2 users, load averages: 0.98, 0.98, 1.07
% xebte ps aux
USER    PID %CPU %MEM   VSZ  RSS  TT  STAT STARTED      TIME COMMAND
dalke   442  0.0  0.3  4908 1328  ??  S     2:44AM   0:00.00 sshd: 
dalke at notty
dalke   447  0.0  0.1  1124  564  ??  Ss    2:44AM   0:00.01 csh -c ps aux
dalke   448  0.0  0.0   392  212  ??  RN    2:44AM   0:00.00 ps aux

   (waited a few minutes)

% xebte ps aux
USER    PID %CPU %MEM   VSZ  RSS  TT  STAT STARTED      TIME COMMAND
dalke  5798  0.0  0.3  4908 1344  ??  S     2:48AM   0:00.00 sshd: 
dalke at notty
dalke  5805  0.0  0.1  1124  712  ??  Ss    2:48AM   0:00.01 csh -c ps aux
dalke  5806  0.0  0.0   392  212  ??  RN    2:48AM   0:00.00 ps aux

It made >4000 processes in a few minutes so it must
have cycled so as to have a pid == 442 after 90 days of uptime.

I was also pretty lucky to get a small pid that was
perfect for this counter-example.  Even at 1,500 processes
a minute it should take 32 months to cycle signed int.
Either FreeBSD uses a smaller range than that or things
get a lot busier during the day on that machine.


				Andrew
				dalke at dalkescientific.com



More information about the Python-list mailing list