19
Systems Programming in Unix/Linux

Systems Programming in Unix/Linux978-3-319-92429...systems programming in Unix/Linux has been a popular subject matter in CS/CE education and also for self-study by advanced programmers

  • Upload
    others

  • View
    21

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Systems Programming in Unix/Linux978-3-319-92429...systems programming in Unix/Linux has been a popular subject matter in CS/CE education and also for self-study by advanced programmers

Systems Programming in Unix/Linux

Page 2: Systems Programming in Unix/Linux978-3-319-92429...systems programming in Unix/Linux has been a popular subject matter in CS/CE education and also for self-study by advanced programmers

K. C. Wang

Systems Programmingin Unix/Linux

Page 3: Systems Programming in Unix/Linux978-3-319-92429...systems programming in Unix/Linux has been a popular subject matter in CS/CE education and also for self-study by advanced programmers

K. C. WangSchool of Electrical EngineeringWashington State UniversityPullman, WA, USA

ISBN 978-3-319-92428-1 ISBN 978-3-319-92429-8 (eBook)https://doi.org/10.1007/978-3-319-92429-8

Library of Congress Control Number: 2018945425

# Springer International Publishing AG, part of Springer Nature 2018This work is subject to copyright. All rights are reserved by the Publisher, whether the whole orpart of the material is concerned, specifically the rights of translation, reprinting, reuse ofillustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way,and transmission or information storage and retrieval, electronic adaptation, computer software, orby similar or dissimilar methodology now known or hereafter developed.The use of general descriptive names, registered names, trademarks, service marks, etc. in thispublication does not imply, even in the absence of a specific statement, that such names areexempt from the relevant protective laws and regulations and therefore free for general use.The publisher, the authors, and the editors are safe to assume that the advice and information inthis book are believed to be true and accurate at the date of publication. Neither the publisher northe authors or the editors give a warranty, express or implied, with respect to the materialcontained herein or for any errors or omissions that may have been made. The publisherremains neutral with regard to jurisdictional claims in published maps and institutional affiliations.

This Springer imprint is published by the registered company Springer Nature Switzerland AGThe registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland

Page 4: Systems Programming in Unix/Linux978-3-319-92429...systems programming in Unix/Linux has been a popular subject matter in CS/CE education and also for self-study by advanced programmers

Dedicated toCindy

Page 5: Systems Programming in Unix/Linux978-3-319-92429...systems programming in Unix/Linux has been a popular subject matter in CS/CE education and also for self-study by advanced programmers

Preface

Systems programming is an indispensable part of Computer Science andComputer Engineering education. System programming courses in ComputerScience/Engineering curriculums play two important roles. First, it providesstudents with a wide range of knowledge about computer system software andadvanced programming skills, allowing them to interface with operatingsystem kernel, perform file operations and network programming, and makeefficient use of system resources to develop application programs. Second, itprepares students with needed background to pursue advanced studies inComputer Science/Engineering, such as operating systems, embeddedsystems, database systems, data mining, artificial intelligence, computernetworks, and distributed and parallel computing. Due to its importance,systems programming in Unix/Linux has been a popular subject matter inCS/CE education and also for self-study by advanced programmers. As aresult, there are a tremendous number of books and online articles in this area.Despite these, I still find it difficult to choose a suitable book for the SystemsProgramming course I teach at WSU. For many years, I had to use my ownclass notes and programming assignments in the course. After careful think-ing, I have decided to put together the materials into a book form.

The purpose of this book is to provide a suitable platform for teaching andlearning the theory and practice of systems programming. Unlike most otherbooks, this book covers systems programming topics in greater depth andit stresses programming practice. It uses a series of programming projects tolet students apply their acquired knowledge and programming skills todevelop practical and useful programs. The book is intended as a textbookin technical-oriented systems programming courses. It contains detailedexample programs with complete source code, making it suitable for self-study also.

Undertaking this book project has proved to be another very demandingand time-consuming endeavor. While preparing the manuscripts for publica-tion, I have been blessed with the encouragement and help from many people.I would like to take this opportunity to thank all of them. I want to especiallythank Yan Zhang for his help in preparing figures for the book and proof-reading the manuscript.

vii

Page 6: Systems Programming in Unix/Linux978-3-319-92429...systems programming in Unix/Linux has been a popular subject matter in CS/CE education and also for self-study by advanced programmers

Special thanks go to Cindy for her continuing support and inspirations,which have made this book possible. Above all, I would like to thank myfamily for bearing with me with endless excuses of being busy all the time.

Sample solutions of programming projects in the book are available fordownload at http://wang.eecs.wsu.edu/~kcw. For source code, please contactthe author by email.

Pullman, WA, USAApril, 2018

K. C. Wang

viii Preface

Page 7: Systems Programming in Unix/Linux978-3-319-92429...systems programming in Unix/Linux has been a popular subject matter in CS/CE education and also for self-study by advanced programmers

Contents

1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.1 About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Roles of Systems Programming . . . . . . . . . . . . . . . . . . . 11.3 Objectives of This Book . . . . . . . . . . . . . . . . . . . . . . . . 2

1.3.1 Strengthen Students ProgrammingBackground . . . . . . . . . . . . . . . . . . . . . . . . . 2

1.3.2 Applications of Dynamic Data Structures . . . . 21.3.3 Process Concept and Process

Management . . . . . . . . . . . . . . . . . . . . . . . . . 31.3.4 Concurrent Programming . . . . . . . . . . . . . . . . 31.3.5 Timer and Time Functions . . . . . . . . . . . . . . . 31.3.6 Signals, Signal Processing and IPC . . . . . . . . . 31.3.7 File system . . . . . . . . . . . . . . . . . . . . . . . . . . 41.3.8 TCP/IP and Network Programming . . . . . . . . . 4

1.4 Intended Audience . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.5 Unique Features of This Book . . . . . . . . . . . . . . . . . . . 41.6 Use This Book As Textbook in Systems

Programming Courses . . . . . . . . . . . . . . . . . . . . . . . . . 61.7 Other Reference Books . . . . . . . . . . . . . . . . . . . . . . . . . 71.8 Introduction to Unix . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

1.8.1 AT&T Unix . . . . . . . . . . . . . . . . . . . . . . . . . 71.8.2 Berkeley Unix . . . . . . . . . . . . . . . . . . . . . . . . 81.8.3 HP Unix . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81.8.4 IBM Unix . . . . . . . . . . . . . . . . . . . . . . . . . . . 81.8.5 Sun Unix . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

1.9 Introduction to Linux . . . . . . . . . . . . . . . . . . . . . . . . . . 91.10 Linux Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

1.10.1 Debian Linux . . . . . . . . . . . . . . . . . . . . . . . . 91.10.2 Ubuntu Linux . . . . . . . . . . . . . . . . . . . . . . . . 91.10.3 Linux Mint . . . . . . . . . . . . . . . . . . . . . . . . . . 91.10.4 RPM-Based Linux . . . . . . . . . . . . . . . . . . . . . 101.10.5 Slackware Linux . . . . . . . . . . . . . . . . . . . . . . 10

1.11 Linux Hardware Platforms . . . . . . . . . . . . . . . . . . . . . . 10

ix

Page 8: Systems Programming in Unix/Linux978-3-319-92429...systems programming in Unix/Linux has been a popular subject matter in CS/CE education and also for self-study by advanced programmers

1.12 Linux on Virtual Machines . . . . . . . . . . . . . . . . . . . . . . 101.12.1 VirtualBox . . . . . . . . . . . . . . . . . . . . . . . . . . 101.12.2 VMware . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121.12.3 Dual Boot Slackware and Ubuntu Linux . . . . . 14

1.13 Use Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151.13.1 Linux kernel image . . . . . . . . . . . . . . . . . . . . 151.13.2 Linux Booters . . . . . . . . . . . . . . . . . . . . . . . . 151.13.3 Linux Booting . . . . . . . . . . . . . . . . . . . . . . . . 151.13.4 Linux Run-levels . . . . . . . . . . . . . . . . . . . . . . 161.13.5 Login Process . . . . . . . . . . . . . . . . . . . . . . . . 161.13.6 Command Executions . . . . . . . . . . . . . . . . . . 16

1.14 Use Ubuntu Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161.14.1 Ubuntu Versions . . . . . . . . . . . . . . . . . . . . . . 161.14.2 Special Features of Ubuntu Linux . . . . . . . . . . 17

1.15 Unix/Linux File System Organization . . . . . . . . . . . . . . 181.15.1 File Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 181.15.2 File Pathnames . . . . . . . . . . . . . . . . . . . . . . . 191.15.3 Unix/Linux Commands . . . . . . . . . . . . . . . . . 191.15.4 Linux Man Pages . . . . . . . . . . . . . . . . . . . . . 19

1.16 Ubuntu Linux System Administration . . . . . . . . . . . . . . 201.16.1 User Accounts . . . . . . . . . . . . . . . . . . . . . . . . 201.16.2 Add New User . . . . . . . . . . . . . . . . . . . . . . . 201.16.3 The sudo Command . . . . . . . . . . . . . . . . . . . . 21

1.17 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

2 Programming Background . . . . . . . . . . . . . . . . . . . . . . . . . . . 232.1 Text Editors in Linux . . . . . . . . . . . . . . . . . . . . . . . . . . 23

2.1.1 Vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232.1.2 Gedit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242.1.3 Emacs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

2.2 Use Text Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252.2.1 Use Emacs . . . . . . . . . . . . . . . . . . . . . . . . . . 252.2.2 Emacs Menus . . . . . . . . . . . . . . . . . . . . . . . . 262.2.3 IDE of Emacs . . . . . . . . . . . . . . . . . . . . . . . . 26

2.3 Program Development . . . . . . . . . . . . . . . . . . . . . . . . . 272.3.1 Program Development Steps . . . . . . . . . . . . . 272.3.2 Variables in C . . . . . . . . . . . . . . . . . . . . . . . . 272.3.3 Compile-Link in GCC . . . . . . . . . . . . . . . . . . 282.3.4 Static vs. Dynamic Linking . . . . . . . . . . . . . . 292.3.5 Executable File Format . . . . . . . . . . . . . . . . . 302.3.6 Contents of a.out File . . . . . . . . . . . . . . . . . . . 302.3.7 Program Execution . . . . . . . . . . . . . . . . . . . . 312.3.8 Program Termination . . . . . . . . . . . . . . . . . . . 32

2.4 Function Call in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332.4.1 Run-Time Stack Usage in 32-Bit GCC . . . . . . 332.4.2 Stack Frames . . . . . . . . . . . . . . . . . . . . . . . . . 352.4.3 Return From Function Call . . . . . . . . . . . . . . . 35

x Contents

Page 9: Systems Programming in Unix/Linux978-3-319-92429...systems programming in Unix/Linux has been a popular subject matter in CS/CE education and also for self-study by advanced programmers

2.4.4 Long Jump . . . . . . . . . . . . . . . . . . . . . . . . . . 362.4.5 Run-Time Stack Usage in 64-Bit GCC . . . . . . 37

2.5 Link C Program with Assembly Code . . . . . . . . . . . . . . 402.5.1 Programming in Assembly . . . . . . . . . . . . . . . 412.5.2 Implement Functions in Assembly . . . . . . . . . 432.5.3 Call C functions from Assembly . . . . . . . . . . . 44

2.6 Link Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452.6.1 Static Link Library . . . . . . . . . . . . . . . . . . . . 452.6.2 Dynamic Link Library . . . . . . . . . . . . . . . . . . 46

2.7 Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462.7.1 Makefile Format . . . . . . . . . . . . . . . . . . . . . . 462.7.2 The make Program . . . . . . . . . . . . . . . . . . . . 462.7.3 Makefile Examples . . . . . . . . . . . . . . . . . . . . 47

2.8 The GDB Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . 532.8.1 Use GDB in Emacs IDE . . . . . . . . . . . . . . . . 532.8.2 Advices on Using Debugging Tools . . . . . . . . 582.8.3 Common Errors in C programs . . . . . . . . . . . . 58

2.9 Structures in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622.9.1 Structure and Pointers . . . . . . . . . . . . . . . . . . 642.9.2 Typecast in C . . . . . . . . . . . . . . . . . . . . . . . . 64

2.10 Link List Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . 662.10.1 Link Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . 662.10.2 Link List Operations . . . . . . . . . . . . . . . . . . . 662.10.3 Build Link List . . . . . . . . . . . . . . . . . . . . . . . 672.10.4 Link List Traversal . . . . . . . . . . . . . . . . . . . . 702.10.5 Search Link List . . . . . . . . . . . . . . . . . . . . . . 722.10.6 Insert Operation . . . . . . . . . . . . . . . . . . . . . . . 722.10.7 Priority Queue . . . . . . . . . . . . . . . . . . . . . . . . 732.10.8 Delete Operation . . . . . . . . . . . . . . . . . . . . . . 742.10.9 Circular Link List . . . . . . . . . . . . . . . . . . . . . 752.10.10 Open-Ended C Structures . . . . . . . . . . . . . . . . 752.10.11 Doubly Link Lists . . . . . . . . . . . . . . . . . . . . . 762.10.12 Doubly Link Lists Example Programs . . . . . . . 76

2.11 Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 852.12 Binary Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

2.12.1 Binary Search Tree . . . . . . . . . . . . . . . . . . . . 852.12.2 Build a Binary Search Tree (BST) . . . . . . . . . 862.12.3 Binary Tree Traversal Algorithms . . . . . . . . . . 872.12.4 Depth-First Traversal Algorithms . . . . . . . . . . 872.12.5 Breadth-First Traversal Algorithms . . . . . . . . . 88

2.13 Programming Project: Unix/Linux File SystemTree Simulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 902.13.1 Unix/Linux File System Tree . . . . . . . . . . . . . 902.13.2 Implement General Tree by Binary Tree . . . . . 902.13.3 Project Specification and Requirements . . . . . . 902.13.4 Commands Specification . . . . . . . . . . . . . . . . 912.13.5 Program Organization . . . . . . . . . . . . . . . . . . 91

Contents xi

Page 10: Systems Programming in Unix/Linux978-3-319-92429...systems programming in Unix/Linux has been a popular subject matter in CS/CE education and also for self-study by advanced programmers

2.13.6 Command Algorithms . . . . . . . . . . . . . . . . . . 942.13.7 Sample Solution . . . . . . . . . . . . . . . . . . . . . . 97

2.14 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

3 Process Management in Unix/Linux . . . . . . . . . . . . . . . . . . . . 1013.1 Multitasking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1013.2 The Process Concept . . . . . . . . . . . . . . . . . . . . . . . . . . 1023.3 A Multitasking System . . . . . . . . . . . . . . . . . . . . . . . . . 103

3.3.1 type.h file . . . . . . . . . . . . . . . . . . . . . . . . . . . 1033.3.2 The ts.s file . . . . . . . . . . . . . . . . . . . . . . . . . . 1033.3.3 The queue.c file . . . . . . . . . . . . . . . . . . . . . . . 1043.3.4 The t.c file . . . . . . . . . . . . . . . . . . . . . . . . . . 1053.3.5 Explanations of the Multitasking

System Code . . . . . . . . . . . . . . . . . . . . . . . . . 1083.4 Process Synchronization . . . . . . . . . . . . . . . . . . . . . . . . 111

3.4.1 Sleep Operation . . . . . . . . . . . . . . . . . . . . . . . 1113.4.2 Wakeup Operation . . . . . . . . . . . . . . . . . . . . . 112

3.5 Process Termination . . . . . . . . . . . . . . . . . . . . . . . . . . . 1123.5.1 Algorithm of kexit() . . . . . . . . . . . . . . . . . . . . 1123.5.2 Process Family Tree . . . . . . . . . . . . . . . . . . . 1133.5.3 Wait for Child Process Termination . . . . . . . . 114

3.6 Process Management in the MT MultitaskingSystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

3.7 Processes in Unix/Linux . . . . . . . . . . . . . . . . . . . . . . . . 1153.7.1 Process Origin . . . . . . . . . . . . . . . . . . . . . . . . 1153.7.2 INIT and Daemon Processes . . . . . . . . . . . . . 1163.7.3 Login Processes . . . . . . . . . . . . . . . . . . . . . . 1173.7.4 Sh Process . . . . . . . . . . . . . . . . . . . . . . . . . . 1173.7.5 Process Execution Modes . . . . . . . . . . . . . . . . 117

3.8 System Calls for Process Management . . . . . . . . . . . . . . 1183.8.1 fork() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1193.8.2 Process Execution Order . . . . . . . . . . . . . . . . 1203.8.3 Process Termination . . . . . . . . . . . . . . . . . . . 1213.8.4 Wait for Child Process Termination . . . . . . . . 1223.8.5 Subreaper Process in Linux . . . . . . . . . . . . . . 1233.8.6 exec(): Change Process Execution Image . . . . 1253.8.7 Environment Variables . . . . . . . . . . . . . . . . . . 126

3.9 I/O Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1293.9.1 FILE Streams and File Descriptors . . . . . . . . . 1293.9.2 FILE Stream I/O and System Call . . . . . . . . . . 1303.9.3 Redirect stdin . . . . . . . . . . . . . . . . . . . . . . . . 1303.9.4 Redirect stdout . . . . . . . . . . . . . . . . . . . . . . . 131

3.10 Pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1313.10.1 Pipe Programming in Unix/Linux . . . . . . . . . . 1313.10.2 Pipe Command Processing . . . . . . . . . . . . . . . 1343.10.3 Connect PIPE writer to PIPE reader . . . . . . . . 1343.10.4 Named pipes . . . . . . . . . . . . . . . . . . . . . . . . . 135

xii Contents

Page 11: Systems Programming in Unix/Linux978-3-319-92429...systems programming in Unix/Linux has been a popular subject matter in CS/CE education and also for self-study by advanced programmers

3.11 Programming Project: sh Simulator . . . . . . . . . . . . . . . . 1373.11.1 Single Command with I/O Redirection . . . . . . 1373.11.2 Commands with Pipes . . . . . . . . . . . . . . . . . . 1383.11.3 ELF executable vs. sh script files . . . . . . . . . . 1383.11.4 Sample Solution . . . . . . . . . . . . . . . . . . . . . . 138

3.12 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

4 Concurrent Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . 1414.1 Introduction to Parallel Computing . . . . . . . . . . . . . . . . 141

4.1.1 Sequential Algorithms vs. ParallelAlgorithms . . . . . . . . . . . . . . . . . . . . . . . . . . 142

4.1.2 Parallelism vs. Concurrency . . . . . . . . . . . . . . 1424.2 Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

4.2.1 Principle of Threads . . . . . . . . . . . . . . . . . . . . 1434.2.2 Advantages of Threads . . . . . . . . . . . . . . . . . 1434.2.3 Disadvantages of Threads . . . . . . . . . . . . . . . 144

4.3 Threads Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 1444.4 Threads Management Functions . . . . . . . . . . . . . . . . . . 144

4.4.1 Create Thread . . . . . . . . . . . . . . . . . . . . . . . . 1454.4.2 Thread ID . . . . . . . . . . . . . . . . . . . . . . . . . . . 1464.4.3 Thread Termination . . . . . . . . . . . . . . . . . . . . 1464.4.4 Thread Join . . . . . . . . . . . . . . . . . . . . . . . . . . 146

4.5 Threads Example Programs . . . . . . . . . . . . . . . . . . . . . 1474.5.1 Sum of Matrix by Threads . . . . . . . . . . . . . . . 1474.5.2 Quicksort by Threads . . . . . . . . . . . . . . . . . . . 148

4.6 Threads Synchronization . . . . . . . . . . . . . . . . . . . . . . . 1514.6.1 Mutex Locks . . . . . . . . . . . . . . . . . . . . . . . . . 1514.6.2 Deadlock Prevention . . . . . . . . . . . . . . . . . . . 1544.6.3 Condition Variables . . . . . . . . . . . . . . . . . . . . 1554.6.4 Producer-Consumer Problem . . . . . . . . . . . . . 1564.6.5 Semaphores . . . . . . . . . . . . . . . . . . . . . . . . . 1594.6.6 Barriers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1604.6.7 Solve System of Linear Equations by

Concurrent Threads . . . . . . . . . . . . . . . . . . . . 1614.6.8 Threads in Linux . . . . . . . . . . . . . . . . . . . . . . 165

4.7 Programming Project: User-Level Threads . . . . . . . . . . . 1654.7.1 Project Base Code: A Multitasking System . . . 1654.7.2 User-Level Threads . . . . . . . . . . . . . . . . . . . . 1694.7.3 Implementation of Thread Join Operation . . . . 1724.7.4 Implementation of Mutex Operations . . . . . . . 1764.7.5 Test Project with Mutex by Concurrent

Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . 1774.7.6 Implementation of Semaphores . . . . . . . . . . . . 1814.7.7 Producer-Consumer Problem using

Semaphores . . . . . . . . . . . . . . . . . . . . . . . . . 1814.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

Contents xiii

Page 12: Systems Programming in Unix/Linux978-3-319-92429...systems programming in Unix/Linux has been a popular subject matter in CS/CE education and also for self-study by advanced programmers

5 Timers and Time Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1875.1 Hardware Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1875.2 PC Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1875.3 CPU Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1885.4 Interrupt Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . 1895.5 Time Service Functions . . . . . . . . . . . . . . . . . . . . . . . . 189

5.5.1 Gettimeofday-Settimeofday . . . . . . . . . . . . . . 1895.5.2 The Time System Call . . . . . . . . . . . . . . . . . . 1915.5.3 The Times System Call . . . . . . . . . . . . . . . . . 1915.5.4 Time and Date Commands . . . . . . . . . . . . . . . 192

5.6 Interval Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1925.7 REAL Mode Interval Timer . . . . . . . . . . . . . . . . . . . . . 1955.8 Programming Project . . . . . . . . . . . . . . . . . . . . . . . . . . 195

5.8.1 System Base Code . . . . . . . . . . . . . . . . . . . . . 1955.8.2 Timer Interrupts . . . . . . . . . . . . . . . . . . . . . . 1995.8.3 Timer Queue . . . . . . . . . . . . . . . . . . . . . . . . . 2005.8.4 Critical Regions . . . . . . . . . . . . . . . . . . . . . . . 2025.8.5 Advanced Topics . . . . . . . . . . . . . . . . . . . . . . 203

5.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204

6 Signals and Signal Processing . . . . . . . . . . . . . . . . . . . . . . . . . 2056.1 Signals and Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . 2056.2 Examples of Unix/Linux Signals . . . . . . . . . . . . . . . . . . 2076.3 Signal Processing in Unix/Linux . . . . . . . . . . . . . . . . . . 208

6.3.1 Signal Types . . . . . . . . . . . . . . . . . . . . . . . . . 2086.3.2 Origin of Signals . . . . . . . . . . . . . . . . . . . . . . 2096.3.3 Signals in Process PROC Structure: . . . . . . . . 2096.3.4 Signal Handlers . . . . . . . . . . . . . . . . . . . . . . . 2096.3.5 Install Signal Catchers . . . . . . . . . . . . . . . . . . 210

6.4 Signal Processing Steps . . . . . . . . . . . . . . . . . . . . . . . . 2126.5 Signals and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . 2136.6 Signals as IPC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2136.7 IPC in Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215

6.7.1 Pipes and FIFOs . . . . . . . . . . . . . . . . . . . . . . 2156.7.2 Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2156.7.3 System V IPC . . . . . . . . . . . . . . . . . . . . . . . . 2156.7.4 POSIX Message Queues . . . . . . . . . . . . . . . . 2166.7.5 Threads Synchronization Mechanisms . . . . . . . 2166.7.6 Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216

6.8 Programming Project: Implement an IPCfor Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216

6.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219

7 File Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2217.1 File Operation Levels . . . . . . . . . . . . . . . . . . . . . . . . . . 2217.2 File I/O Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

xiv Contents

Page 13: Systems Programming in Unix/Linux978-3-319-92429...systems programming in Unix/Linux has been a popular subject matter in CS/CE education and also for self-study by advanced programmers

7.3 Low Level File Operations . . . . . . . . . . . . . . . . . . . . . . 2267.3.1 Partitions . . . . . . . . . . . . . . . . . . . . . . . . . . . 2267.3.2 Format Partitions . . . . . . . . . . . . . . . . . . . . . . 2297.3.3 Mount Partitions . . . . . . . . . . . . . . . . . . . . . . 230

7.4 Introduction to EXT2 File System . . . . . . . . . . . . . . . . . 2317.4.1 EXT2 File System Data Structures . . . . . . . . . 2317.4.2 Superblock . . . . . . . . . . . . . . . . . . . . . . . . . . 2317.4.3 Group Descriptor . . . . . . . . . . . . . . . . . . . . . . 2327.4.4 Bitmaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2337.4.5 Inodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2337.4.6 Directory Entries . . . . . . . . . . . . . . . . . . . . . . 234

7.5 Programming Examples . . . . . . . . . . . . . . . . . . . . . . . . 2357.5.1 Display Superblock . . . . . . . . . . . . . . . . . . . . 2357.5.2 Display Bitmaps . . . . . . . . . . . . . . . . . . . . . . 2377.5.3 Display root Inode . . . . . . . . . . . . . . . . . . . . . 2397.5.4 Display Directory Entries . . . . . . . . . . . . . . . . 241

7.6 Programming Project: Convert File Pathname to Inode . . 2437.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244

8 System Calls for File Operations . . . . . . . . . . . . . . . . . . . . . . 2458.1 Systems Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2458.2 System Call Man Pages . . . . . . . . . . . . . . . . . . . . . . . . 2458.3 System Calls for File Operations . . . . . . . . . . . . . . . . . . 2468.4 Commonly used system Calls . . . . . . . . . . . . . . . . . . . . 2488.5 Link Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250

8.5.1 Hard Link Files . . . . . . . . . . . . . . . . . . . . . . . 2508.5.2 Symbolic Link Files . . . . . . . . . . . . . . . . . . . 250

8.6 The stat Systen Call . . . . . . . . . . . . . . . . . . . . . . . . . . . 2518.6.1 Stat File Status . . . . . . . . . . . . . . . . . . . . . . . 2518.6.2 The stat Structure . . . . . . . . . . . . . . . . . . . . . 2528.6.3 Stat and File Inode . . . . . . . . . . . . . . . . . . . . 2538.6.4 File Type and Permissions . . . . . . . . . . . . . . . 2548.6.5 Opendir-Readdir Functions . . . . . . . . . . . . . . 2558.6.6 Readlink Function . . . . . . . . . . . . . . . . . . . . . 2568.6.7 The ls Program . . . . . . . . . . . . . . . . . . . . . . . 256

8.7 open-close-lseek System Calls . . . . . . . . . . . . . . . . . . . 2588.7.1 Open File and File Descriptor . . . . . . . . . . . . . 2588.7.2 Close File Descriptor . . . . . . . . . . . . . . . . . . . 2598.7.3 lseek File Descriptor . . . . . . . . . . . . . . . . . . . 259

8.8 Read() System Call . . . . . . . . . . . . . . . . . . . . . . . . . . . 2598.9 Write() System Call . . . . . . . . . . . . . . . . . . . . . . . . . . . 2608.10 File Operation Example Programs . . . . . . . . . . . . . . . . . 260

8.10.1 Display File Contents . . . . . . . . . . . . . . . . . . 2608.10.2 Copy Files . . . . . . . . . . . . . . . . . . . . . . . . . . 2618.10.3 Selective File Copy . . . . . . . . . . . . . . . . . . . . 262

8.11 Programming Project: Recursive Copy Filesusing System Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264

Contents xv

Page 14: Systems Programming in Unix/Linux978-3-319-92429...systems programming in Unix/Linux has been a popular subject matter in CS/CE education and also for self-study by advanced programmers

8.11.1 Hints and Helps . . . . . . . . . . . . . . . . . . . . . . . 2648.11.2 Sample Solution . . . . . . . . . . . . . . . . . . . . . . 264

8.12 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265

9 Library I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2679.1 Library I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 2679.2 Library I/O Functions vs. System Calls . . . . . . . . . . . . . 2679.3 Algorithms of Library I/O Functions . . . . . . . . . . . . . . . 271

9.3.1 Algorithm of fread . . . . . . . . . . . . . . . . . . . . . 2719.3.2 Algorithm of fwrite . . . . . . . . . . . . . . . . . . . . 2719.3.3 Algorithm of fclose . . . . . . . . . . . . . . . . . . . . 271

9.4 Use Library I/O Function or System Call . . . . . . . . . . . . 2729.5 Library I/O Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272

9.5.1 Char Mode I/O . . . . . . . . . . . . . . . . . . . . . . . 2729.5.2 Line mode I/O . . . . . . . . . . . . . . . . . . . . . . . . 2739.5.3 Formatted I/O . . . . . . . . . . . . . . . . . . . . . . . . 2749.5.4 In-memory Conversion Functions . . . . . . . . . . 2749.5.5 Other Library I/O Functions . . . . . . . . . . . . . . 2749.5.6 Restriction on Mixed fread-fwrite . . . . . . . . . . 275

9.6 File Stream Buffering . . . . . . . . . . . . . . . . . . . . . . . . . . 2759.7 Functions with Varying Parameters . . . . . . . . . . . . . . . . 2769.8 Programming Project: Printf-like Function . . . . . . . . . . . 277

9.8.1 Project Specification . . . . . . . . . . . . . . . . . . . 2789.8.2 Base Code of Project . . . . . . . . . . . . . . . . . . . 2789.8.3 Algorithm of myprintf() . . . . . . . . . . . . . . . . . 2799.8.4 Project Refinements . . . . . . . . . . . . . . . . . . . . 2799.8.5 Project Demonstration and Sample

Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . 2799.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

10 Sh Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28310.1 sh Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28310.2 sh Scripts vs. C Programs . . . . . . . . . . . . . . . . . . . . . . . 28410.3 Command-line parameters . . . . . . . . . . . . . . . . . . . . . . 28410.4 Sh Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28510.5 Quotes in sh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28510.6 sh Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28610.7 sh Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286

10.7.1 Built-in Commands . . . . . . . . . . . . . . . . . . . . 28610.7.2 Linux Commands . . . . . . . . . . . . . . . . . . . . . 287

10.8 Command Substitution . . . . . . . . . . . . . . . . . . . . . . . . . 28810.9 Sh Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . 288

10.9.1 if-else-fi statement . . . . . . . . . . . . . . . . . . . . . 28810.9.2 for Statement . . . . . . . . . . . . . . . . . . . . . . . . . 29010.9.3 while Statement . . . . . . . . . . . . . . . . . . . . . . . 29110.9.4 until-do Statement . . . . . . . . . . . . . . . . . . . . . 291

xvi Contents

Page 15: Systems Programming in Unix/Linux978-3-319-92429...systems programming in Unix/Linux has been a popular subject matter in CS/CE education and also for self-study by advanced programmers

10.9.5 case Statement . . . . . . . . . . . . . . . . . . . . . . . . 29210.9.6 continue and break Statements . . . . . . . . . . . . 292

10.10 I/O Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29210.11 Here Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29210.12 sh Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29310.13 Wild Cards in sh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29410.14 Command Grouping . . . . . . . . . . . . . . . . . . . . . . . . . . . 29510.15 eval Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29510.16 Debugging sh Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . 29610.17 Applications of sh scripts . . . . . . . . . . . . . . . . . . . . . . . 29610.18 Programming Project: Recursive File Copy

by sh Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29810.19 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300

11 EXT2 File System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30111.1 EXT2 File System . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30111.2 EXT2 File System Data Structures . . . . . . . . . . . . . . . . 302

11.2.1 Create Virtual Disk by mkfs . . . . . . . . . . . . . . 30211.2.2 Virtual Disk Layout . . . . . . . . . . . . . . . . . . . . 30211.2.3 Superblock . . . . . . . . . . . . . . . . . . . . . . . . . . 30211.2.4 Group Descriptors . . . . . . . . . . . . . . . . . . . . . 30311.2.5 Block and Inode Bitmaps . . . . . . . . . . . . . . . . 30411.2.6 Inodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30411.2.7 Data Blocks . . . . . . . . . . . . . . . . . . . . . . . . . 30511.2.8 Directory Entries . . . . . . . . . . . . . . . . . . . . . . 305

11.3 Mailman’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . 30611.3.1 Test-Set-Clear Bits in C . . . . . . . . . . . . . . . . . 30611.3.2 Convert INODE Number to INODE

on Disk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30711.4 Programming Examples . . . . . . . . . . . . . . . . . . . . . . . . 307

11.4.1 Display Superblock . . . . . . . . . . . . . . . . . . . . 30811.4.2 Display Bitmaps . . . . . . . . . . . . . . . . . . . . . . 31011.4.3 Display Root Inode . . . . . . . . . . . . . . . . . . . . 31211.4.4 Display Directory Entries . . . . . . . . . . . . . . . . 313

11.5 Traverse EXT2 File System Tree . . . . . . . . . . . . . . . . . 31511.5.1 Traversal Algorithm . . . . . . . . . . . . . . . . . . . . 31511.5.2 Convert Pathname to INODE . . . . . . . . . . . . . 31711.5.3 Display INODE Disk Blocks . . . . . . . . . . . . . 317

11.6 Implementation of EXT2 File System . . . . . . . . . . . . . . 31711.6.1 File System Organization . . . . . . . . . . . . . . . . 31711.6.2 Files System Levels . . . . . . . . . . . . . . . . . . . . 318

11.7 Base File System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31911.7.1 type.h file . . . . . . . . . . . . . . . . . . . . . . . . . . . 31911.7.2 Utility Functions . . . . . . . . . . . . . . . . . . . . . . 32311.7.3 Mount-Root . . . . . . . . . . . . . . . . . . . . . . . . . 32711.7.4 Implementation of Base File System . . . . . . . . 331

Contents xvii

Page 16: Systems Programming in Unix/Linux978-3-319-92429...systems programming in Unix/Linux has been a popular subject matter in CS/CE education and also for self-study by advanced programmers

11.8 File System Level-1 Functions . . . . . . . . . . . . . . . . . . . 33211.8.1 Algorithm of mkdir . . . . . . . . . . . . . . . . . . . . 33211.8.2 Algorithm of creat . . . . . . . . . . . . . . . . . . . . . 33611.8.3 Implementation of mkdir-creat . . . . . . . . . . . . 33611.8.4 Algorithm of rmdir . . . . . . . . . . . . . . . . . . . . 33711.8.5 Implementation of rmdir . . . . . . . . . . . . . . . . 34011.8.6 Algorithm of link . . . . . . . . . . . . . . . . . . . . . 34011.8.7 Algorithm of unlink . . . . . . . . . . . . . . . . . . . . 34211.8.8 Algorithm of symlink . . . . . . . . . . . . . . . . . . 34311.8.9 Algorithm of readlink . . . . . . . . . . . . . . . . . . 34311.8.10 Other Level-1 Functions . . . . . . . . . . . . . . . . 34311.8.11 Programming Project #1: Implementation

of File System Level-1 . . . . . . . . . . . . . . . . . . 34411.9 File System Level-2 Functions . . . . . . . . . . . . . . . . . . . 344

11.9.1 Algorithm of open . . . . . . . . . . . . . . . . . . . . . 34511.9.2 lseek . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34611.9.3 Algorithm of close . . . . . . . . . . . . . . . . . . . . . 34611.9.4 Read Regular Files . . . . . . . . . . . . . . . . . . . . 34611.9.5 Write Regular Files . . . . . . . . . . . . . . . . . . . . 34811.9.6 Opendir-Readdir . . . . . . . . . . . . . . . . . . . . . . 35011.9.7 Programming Project #2: Implementation

of File System Level-2 . . . . . . . . . . . . . . . . . . 35011.10 File System Level-3 . . . . . . . . . . . . . . . . . . . . . . . . . . . 351

11.10.1 Algorithm of mount . . . . . . . . . . . . . . . . . . . . 35111.10.2 Algorithm of umount . . . . . . . . . . . . . . . . . . . 35211.10.3 Cross Mounting Points . . . . . . . . . . . . . . . . . . 35211.10.4 File Protection . . . . . . . . . . . . . . . . . . . . . . . . 35311.10.5 Real and Effective uid . . . . . . . . . . . . . . . . . . 35311.10.6 File Locking . . . . . . . . . . . . . . . . . . . . . . . . . 35411.10.7 Programming Project #3: Implementation

of Complete File System . . . . . . . . . . . . . . . . 35411.11 Extensions of File System Project . . . . . . . . . . . . . . . . . 35411.12 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356

12 Block Device I/O and Buffer Management . . . . . . . . . . . . . . . 35712.1 Block Device I/O Buffers . . . . . . . . . . . . . . . . . . . . . . . 35712.2 Unix I/O Buffer Management Algorithm . . . . . . . . . . . . 359

12.2.1 Shortcomings of Unix Algorithm . . . . . . . . . . 36212.3 New I/O Buffer Management Algorithm . . . . . . . . . . . . 362

12.3.1 Buffer Management Algorithm usingSemaphores . . . . . . . . . . . . . . . . . . . . . . . . . 363

12.4 PV Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36312.5 Programming Project: Comparison of I/O Buffer

Management Algorithms . . . . . . . . . . . . . . . . . . . . . . . 36512.5.1 System Organization . . . . . . . . . . . . . . . . . . . 36512.5.2 Multitasking System . . . . . . . . . . . . . . . . . . . 36612.5.3 Buffer Manager . . . . . . . . . . . . . . . . . . . . . . . 367

xviii Contents

Page 17: Systems Programming in Unix/Linux978-3-319-92429...systems programming in Unix/Linux has been a popular subject matter in CS/CE education and also for self-study by advanced programmers

12.5.4 Disk Driver . . . . . . . . . . . . . . . . . . . . . . . . . . 36712.5.5 Disk Controller . . . . . . . . . . . . . . . . . . . . . . . 36812.5.6 Disk Interrupts . . . . . . . . . . . . . . . . . . . . . . . 36812.5.7 Virtual Disks . . . . . . . . . . . . . . . . . . . . . . . . . 36812.5.8 Project Requirements . . . . . . . . . . . . . . . . . . . 36812.5.9 Sample Base Code . . . . . . . . . . . . . . . . . . . . . 36912.5.10 Sample Solutions . . . . . . . . . . . . . . . . . . . . . . 374

12.6 Refinements of Simulation System . . . . . . . . . . . . . . . . 37512.7 Refinements of PV Algorithm . . . . . . . . . . . . . . . . . . . . 37512.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376

13 TCP/IP and Network Programming . . . . . . . . . . . . . . . . . . . . 37713.1 Introduction to Network Programming . . . . . . . . . . . . . . 37713.2 TCP/IP Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37713.3 IP Host and IP address . . . . . . . . . . . . . . . . . . . . . . . . . 37913.4 IP Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37913.5 IP Packet Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37913.6 Routers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38013.7 UDP User Datagram Protocol . . . . . . . . . . . . . . . . . . . . 38013.8 TCP Transmission Control Protocol . . . . . . . . . . . . . . . 38013.9 Port Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38013.10 Network and Host Byte Orders . . . . . . . . . . . . . . . . . . . 38113.11 Data Flow in TCP/IP Networks . . . . . . . . . . . . . . . . . . . 38113.12 Network Programming . . . . . . . . . . . . . . . . . . . . . . . . . 382

13.12.1 Network Programming Platforms . . . . . . . . . . 38213.12.2 Server-Client Computing Model . . . . . . . . . . . 383

13.13 Socket Programming . . . . . . . . . . . . . . . . . . . . . . . . . . 38313.13.1 Socket Address . . . . . . . . . . . . . . . . . . . . . . . 38313.13.2 The Socket API . . . . . . . . . . . . . . . . . . . . . . . 384

13.14 UDP Echo Server-Client Program . . . . . . . . . . . . . . . . . 38513.15 TCP Echo Server-Client Program . . . . . . . . . . . . . . . . . 38713.16 Hostname and IP Address . . . . . . . . . . . . . . . . . . . . . . . 39113.17 TCP Programming Project: File Server on Internet . . . . . 394

13.17.1 Project Specification . . . . . . . . . . . . . . . . . . . 39413.17.2 Helps and Hints . . . . . . . . . . . . . . . . . . . . . . . 39513.17.3 Multi-threaded TCP Server . . . . . . . . . . . . . . . 396

13.18 Web and CGI Programming . . . . . . . . . . . . . . . . . . . . . 39613.18.1 HTTP Programming Model . . . . . . . . . . . . . . 39613.18.2 Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . 39713.18.3 Hosting Web Pages . . . . . . . . . . . . . . . . . . . . 39913.18.4 Configure HTTPD for Web Pages . . . . . . . . . . 40013.18.5 Dynamic Web Pages . . . . . . . . . . . . . . . . . . . 40113.18.6 PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40113.18.7 CGI Programming . . . . . . . . . . . . . . . . . . . . . 40613.18.8 Configure HTTPD for CGI . . . . . . . . . . . . . . . 406

Contents xix

Page 18: Systems Programming in Unix/Linux978-3-319-92429...systems programming in Unix/Linux has been a popular subject matter in CS/CE education and also for self-study by advanced programmers

13.19 CGI Programming Project: Dynamic Webpageby CGI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407

13.20 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412

14 MySQL Database System . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41314.1 Introduction to MySQL . . . . . . . . . . . . . . . . . . . . . . . . 41314.2 Install MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414

14.2.1 Ubuntu Linux . . . . . . . . . . . . . . . . . . . . . . . . 41414.2.2 Slackware Linux . . . . . . . . . . . . . . . . . . . . . . 414

14.3 Use MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41514.3.1 Connect to MySQL Server . . . . . . . . . . . . . . . 41514.3.2 Show Databases . . . . . . . . . . . . . . . . . . . . . . 41514.3.3 Create New database . . . . . . . . . . . . . . . . . . . 41614.3.4 Drop Database . . . . . . . . . . . . . . . . . . . . . . . 41614.3.5 Choose Database . . . . . . . . . . . . . . . . . . . . . . 41714.3.6 Create Tables . . . . . . . . . . . . . . . . . . . . . . . . 41714.3.7 Drop Tables . . . . . . . . . . . . . . . . . . . . . . . . . 41814.3.8 Data Types in MySQL . . . . . . . . . . . . . . . . . . 41914.3.9 Insert Rows . . . . . . . . . . . . . . . . . . . . . . . . . . 41914.3.10 Delete Rows . . . . . . . . . . . . . . . . . . . . . . . . . 42014.3.11 Update Table . . . . . . . . . . . . . . . . . . . . . . . . 42114.3.12 Alter Table . . . . . . . . . . . . . . . . . . . . . . . . . . 42214.3.13 Related Tables . . . . . . . . . . . . . . . . . . . . . . . . 42414.3.14 Join Operations . . . . . . . . . . . . . . . . . . . . . . . 42714.3.15 MySQL Database Diagram . . . . . . . . . . . . . . 42914.3.16 MySQL Scripts . . . . . . . . . . . . . . . . . . . . . . . 429

14.4 MySQL Programming in C . . . . . . . . . . . . . . . . . . . . . . 43314.4.1 Build MySQL Client Program in C . . . . . . . . . 43314.4.2 Connect to MySQL Server in C . . . . . . . . . . . 43314.4.3 Build MySQL Database in C . . . . . . . . . . . . . 43514.4.4 Retrieve Results of MySQL Queries in C . . . . 437

14.5 MySQL Programming in PHP . . . . . . . . . . . . . . . . . . . 44014.5.1 Connect to MySQL Server in PHP . . . . . . . . . 44114.5.2 Create Database Tables in PHP . . . . . . . . . . . 44214.5.3 Insert Records into Table in PHP . . . . . . . . . . 44214.5.4 Retrieve Results of MySQL Queries

in PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44514.5.5 Update Operation in PHP . . . . . . . . . . . . . . . . 44614.5.6 Delete Rows in PHP . . . . . . . . . . . . . . . . . . . 447

14.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449

xx Contents

Page 19: Systems Programming in Unix/Linux978-3-319-92429...systems programming in Unix/Linux has been a popular subject matter in CS/CE education and also for self-study by advanced programmers

About the Author

K. C. Wang received the BSEE degree fromNational Taiwan University in 1960 and thePhD degree in Electrical Engineering fromNorthwestern University, Evanston, IL, in1965. He is currently a professor in the Schoolof Electrical Engineering and Computer Scienceat Washington State University. His academicinterests are in operating systems, distributedsystems, and parallel computing.

xxi