The tsort program is a
command line
A command-line interpreter or command-line processor uses a command-line interface (CLI) to receive commands from a user in the form of lines of text. This provides a means of setting parameters for the environment, invoking executables and pro ...
utility on
Unix
Unix (; trademarked as UNIX) is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
and
Unix-like
A Unix-like (sometimes referred to as UN*X or *nix) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Unix-li ...
platforms, that performs a
topological sort on its input. , it is part of the
POSIX
The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines both the system- and user-level application programming inte ...
.1 standard.
History
According to its
info page, this command was initially written for providing an ordering of object files that allowed the
linker
Linker or linkers may refer to:
Computing
* Linker (computing), a computer program that takes one or more object files generated by a compiler or generated by an assembler and links them with libraries, generating an executable program or shar ...
to process them sequentially (each one exactly once, and in order). The FreeBSD manual page dates its appearance to
Version 7 Unix
Seventh Edition Unix, also called Version 7 Unix, Version 7 or just V7, was an important early release of the Unix operating system. V7, released in 1979, was the last Bell Laboratories release to see widespread distribution before the commercia ...
.
Note that the following description is describing the behaviour of the
FreeBSD
FreeBSD is a free and open-source Unix-like operating system descended from the Berkeley Software Distribution (BSD), which was based on Research Unix. The first version of FreeBSD was released in 1993. In 2005, FreeBSD was the most popular ...
implementation of tsort and mentions GNU features where they may exist. Other implementations or versions may differ.
Syntax
tsort
dlqILE
Ile may refer to:
* iLe, a Puerto Rican singer
* Ile District (disambiguation), multiple places
* Ilé-Ifẹ̀, an ancient Yoruba city in south-western Nigeria
* Interlingue (ISO 639:ile), a planned language
* Isoleucine, an amino acid
* Another ...
''
FreeBSD options can be:
-d turn on debugging
-l search for and display the longest cycle.
-q Do not display informational messages about cycles.
GNU provides the following options only:
--help display help message and exit
--version display version information and exit
There are no options prescribed by POSIX.
Behavior
tsort reads its input (from the given FILE, or
standard input if no input file is given or for a FILE of '-') as pairs of strings, separated by blanks, indicating a partial ordering. The output is a total ordering that corresponds to the given partial ordering.
In other words: for a
directed acyclic graph
In mathematics, particularly graph theory, and computer science, a directed acyclic graph (DAG) is a directed graph with no directed cycles. That is, it consists of vertices and edges (also called ''arcs''), with each edge directed from one v ...
(used as a
dependency graph), tsort produces a listing of the
vertices so that for all edges 'a->b', 'a' comes before 'b' in the listing.
Examples
tsort lists the vertices of a
directed acyclic graph
In mathematics, particularly graph theory, and computer science, a directed acyclic graph (DAG) is a directed graph with no directed cycles. That is, it consists of vertices and edges (also called ''arcs''), with each edge directed from one v ...
in such an order that all ordering/direction relations are respected:
Call graph
tsort can help rearranging functions in a source file so that as many as possible are defined before they are used (Interpret the following as: calls , and ; calls , and so on. The result is that should be defined first, second, etc.):
Library
The traditional
ld (Unix linker) requires that its library inputs be sorted in topological order, since it processes files in a single pass. This applies both to static libraries () and dynamic libraries (), and in the case of static libraries preferably for the individual object files contained within.
BSD UNIX uses tsort as a common part of the typical
ar &
ranlib command invocations (from /usr/share/mk/bsd.lib.mk):
lib$.a: $ $
@$ building static $ library
@$ cq $ `lorder $ $ , tsort -q` $
$ $
Here ("library order") is used to generate the inter-file dependency list by inspecting the symbol table.
Usage notes
Notice the interchangeability of white space separators so the following inputs are equivalent:
Pairs of identical items indicate presence of a vertex, but not ordering (so the following represents one vertex without edges):
a a
Strictly speaking there is no topological ordering of a graph that contains one or more
cycles. However tsort prints a warning and GNU tsort prints the
detected cycles to
standard error
The standard error (SE) of a statistic (usually an estimate of a parameter) is the standard deviation of its sampling distribution or an estimate of that standard deviation. If the statistic is the sample mean, it is called the standard error ...
(lines beginning with 'tsort:'):
$ tsort < a b
> b c
> c a
> EOF
UX: tsort: INFORM: cycle in data
tsort: a
tsort: b
tsort: c
a
b
c
See also
*
Sort (Unix)
*
Make (software)
In software development, Make is a build automation tool that automatically builds executable programs and libraries from source code by reading files called ''Makefiles'' which specify how to derive the target program. Though integrated develo ...
*
Topological sorting
In computer science, a topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for every directed edge ''uv'' from vertex ''u'' to vertex ''v'', ''u'' comes before ''v'' in the ordering. For in ...
*
List of Unix commands
This is a list of Unix commands as specified by IEEE Std 1003.1-2008, which is part of the Single UNIX Specification (SUS). These commands can be found on Unix operating systems and most Unix-like operating systems.
List
See also
* List of G ...
*
Call graph
A call graph (also known as a call multigraph) is a control-flow graph, which represents calling relationships between subroutines in a computer program. Each node represents a procedure and each edge ''(f, g)'' indicates that procedure ''f'' call ...
References
Further reading
*
*
External links
manual page of tsort on
FreeBSD
OpenBSD
NetBSD
HP-UXdep-traceOrders basic dependencies and unfolds nested ones. (basic: without 2D graphical presumption)
{{Core Utilities commands
Unix SUS2008 utilities
Inferno (operating system) commands