UNIX/Linux & C Programming:
Chapter n: The Token Ring



Coverage: [USP] Chapter 7


Introduction to the token ring (of processes)

  • nodes = processes
  • links = pipes
  • each process is a filter
    • reads from standard input
    • writes to standard output
  • simple
  • inexpensive
  • unidirectional communication: clockwise; each entity has a connection for input and output
  • latency increases as #nodes increase
  • bandwidth is independent of ring size (#nodes)

  • (Full token ring of n processes. Regenerated with minor modifications from [USP] Fig. 7.1, p. )


Base case: token ring of one process

  • process communicates to itself through a pipe
  • notice data written using printf is buffered

  • (After first call to pipe; regenerated with minor modifications from [USP] Fig. 7.2, p. )


    (After first and second calls to dup2; regenerated with minor modifications from [USP] Fig. 7.3, p. )


    (After frist and second calls to close. One process communicating with itself through standard input and output, or a token ring of one process; regenerated with minor modifications from [USP] Fig. 7.4, p. )


Now n=2

  • extending a ring of one process to one of two processes is fairly straightforward
  • now we need two pipes
  • parent's standard input read from standard output of child
  • child's standard input read from standard output of parent

  • (After second call to pipe; regenerated with minor modifications from [USP] Fig. 7.5, p. )


    (After first and only call to fork; regenerated with minor modifications from [USP] Fig. 7.6, p. )


    (After third and fourth calls to dup2; regenerated with minor modifications from [USP] Fig. 7.7, p. 232)


    (After third and fourth calls to close; a token ring of two processes; regenerated with minor modifications from [USP] Fig. 7.7, p. 232)


Extending the ring to an arbitrary number of processes

  • easily extended
  • #pipes necessary = #processes
  • still only 1 array of size 2 necessary for pipe file descriptors
  • resembles process chain (i.e., each process only forks once)
  • in-class exercise: coding infrastructure necessary for token ring with an arbitrary number of processes
    • take #processes n as a command-line argument
    • done in <65 loc, including all of the error checking code

  • (Full token ring of n processes. Regenerated with minor modifications from [USP] Fig. 7.1, p. )


Ring exploration

  • [USP] §7.3
  • in-lab exercise


Ring of processes communicating through (unamed) pipes vs.
Ring of threads communicating through channels

Key: ▢ = process, { or } = thread, → = data, and ~ = pipe or channel.

Ring of processes communicating through (unamed) pipes (C model)           Rings of threads communicating through channels (Go model)
         


Demo of Robbins and Robbins' process ring simulator


The Fibonacci series on the ring

  • [USP] §7.4
  • in-class exercise
  • we were able to compute up to and including the 78th Fibonacci number, using 77 processes; using unsigned long long and the %llu conversion specifier


References

    [USP] K.A. Robbins and S. Robbins. UNIX Systems Programming: Concurrency, Communication, and Threads. Prentice Hall, Upper Saddle River, NJ, Second edition, 2003.

Return Home