ZetCode

Tcl pid Command

last modified October 20, 2026

The Tcl pid command returns the process ID of the current process or the process IDs of a command pipeline opened with open. Process IDs are numbers assigned by the operating system to running processes; they are unique only among the processes running at the same time.

Basic Definition

The pid command with no arguments returns the process ID of the current Tcl process. With a channel argument, it returns a list of the IDs of the processes in the pipeline that the channel is connected to. For a channel that is not a pipeline, such as stdout, the result is an empty string.

Syntax: pid ?channelId?. The channelId is optional and refers to a command pipeline created with open. A background command started with exec has no channel; exec returns its process IDs instead.

Getting Current Process ID

This example shows the simplest usage of pid to get the current process ID.

basic_pid.tcl
set current_pid [pid]
puts "Current process ID: $current_pid"

This retrieves and prints the process ID of the running Tcl interpreter. The ID will be different each time the script runs.

Getting Process ID of a Pipeline

When working with command pipelines, pid can return the process ID of the external command. The result is a list with one ID per process in the pipeline; here there is only one.

pid_pipeline.tcl
set pipeline [open "|ls -l"]
set ls_pid [pid $pipeline]
puts "ls command PID: $ls_pid"
set output [read $pipeline]
close $pipeline

This creates a pipeline to run the ls -l command and gets its process ID. The output is read before the pipeline is closed; closing it while the command still has data to write would kill the command with a broken pipe error. Always remember to close pipelines when done.

Comparing Process IDs

You can compare process IDs to determine if two commands run in the same process.

pid_compare.tcl
set pid1 [pid]
set pid2 [pid]
if {$pid1 == $pid2} {
    puts "Both PIDs are the same (same process)"
} else {
    puts "Different processes"
}

This demonstrates that multiple pid calls in the same process return the same value. The comparison will always be true in this case.

Process IDs of Background Processes

When exec starts a background process, it returns the process ID, which can then be used to control the child process.

pid_exec.tcl
set child_pid [exec sleep 10 &]
puts "Child process PID: $child_pid"
exec kill $child_pid

This runs sleep in the background. When exec ends with & it does not wait for the command and returns its process ID instead of a channel. The script then ends the process with kill.

Process ID in File Operations

Process IDs can be useful when working with temporary files to create unique names.

pid_file.tcl
set tempfile "/tmp/temp_[pid].txt"
set fd [open $tempfile w]
puts $fd "Temporary data"
close $fd
puts "Created temporary file: $tempfile"

This creates a temporary file with the process ID in its name, so that two running programs do not use the same file. The file is opened, written to, and then closed. The name is easy to guess and a stale file from an earlier process with the same ID may exist, so for real temporary files prefer file tempfile.

Monitoring Process Completion

A pipeline opened with open can be used to wait for an external process to complete.

pid_monitor.tcl
set channel [open "|sleep 5"]
set process_id [pid $channel]
puts "Waiting for process $process_id to complete..."
close $channel
puts "Process completed"

This starts a sleep command, prints its process ID and waits for it to finish. The close command blocks until the process completes, so "Process completed" is printed after about five seconds.

Best Practices

This tutorial covered the Tcl pid command with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Tcl Tutorials.