UNIX Lecture Notes – Chapter 05

Categories:

Recommended

5.1 The Different Types of UNIX Programs

Programs that run in a UNIX environment can be classified by their relationship to terminal devices and by their input/output streams. They generally fall into one of three categories: software tools, daemons, and interactive user programs.

5.1.1 Software Tools (Filters)

A software tool is a program that

  • receives its input from either 
    • one or more files given as command-line arguments, or 
    • from standard input if no filename arguments are present,
  • expects its input to be an unstructured stream of bytes, almost always treated as plain text, and
  • puts its output, which is also a stream of bytes, usually plain text, on standard output.

Because software tools can read from standard input and write to standard output, they can be connected via shell pipes to form pipelines, like factory assembly lines. UNIX has many software tools, including awk, cat, cut, du, fold, grep, od, sed, sort, tr, and uniq.

5.1.2 Daemons

Another class of programs are device drivers, which are not even attached to a terminal. A program that is not attached to a terminal is called a daemon in UNIX. A commonly used, but inaccurate, definition of a daemon is that it is a “background” process. To be precise, it is a process that executes without an associated terminal or login shell, waiting for an event to occur. The event might be a user request for a service such as printing or connecting to the Internet, or a clock tick indicating that it is time to run. The word “daemon” is from Greek mythology, and refers to a lesser god who did helpful tasks for the people he or she protected. Daemons are like these lesser gods; they are created at boot time, and exist, in hiding, ready to provide services when called upon.

Because daemons must not be connected to a terminal, one of their first tasks is to close all open file descriptors (in particular, standard input, standard output and standard error). They usually make their working directory the root of the file system. They then take additional steps to break their association with any shell or terminal, among which are leaving their process group and registering their intent to ignore all incoming signals. The concept of process groups will be discussed in Chapter 8. Signals are covered later in this chapter.

Daemon process names typically end in ‘d’. This is one way to identify a daemon process in the output of the ps -ef command; nd the names ending in “d” as in ftpd, httpd, lpd, sshd, syslogd, and telnetd. Daemons will be covered thoroughly in Chapter 8.

5.1.3 Interactive User Programs

Another category of programs are those that are tied to the user terminal in an inextricable way, because they customize the terminal for their own use. Programs that interact with the user through the terminal, such as text editors (vi, nano, or emacs), pagers (more and less), terminal-based administrative tools such as top, games (snake, worm, and chess), and terminal-based mail clients (pine and mailx), are tightly coupled to the terminal and must control its settings and attributes. They cannot use the standard input and output streams for communicating with the user because these lack the types of controls that a terminal has. These types of programs usually need to control

whether or not characters are echoed,

the number of characters that are buffered, if any,

the movement of the cursor on the screen,

whether certain key presses should have their default meaning or have application-defined meaning,

whether timeouts should occur on input,

whether signals such as Ctrl-C should be ignored, queued, or handled immediately.

We already saw how to control the state of the terminal using stty at the command level and the tcgetattr() and tcsetattr() functions at the programming level. Here we will explore the various modes into which we can put the terminal for the benefit of creating interactive programs.

Attribution

Stewart Weiss (2019), UNIX Application and System Programming, URL: http://www.compsci.hunter.cuny.edu/~sweiss/course_materials/unix_lecture_notes.php

This work is licensed under Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) :  (https://creativecommons.org/licenses/by-sa/4.0/).

VP Flipbook Maker

Create captivating flipbooks with Visual Paradigm Online Flipbook Maker! Convert your content into interactive flipbooks in a few clicks and customize them with intuitive design tools. Try it now!