UNIX: Is It For You? Part II: For The Programmer

Copyright 1991 Daniel J. Barrett. This article originally appeared in .INFO Magazine, November 1991. This article is freely distributable as long as it is not modified.

Last time, we discussed some differences between the UNIX and Amiga operating systems that affect all users. We now discuss issues relevant to programmers and power-users: sophisticated UNIX use, programming, and systems administration.

BEING A UNIX POWER USER

UNIX has lots of opportunities for users who like to get the most out of their computers. Most UNIX programs, including the shell user interface, are highly configurable. Some of them even let you define the behavior of every single keystroke and mouse-click (or you can accept the defaults).

Once you have mastered various UNIX programs, the power doesn't stop there. The UNIX shell lets you build sophisticated "pipelines" that send data from one program into another. As an example, let's find out the most common first name of all the users on a UNIX machine. In a single command, you can get a list of all user names from the file /etc/passwd, extract the first names, sort them, count adjacent identical names, sort the resulting numbers, and then find the largest:

	Command:	cut -d: -f5 /etc/passwd		\
				| cut -d' ' -f1		\
				| sort			\
				| uniq -c		\
				| sort -nr		\
				| head -1

	Response:	12 John
(The forward slashes '\' mean "continued on the next line"; I used them to emphasize the different components of the command.) The command probably looks cryptic to you now, but this kind of operation quickly becomes second-nature as you use UNIX. Because all of the above programs (cut, sort, uniq, head) come standard with UNIX, you can expect the command to work on almost anybody's UNIX machine.

How does one become a UNIX power user? Mainly by exploring the system directories and becoming familiar with many programs. In my experience, if you spend time learning a new UNIX utility, you gain the time back (and more) in your first week of use.

PROGRAM DEVELOPMENT

The most popular UNIX programming language is C. In fact, UNIX itself is written almost entirely in C. (Some assembly required.) Many people consider UNIX to be one of the most versatile and productive programming environments available, for several reasons. First, it has many programmer's tools. Second, it does not allow individual programs to crash the machine. Third, the source code for many UNIX utilities is generally available.

All commercial Amiga C compilers come with a utility called "make" which helps the programmer keep track of large programs. Would you like to guess where "make" originated? It was written for UNIX by Stu Feldman of Bell Laboratories. UNIX provides a veritable arsenal of programmer's tools. There's "lex" and "yacc" to help you write complicated input routines, "sccs" and "rcs" for maintaining multiple versions of programs, "lint" to spot-check your C code for common errors, "ctags" for jumping quickly between functions in your text editor, "ctrace" for tracing program execution, "prof" for identifying the slowest parts of your code, various debuggers, and much more. Combine these tools with a powerful shell and a multitasking operating system, and you have one SERIOUS programming environment. (Some of these tools have been ported to the Amiga and are available on Fish Disks.)

VIRTUALLY SPEAKING...

UNIX has three important features for programmers that AmigaOS does not: memory protection, virtual memory, and resource tracking. These features come at a price, however: UNIX has much more overhead than does the Amiga kernel, and therefore runs more slowly.

Memory protection prevents one program from affecting the memory owned by another program. On the Amiga, this is not true: programs are free to scribble all over each other's memory. This is why individual programs are capable of crashing the computer (the famous guru meditation). When a UNIX program crashes, it doesn't bring down the whole machine; instead, UNIX takes a "photograph" of the program's memory, stores it in a file, and then terminates the program. This file, called a "core dump", may now be examined with a debugger to determine the cause of the crash.

Virtual memory allows a program to access more RAM than actually exists on the machine. This is done by causing a section of your hard disk act as if it were RAM. The operating system moves running programs between the hard disk and true RAM as needed.

Resource tracking means that the programmer doesn't have to free all the resources that she allocates. When a program exits, all of its allocated memory, files, devices, and so on, are deallocated automatically. This means that the operating system can kill a running program and be sure that everything gets cleaned up properly. On the Amiga, this is not the case: programs are required to keep track of their own allocated resources and explicitly deallocate them. There is no completely reliable way to make the operating system kill a running program, although some clever programs such as "xoper" make a good attempt.

According to rumors floating around the Amiga community, the Amiga operating system may one day incorporate memory protection, virtual memory, and/or resource tracking. However, the addition of some of these features would cause serious incompatibility with many existing programs, and some people do not want to sacrifice the CPU cycles that these features will require. Let's hope that Commodore comes up with a solution that makes most users and developers happy.

SYSTEMS PROGRAMMING

Like AmigaOS, UNIX provides programmers with hundreds of system functions and structures for manipulating UNIX-specific information. For example, if you want to learn the username of the owner of a file, you can call stat() to find the user ID number of the owner, pass that value to getpwuid() to look up the user's name, and then print the answer.
/* Given a filename as argv[1], print the name of the owner. */

#include < stdio.h>
#include < sys/types.h>
#include < sys/stat.h>
#include < pwd.h>

main(int argc, char *argv[])
{
	struct stat info;		/* A buffer for file information.  */
	struct passwd *pw;		/* Pointer to to user information. */

	if (argc != 2)
		fprintf(stderr, "Usage: %s filename\n", argv[0]);

	else if (stat(argv[1], &info) < 0)
		fprintf(stderr, "File %s does not exist.\n", argv[1]);

	else if ((pw = getpwuid(info.st_uid)) == NULL)
		fprintf(stderr, "I can't find the owner's name!\n");

	else
		printf("The owner of file %s is named %s.\n",
			argv[1],  pw->pw_name);
}
A major programming advantage of UNIX is that the operating system itself may be modified conveniently: some or all of its source code comes supplied with the UNIX distribution. Recompiling the kernel (the low-level part of UNIX) requires just a few commands. The catch is that the kernel source code itself can be difficult to understand.

SYSTEMS ADMINISTRATION

Remember when you first started using an Amiga? Although you could do some fun things right away, it probably took you a while to set up the Amiga just the way you like it: modifying the Startup-Sequence and the Mountlist files, setting up your printer with Preferences, changing the screen colors and fonts, installing commercial and public domain software packages, organizing files on the hard drive, making a search path, and so on. A similar process must be done on almost any other computer before you feel completely comfortable using it.

Under UNIX, this process of "systems administration" is much more complex than under the Amiga operating system, for several reasons. First of all, there is no consistent method for tailoring a UNIX machine. For example, printer setup is done totally differently from network management or electronic mail configuration. There's no equivalent of Preferences on UNIX. (Some companies have tried to make such programs, like IBM's "smit" program for AIX UNIX, but these have interestingly enough been accused of being "un-UNIX-like" by members of the UNIX community.)

What is the reason for this inconsistency? Realize that UNIX was written by hundreds of different people over a 20-year period. Although many of the individual programs were carefully crafted in advance, the overall framework was not. UNIX was originally written by programmers and for programmers.

Another reason that UNIX administration is more difficult than the Amiga's is that there simply are more things to administrate! You have multiple printers being used by many machines simultaneously, network connection and data routing, electronic mail, automatic lookup of other computer addresses ("name service"), adding and removing users, automatic startup of programs at regular intervals, system accounting and usage statistics, security tools, and more. And don't forget that these services may be used by many users at once, adding to the complication.

Don't let this difficultly discourage you from using UNIX! If you are a reasonably intelligent person with programming experience who likes to learn about computers, then UNIX systems administration is within your abilities. If you encounter problems that you do not know how to solve, don't panic. UNIX has been around long enough that many problems have already been solved by someone else. If you have access to USENET or other electronic news services, you will find thousands of people willing to help you.

DO I NEED UNIX?

Serious programmers should definitely check out UNIX. With its large selection of languages, tools, and shells, UNIX can be a great development environment. Amiga power users may also find UNIX to be useful and fun, especially if they like to explore and tailor the computer environment to its fullest.

SUGGESTED READING

Assuming that you already know C, here are some good UNIX programming books. "Using C On The UNIX System" by David A. Curry (O'Reilly & Associates, Inc., 1989) is an excellent guide containing many example programs. This book is one in a series of "Nutshell Handbooks" that are enjoyed and recommended by many UNIX programmers. After this introduction, check out "Advanced UNIX Programming" by Marc J. Rochkind (Prentice-Hall, 1985) for more in-depth information. Low-level operating system information may be found in "The Design Of The UNIX Operating System" by Maurice Bach (Prentice-Hall, 1986). To learn more about systems administration, see "UNIX System Administration" by David Fiedler and Bruce Hunter (Hayden Book Company, 1986).

About The Author

Daniel Barrett is a long-time Amiga user and UNIX systems administrator. You are welcome to contact him by e-mail:
	INTERNET:	barrett@cs.umass.edu
	COMPUSERVE:	>internet:barrett@cs.umass.edu

- barrett@cs.umass.edu