Introduction

This is a book about UNIX programming. It starts with basic concepts and ends with coverage of advanced topics. It is a self-teaching guide, and yet it functions as a UNIX reference book.

The examples provided are written in the C and C++ languages. The examples are short programs, each intended to demonstrate use of a particular programming facility. The C++ programs are written as simple programs and should be well understood by those that do not program in C++.

This book attempts to be UNIX platform neutral. Throughout the book, differences in functionality are noted for your convenience. This will save you time when you must write projects that must be UNIX portable.

FreeBSD 3.4 release is used throughout this book for demonstration purposes. This guarantees that the example programs will compile and run without any additional effort on that platform. This also grants a specific level of functionality, since some functions are lacking or vary on other platforms. You can obtain FreeBSD from the Internet or purchase it on a CD-ROM at a nominal cost. This allows you to work through the book on a platform that is on a par with other professional UNIX platforms.

Who Should Use This Book

This book is written for C and C++ UNIX programmers, but it is not limited to that audience. Even a Java or Perl programmer might have an occasional need to write a small C function to invoke a UNIX system call.

Programmers at both the application and system levels will benefit from this book. There is coverage ranging from basic to advanced functionality that will aid any UNIX application developer. And difficult topics such as semaphores and memory-mapped files are covered for system level programmers.

What You Should Know

To gain the most from this book, the reader should be comfortable with the C programming language. A rudimentary understanding of C++ is helpful but not mandatory. Most C language texts cover the use of the standard I/O stream functions such as fopen(3) and fgets(3). Consequently, these file stream functions are not repeated in this book.

It is assumed that the reader has mastered the basics of working with the UNIX shell and has a basic familiarity with the standard commands. Some examples used in this book run the programs in the background using the shell & character. Consequently, the reader should be familiar with basic job control within the shell.

What You Will Learn

This book should appeal to both the beginning and the advanced programmer. The first part of the book covers the basics of UNIX file system concepts, file input and output, and directory management. For the advanced reader, this functions as a review and a reference.

The second part of the book covers intermediate basics, such as numeric conversion and date/time facilities. Other application concepts such as command-line processing and embedded database routines are also covered. Consequently, this part tends to be focused somewhat toward UNIX applications.

The third and final part of the book covers advanced topics. It begins with coverage of signals, input and output scheduling, and interval timers. Process control and the use of pipes and forked processes are also covered. Combined with complete coverage of interprocess communication, this part tends to benefit primarily the system programmer. Chapters on pattern matching and regular expressions and an introduction to X Window programming are helpful to the application programmer.

The Structure of This Book

This section outlines the general structure of the book and describes what each chapter explores.

Chapter 1: Compiler Notes and Options

Chapter 1 begins with basic coverage of the man(1) command and provides references to Internet resources for manual pages of other UNIX platforms. An introduction to compiling under FreeBSD is included, with a review of standard compile options for all UNIX platforms. The remainder of the chapter provides helpful hints on how to manage compiler warnings effectively.

Chapter 2: UNIX File System Objects

This chapter reviews the various UNIX file system object types. Some discussion of the unique characteristics of each is provided, primarily for the beginner's benefit. The chapter continues with a review of the role that access permissions play with each file object type. The remainder of the chapter introduces file descriptors and illustrates how UNIX files are opened, duplicated, and closed.

Chapter 3: Error Handling and Reporting

This is a foundation builder, primarily for the benefit of the novice, and demonstrates how system and library calls interact with the global variable errno. The reader is shown the various ways that system error codes are translated into text error messages.

Chapter 4: UNIX Input and Output

This chapter provides an overview of the basics of UNIX input and output. It begins with a review of permission bits and discusses the effect of the umask(2) system call. The chapter continues with coverage of the read(2) and write(2) system calls, with examples. The seeking and truncation file operations are also covered. Other topics include sparse files, the sync(2) and fsync(2) system calls, and scatter read and scatter write calls.

Chapter 5: File Locking

Here we cover all aspects of locking files and file regions under UNIX. This includes the use of lock files and the use of advisory and mandatory locks on regions and entire files.

Chapter 6: Managing Files and Their Properties

Chapter 6 concerns itself with the management of files and their UNIX properties. The system calls covered allow a program to remove, link, rename, and inquire of file properties. Functions that manage symbolic links are also covered. The chapter concludes with coverage of the system calls that permit changing permissions and ownership of file system objects.

Chapter 7: Directory Management

This chapter is focused on the UNIX handling of directories. Functions that change, save, and restore the current directory are covered. Additional coverage includes creating, removing, opening, searching, and closing directories. Finally, changing the root directory is explored.

Chapter 8: Temporary Files and Process Cleanup

In Chapter 8, we cover the various library functions that are available for creating and managing temporary files. The chapter also explores ways that applications can clean up temporary files, even when they terminate unexpectedly.

Chapter 9: UNIX Command-Line Processing

Even X Window graphical programs accept command-line arguments. This chapter explores the UNIX standard method of parsing command-line arguments, with a minimum of written user code. Coverage includes the GNU long option support for the GNU-based function getopt_long(3). Suboption processing is also explored using the getsubopt(3) function.

Chapter 10: Conversion Functions

This chapter looks at the challenges that programmers must face when they convert ASCII strings into numeric values. The simplest methods are contrasted with the more effective functions such as strtol(3). Detailed instruction for dealing with conversion errors is included.

Chapter 11: UNIX Date and Time Facilities

Date and time facilities are the focus of this chapter. Functions that obtain date and time components are described in detail. Conversion to and from various date and time formats is covered.

Chapter 12: User ID, Password, and Group Management

Complete descriptions of the UNIX user and group ID functions are provided in this chapter. The effects of real, saved, and effective IDs are documented. Supplementary user and group IDs are discussed, complete with their management functions.

Chapter 13: Static and Shared Libraries

This chapter explores the differences between static and shared libraries, covering the creation and management of each type of library. It concludes with the functions that permit a program to dynamically load shared libraries upon demand.

Chapter 14: Database Library Routines

Chapter 14 explores the embedded database routines known as the NDBM functions. It covers the functions necessary to create, open, and close these databases. Additionally, the text explains and demonstrates how to create, retrieve, and delete records from the database.

Chapter 15: Signals

This chapter explores the UNIX concept of signals. The reliable signal interface is described, complete with all functions that manage aspects of signal handling.

Chapter 16: Efficient I/O Scheduling

The select(2) and poll(2) system calls are explained with examples in this chapter. These system calls permit applications to perform input and output effectively on several different file descriptors.

Chapter 17: Timers

This chapter focuses its discussion on sleep function calls and interval timers. A possible implementation of the sleep(3) system call is demonstrated in an example program.

Chapter 18: Pipes and Processes

In this chapter we introduce the concept of process management by looking at the functions popen(3) and system(3). These are described and explored with example programs.

Chapter 19: Forked Processes

This chapter examines the more advanced methods of process management by describing the fork(2) and exec(2) sets of system calls. The chapter also includes a discussion of zombie processes and the wait(2) family of system calls.

Chapter 20: Pattern Matching

Library functions that perform simple file pattern matching, as used by the shell, are examined. The chapter includes a description of the fnmatch(3) and glob(3) functions, with test programs that permit you to put them through their paces.

Chapter 21: Regular Expressions

Building upon the previous chapter, the more advanced regular expression matching functions are explored. A review of regular expression syntax is provided before presenting the support functions. A demonstration program puts the various optional features to the test.

Chapter 22: Interprocess Communications

This chapter provides an introduction to interprocess communications. The reader is introduced to IPC keys, IPC IDs, and how various IPC resources are created and accessed.

Chapter 23: Message Queues

The message queue is a member of the interprocess communication set of resources. The system calls that manage its creation, use, and destruction are covered with a demonstration program.

Chapter 24: Semaphores

This chapter continues the interprocess communications theme by exploring what a semaphore is, how it helps, and how it is used. An example program allows you to experiment.

Chapter 25: Shared Memory

The last on the topic of interprocess communication, this chapter focuses on the creation, use, and destruction of shared memory. An example program that makes use of the semaphore and shared memory demonstrates its use.

Chapter 26: Memory-Mapped Files

Memory-mapped files are explored, with a description of the different ways they can be applied. A demonstration program shows how a memory-mapped file can be used to select the language of text messages within an application.

Chapter 27: X Window Programming

The emphasis of this chapter is on event-driven programming. The reader is introduced to some of the basic concepts of X Window programming, focusing on the event loop and X Window event processing, which the example program demonstrates.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.238.76