Chapter Outline
Getting Started
What is UNIX?
What is Linux?
Distributions
The GNU Project and the Free Software Foundation
Programming Linux
UNIX Programs
The C CompilerHow It Works
Getting Help
Development SystemRoadmapPrograms
UNIX Philosophy
Header Files
Library Files
Static Libraries
Shared LibrariesSimplicity
Summary
Focus
Reusable Components
Filters
Open File Formats
FlexibilityLecture Notes
Getting Started
The UNIX system comes with a UNIX development system. You are about to write your first UNIX program.
What is UNIX?
The UNIX operating system was originally developed at Bell Laboratories, in the 1970's, for the Digital Equipment PDP computers. It is a multiuser, multitasking operating system.
What is Linux?
Linux is a freely distributed implementation of a UNIX-like kernel. It is a computer operating system that conforms to the X/Open specification XPG4.2.
Distributions
A distribution is a complete UNIX-like system.
The GNU Project and the Free Software Foundation
The Linux community supports free software, that is free from restrictions, subject to the GNU General Public License. The Free Software Foundation was set up by Richard Stallman, the author of GNU Emacs. Some of the software distributed under the GPL includes: gcc, g++, gdb, gnumake, bison, bash, and emacs.
Programming Linux
A vast range of programming languages are available for UNIX systems, and many are free. Here are some of them:
UNIX Programs
Applications under UNIX are prepresented by two special types of file: executables and scripts. UNIX doesn't require that executables or scripts have a specific file name nor any particular extension.
When you log in to a UNIX system, you interact with a shell program (often sh) that undertakes to run programs for you. The directories to search are stored in a shell variable, PATH. It uses : to separate entries. The PATH variable could look like:
/usr/local/bin:/bin:/usr/bin:.:/usr/X11/bin:/usr?openwin/bin
The C Compiler
Let's start developing for UNIX using C by writing, compiling, and running our first UNIX program.
You are now ready to do your first Try It Out exercise on the bottom of page 10. First you need to start and log on to a UNIX/Linux system. Mandrake 8.1 release of Linux will work fine for this task!
1. Get to the command line (or a terminal window if you are in X-Windows.) To start a terminal window, Click on the K on the lower-left if the task bar, then on terminals in the pop-up window, then A Term or X Term.
2. Start the vi editor for a new filed called hello.c by typing:vi hello.c
3. Press the i key, to start the insertion mode.
4. Enter the following C program statements:#include
5. Now press the Esc key to stop the insertion mode.
int main()
{printf("Hello World\n");
}
exit (0);
6. Type the following::wq
and then press the Enter key, to save the file and quit the vi editor.
7. To compile the pile, type:cc -o hello hello.c
and press the Enter key.
8. To execute the program, type:./hello
and press the Enter key.
9. You should see the following output:Hello World
How It Works
The cc -o hello hello.c command invoked the C compiler (cc), translated it into machine language and saved it in the executable file -- hello. Since you probably save the hello.c file in your home directory, and it is not in the PATH variable, you need to type ./hello to execute the hello file. The ./ means to execute the program in the current directory with the given name.
Getting Help.
The man command provides access to the online manual pages. To get help on the GNU C compiler (gcc), type:
man gcc
Press the q key to exit man.The info command provides online documentation using the emacs editor. It can jump to the cross-reference links. to get more help on the C compiler type:
info gcc
Press the q key to exit info.Development System Roadmap
For the UNIX developer, it can be important to know a little about where tools and development resources are located.
Programs
Programs are usually kept in directories reserved for the purpose. Programs supplied by the system for general use are found in /usr/bin. Programs added by system administrators are found in /usr/local/bin.
Header Files
For programming in C and other languages, we need header files to provide definitions of constants and declarations for system and libaray function calls. These are generally located in /usr/include directory.
Library Files
Libraries are collections of precompiled functions that have been written to be reusable. For example include libraries for input and output functions are in the stdio library.
The .a is used for the extension for traditional, static libraries. The .so and .sa is used for the shared libraries.
Static Libraries
Static libraries are collections of object files kept together in a ready-to-use form.
When you do the Try It Out, remember:
1. to type ./program when trying to execute the program program.
2. In step for, the command ar ccrv lib.a bill.o fred.o gives the following output:a - bill.o
a - fred.oShared Libraries
The disadvantage of static libraries is that if more than one program is using the same libraries and running at the same time, you end up with many copies of the same functions in memory. Shared libraries allow more than one program at a time to use the same function.
UNIX Philosophy
The UNIX operating system encourages a certain programming paradigm or philosophy, that includes: simplicity, focus, reusable components, filters, open file formats, and flecibility.
Simplicity
Mnay UNIX utilites are very simple and, as a result, small and easy to understand. They use the technique of KISS (Keep It Small and Simple) .
Focus
It's often better to make a program perform a single task well.
Reusable Components
Make the core of your application available as a library.
Filters
Very many UNIX applications can be used as filters. That is, they transform their input and produce an output.
Open File Format
The more successful and popular UNIX programs use configuration files and data files that are plain ASCII text.
Flexibility
Try to be as flexible as possible in your programming.
Summary
This chapter noted things iin common between Linux and commercial UNIX systems. You also got to write a simple program and library using the basic C tools.
CS 248 - UNIX Programming Web Site Menu
Information | Syllabus | Schedule | Online "Lectures" | Projects | Quizzes | Web Board
Copyright © 2001 by James L. Fuller, all rights reserved.