get current PID in ksh

get current PID in ksh

am 07.09.2005 11:20:37 von mark

Here's a variation on an age old topic. I'm having trouble, in ksh, of
getting my own PID. Have tried $$, but unfortunately.. this doesn't
work because..

Try this:

silly.ksh is:

#!/bin/ksh
.. ./silly2.ksh &

silly2.ksh is:
#!/bin/ksh
print $$

now, when you run ./silly.ksh, printed is the PID of silly.ksh NOT
silly2.ksh which is what I want. Actually, silly2.ksh appears in a
process listing as silly.ksh which is the desired behaviour but because
of the &, $$ when printed, isn't actually a running process of any
form.

Any thoughts / suggestions?

Cheers,

Mark

Re: get current PID in ksh

am 07.09.2005 17:43:27 von Greg Beeker

mark@gowans.org wrote:
> Here's a variation on an age old topic. I'm having trouble, in ksh, of
> getting my own PID. Have tried $$, but unfortunately.. this doesn't
> work because..
>
> Try this:
>
> silly.ksh is:
>
> #!/bin/ksh
> . ./silly2.ksh &
>
> silly2.ksh is:
> #!/bin/ksh
> print $$
>
> now, when you run ./silly.ksh, printed is the PID of silly.ksh NOT
> silly2.ksh which is what I want. Actually, silly2.ksh appears in a
> process listing as silly.ksh which is the desired behaviour but because
> of the &, $$ when printed, isn't actually a running process of any
> form.
>
> Any thoughts / suggestions?

Try this:
$ more myppid.ksh
(echo "my pid "$$; ps -fp $$ | tail -1 | awk '{print "my ppid ",$3}' )
sleep 999

Tested on AIX 51

Re: get current PID in ksh

am 07.09.2005 18:55:41 von cfajohnson

On 2005-09-07, mark@gowans.org wrote:
> Here's a variation on an age old topic. I'm having trouble, in ksh, of
> getting my own PID. Have tried $$, but unfortunately.. this doesn't
> work because..
>
> Try this:
>
> silly.ksh is:
>
> #!/bin/ksh
> . ./silly2.ksh &
>
> silly2.ksh is:
> #!/bin/ksh
> print $$
>
> now, when you run ./silly.ksh, printed is the PID of silly.ksh NOT
> silly2.ksh which is what I want. Actually, silly2.ksh appears in a
> process listing as silly.ksh which is the desired behaviour but because
> of the &, $$ when printed, isn't actually a running process of any
> form.

Using both ',' and '&' is meaningless; the behaviour is undefined.

If you want to run the script in the current shell, leave out '&';
if you want to run it in the background, leave out '.'.

--
Chris F.A. Johnson
============================================================ ======
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress

Re: get current PID in ksh

am 08.09.2005 11:24:12 von ravindra.s.agarwal

If you want the PID of the background process, use the following in
silly.sh script:

#!/bin/ksh
.. ./silly2.ksh &
echo $!

Re: get current PID in ksh

am 08.09.2005 15:11:10 von Geoff Clare

"Chris F.A. Johnson" wrote, on Wed, 07 Sep 2005:

>> #!/bin/ksh
>> . ./silly2.ksh &
>>
>> silly2.ksh is:
>> #!/bin/ksh
>> print $$
[snip]
>
> Using both ',' and '&' is meaningless; the behaviour is undefined.

I don't think the behaviour is undefined.

POSIX says "If a command is terminated by the control operator
ampersand ( '&' ), the shell shall execute the command asynchronously
in a subshell."

So

. ./silly2.ksh &

is equivalent to:

(
. ./silly2.ksh
) &

which in turn is equivalent to:

(
#!/bin/ksh
print $$
) &

--
Geoff Clare

Re: get current PID in ksh

am 08.09.2005 15:55:32 von cfajohnson

On 2005-09-08, Geoff Clare wrote:
> "Chris F.A. Johnson" wrote, on Wed, 07 Sep 2005:
>
>>> #!/bin/ksh
>>> . ./silly2.ksh &
>>>
>>> silly2.ksh is:
>>> #!/bin/ksh
>>> print $$
> [snip]
>>
>> Using both ',' and '&' is meaningless; the behaviour is undefined.
>
> I don't think the behaviour is undefined.

OK; just meaningless.

> POSIX says "If a command is terminated by the control operator
> ampersand ( '&' ), the shell shall execute the command asynchronously
> in a subshell."

How did the word asynchronously come to mean the opposite of its
meaning in English? It means the opposite of synchronously, which
means "at the same time".

From The Collaborative International Dictionary of English v.0.44 [gcide]:

Asynchronous \A*syn"chro*nous\, a. [Gr. ? not + synchronous.]
Not simultaneous; not concurrent in time; -- opposed to
{synchronous}.


From The Free On-line Dictionary of Computing (09 FEB 02) [foldoc]:

asynchronous
....
1. A {process} in a {multitasking} system
whose execution can proceed independently, "in the
{background}". Other processes may be started before the
asynchronous process has finished.


--
Chris F.A. Johnson
============================================================ ======
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress

Re: get current PID in ksh

am 08.09.2005 17:12:02 von joe

"Chris F.A. Johnson" writes:

> On 2005-09-08, Geoff Clare wrote:
> > "Chris F.A. Johnson" wrote, on Wed, 07 Sep 2005:

> > POSIX says "If a command is terminated by the control operator
> > ampersand ( '&' ), the shell shall execute the command
> > asynchronously in a subshell."
>
> How did the word asynchronously come to mean the opposite of its
> meaning in English? It means the opposite of synchronously, which
> means "at the same time".

In this context asynchronous generally means "can't be predicted" or
"can be interrupted" or something of the kind. Synchronous means
"predictable", "continuous", etc.

Joe
--
I never veer to starboard 'cuz I never sail at all
- The Pirates Who Don't do Anything

Re: get current PID in ksh

am 08.09.2005 18:44:15 von Enrique Perez-Terron

On Thu, 08 Sep 2005 15:55:32 +0200, Chris F.A. Johnson
wrote:
> How did the word asynchronously come to mean the opposite of its
> meaning in English? It means the opposite of synchronously, which
> means "at the same time".

Good question!

However, in computing, it seems that "synchronous" has come to mean "bound
into the timeline, in strictly ordered fashion", while "asynchronous"
means "not synchronous" or "at some independent time" or "having an
independent timeline"

Regards
Enrique

Re: get current PID in ksh

am 08.09.2005 18:59:57 von cfajohnson

On 2005-09-08, Enrique Perez-Terron wrote:
> On Thu, 08 Sep 2005 15:55:32 +0200, Chris F.A. Johnson
> wrote:
>> How did the word asynchronously come to mean the opposite of its
>> meaning in English? It means the opposite of synchronously, which
>> means "at the same time".
>
> Good question!
>
> However, in computing, it seems that "synchronous" has come to mean "bound
> into the timeline, in strictly ordered fashion", while "asynchronous"
> means "not synchronous" or "at some independent time" or "having an
> independent timeline"

Because of the conflicting meanings, I never use "synchronous" or
"asynchronous". I say that commands run consecutively, except for
background tasks which run concurrently.

--
Chris F.A. Johnson
============================================================ ======
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress

Re: get current PID in ksh

am 08.09.2005 20:28:47 von joe

"Chris F.A. Johnson" writes:

> On 2005-09-08, Enrique Perez-Terron wrote:
> > On Thu, 08 Sep 2005 15:55:32 +0200, Chris F.A. Johnson
> > wrote:
> >> How did the word asynchronously come to mean the opposite of its
> >> meaning in English? It means the opposite of synchronously, which
> >> means "at the same time".
> >
> > Good question!
> >
> > However, in computing, it seems that "synchronous" has come to
> > mean "bound into the timeline, in strictly ordered fashion", while
> > "asynchronous" means "not synchronous" or "at some independent
> > time" or "having an independent timeline"
>
> Because of the conflicting meanings, I never use "synchronous" or
> "asynchronous". I say that commands run consecutively, except for
> background tasks which run concurrently.

Which term do you use for callback functions in a multithreaded
program? Not really a shell question but I'm curious.

Joe
--
I never veer to starboard 'cuz I never sail at all
- The Pirates Who Don't do Anything

Re: get current PID in ksh

am 08.09.2005 20:33:58 von cfajohnson

On 2005-09-08, joe@invalid.address wrote:
> "Chris F.A. Johnson" writes:
>
>> On 2005-09-08, Enrique Perez-Terron wrote:
>> > On Thu, 08 Sep 2005 15:55:32 +0200, Chris F.A. Johnson
>> > wrote:
>> >> How did the word asynchronously come to mean the opposite of its
>> >> meaning in English? It means the opposite of synchronously, which
>> >> means "at the same time".
>> >
>> > Good question!
>> >
>> > However, in computing, it seems that "synchronous" has come to
>> > mean "bound into the timeline, in strictly ordered fashion", while
>> > "asynchronous" means "not synchronous" or "at some independent
>> > time" or "having an independent timeline"
>>
>> Because of the conflicting meanings, I never use "synchronous" or
>> "asynchronous". I say that commands run consecutively, except for
>> background tasks which run concurrently.
>
> Which term do you use for callback functions in a multithreaded
> program? Not really a shell question but I'm curious.

I don't.

Do they run consecutively or concurrently?

--
Chris F.A. Johnson
============================================================ ======
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress

Re: get current PID in ksh

am 08.09.2005 23:13:10 von joe

"Chris F.A. Johnson" writes:

> On 2005-09-08, joe@invalid.address wrote:
> > "Chris F.A. Johnson" writes:
> >
> >> On 2005-09-08, Enrique Perez-Terron wrote:
> >> > On Thu, 08 Sep 2005 15:55:32 +0200, Chris F.A. Johnson
> >> > wrote:
> >> >> How did the word asynchronously come to mean the opposite
> >> >> of its meaning in English? It means the opposite of
> >> >> synchronously, which means "at the same time".
> >> >
> >> > Good question!
> >> >
> >> > However, in computing, it seems that "synchronous" has come to
> >> > mean "bound into the timeline, in strictly ordered fashion",
> >> > while "asynchronous" means "not synchronous" or "at some
> >> > independent time" or "having an independent timeline"
> >>
> >> Because of the conflicting meanings, I never use
> >> "synchronous" or "asynchronous". I say that commands run
> >> consecutively, except for background tasks which run
> >> concurrently.
> >
> > Which term do you use for callback functions in a multithreaded
> > program? Not really a shell question but I'm curious.
>
> I don't.
>
> Do they run consecutively or concurrently?

That's the point.

Joe
--
I never veer to starboard 'cuz I never sail at all
- The Pirates Who Don't do Anything

Re: get current PID in ksh

am 08.09.2005 23:41:07 von Bruce Barnett

"Enrique Perez-Terron" writes:

> However, in computing, it seems that "synchronous" has come to mean
> "bound into the timeline, in strictly ordered fashion", while
> "asynchronous" means "not synchronous" or "at some independent time"
> or "having an independent timeline"

Yup, I think some other operating systems used to have both synchronous and
asynchronous I/O system calls. (like VMS?)

Asynchronous calls meant you started them up, and then continued with
your program while the I/O was being processed, But you have to get
the results later with a second call.

Early unix systems didn't have async I/O AFAIK.

--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.

Re: get current PID in ksh

am 17.09.2005 20:05:31 von bonomi

In article ,
Chris F.A. Johnson wrote:
>On 2005-09-08, Geoff Clare wrote:
>> "Chris F.A. Johnson" wrote, on Wed, 07 Sep 2005:
>>
>>>> #!/bin/ksh
>>>> . ./silly2.ksh &
>>>>
>>>> silly2.ksh is:
>>>> #!/bin/ksh
>>>> print $$
>> [snip]
>>>
>>> Using both ',' and '&' is meaningless; the behaviour is undefined.
>>
>> I don't think the behaviour is undefined.
>
> OK; just meaningless.
>
>> POSIX says "If a command is terminated by the control operator
>> ampersand ( '&' ), the shell shall execute the command asynchronously
>> in a subshell."
>
> How did the word asynchronously come to mean the opposite of its
> meaning in English? It means the opposite of synchronously, which
> means "at the same time".

WRONG meaning for 'synchronously'. :)

>
>From The Collaborative International Dictionary of English v.0.44 [gcide]:
>
> Asynchronous \A*syn"chro*nous\, a. [Gr. ? not + synchronous.]
> Not simultaneous; not concurrent in time; -- opposed to
> {synchronous}.

*bad* definition.
>
>
>From The Free On-line Dictionary of Computing (09 FEB 02) [foldoc]:
>
> asynchronous
> ....
> 1. A {process} in a {multitasking} system
> whose execution can proceed independently, "in the
> {background}". Other processes may be started before the
> asynchronous process has finished.
>

Synchronous -- (see synchronized) -- happens with a fixed timing relationship
to other events. Can mean that both things _start_ at the same point in
time, can mean that the 2nd thing can start only when the first one _ends_.

Asynchronous -- happens _without_regard_ to the timing of any other events.


In the UNIX world, 'foreground' tasks are limited to synchronous operation.
the 'next' task has to wait for the previous one to complete before starting.
'Background' tasks, on the other hand, proceed independantly of whatever is
going on in the 'forground', *or* of what any other background task is doing.
hence they are said to execute asynchronously -- they do their thing 'without
regard' for the timing of any other events.

all clear now?


>
>--
> Chris F.A. Johnson
> ============================================================ ======
> Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
>

Re: get current PID in ksh

am 17.09.2005 20:35:03 von cfajohnson

On 2005-09-17, Robert Bonomi wrote:
> In article ,
> Chris F.A. Johnson wrote:
>>On 2005-09-08, Geoff Clare wrote:
>>> "Chris F.A. Johnson" wrote, on Wed, 07 Sep 2005:
>>>
>>>>> #!/bin/ksh
>>>>> . ./silly2.ksh &
>>>>>
>>>>> silly2.ksh is:
>>>>> #!/bin/ksh
>>>>> print $$
>>> [snip]
>>>>
>>>> Using both ',' and '&' is meaningless; the behaviour is undefined.
>>>
>>> I don't think the behaviour is undefined.
>>
>> OK; just meaningless.
>>
>>> POSIX says "If a command is terminated by the control operator
>>> ampersand ( '&' ), the shell shall execute the command asynchronously
>>> in a subshell."
>>
>> How did the word asynchronously come to mean the opposite of its
>> meaning in English? It means the opposite of synchronously, which
>> means "at the same time".
>
> WRONG meaning for 'synchronously'. :)

Wrong only for the context, not for the standard English meaning
of the word.

>>From The Collaborative International Dictionary of English v.0.44 [gcide]:
>>
>> Asynchronous \A*syn"chro*nous\, a. [Gr. ? not + synchronous.]
>> Not simultaneous; not concurrent in time; -- opposed to
>> {synchronous}.
>
> *bad* definition.

Good (i.e., English) definition.

>>From The Free On-line Dictionary of Computing (09 FEB 02) [foldoc]:
>>
>> asynchronous
>> ....
>> 1. A {process} in a {multitasking} system
>> whose execution can proceed independently, "in the
>> {background}". Other processes may be started before the
>> asynchronous process has finished.
>>
>
> Synchronous -- (see synchronized) -- happens with a fixed timing relationship
> to other events. Can mean that both things _start_ at the same point in
> time, can mean that the 2nd thing can start only when the first one _ends_.
>
> Asynchronous -- happens _without_regard_ to the timing of any other events.

Whis is computerese, not English.

> In the UNIX world, 'foreground' tasks are limited to synchronous
>>operation.

Not according to the English meaning of the word.

> the 'next' task has to wait for the previous one to complete before starting.
> 'Background' tasks, on the other hand, proceed independantly of whatever is
> going on in the 'forground', *or* of what any other background task is doing.
> hence they are said to execute asynchronously -- they do their thing 'without
> regard' for the timing of any other events.
>
> all clear now?

Yes. The word synchronous has been adopted by the computer culture
when it meant synchronized.

--
Chris F.A. Johnson
============================================================ ======
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress

Re: get current PID in ksh

am 17.09.2005 21:29:41 von bonomi

In article ,
Chris F.A. Johnson wrote:
>On 2005-09-17, Robert Bonomi wrote:
>> In article ,
>> Chris F.A. Johnson wrote:
>>>On 2005-09-08, Geoff Clare wrote:
>>>> "Chris F.A. Johnson" wrote, on Wed, 07 Sep 2005:
>>>>
>>>>>> #!/bin/ksh
>>>>>> . ./silly2.ksh &
>>>>>>
>>>>>> silly2.ksh is:
>>>>>> #!/bin/ksh
>>>>>> print $$
>>>> [snip]
>>>>>
>>>>> Using both ',' and '&' is meaningless; the behaviour is undefined.
>>>>
>>>> I don't think the behaviour is undefined.
>>>
>>> OK; just meaningless.
>>>
>>>> POSIX says "If a command is terminated by the control operator
>>>> ampersand ( '&' ), the shell shall execute the command asynchronously
>>>> in a subshell."
>>>
>>> How did the word asynchronously come to mean the opposite of its
>>> meaning in English? It means the opposite of synchronously, which
>>> means "at the same time".
>>
>> WRONG meaning for 'synchronously'. :)
>
> Wrong only for the context, not for the standard English meaning
> of the word.

BZZZT!

Synchronous:
1) occurring at the same time.
2) moving or operating at the same rate
3a) having identical periods
3b) having identical period and phase.

In the computer world, "meaning "2 of the word is the generally applicable one.

And 'asynchronous'

>
>>>From The Collaborative International Dictionary of English v.0.44 [gcide]:
>>>
>>> Asynchronous \A*syn"chro*nous\, a. [Gr. ? not + synchronous.]
>>> Not simultaneous; not concurrent in time; -- opposed to
>>> {synchronous}.
>>
>> *bad* definition.
>
> Good (i.e., English) definition.

FALSE TO FACT.

"simultaneous; concurrent in time" is the dictionary definition of
'synchronize', *not* 'synchronous'>

Thus, it is an ERRONEOUS definition -- as it implies that 'asynchronous' forces
one particular meaning, of *several*, on the root word. (synchronous)

>>>From The Free On-line Dictionary of Computing (09 FEB 02) [foldoc]:
>>>
>>> asynchronous
>>> ....
>>> 1. A {process} in a {multitasking} system
>>> whose execution can proceed independently, "in the
>>> {background}". Other processes may be started before the
>>> asynchronous process has finished.
>>>
>>
>> Synchronous -- (see synchronized) -- happens with a fixed timing relationship
>> to other events. Can mean that both things _start_ at the same point in
>> time, can mean that the 2nd thing can start only when the first one _ends_.
>>
>> Asynchronous -- happens _without_regard_ to the timing of any other events.
>
> Whis is computerese, not English.

If you say so. Of course the words were used that way as far back as the
1920s (probably further, but the references I have on hand are from that era),
in describing industrial process-control management -- where the 'events' were
being executed *manually*.

>
>> In the UNIX world, 'foreground' tasks are limited to synchronous
>>>operation.
>
> Not according to the English meaning of the word.

Yes, according to _one_of_ the long-accepted English meanings.
Synchronous does *NOT* necessarily imply simultaneous/parallel operation.

See 'synchronous motor' for one example that predates computers.
Again, references for that are available that go back to the 1930s, and before.

>> the 'next' task has to wait for the previous one to complete before starting.
>> 'Background' tasks, on the other hand, proceed independantly of whatever is
>> going on in the 'forground', *or* of what any other background task is doing.
>> hence they are said to execute asynchronously -- they do their thing 'without
>> regard' for the timing of any other events.
>>
>> all clear now?
>
> Yes. The word synchronous has been adopted by the computer culture
> when it meant synchronized.

"not exactly". There are fine points of difference in the usage of the two
words. When you have a 'clock' for a timing reference, then 'synchronized'
means that events occur at a time relative to a *SPECIFIC* clock-tick --
whether or not they occur simultaneously with each other. "Synchronous"
means that each event occurs at a time relative to "an arbitrary" clock-tick,
and *NOT*NECESSARILY* involving the same 'reference' tick for each event.

"Common usage" English has gotten 'sloppy' about the fine shades of meaning,
leading to confusion.

The technical community has utilized the proper distinction, long before
computers came on the scene.

"Just because" two processes are occurring CONCURRENTLY, that does not mean
that they are necessarily synchronous nor necessarily synchronized.

'Background' tasks on a computer are *not* synchronous, nor synchronized,
with each other, or with any foreground tasks. they do not *start* at
the same time, they do not _finish_ at the same time, they do not necessarily,
move at the same rate, etc.

Lacking that begin/end synchronization, as well as lacking any synchronization
at any intermediate points in processing the task, *and* considering that said
background tasks usually operate at a reduced scheduling priority relative to
foreground tasks (so they are not even on the same 'clock', as it were) it
_is_ fair, reasonable -- and entirely accurate -- to describe background
tasks as operating 'asynchronously' from those in the foreground.

The traffic light on the corner, and the water sprinkler in my front yard
are both operating at the same time ("concurrently"). By your definition,
because of that, they are operating synchronously.