Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Process id of tcl script?

1,189 views
Skip to first unread message

Richard Yen

unread,
Aug 11, 1993, 1:36:48 PM8/11/93
to
How do you get the process id of a tcl script? I'm writing an application where
information has to be stored in a /tmp file. If the user is running several of the
applications at the same time, then I want the /tmp files to be different so there
is no conflict, so I was thinking of using something like /tmp/file.$$, as with a
Bourne shell script.

Using something like "exec ps ax |grep application" wouldn't work since all the
applications that are running will assume it's the first one so they'll all have the
same id number.

Thanks in advance for your help.

--Richard Yen


steven c demlow

unread,
Aug 11, 1993, 2:17:56 PM8/11/93
to
In article <m6ibhg...@exodus.Eng.Sun.COM> ry...@gobears.Corp.Sun.COM (Richard Yen) writes:

>How do you get the process id of a tcl script? I'm writing an application where
>information has to be stored in a /tmp file. If the user is running several of the
>applications at the same time, then I want the /tmp files to be different so there
>is no conflict, so I was thinking of using something like /tmp/file.$$, as with a
>Bourne shell script.

I use the following to create /tmp file names:

global errorCode
catch {exec /bin/false}
set pid [lindex $errorCode 1]
set tmpfile "/tmp/file.$pid"

This returns the pid of the child process (/bin/false), not the script's pid.
This can cause problems for deletion, etc., but you won't run into duplicate
numbers until the pids wrap around. I assume that something similar can be
used to find the script's own pid, but I haven't figured it out yet. I'll
be curious to see what else people have done.

Later,
Steve
--
Steve Demlow dem...@cis.ohio-state.edu
Systems Staff Graduate Assistant
Department of Computer and Information Science, The Ohio State University

Stephen Daedalus

unread,
Aug 11, 1993, 5:21:48 PM8/11/93
to
In article <24bd4k...@frisbee.cis.ohio-state.edu>, dem...@cis.ohio-state.edu (steven c demlow) writes:
|> In article <m6ibhg...@exodus.Eng.Sun.COM> ry...@gobears.Corp.Sun.COM (Richard Yen) writes:
|>
|> >How do you get the process id of a tcl script? I'm writing an application where
|> >information has to be stored in a /tmp file. If the user is running several of the
|> >applications at the same time, then I want the /tmp files to be different so there
|> >is no conflict, so I was thinking of using something like /tmp/file.$$, as with a
|> >Bourne shell script.
|>
|> I use the following to create /tmp file names:
|>
|> global errorCode
|> catch {exec /bin/false}
|> set pid [lindex $errorCode 1]
|> set tmpfile "/tmp/file.$pid"
|>
|> This returns the pid of the child process (/bin/false), not the script's pid.
|> This can cause problems for deletion, etc., but you won't run into duplicate
|> numbers until the pids wrap around. I assume that something similar can be
|> used to find the script's own pid, but I haven't figured it out yet. I'll
|> be curious to see what else people have done.
We use Extended Tcl, which has the id command. We just
do a [id process].
|> Later,
|> Steve
|> --
|> Steve Demlow dem...@cis.ohio-state.edu
|> Systems Staff Graduate Assistant
|> Department of Computer and Information Science, The Ohio State University

--
---
Richard Farrar ADAC Laboratories far...@adaclabs.com
These views are my own, not my company's or country's.
Love, and do what thou wilt shall be the whole of the
law. -- Aleister Crowley

David Sibley

unread,
Aug 11, 1993, 9:29:18 PM8/11/93
to
In article <24bd4k...@frisbee.cis.ohio-state.edu> dem...@cis.ohio-state.edu (steven c demlow) writes:
>In article <m6ibhg...@exodus.Eng.Sun.COM> ry...@gobears.Corp.Sun.COM (Richard Yen) writes:
>
>>How do you get the process id of a tcl script? I'm writing an application where
>>information has to be stored in a /tmp file. If the user is running several of the
>>applications at the same time, then I want the /tmp files to be different so there
>>is no conflict, so I was thinking of using something like /tmp/file.$$, as with a
>>Bourne shell script.
>
>I use the following to create /tmp file names:
>
>global errorCode
>catch {exec /bin/false}
>set pid [lindex $errorCode 1]
>set tmpfile "/tmp/file.$pid"
>

I use

set PID [exec /bin/sh -c "echo $$"]

This likewise returns the PID of a child process. Not exactly what one
wants, but it will do.


David Sibley | "Accurate reckoning. The entrance into knowledge
Amateur radio NT3O | of all existing things and all obscure secrets."
sib...@math.psu.edu | -- The Rhind Papyrus

Scott Schwartz

unread,
Aug 11, 1993, 11:39:22 PM8/11/93
to
sib...@math.psu.edu (David Sibley) writes:
set PID [exec /bin/sh -c "echo $$"]

That leaves me cold. It doesn't give you a useful result, since pids
_do_ wrap around. It is worth doing this in a way that gives a
correct answer, something like

set PID [exec /usr/local/bin/ppid]

One implementation of ppid is ``perl -e "print getppid()"''.
Another is

main()
{
char buf[128];
sprintf (buf, "%d\n", getppid());
write (1, buf, strlen(buf));
}

My feeling is that tcl is _by design_ far short of being a general
purpose or systems programming language. If you want scheme or perl
you know where to find them. If you use tcl, plan on doing some
coding in C (except in very rare cases.)

Gerald W. Lester

unread,
Aug 11, 1993, 6:03:10 PM8/11/93
to

If you are using TclX, try:

set pid [id process]


Also please try to limit lines to under 80 characters, thanks.

==========================================================================
* Gerald W. Lester ! Voice: (504)-889-2784 *
* Computerized Processes Unlimited ! FAX: (504)-889-2799 *
* 4200 S. I-10 Service Road, Suite #205 ! E-Mail: g...@cpu.com *
* Metairie, LA 70001 ! *
==========================================================================

Juergen Wagner

unread,
Aug 12, 1993, 5:08:02 AM8/12/93
to
Three alternatives:

[1] Use the date and time (this may break under some operating systems
like VMS where some string replacement for special characters has to
be performed first):

set file /tmp/tmp.[date]

[2] Use a shell to get the (unique) pid of a child process:

set file /tmp/tmp.[sh -c {echo $$}]

[3] Integrate the following piece of C code as a Tcl command "getpid"
into your wish. This provides a command "getpid ?-parent?", returning
the pid of the current or its parent process.

/*
* The_getpid ?-parent?
*
* Determine the process id of this process or of its parent.
*
* Results:
* A string containing the requested process id.
*
*/

int
The_getpidCmd(clientData, interp, argc, argv)
ClientData clientData; /* Main window of interp. */
Tcl_Interp *interp; /* Current interpreter. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
if (argc == 1) {
sprintf(tmp, "%d", getpid());
} else if (argc > 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" ?-parent?\"", (char *) NULL);
return(TCL_ERROR);
} else if (strcmp(argv[1], "-parent")) {
Tcl_AppendResult(interp, "syntax: ", argv[0], " ?-parent?", NULL);
return(TCL_ERROR);
} else {
sprintf(tmp, "%d", getppid());
}

Tcl_SetResult(interp, tmp, TCL_VOLATILE);
return(TCL_OK);
}

Use it in a Tcl script to create the file name:

set file /tmp/tmp.[getpid]

--Juergen

J_Wa...@iao.fhg.de
gan...@csli.stanford.edu

PS: "getpid" will be included in theObjects-2.2. I was in need of
exactly such a function for creating temporary files, e.g., for
printing.

Andreas Gustafsson

unread,
Aug 12, 1993, 7:22:23 AM8/12/93
to
In article <m6ibhg...@exodus.Eng.Sun.COM> ry...@gobears.Corp.Sun.COM asks:

> How do you get the process id of a tcl script?

I'm surprised that none of the numerous followups have mentioned that
Tcl 7.0 has a built-in command "pid".
--
Andreas Gustafsson
Internet: gs...@niksula.hut.fi
Voice: +358 0 422 682

Sven Delmas

unread,
Aug 12, 1993, 8:37:06 AM8/12/93
to
In article <CBMHG...@cse.psu.edu> sib...@math.psu.edu (David Sibley) writes:

In article <24bd4k...@frisbee.cis.ohio-state.edu> dem...@cis.ohio-state.edu (steven c demlow) writes:
>In article <m6ibhg...@exodus.Eng.Sun.COM> ry...@gobears.Corp.Sun.COM (Richard Yen) writes:
>
>>How do you get the process id of a tcl script? I'm writing an application where
>>information has to be stored in a /tmp file. If the user is running several of the
>>applications at the same time, then I want the /tmp files to be different so there
>>is no conflict, so I was thinking of using something like /tmp/file.$$, as with a
>>Bourne shell script.
>
>I use the following to create /tmp file names:
>
>global errorCode
>catch {exec /bin/false}
>set pid [lindex $errorCode 1]
>set tmpfile "/tmp/file.$pid"
>

I use

set PID [exec /bin/sh -c "echo $$"]

I use Tcl7.0b2 and the builtin command pid(n).

Greetings, Sven

--
----
Sven Delmas (Techn. Univ. Berlin)
garf...@cs.tu-berlin.de
----
For every problem there is a solution which is simple, clean and wrong.
Henry Louis Mencken

Karl Lehenbauer

unread,
Aug 12, 1993, 7:23:02 AM8/12/93
to
In article <SCHWARTZ.93...@groucho.cse.psu.edu> schw...@groucho.cse.psu.edu (Scott Schwartz) writes:
>My feeling is that tcl is _by design_ far short of being a general
>purpose or systems programming language. If you want scheme or perl
>you know where to find them. If you use tcl, plan on doing some
>coding in C (except in very rare cases.)

Extended Tcl is a standard and widely used extension set to Tcl that provides
interfaces to most system calls and library routines. After adding Extended
Tcl, Tcl is a systems programming language. Below are most of the TclX
commands that facilitate systems programming:

alarm set an alarm to go off in a specified number of seconds
chgrp change group of a file
chmod change access modes of a file
chown change ownership of a file
copyfile copy the rest of one open file into another

dup dup(2) a file descriptor
execl do an execl(2), replacing the current process with one specified
fork fork the current process, returning 0 to child and pid to parent
kill send a signal to a process
pipe create a pipe, returning two file descriptors
signal trap, ignore, etc, a signal
select wait for I/O to be ready on multiple descriptors, or timeout
wait wait for a process to terminate

fcntl perform fcntl(2) functions on the specified file descriptor
flock file locking
funlock
fstat get stat data for an open file
getclock get the system time in seconds since 1970

id get and set (where relevant) the process ID, parent's process
ID, user ID, effective user ID, group ID, effective group ID,
process group and some other stuff

link create a link (symbolic link option if symlinks are available)
mkdir create a directory
rmdir remote a directory
system call system(3)
unlink delete a file

If you would care to dispute that these constructs provide systems programming
capabilities, please do so in some factual manner. If something useful that
you need is missing, please let us know what it is.

--
-- Email in...@NeoSoft.com for info on getting interactive Internet access.
"Only a brave person is willing honestly to admit, and fearlessly to face,
what a sincere and logical mind discovers." -- Rodan of Alexandria

Scott Schwartz

unread,
Aug 12, 1993, 2:01:10 PM8/12/93
to
ka...@NeoSoft.com (Karl Lehenbauer) writes:
Extended Tcl is a standard and widely used extension set to Tcl
that provides interfaces to most system calls and library routines.
After adding Extended Tcl, Tcl is a systems programming language.

Yes, yes, I wasn't trying to slight TclX. I consider it to be an
instance of "writing some C code", since it is an extenstion to tcl,
and even more so because it requires altering tcl internals to support
signals.

Karl Lehenbauer

unread,
Aug 13, 1993, 8:17:28 AM8/13/93
to
In article <SCHWARTZ.93...@roke.cse.psu.edu> schw...@roke.cse.psu.edu (Scott Schwartz) writes:
>Yes, yes, I wasn't trying to slight TclX. I consider it to be an
>instance of "writing some C code", since it is an extenstion to tcl,
>and even more so because it requires altering tcl internals to support
>signals.

Well, YOU didn't have to write any C code. What happened to "Tcl _by design_
can't do systems programming?" And, last of all, John is adding signal
support into Tcl 7, so TclX won't have to modify the baseline anymore.

Scott Schwartz

unread,
Aug 13, 1993, 1:52:33 PM8/13/93
to
ka...@NeoSoft.com (Karl Lehenbauer) writes:
Well, YOU didn't have to write any C code.

I did have to augment tcl. That's the point, see?

What happened to "Tcl _by design_ can't do systems programming?"

The correct quote is ``My feeling is that tcl is _by design_ far short
of being a general purpose or systems programming language.'' Tcl
omits all sorts of things that Scheme, Perl, and TclX include. John
can correct me if I'm wrong, but my understanding is that this is a
deliberate design decision.

Tuan Doan

unread,
Aug 14, 1993, 11:36:49 PM8/14/93
to
>My feeling is that tcl is _by design_ far short of being a general
>purpose or systems programming language. If you want scheme or perl
>you know where to find them. If you use tcl, plan on doing some
>coding in C (except in very rare cases.)

You are correct. TCL is an application language; which is in turn written
in C, a system language. So it does, in essence, has the best of
both world. (1) a high-level application platform (2) a way to change it
to fit your needs.

Regards,

__ __/ / / __ / | / Tuan T. Doan
/ / / / / / | / IEC Layer Testing and Advance Technology
/ / / __ / / | / 2201 Lakeside Blvd. P.O. Box 833871
__/ ______/ __/ __/ __/ __/ Richardson, TX 75083-3871
Phone: 6-444-4575/214-684-4575
Internet: td...@bnr.ca Fax: 6-444-3716/214-684-3716
or td...@x400gate.bnr.ca


0 new messages