race condition: 'ps -o ppid -p $$' outputs ppid=1

From: Spurgeon, John P (john.p.spurgeon@intel.com)
Date: Thu May 13 2004 - 16:47:11 EDT


I'm using Solaris 7 on IA and I've encountered a problem when trying to
use the ps command in a Korn shell script to obtain the parent process
id of the executing process. I'm able to reproduce the problem using the
two test programs below (test-a and test-b). As the "typical test
results" show, the ps command appears to suffer from some sort of race
condition that causes it to occasionally output ppid 1 instead of the
actual ppid. I'm running my programs on a server with 4 processors. I
suspect that the problem is more likely to be encountered on a
multi-processor system.

Does anyone know what's going on here? Is this really a problem with ps,
or is there some other explanation? Does anyone know of another simple
way to reliably determine the ppid from within a shell script?

John

Listing 1: /tmp/test-a
======================
#!/bin/ksh
for i in 1 2 3 4 5 6 7 8 9 10
do
    /tmp/test-b &
done

Listing 2: /tmp/test-b
======================
#!/bin/ksh
ps -o ppid -p $$ | tail -1

Listing 3: typical test results
===============================
% /tmp/test-a
23157
23157
23157
23157
% 23157
1
1
23157
1
1
_______________________________________________
sunmanagers mailing list
sunmanagers@sunmanagers.org
http://www.sunmanagers.org/mailman/listinfo/sunmanagers



This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 23:28:39 EDT