Discussion:
How to get pid of a process when is started using open command
(too old to reply)
a***@gmail.com
2015-04-29 09:43:03 UTC
Permalink
Hi,

I have pipeline a command using open,something like below

set process [open "|\"new.exe" \"inputfile.txt\" 1000" r+]

which intern start a new process "new.exe" which takes some arguments.
when I query for the process id uisng [pid $process] I get the process id of the file operation ,
But in my case I also need the new process that is open commands kick starts.
How can I get the process id of the new.exe that is started by open?

Regards,
Raju
a***@gmail.com
2015-04-29 10:09:04 UTC
Permalink
Post by a***@gmail.com
Hi,
I have pipeline a command using open,something like below
set process [open "|\"new.exe" \"inputfile.txt\" 1000" r+]
which intern start a new process "new.exe" which takes some arguments.
when I query for the process id uisng [pid $process] I get the process id of the file operation ,
But in my case I also need the new process that is open commands kick starts.
How can I get the process id of the new.exe that is started by open?
Regards,
Raju
I actually need to know the new process id to kill the new process created.
Alexandre Ferrieux
2015-04-29 11:21:23 UTC
Permalink
Post by a***@gmail.com
set process [open "|\"new.exe" \"inputfile.txt\" 1000" r+]
How can I get the process id of the new.exe that is started by open?
[pid $process]
Rich
2015-04-29 11:31:15 UTC
Permalink
Post by a***@gmail.com
Post by a***@gmail.com
set process [open "|\"new.exe" \"inputfile.txt\" 1000" r+]
How can I get the process id of the new.exe that is started by open?
[pid $process]
Note that [pid $process] is documented as returning a list of process
ids:

If the fileId argument is given then it should normally refer to
a process pipeline created with the open command. In this case
the pid command will return a _list_ whose elements are the
process identifiers of all the processes in the pipeline, in
order.

For a single process, the list will be exactly the same format as a
single pid.

For a multiple process pipeline, using the raw return value without a
list operation to extract an element will create data dependent issues
with mysterious curly brackets appearing in odd places very similar to
the issues from Jan. or Feb this year with lists whos first elements
began with a hash mark: "#abcdef".

So everything will work fine, until the day that someone changes the
open to be something like this: [open "|new.exe | another.exe" r+] and
creates a two process pipeline, at which point pid would return [list 1234
5678], which if accessed by string operators will appear as {1234 5678}.

To avoid this potential issue, use [lindex] or [foreach] to access the
individual elements of the return value, even if there is only one,
then the following code that uses the output of [pid] is already ready
for the day the pipeline might become a multi-process pipeline.
a***@gmail.com
2015-04-29 12:02:25 UTC
Permalink
Post by Rich
Post by a***@gmail.com
Post by a***@gmail.com
set process [open "|\"new.exe" \"inputfile.txt\" 1000" r+]
How can I get the process id of the new.exe that is started by open?
[pid $process]
Note that [pid $process] is documented as returning a list of process
If the fileId argument is given then it should normally refer to
a process pipeline created with the open command. In this case
the pid command will return a _list_ whose elements are the
process identifiers of all the processes in the pipeline, in
order.
For a single process, the list will be exactly the same format as a
single pid.
For a multiple process pipeline, using the raw return value without a
list operation to extract an element will create data dependent issues
with mysterious curly brackets appearing in odd places very similar to
the issues from Jan. or Feb this year with lists whos first elements
began with a hash mark: "#abcdef".
So everything will work fine, until the day that someone changes the
open to be something like this: [open "|new.exe | another.exe" r+] and
creates a two process pipeline, at which point pid would return [list 1234
5678], which if accessed by string operators will appear as {1234 5678}.
To avoid this potential issue, use [lindex] or [foreach] to access the
individual elements of the return value, even if there is only one,
then the following code that uses the output of [pid] is already ready
for the day the pipeline might become a multi-process pipeline.
But in my case I see [pid $process] returns me only one process id(the one with respect to the file operation) ,
where as I can see the new.exe also being run in the process list which is not listed with [pid $process].
I am actually using a new.bat file to invoke the new.exe ,does that make a difference?
[open "|\"new.bat" \"inputfile.txt\" 1000" r+]
heinrichmartin
2015-04-29 13:47:50 UTC
Permalink
Post by a***@gmail.com
Post by Rich
Post by a***@gmail.com
Post by a***@gmail.com
set process [open "|\"new.exe" \"inputfile.txt\" 1000" r+]
How can I get the process id of the new.exe that is started by open?
[pid $process]
Note that [pid $process] is documented as returning a list of process
If the fileId argument is given then it should normally refer to
a process pipeline created with the open command. In this case
the pid command will return a _list_ whose elements are the
process identifiers of all the processes in the pipeline, in
order.
For a single process, the list will be exactly the same format as a
single pid.
For a multiple process pipeline, using the raw return value without a
list operation to extract an element will create data dependent issues
with mysterious curly brackets appearing in odd places very similar to
the issues from Jan. or Feb this year with lists whos first elements
began with a hash mark: "#abcdef".
So everything will work fine, until the day that someone changes the
open to be something like this: [open "|new.exe | another.exe" r+] and
creates a two process pipeline, at which point pid would return [list 1234
5678], which if accessed by string operators will appear as {1234 5678}.
To avoid this potential issue, use [lindex] or [foreach] to access the
individual elements of the return value, even if there is only one,
then the following code that uses the output of [pid] is already ready
for the day the pipeline might become a multi-process pipeline.
But in my case I see [pid $process] returns me only one process id(the one with respect to the file operation) ,
where as I can see the new.exe also being run in the process list which is not listed with [pid $process].
I am actually using a new.bat file to invoke the new.exe ,does that make a difference?
[open "|\"new.bat" \"inputfile.txt\" 1000" r+]
It looks like you want to find pids of child processes.

[exec ps -o pid= --ppid [lindex [pid $process] 0]] ;# linux only and un-tested

I can't help with a Tcl-only, OS-independent solution.

HTH,
Martin
Rich
2015-04-29 20:09:19 UTC
Permalink
Post by a***@gmail.com
Post by Rich
Post by a***@gmail.com
Post by a***@gmail.com
set process [open "|\"new.exe" \"inputfile.txt\" 1000" r+]
How can I get the process id of the new.exe that is started by open?
[pid $process]
Note that [pid $process] is documented as returning a list of process
If the fileId argument is given then it should normally refer to
a process pipeline created with the open command. In this case
the pid command will return a _list_ whose elements are the
process identifiers of all the processes in the pipeline, in
order.
For a single process, the list will be exactly the same format as a
single pid.
For a multiple process pipeline, using the raw return value without a
list operation to extract an element will create data dependent issues
with mysterious curly brackets appearing in odd places very similar to
the issues from Jan. or Feb this year with lists whos first elements
began with a hash mark: "#abcdef".
So everything will work fine, until the day that someone changes the
open to be something like this: [open "|new.exe | another.exe" r+] and
creates a two process pipeline, at which point pid would return [list 1234
5678], which if accessed by string operators will appear as {1234 5678}.
To avoid this potential issue, use [lindex] or [foreach] to access the
individual elements of the return value, even if there is only one,
then the following code that uses the output of [pid] is already ready
for the day the pipeline might become a multi-process pipeline.
But in my case I see [pid $process] returns me only one process
id(the one with respect to the file operation) , where as I can see
the new.exe also being run in the process list which is not listed
with [pid $process]. I am actually using a new.bat file to invoke the
new.exe ,does that make a difference?
[open "|\"new.bat" \"inputfile.txt\" 1000" r+]
If you only ever have one single pipe character (|) in the open line
then you will only see one process ID. My text was cautioning you that
if you assume you receive a string, when in fact you receive a list,
that some day, if you were to ever edit the open to have two pipes ( |
process1 | process2 ) in the command, you'll get weird error messages
elsewhere from code you did not change.
Donal K. Fellows
2015-04-29 20:41:12 UTC
Permalink
Post by a***@gmail.com
I am actually using a new.bat file to invoke the new.exe ,does that make a difference?
Yes. Tcl only sees process identifiers one level deep, of the processes
that it directly creates. Since as far as it knows, they could do
arbitrarily complex things, it doesn't try to get any more complex than
that. Listing the child processes of a process isn't something that Tcl
provides a direct mechanism for doing.

Since you are using Windows (based on your descriptions of how you're
working), I suggest that you can use TWAPI to get the information
(http://twapi.sourceforge.net/). Unfortunately, you will need to do it
by listing all the user's processes and then checking whether the parent
process ID is what you expect; that's what the underlying OS calls do,
so that's what you have to work with. Here's the calls you need:

http://twapi.sourceforge.net/v4.1/process.html#get_process_ids
http://twapi.sourceforge.net/v4.1/process.html#get_process_parent

Donal.
--
Donal Fellows — Tcl user, Tcl maintainer, TIP editor.
Continue reading on narkive:
Loading...