Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Re: Unix process ID
On 2008-04-01, yocohen <yocohen@gmail.com> wrote:
>
> Thanks for prompt response. I find some ambiguity in the answer as I
> understand it, so I will rephrase the question :)
> Assuming there is no number limit, will process id be assigned in
> ascending pattern?

Yes, but you can't assume even for a minute that the limit isn't
there - wrapping around is common.  I just control-Z'ed out of here
and did a quick ps:

$ ps
PID TT STAT    TIME COMMAND
6011 ro T    0:01.41 slrn
17400 ro Ss   0:00.03 -ksh
26633 ro R+   0:00.00 ps

Obviously I started my login shell before slrn, but since slrn's
PID is lower than ksh's we can see very clearly that it has wrapped
around.  This kind of thing is _very_ common on a busy multiuser
machine.  On a quiescent personal machine the PIDs give a much
better aprroximation of slowly incrementing over time but it is
still far too unpredictable to be relied on.

--
Andrew Smallshaw
andrews@sdf.lonestar.org

Report this thread to moderator Post Follow-up to this message
Old Post
Andrew Smallshaw
04-02-08 12:27 AM


Re: Unix process ID
On Apr 1, 11:42=A0pm, Andrew Smallshaw <andr...@sdf.lonestar.org> wrote:
> On 2008-04-01, yocohen <yoco...@gmail.com> wrote:
>
>
> 
>
> Yes, but you can't assume even for a minute that the limit isn't
> there - wrapping around is common. =A0I just control-Z'ed out of here
> and did a quick ps:
>
> $ ps
> =A0 PID TT STAT =A0 =A0TIME COMMAND
> =A06011 ro T =A0 =A00:01.41 slrn
> 17400 ro Ss =A0 0:00.03 -ksh
> 26633 ro R+ =A0 0:00.00 ps
>
> Obviously I started my login shell before slrn, but since slrn's
> PID is lower than ksh's we can see very clearly that it has wrapped
> around. =A0This kind of thing is _very_ common on a busy multiuser
> machine. =A0On a quiescent personal machine the PIDs give a much
> better aprroximation of slowly incrementing over time but it is
> still far too unpredictable to be relied on.
>
> --
> Andrew Smallshaw
> andr...@sdf.lonestar.org

Thanks everyone for the replies.
BTW, my problem is on a Solaris 10 machine. I have a tcsh script that
I want to add a check in its beginning. I want to add this check for a
case that someone from some reason will run several instances of this
script simultaneously. I want the script to check the minimum PID of
any other scripts like it and only the instance with the lowest PID
will continue running, others will abort. I think I will look for
other options to do it because the assumption of ascending PIDs is
crucial for this check...

Thanks,

Yossi

Report this thread to moderator Post Follow-up to this message
Old Post
yocohen
04-02-08 09:43 AM


Re: Unix process ID
In article
<f62086a0-38ea-4bbd-8144-abb8063fc908@d45g2000hsc.googlegroups.com>,
yocohen <yocohen@gmail.com> wrote:

> BTW, my problem is on a Solaris 10 machine. I have a tcsh script that
> I want to add a check in its beginning. I want to add this check for a
> case that someone from some reason will run several instances of this
> script simultaneously. I want the script to check the minimum PID of
> any other scripts like it and only the instance with the lowest PID
> will continue running, others will abort. I think I will look for
> other options to do it because the assumption of ascending PIDs is
> crucial for this check...

Sounds like a job for a lock file.  The script checks whether the file
exists; if it does, it aborts, otherwise it creates it and continues.

This is actually somewhat tricky to do atomically from a script.

BTW, tcsh is not a good shell for scripting in, I suggest you use ksh or
bash.  And for creating the lock file, you may need to use something
more powerful, like perl.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE don't copy me on replies, I'll read them in the group ***

Report this thread to moderator Post Follow-up to this message
Old Post
Barry Margolin
04-02-08 09:43 AM


Re: Unix process ID
yocohen <yocohen@gmail.com> wrote:
> Thanks everyone for the replies.
> BTW, my problem is on a Solaris 10 machine. I have a tcsh script that
> I want to add a check in its beginning. I want to add this check for a
> case that someone from some reason will run several instances of this
> script simultaneously. I want the script to check the minimum PID of
> any other scripts like it and only the instance with the lowest PID
> will continue running, others will abort. I think I will look for
> other options to do it because the assumption of ascending PIDs is
> crucial for this check...

As another person already said, this is normally done via a lock file.

But to find the oldest instance of a given process list look at the
ps -o etime -p <PID>
output. This is more reliable than doing heuristics on the PID itself.

--
Daniel

Report this thread to moderator Post Follow-up to this message
Old Post
Daniel Rock
04-02-08 01:26 PM


Re: Unix process ID
On Apr 2, 12:30=A0pm, "Daniel Rock" <v200...@deadcafe.de> wrote:
> yocohen <yoco...@gmail.com> wrote: 
>
> As another person already said, this is normally done via a lock file.
>
> But to find the oldest instance of a given process list look at the
> =A0 =A0 =A0 =A0 ps -o etime -p <PID>
> output. This is more reliable than doing heuristics on the PID itself.
>
> --
> Daniel

Yes, I know a lock file is a better way to do it. I was looking for a
simple/easier way. I must use tcsh (I bet you all know the limitations
on existing big projects and historic code...).
Regarding the etime option - is there a way to get this time in
milliseconds because the seconds accuration is not good for me...?

Thanks,

Yossi

Report this thread to moderator Post Follow-up to this message
Old Post
yocohen
04-02-08 01:26 PM


Re: Unix process ID
On 2008-04-02, yocohen <yocohen@gmail.com> wrote:
> On Apr 2, 12:30_pm, "Daniel Rock" <v200...@deadcafe.de> wrote: 
>
> Yes, I know a lock file is a better way to do it. I was looking for a
> simple/easier way. I must use tcsh (I bet you all know the limitations
> on existing big projects and historic code...).
> Regarding the etime option - is there a way to get this time in
> milliseconds because the seconds accuration is not good for me...?
>
> Thanks,
>
> Yossi

Lock files aren't that tough.  Easiest way to do it from a shell is to
use mkdir.  Since it fails if the directory already exists, it makes for
a dandy test-and-set.


--
Christopher Mattern

NOTICE
Thank you for noticing this new notice
Your noticing it has been noted
And will be reported to the authorities

Report this thread to moderator Post Follow-up to this message
Old Post
Chris Mattern
04-03-08 12:41 AM


Re: Unix process ID
On Apr 1, 2:51 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
> Hi sjdevnull! :-)
>
>
>
> sjdevn...@yahoo.com wrote: 
> 
> 
> 
> 
> 
> 
> 
>
> I am not sure that's a perfect clarification :-}
>
> The process will keep that PID even if not "running", e.g. if waiting
> for I/O or being otherwise suspended.
>
> Once a process terminated and is removed from the process table the
> PID can be re-assigned.

Yes, that's better.  A process has a (single) PID until it terminates
and is reaped, after that the PID may be reused.  Any two processes
that exist _at the same time_ will have distinct PIDs.  (and of course
if the machine dies/reboots then PIDs are all free at boot even though
the processes may not technically have been reaped).

The important thing is that PIDs aren't unique in the sense you cannot
look and see that process A has PID X, and then later on assume that
PID X refers to process A (unless you know that process A is still
around).

Report this thread to moderator Post Follow-up to this message
Old Post
sjdevnull@yahoo.com
04-03-08 12:41 AM


Re: Unix process ID
yocohen <yocohen@gmail.com> writes:

> Yes, I know a lock file is a better way to do it. I was looking for a
> simple/easier way. I must use tcsh (I bet you all know the limitations
> on existing big projects and historic code...).

You can't write a 4-line /bin/sh script that calls a C shell script?

You can't call a /bin/sh script from a csh script?

Report this thread to moderator Post Follow-up to this message
Old Post
Maxwell Lol
04-03-08 12:41 AM


Re: Unix process ID
On Apr 2, 10:20=A0pm, "sjdevn...@yahoo.com" <sjdevn...@yahoo.com> wrote:
> On Apr 1, 2:51 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
> wrote:
>
>
>
>
> 
> 
> 
> 
> 
 
> 
Some 
 
> 
> 
> 
> 
> 
> 
>
> Yes, that's better. =A0A process has a (single) PID until it terminates
> and is reaped, after that the PID may be reused. =A0Any two processes
> that exist _at the same time_ will have distinct PIDs. =A0(and of course
> if the machine dies/reboots then PIDs are all free at boot even though
> the processes may not technically have been reaped).
>
> The important thing is that PIDs aren't unique in the sense you cannot
> look and see that process A has PID X, and then later on assume that
> PID X refers to process A (unless you know that process A is still
> around).- Hide quoted text -
>
> - Show quoted text -

Yes, I never said I rely on the same PIDs.
Thanks.

Report this thread to moderator Post Follow-up to this message
Old Post
yocohen
04-03-08 11:26 AM


Re: Unix process ID
On Apr 2, 11:10=A0pm, Maxwell Lol <nos...@com.invalid> wrote:
> yocohen <yoco...@gmail.com> writes: 
>
> You can't write a 4-line /bin/sh script that calls a C shell script?
>
> You can't call a /bin/sh script from a csh script?

Yes, I believe anything can be done. But being a Windows/Java person I
am looking for the fastest simple solution that will not take me too
much into those scripts. It already took more time than I intended.
But it was interesting...

Thanks for the help!

Report this thread to moderator Post Follow-up to this message
Old Post
yocohen
04-03-08 11:26 AM


Sponsored Links




Last Thread Next Thread Next
Pages (3): « 1 [2] 3 »
Search this forum -> 
Post New Thread

Unix Shell Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 09:39 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.