Tsort Explained

tsort
Operating System:Unix, Unix-like, V, Inferno
Platform:Cross-platform
Genre:Command

The tsort program is a command line utility on Unix and Unix-like platforms, that performs a topological sort on its input., it is part of the POSIX.1 standard.[1]

History

According to its info[2] page, this command was initially written for providing an ordering of object files that allowed the linker to process them sequentially (each one exactly once, and in order). The FreeBSD manual page dates its appearance to Version 7 Unix.[3]

Note that the following description is describing the behaviour of the FreeBSD implementation of tsort and mentions GNU features where they may exist. Other implementations or versions may differ.

Syntax

tsort [-dlq] [FILE]

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.[4]

In other words: for a directed acyclic graph (used as a dependency graph), tsort produces a listing of thevertices 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 such an order that all ordering/direction relations are respected:

$ tsort < 3 8> 3 10> 5 11> 7 8> 7 11> 8 9> 11 2> 11 9> 11 10> EOF3571181029

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.):

$ cat call-graphmain parse_optionsmain tail_filemain tail_forevertail_file pretty_nametail_file write_headertail_file tailtail_forever rechecktail_forever pretty_nametail_forever write_headertail_forever dump_remaindertail tail_linestail tail_bytestail_lines start_linestail_lines dump_remaindertail_lines file_linestail_lines pipe_linestail_bytes xlseektail_bytes start_bytestail_bytes dump_remaindertail_bytes pipe_bytesfile_lines dump_remainderrecheck pretty_name$ # note: 'tac' reverses the order$ tsort call-graph tacdump_remainderstart_linesfile_linespipe_linesxlseekstart_bytespipe_bytestail_linestail_bytespretty_namewrite_headertailrecheckparse_optionstail_filetail_forevermain

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.[5]

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:

a b b ca b b ca b b ca b b ca b b c

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 (lines beginning with 'tsort:'):$ tsort < a b> b c> c a> EOFUX: tsort: INFORM: cycle in datatsort: atsort: btsort: cabc

See also

References

Further reading

. Donald E. Knuth . . 3rd . 1 . 261–268 . 1997 . 0-201-89683-4.

External links

manual page of tsort on

Notes and References

  1. Web site: tsort. The Open Group. The Open Group Base Specifications Issue 7, 2018 edition.
  2. Web site: Tsort background (GNU Coreutils 9.0).
  3. Web site: Tsort.
  4. Web site: Tsort invocation (GNU Coreutils 9.0).
  5. Web site: c++ - gcc ld: method to determine link order of static libraries . Stack Overflow.