Computer Science

This is a book on Linux and C. This is not a passive book.

Why Study This Stuff Anyway?

• an improved understanding/appreciation of the internals of your system and sys- tems software will make you a better application programmer

• UNIX and C are an enabling environment/language for wide variety of science and engineering disciplines (e.g., bioinformatics)

• since UNIX and C are ubiquitous in our field, in general, to be a well-rounded com- puter scientist

• communication and concurrency are everything in today’s software

• ability to write reliable and secure code is indispensable (counter-terrorism) gate- way to studies in distributed computing and networking

Use of this Book

UNIX Compliance

Prerequisites

Book Objectives

• Develop a proficiency in Linux and C as a systems programming language/envi- ronment.

• Establish an understanding of the Linux style of programming and problem solving.

• Survey various system-oriented software tools, including debuggers, and compila- tion and configuration managers.

iii

C O

N FI

D E

N TI

A L

D R

A FT

iv

• Establish an understanding of the design and development of systems software, such as command interpreters and compilers, through the study of pattern match- ing and filters, interprocess communication, system libraries, signals, and automatic program generation.

• Explore Linux internals and establish an understanding of Linux system calls.

• Introduce the client/server model of computation.

Graphic View of Outline

Linux

calls

code machine

code assembly

system

lex & yacc

regular expressions,

ksh, sed,

C++

C

Go

Qt awk

Linux and C Fundamentals:

Programming

Part I Part II

Scripting:

Part III

Automatic Program

Generation:

Part IV

Processes Pattern Matching,

Filters, and Shell Programming

Level of Abstraction

Module

We aim for breadth rather than depth here.

The following figure illustrates the dependencies between the chapters of this book.

C O

N FI

D E

N TI

A L

D R

A FT

v

Part I: Fundamentals

Part IV: Automatic Program GenerationPart II: Processes

Part III: Scripting

1

2

34

6 895

7 10

Book Conventions

• For ease of exposition, we use decimals rather than hexidecimals to denote pointer values in C.

Exercises and Programming Projects

Support on the World Wide Web

The author maintains supplemental material for this textbook online at http:// academic.udayton.edu/SaverioPerugini/SPUC/.

CONFIDENTIAL DRAFT

v i

C O

N FI

D E

N TI

A L

D R

A FT

Contents

Preface iii

List of Figures xvi

List of Tables xx

1 Introduction to Linux 1 1.1 Chapter Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.2.1 What is Linux Programming? . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2.2 What is Systems Software? . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2.3 Examples of Systems Software . . . . . . . . . . . . . . . . . . . . . . 2 1.2.4 One Dichotomy of Programming . . . . . . . . . . . . . . . . . . . . . 2 1.2.5 Another Viewpoint (Course Themes) . . . . . . . . . . . . . . . . . . 3 1.2.6 Review of Operating System Nomenclature . . . . . . . . . . . . . . . 3 1.2.7 Why Study This Stuff Anyway? . . . . . . . . . . . . . . . . . . . . . 5 1.2.8 Conceptual Exercises for Section 1.2 . . . . . . . . . . . . . . . . . . . 5 1.2.9 Programming Exercises for Section 1.2 . . . . . . . . . . . . . . . . . . 7

1.3 Introduction to Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.3.1 What is Linux? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.3.2 Hallmarks of Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.3.3 Historical Perspective . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.3.4 The UNIX Philosophy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.3.5 History of UNIX and C . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.3.6 Conceptual UNIX Architecture . . . . . . . . . . . . . . . . . . . . . . 13 1.3.7 Accessing a UNIX Account . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.3.8 General Syntax of UNIX Commands . . . . . . . . . . . . . . . . . . . 13 1.3.9 Getting Help on the UNIX System . . . . . . . . . . . . . . . . . . . . 13 1.3.10 UNIX Manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.3.11 Introduction to the vi Editor . . . . . . . . . . . . . . . . . . . . . . . 14 1.3.12 Conceptual Exercises for Section 1.3 . . . . . . . . . . . . . . . . . . . 17

1.4 Thematic Take-Aways . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.5 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.6 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.7 Bibliographic Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

vii

C O

N FI

D E

N TI

A L

D R

A FT

viii CONTENTS

2 Files and Directories I:Manipulation and Management 21 2.1 Chapter Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.2 Basic UNIX File Nomenclature . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.3 ls and cal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.4 Explanation of ls -l Output . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.5 UNIX Filesystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.6 Absolute vs. Relative Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.7 Two Special Files in Every Directory . . . . . . . . . . . . . . . . . . . . . . . 24 2.8 Navigating through Directories . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.9 File Manipulation and Management . . . . . . . . . . . . . . . . . . . . . . . 24 2.10 Conceptual Exercises for Chapter 2 . . . . . . . . . . . . . . . . . . . . . . . . 24 2.11 Programming Exercises for Chapter 2 . . . . . . . . . . . . . . . . . . . . . . 26 2.12 Thematic Take-Aways . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.13 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.14 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.15 Bibliographic Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

3 The Linux Shell 27 3.1 Chapter Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.3 Shell Commands vs. UNIX Commands . . . . . . . . . . . . . . . . . . . . . . 28 3.4 More on Redirecting Standard Error . . . . . . . . . . . . . . . . . . . . . . . 28 3.5 Kernel metacharacters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.6 stty Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.7 Korn Shell metacharacters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

3.7.1 Metacharacters at Different Levels of Interpretation . . . . . . . . . . 28 3.8 Command Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.9 Shell metacharacter interpretation . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.10 Shell Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.11 Conceptual Exercises for Chapter 3 . . . . . . . . . . . . . . . . . . . . . . . . 28 3.12 Programming Exercises for Chapter 3 . . . . . . . . . . . . . . . . . . . . . . 34 3.13 Programming Project for Chapter 3 . . . . . . . . . . . . . . . . . . . . . . . . 34 3.14 Thematic Take-Aways . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 3.15 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 3.16 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 3.17 Bibliographic Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

4 Introduction to C Programming:System Libraries and I/O 37 4.1 Chapter Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 4.2 Header Files vs. Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.3 Standard C Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.4 Standard I/O vs. File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.5 Standard I/O Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.6 Demo of cat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.7 Redirecting Standard I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

C O

N FI

D E

N TI

A L

D R

A FT

CONTENTS ix

4.8 File Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.9 Demo of wc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.10 I/O in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.11 Effect of a Successful Open on a File . . . . . . . . . . . . . . . . . . . . . . . 39 4.12 Analogs from C++ to C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.13 Review of Standard I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . 39 4.14 Developing cat in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 4.15 Portability (Safety) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 4.16 String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 4.17 ‘s’ Family of printf/scanf Functions . . . . . . . . . . . . . . . . . . . . . 43 4.18 Using a Pointer to Traverse an Array . . . . . . . . . . . . . . . . . . . . . . . 43 4.19 Simple Macro vs. Constant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 4.20 String Copy Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 4.21 Command-line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 4.22 The argv Array for the Call a.out -wlc myfile . . . . . . . . . . . . . . 44 4.23 Compiling a C Program in UNIX . . . . . . . . . . . . . . . . . . . . . . . . . . 47 4.24 Compiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 4.25 C Compilation Steps Using gcc . . . . . . . . . . . . . . . . . . . . . . . . . . 47 4.26 The key options to gcc graphically . . . . . . . . . . . . . . . . . . . . . . . . 47 4.27 C Compilation Steps Graphically . . . . . . . . . . . . . . . . . . . . . . . . . 47 4.28 file Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 4.29 Memory Management: Memory Allocation and Deallocation . . . . . . . . . 47 4.30 Conceptual Exercises for Chapter 4 . . . . . . . . . . . . . . . . . . . . . . . . 47 4.31 Programming Exercises for Chapter 4 . . . . . . . . . . . . . . . . . . . . . . 50 4.32 Programming Project for Chapter 4 . . . . . . . . . . . . . . . . . . . . . . . . 67 4.33 Thematic Take-Aways . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 4.34 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 4.35 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 4.36 Bibliographic Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

5 Compiling C in Linux 69 5.1 Chapter Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 5.2 Compiling C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

5.2.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 5.2.2 Static vs. Dynamic Linking . . . . . . . . . . . . . . . . . . . . . . . . 70 5.2.3 More on Compiling with gcc . . . . . . . . . . . . . . . . . . . . . . . 70 5.2.4 Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 5.2.5 Process Termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 5.2.6 NULL Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 5.2.7 extern Modifier in C . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 5.2.8 Conditional Compilation . . . . . . . . . . . . . . . . . . . . . . . . . 71 5.2.9 Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 5.2.10 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 5.2.11 Conceptual Exercises for Section 5.2 . . . . . . . . . . . . . . . . . . . 72 5.2.12 Programming Exercises for Section 5.2 . . . . . . . . . . . . . . . . . . 72

C O

N FI

D E

N TI

A L

D R

A FT

x CONTENTS

5.3 Building a Library in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 5.3.1 Conceptual Exercises for Section 5.3 . . . . . . . . . . . . . . . . . . . 72 5.3.2 Programming Exercises for Section 5.3 . . . . . . . . . . . . . . . . . . 73

5.4 More topics in C: Storage Classes, Thread-safe Functions, and Macros . . . 74 5.4.1 Declarations and Definitions . . . . . . . . . . . . . . . . . . . . . . . 74 5.4.2 Storage and Linkage Classes . . . . . . . . . . . . . . . . . . . . . . . 74 5.4.3 static Modifier in C . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 5.4.4 Summary of static Reserved Word . . . . . . . . . . . . . . . . . . 74 5.4.5 C Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 5.4.6 Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 5.4.7 Thread Safe Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 5.4.8 makeargv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 5.4.9 Self-study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 5.4.10 Macros: The #define Preprocessor Directive . . . . . . . . . . . . . 77 5.4.11 Macros vs. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 5.4.12 Conceptual Exercises for Section 5.4 . . . . . . . . . . . . . . . . . . . 77 5.4.13 Programming Exercises for Section 5.4 . . . . . . . . . . . . . . . . . . 80

5.5 Compilation and Configuration Management . . . . . . . . . . . . . . . . . . 88 5.5.1 Compilation Management: make . . . . . . . . . . . . . . . . . . . . . 88 5.5.2 Configuration Management (RCS) . . . . . . . . . . . . . . . . . . . . 90 5.5.3 Distributed Configuration Management (GIT) . . . . . . . . . . . . . 91 5.5.4 Conceptual Exercises for Section 5.5 . . . . . . . . . . . . . . . . . . . 91 5.5.5 Programming Exercises for Section 5.5 . . . . . . . . . . . . . . . . . . 97

5.6 Packaging and Compression Utilities . . . . . . . . . . . . . . . . . . . . . . . 100 5.6.1 ar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 5.6.2 tar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 5.6.3 gzip/gunzip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 5.6.4 compress/uncompress . . . . . . . . . . . . . . . . . . . . . . . . . 101 5.6.5 Conceptual Exercises for Section 5.6 . . . . . . . . . . . . . . . . . . . 101

5.7 Thematic Take-Aways . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 5.8 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 5.9 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 5.10 Bibliographic Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

6 Files and Directories II:Inodes, Hard and Symbolic Links 103 6.1 Chapter Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 6.2 Low-Level I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

6.2.1 Review of Linux I/O Data Structures . . . . . . . . . . . . . . . . . . 104 6.2.2 Review of Buffered Output . . . . . . . . . . . . . . . . . . . . . . . . 104 6.2.3 Library vs. System Calls . . . . . . . . . . . . . . . . . . . . . . . . . . 104 6.2.4 I/O Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 6.2.5 select and poll . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

6.3 Disk Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 6.4 File Access (3 Types) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 6.5 File Permissions, Owners, and Groups . . . . . . . . . . . . . . . . . . . . . . 104

C O

N FI

D E

N TI

A L

Order now and get 10% discount on all orders above $50 now!!The professional are ready and willing handle your assignment.

ORDER NOW »»