Part 26: Systems

So far every program has been a world of its own: it computed, read a file or two, and printed. Real programs live among others. They are started by a shell with arguments and an environment, and they answer with an exit status. They start programs of their own. They are driven by options typed on a command line, they keep a log for whoever has to find out later what happened, and they talk to other programs over the network.

This part covers that boundary, with four standard packages: Process for the program's own arguments, environment and exit, and for child processes; CommandLine for options and help text; Log for structured records; and Net for addresses, TCP and UDP. Everything in it runs on one machine — the network lessons use the loopback address, which never leaves it — so every program prints the same on every run.

What you will learn

  • Reading the arguments and environment a program was started with, as native text, and ending it early with Exit.
  • Starting another program with a Command: waiting for its exit status, capturing what it prints, giving it an environment, and talking to it through pipes.
  • Describing a command's options, positional arguments and subcommands once, and getting parsing, error messages and --help from that one description.
  • Writing log records with a level and typed fields, as text for people or JSON lines for tools, and keeping log files from growing without end.
  • Reading, writing and classifying IPv4, IPv6 and socket addresses.
  • A TCP server and client exchanging lines over a stream, and the framing a stream needs.
  • UDP datagrams, which keep their boundaries but promise nothing, and a timeout for an answer that never comes.

The part at a glance

flowchart LR
    shell(["Shell"]) -- "arguments<br/>environment" --> p["Your program"]
    p -- "exit status" --> shell
    p -- "Command" --> child["Child process"]
    p -- "Logger" --> log[("Log")]
    p -- "TcpStream<br/>UdpSocket" --> peer["Another program"]
PackageLessonsWhat it is for
ProcessEnvironment, Child processthis process, and the processes it starts
CommandLineCommand lineoptions, positionals, subcommands and help
LogLogrecords with levels and typed fields
NetIP addresses, TCP, UDPaddresses, streams and datagrams

Lessons

LessonWhat you will learn
26.1Environmentthe program's arguments, environment variables and exit status
26.2Child processrun another program and capture its output
26.3Command linedeclare options and arguments, and get the help text for free
26.4Logstructured log records with levels and typed fields
26.5IP addressesIPv4 and IPv6 addresses, and socket addresses with a port
26.6TCPa TCP server and client talking over the loopback interface
26.7UDPdatagrams between two UDP sockets

Before you start

Arguments, environment values and file names are all native text, so this part starts from Path in Part 19: Files, and its streams — pipes and sockets — are the same readers and writers as files, read with Input's ReadLineFrom. Almost every call can fail, which brings back Part 9: Errors. The TCP lesson runs its client on a thread from Part 25: Concurrency. Each lesson's package is in the Examples repository's Systems/ folder:

cd Examples/Systems/Environment
rux run

Two lessons are best tried with arguments of your own as well, given after --:

rux run -- --quit

Only one line in the whole part changes between runs — the process identifier in Environment — and its page says so.

After this part

Try the checkpoint project: Grep is a small version of the Unix tool that prints the lines of files matching a regular expression, with options read by the CommandLine package and an exit status that scripts can rely on.

Then go on to Part 27: Projects, the complete programs that close the course — each one a checkpoint for an earlier part.

For the rules underneath, see The Main function and Conditional compilation in the Rux Reference, and the Process, CommandLine, Log and Net packages in the API reference.