Karl Cunningham <[EMAIL PROTECTED]> wrote:
> Thanks for the suggestions. I'm getting inconsistent results,
> though. When I run the following, the most common result is 5 but once in
> a while there is a 2, 3, or 4. I'm not sure what's going on.
>
> cnt=0
> while [ $cnt -lt 70 ] ; do
> echo -n `ps -ef | grep -v grep | grep $0 | wc -l`
> let cnt+=1
> done
> echo
>
> I can test for the maximum value out of 20 tests and I'm sure it would get
> the job done. But at this point I'm curious why this happens. Any ideas?
Well, the first thing folks should do when trying to figure out what's
going on in a script is capture the output and see whats going on.
So, for example, one might do this:
cnt=0
while [ $cnt -lt 70 ] ; do
ps -ef | egrep -v grep > /tmp/debug.1.$$
grep $0 /tmp/debug.1.$$ > /tmp/debug.2.$$
echo -n `wc -l /tmp/debug.2.$$`
let cnt+=1
done
echo
Then go look at the files and see that sometimes you'll catch
things like:
emacs myscript
./myscript
vi myscript.info
(as a dumb, contrived example)
In my experience, you must be very careful when grepping for command
lines - especially if they are scripts being executed, since all too
often you also happen to have that script open in the editor!
One thing I've seen is where the script saves its pid in a file,
which you then check for. Of course, you have to watch for the
critical section there (and there is no easy way to get rid
of it entirely, so you just have to do the best you can) (Huh?
What critical section??? (or, What's a critical section?) see
below) Another option would be to make a server that would
keep track of things like that and answer the question
'Am I the only one of me wanting to run?' - but that seems like
a LOT of work.... Easiest thing is probably just to try to make
sure you don't run the script TOO often (10 times in a minute
would be a bad idea ;-), and do a 'simple' check like we've been
discussing.
Ok, so what's a critical section? Its a section of code (usually)
that, if you have 2 separate threads of control execute that code
at the same time the results can (might) be erroneous. One of the standard
examples is : static int i; crit_sec_oops(int addme){ int j; j=i + addme; i=j;}
Now, if thread 1 comes through, calculates the sum, then thread 2 comes
along, caluclates the sum AND manages to store it back into i, then
thread 1 continues along and stores ITS version into i - you just lost
thread 2's contribution to i.
Trying to have a file as your locking means leaves you a critical
section also, but we'll leave that as an exercise for the student ;-)
rc
Rusty Carruth Email: [EMAIL PROTECTED] or [EMAIL PROTECTED]
Voice: (480) 345-3621 SnailMail: Schlumberger ATE
FAX: (480) 345-8793 7855 S. River Parkway, Suite 116
Ham: N7IKQ @ 146.82+,pl 162.2 Tempe, AZ 85284-1825
ICBM: 33 20' 44"N 111 53' 47"W