Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Systems Programming in Unix
PREMIUM
Số trang
468
Kích thước
8.6 MB
Định dạng
PDF
Lượt xem
1723

Systems Programming in Unix

Nội dung xem thử

Mô tả chi tiết

K.C. Wang

Systems

Programming

in Unix/Linux

Systems Programming in Unix/Linux

K. C. Wang

Systems Programming

in Unix/Linux

K. C. Wang

School of Electrical Engineering

Washington State University

Pullman, 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 2018

This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or

part of the material is concerned, specifically the rights of translation, reprinting, reuse of

illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way,

and transmission or information storage and retrieval, electronic adaptation, computer software, or

by similar or dissimilar methodology now known or hereafter developed.

The use of general descriptive names, registered names, trademarks, service marks, etc. in this

publication does not imply, even in the absence of a specific statement, that such names are

exempt 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 in

this book are believed to be true and accurate at the date of publication. Neither the publisher nor

the authors or the editors give a warranty, express or implied, with respect to the material

contained herein or for any errors or omissions that may have been made. The publisher

remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.

This Springer imprint is published by the registered company Springer Nature Switzerland AG

The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland

Dedicated to

Cindy

Preface

Systems programming is an indispensable part of Computer Science and

Computer Engineering education. System programming courses in Computer

Science/Engineering curriculums play two important roles. First, it provides

students with a wide range of knowledge about computer system software and

advanced programming skills, allowing them to interface with operating

system kernel, perform file operations and network programming, and make

efficient use of system resources to develop application programs. Second, it

prepares students with needed background to pursue advanced studies in

Computer Science/Engineering, such as operating systems, embedded

systems, database systems, data mining, artificial intelligence, computer

networks, and distributed and parallel computing. Due to its importance,

systems programming in Unix/Linux has been a popular subject matter in

CS/CE education and also for self-study by advanced programmers. As a

result, 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 Systems

Programming course I teach at WSU. For many years, I had to use my own

class 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 and

learning the theory and practice of systems programming. Unlike most other

books, this book covers systems programming topics in greater depth and

it stresses programming practice. It uses a series of programming projects to

let students apply their acquired knowledge and programming skills to

develop practical and useful programs. The book is intended as a textbook

in technical-oriented systems programming courses. It contains detailed

example programs with complete source code, making it suitable for self￾study also.

Undertaking this book project has proved to be another very demanding

and 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 especially

thank Yan Zhang for his help in preparing figures for the book and proof￾reading the manuscript.

vii

Special thanks go to Cindy for her continuing support and inspirations,

which have made this book possible. Above all, I would like to thank my

family for bearing with me with endless excuses of being busy all the time.

Sample solutions of programming projects in the book are available for

download at http://wang.eecs.wsu.edu/~kcw. For source code, please contact

the author by email.

Pullman, WA, USA

April, 2018

K. C. Wang

viii Preface

Contents

1 Introduction ...................................... 1

1.1 About This Book . . ........................... 1

1.2 Roles of Systems Programming ................... 1

1.3 Objectives of This Book . . . . . . . . . . . . . . . . . . . . . . . . 2

1.3.1 Strengthen Students Programming

Background . . . . . . . . . . . . . . . . . . . . . . . . . 2

1.3.2 Applications of Dynamic Data Structures . . . . 2

1.3.3 Process Concept and Process

Management . . . . . . ................... 3

1.3.4 Concurrent Programming . . . . . . . . . . . . . . . . 3

1.3.5 Timer and Time Functions . . ............. 3

1.3.6 Signals, Signal Processing and IPC ......... 3

1.3.7 File system . . . . . . .................... 4

1.3.8 TCP/IP and Network Programming . ........ 4

1.4 Intended Audience ............................ 4

1.5 Unique Features of This Book . .................. 4

1.6 Use This Book As Textbook in Systems

Programming Courses . . . . . . ................... 6

1.7 Other Reference Books ......................... 7

1.8 Introduction to Unix ........................... 7

1.8.1 AT&T Unix . . ....................... 7

1.8.2 Berkeley Unix . . . . . . . . . . . . . . . . . . . . . . . . 8

1.8.3 HP Unix . . .......................... 8

1.8.4 IBM Unix . . . . . ...................... 8

1.8.5 Sun Unix ........................... 8

1.9 Introduction to Linux . . . . . . . . . . . . . . . . . . . . . . . . . . 9

1.10 Linux Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

1.10.1 Debian Linux ........................ 9

1.10.2 Ubuntu Linux ........................ 9

1.10.3 Linux Mint . . . . . . . . . . . . . . . . . . . . . . . . . . 9

1.10.4 RPM-Based Linux . . . . . . . . . . . . . . . . . . . . . 10

1.10.5 Slackware Linux . . . . . . . . . . . . . . . . . . . . . . 10

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

ix

1.12 Linux on Virtual Machines . . . . . . . . . . . . . . . . . . . . . . 10

1.12.1 VirtualBox . . . . . . . . . . . . . . . . . . . . . . . . . . 10

1.12.2 VMware . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

1.12.3 Dual Boot Slackware and Ubuntu Linux . . . . . 14

1.13 Use Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

1.13.1 Linux kernel image . . . . . . . . . . . . . . . . . . . . 15

1.13.2 Linux Booters . . . . . . . . . . . . . . . . . . . . . . . . 15

1.13.3 Linux Booting . . . . . . . . . . . . . . . . . . . . . . . . 15

1.13.4 Linux Run-levels . . . . . . . . . . . . . . . . . . . . . . 16

1.13.5 Login Process . . . . . . . . . . . . . . . . . . . . . . . . 16

1.13.6 Command Executions . . . . . . . . . . . . . . . . . . 16

1.14 Use Ubuntu Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

1.14.1 Ubuntu Versions . . . . . . . . . . . . . . . . . . . . . . 16

1.14.2 Special Features of Ubuntu Linux . . . . . . . . . . 17

1.15 Unix/Linux File System Organization . . . . . . . . . . . . . . 18

1.15.1 File Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

1.15.2 File Pathnames . . . . . . . . . . . . . . . . . . . . . . . 19

1.15.3 Unix/Linux Commands . . . . . . . . . . . . . . . . . 19

1.15.4 Linux Man Pages . . . . . . . . . . . . . . . . . . . . . 19

1.16 Ubuntu Linux System Administration . . . . . . . . . . . . . . 20

1.16.1 User Accounts . . . . . . . . . . . . . . . . . . . . . . . . 20

1.16.2 Add New User . . . . . . . . . . . . . . . . . . . . . . . 20

1.16.3 The sudo Command . . . . . . . . . . . . . . . . . . . . 21

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

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

2 Programming Background . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

2.1 Text Editors in Linux . . . . . . . . . . . . . . . . . . . . . . . . . . 23

2.1.1 Vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

2.1.2 Gedit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

2.1.3 Emacs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

2.2 Use Text Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

2.2.1 Use Emacs . . . . . . . . . . . . . . . . . . . . . . . . . . 25

2.2.2 Emacs Menus . . . . . . . . . . . . . . . . . . . . . . . . 26

2.2.3 IDE of Emacs . . . . . . . . . . . . . . . . . . . . . . . . 26

2.3 Program Development . . . . . . . . . . . . . . . . . . . . . . . . . 27

2.3.1 Program Development Steps . . . . . . . . . . . . . 27

2.3.2 Variables in C . . . . . . . . . . . . . . . . . . . . . . . . 27

2.3.3 Compile-Link in GCC . . . . . . . . . . . . . . . . . . 28

2.3.4 Static vs. Dynamic Linking . . . . . . . . . . . . . . 29

2.3.5 Executable File Format . . . . . . . . . . . . . . . . . 30

2.3.6 Contents of a.out File . . . . . . . . . . . . . . . . . . . 30

2.3.7 Program Execution . . . . . . . . . . . . . . . . . . . . 31

2.3.8 Program Termination . . . . . . . . . . . . . . . . . . . 32

2.4 Function Call in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

2.4.1 Run-Time Stack Usage in 32-Bit GCC . . . . . . 33

2.4.2 Stack Frames . . . . . . . . . . . . . . . . . . . . . . . . . 35

2.4.3 Return From Function Call . . . . . . . . . . . . . . . 35

x Contents

2.4.4 Long Jump . . . . . . . . . . . . . . . . . . . . . . . . . . 36

2.4.5 Run-Time Stack Usage in 64-Bit GCC . . . . . . 37

2.5 Link C Program with Assembly Code . . . . . . . . . . . . . . 40

2.5.1 Programming in Assembly . . . . . . . . . . . . . . . 41

2.5.2 Implement Functions in Assembly . . . . . . . . . 43

2.5.3 Call C functions from Assembly . . . . . . . . . . . 44

2.6 Link Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

2.6.1 Static Link Library . . . . . . . . . . . . . . . . . . . . 45

2.6.2 Dynamic Link Library . . . . . . . . . . . . . . . . . . 46

2.7 Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

2.7.1 Makefile Format . . . . . . . . . . . . . . . . . . . . . . 46

2.7.2 The make Program . . . . . . . . . . . . . . . . . . . . 46

2.7.3 Makefile Examples . . . . . . . . . . . . . . . . . . . . 47

2.8 The GDB Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

2.8.1 Use GDB in Emacs IDE . . . . . . . . . . . . . . . . 53

2.8.2 Advices on Using Debugging Tools . . . . . . . . 58

2.8.3 Common Errors in C programs . . . . . . . . . . . . 58

2.9 Structures in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

2.9.1 Structure and Pointers . . . . . . . . . . . . . . . . . . 64

2.9.2 Typecast in C . . . . . . . . . . . . . . . . . . . . . . . . 64

2.10 Link List Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

2.10.1 Link Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

2.10.2 Link List Operations . . . . . . . . . . . . . . . . . . . 66

2.10.3 Build Link List . . . . . . . . . . . . . . . . . . . . . . . 67

2.10.4 Link List Traversal . . . . . . . . . . . . . . . . . . . . 70

2.10.5 Search Link List . . . . . . . . . . . . . . . . . . . . . . 72

2.10.6 Insert Operation . . . . . . . . . . . . . . . . . . . . . . . 72

2.10.7 Priority Queue . . . . . . . . . . . . . . . . . . . . . . . . 73

2.10.8 Delete Operation . . . . . . . . . . . . . . . . . . . . . . 74

2.10.9 Circular Link List . . . . . . . . . . . . . . . . . . . . . 75

2.10.10 Open-Ended C Structures . . . . . . . . . . . . . . . . 75

2.10.11 Doubly Link Lists . . . . . . . . . . . . . . . . . . . . . 76

2.10.12 Doubly Link Lists Example Programs . . . . . . . 76

2.11 Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

2.12 Binary Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

2.12.1 Binary Search Tree . . . . . . . . . . . . . . . . . . . . 85

2.12.2 Build a Binary Search Tree (BST) . . . . . . . . . 86

2.12.3 Binary Tree Traversal Algorithms . . . . . . . . . . 87

2.12.4 Depth-First Traversal Algorithms . . . . . . . . . . 87

2.12.5 Breadth-First Traversal Algorithms . . . . . . . . . 88

2.13 Programming Project: Unix/Linux File System

Tree Simulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

2.13.1 Unix/Linux File System Tree . . . . . . . . . . . . . 90

2.13.2 Implement General Tree by Binary Tree . . . . . 90

2.13.3 Project Specification and Requirements . . . . . . 90

2.13.4 Commands Specification . . . . . . . . . . . . . . . . 91

2.13.5 Program Organization . . . . . . . . . . . . . . . . . . 91

Contents xi

2.13.6 Command Algorithms . . . . . . . . . . . . . . . . . . 94

2.13.7 Sample Solution . . . . . . . . . . . . . . . . . . . . . . 97

2.14 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

3 Process Management in Unix/Linux . . . . . . . . . . . . . . . . . . . . 101

3.1 Multitasking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

3.2 The Process Concept . . . . . . . . . . . . . . . . . . . . . . . . . . 102

3.3 A Multitasking System . . . . . . . . . . . . . . . . . . . . . . . . . 103

3.3.1 type.h file . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

3.3.2 The ts.s file . . . . . . . . . . . . . . . . . . . . . . . . . . 103

3.3.3 The queue.c file . . . . . . . . . . . . . . . . . . . . . . . 104

3.3.4 The t.c file . . . . . . . . . . . . . . . . . . . . . . . . . . 105

3.3.5 Explanations of the Multitasking

System Code . . . . . . . . . . . . . . . . . . . . . . . . . 108

3.4 Process Synchronization . . . . . . . . . . . . . . . . . . . . . . . . 111

3.4.1 Sleep Operation . . . . . . . . . . . . . . . . . . . . . . . 111

3.4.2 Wakeup Operation . . . . . . . . . . . . . . . . . . . . . 112

3.5 Process Termination . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

3.5.1 Algorithm of kexit() . . . . . . . . . . . . . . . . . . . . 112

3.5.2 Process Family Tree . . . . . . . . . . . . . . . . . . . 113

3.5.3 Wait for Child Process Termination . . . . . . . . 114

3.6 Process Management in the MT Multitasking

System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

3.7 Processes in Unix/Linux . . . . . . . . . . . . . . . . . . . . . . . . 115

3.7.1 Process Origin . . . . . . . . . . . . . . . . . . . . . . . . 115

3.7.2 INIT and Daemon Processes . . . . . . . . . . . . . 116

3.7.3 Login Processes . . . . . . . . . . . . . . . . . . . . . . 117

3.7.4 Sh Process . . . . . . . . . . . . . . . . . . . . . . . . . . 117

3.7.5 Process Execution Modes . . . . . . . . . . . . . . . . 117

3.8 System Calls for Process Management . . . . . . . . . . . . . . 118

3.8.1 fork() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

3.8.2 Process Execution Order . . . . . . . . . . . . . . . . 120

3.8.3 Process Termination . . . . . . . . . . . . . . . . . . . 121

3.8.4 Wait for Child Process Termination . . . . . . . . 122

3.8.5 Subreaper Process in Linux . . . . . . . . . . . . . . 123

3.8.6 exec(): Change Process Execution Image . . . . 125

3.8.7 Environment Variables . . . . . . . . . . . . . . . . . . 126

3.9 I/O Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129

3.9.1 FILE Streams and File Descriptors . . . . . . . . . 129

3.9.2 FILE Stream I/O and System Call . . . . . . . . . . 130

3.9.3 Redirect stdin . . . . . . . . . . . . . . . . . . . . . . . . 130

3.9.4 Redirect stdout . . . . . . . . . . . . . . . . . . . . . . . 131

3.10 Pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

3.10.1 Pipe Programming in Unix/Linux . . . . . . . . . . 131

3.10.2 Pipe Command Processing . . . . . . . . . . . . . . . 134

3.10.3 Connect PIPE writer to PIPE reader . . . . . . . . 134

3.10.4 Named pipes . . . . . . . . . . . . . . . . . . . . . . . . . 135

xii Contents

3.11 Programming Project: sh Simulator . . . . . . . . . . . . . . . . 137

3.11.1 Single Command with I/O Redirection . . . . . . 137

3.11.2 Commands with Pipes . . . . . . . . . . . . . . . . . . 138

3.11.3 ELF executable vs. sh script files . . . . . . . . . . 138

3.11.4 Sample Solution . . . . . . . . . . . . . . . . . . . . . . 138

3.12 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

4 Concurrent Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . 141

4.1 Introduction to Parallel Computing . . . . . . . . . . . . . . . . 141

4.1.1 Sequential Algorithms vs. Parallel

Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . 142

4.1.2 Parallelism vs. Concurrency . . . . . . . . . . . . . . 142

4.2 Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

4.2.1 Principle of Threads . . . . . . . . . . . . . . . . . . . . 143

4.2.2 Advantages of Threads . . . . . . . . . . . . . . . . . 143

4.2.3 Disadvantages of Threads . . . . . . . . . . . . . . . 144

4.3 Threads Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

4.4 Threads Management Functions . . . . . . . . . . . . . . . . . . 144

4.4.1 Create Thread . . . . . . . . . . . . . . . . . . . . . . . . 145

4.4.2 Thread ID . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

4.4.3 Thread Termination . . . . . . . . . . . . . . . . . . . . 146

4.4.4 Thread Join . . . . . . . . . . . . . . . . . . . . . . . . . . 146

4.5 Threads Example Programs . . . . . . . . . . . . . . . . . . . . . 147

4.5.1 Sum of Matrix by Threads . . . . . . . . . . . . . . . 147

4.5.2 Quicksort by Threads . . . . . . . . . . . . . . . . . . . 148

4.6 Threads Synchronization . . . . . . . . . . . . . . . . . . . . . . . 151

4.6.1 Mutex Locks . . . . . . . . . . . . . . . . . . . . . . . . . 151

4.6.2 Deadlock Prevention . . . . . . . . . . . . . . . . . . . 154

4.6.3 Condition Variables . . . . . . . . . . . . . . . . . . . . 155

4.6.4 Producer-Consumer Problem . . . . . . . . . . . . . 156

4.6.5 Semaphores . . . . . . . . . . . . . . . . . . . . . . . . . 159

4.6.6 Barriers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160

4.6.7 Solve System of Linear Equations by

Concurrent Threads . . . . . . . . . . . . . . . . . . . . 161

4.6.8 Threads in Linux . . . . . . . . . . . . . . . . . . . . . . 165

4.7 Programming Project: User-Level Threads . . . . . . . . . . . 165

4.7.1 Project Base Code: A Multitasking System . . . 165

4.7.2 User-Level Threads . . . . . . . . . . . . . . . . . . . . 169

4.7.3 Implementation of Thread Join Operation . . . . 172

4.7.4 Implementation of Mutex Operations . . . . . . . 176

4.7.5 Test Project with Mutex by Concurrent

Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . 177

4.7.6 Implementation of Semaphores . . . . . . . . . . . . 181

4.7.7 Producer-Consumer Problem using

Semaphores . . . . . . . . . . . . . . . . . . . . . . . . . 181

4.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

Contents xiii

5 Timers and Time Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187

5.1 Hardware Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187

5.2 PC Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187

5.3 CPU Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188

5.4 Interrupt Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . 189

5.5 Time Service Functions . . . . . . . . . . . . . . . . . . . . . . . . 189

5.5.1 Gettimeofday-Settimeofday . . . . . . . . . . . . . . 189

5.5.2 The Time System Call . . . . . . . . . . . . . . . . . . 191

5.5.3 The Times System Call . . . . . . . . . . . . . . . . . 191

5.5.4 Time and Date Commands . . . . . . . . . . . . . . . 192

5.6 Interval Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192

5.7 REAL Mode Interval Timer . . . . . . . . . . . . . . . . . . . . . 195

5.8 Programming Project . . . . . . . . . . . . . . . . . . . . . . . . . . 195

5.8.1 System Base Code . . . . . . . . . . . . . . . . . . . . . 195

5.8.2 Timer Interrupts . . . . . . . . . . . . . . . . . . . . . . 199

5.8.3 Timer Queue . . . . . . . . . . . . . . . . . . . . . . . . . 200

5.8.4 Critical Regions . . . . . . . . . . . . . . . . . . . . . . . 202

5.8.5 Advanced Topics . . . . . . . . . . . . . . . . . . . . . . 203

5.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204

6 Signals and Signal Processing . . . . . . . . . . . . . . . . . . . . . . . . . 205

6.1 Signals and Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . 205

6.2 Examples of Unix/Linux Signals . . . . . . . . . . . . . . . . . . 207

6.3 Signal Processing in Unix/Linux . . . . . . . . . . . . . . . . . . 208

6.3.1 Signal Types . . . . . . . . . . . . . . . . . . . . . . . . . 208

6.3.2 Origin of Signals . . . . . . . . . . . . . . . . . . . . . . 209

6.3.3 Signals in Process PROC Structure: . . . . . . . . 209

6.3.4 Signal Handlers . . . . . . . . . . . . . . . . . . . . . . . 209

6.3.5 Install Signal Catchers . . . . . . . . . . . . . . . . . . 210

6.4 Signal Processing Steps . . . . . . . . . . . . . . . . . . . . . . . . 212

6.5 Signals and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . 213

6.6 Signals as IPC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213

6.7 IPC in Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215

6.7.1 Pipes and FIFOs . . . . . . . . . . . . . . . . . . . . . . 215

6.7.2 Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215

6.7.3 System V IPC . . . . . . . . . . . . . . . . . . . . . . . . 215

6.7.4 POSIX Message Queues . . . . . . . . . . . . . . . . 216

6.7.5 Threads Synchronization Mechanisms . . . . . . . 216

6.7.6 Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216

6.8 Programming Project: Implement an IPC

for Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216

6.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219

7 File Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221

7.1 File Operation Levels . . . . . . . . . . . . . . . . . . . . . . . . . . 221

7.2 File I/O Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

xiv Contents

7.3 Low Level File Operations . . . . . . . . . . . . . . . . . . . . . . 226

7.3.1 Partitions . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

7.3.2 Format Partitions . . . . . . . . . . . . . . . . . . . . . . 229

7.3.3 Mount Partitions . . . . . . . . . . . . . . . . . . . . . . 230

7.4 Introduction to EXT2 File System . . . . . . . . . . . . . . . . . 231

7.4.1 EXT2 File System Data Structures . . . . . . . . . 231

7.4.2 Superblock . . . . . . . . . . . . . . . . . . . . . . . . . . 231

7.4.3 Group Descriptor . . . . . . . . . . . . . . . . . . . . . . 232

7.4.4 Bitmaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233

7.4.5 Inodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233

7.4.6 Directory Entries . . . . . . . . . . . . . . . . . . . . . . 234

7.5 Programming Examples . . . . . . . . . . . . . . . . . . . . . . . . 235

7.5.1 Display Superblock . . . . . . . . . . . . . . . . . . . . 235

7.5.2 Display Bitmaps . . . . . . . . . . . . . . . . . . . . . . 237

7.5.3 Display root Inode . . . . . . . . . . . . . . . . . . . . . 239

7.5.4 Display Directory Entries . . . . . . . . . . . . . . . . 241

7.6 Programming Project: Convert File Pathname to Inode . . 243

7.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244

8 System Calls for File Operations . . . . . . . . . . . . . . . . . . . . . . 245

8.1 Systems Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245

8.2 System Call Man Pages . . . . . . . . . . . . . . . . . . . . . . . . 245

8.3 System Calls for File Operations . . . . . . . . . . . . . . . . . . 246

8.4 Commonly used system Calls . . . . . . . . . . . . . . . . . . . . 248

8.5 Link Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250

8.5.1 Hard Link Files . . . . . . . . . . . . . . . . . . . . . . . 250

8.5.2 Symbolic Link Files . . . . . . . . . . . . . . . . . . . 250

8.6 The stat Systen Call . . . . . . . . . . . . . . . . . . . . . . . . . . . 251

8.6.1 Stat File Status . . . . . . . . . . . . . . . . . . . . . . . 251

8.6.2 The stat Structure . . . . . . . . . . . . . . . . . . . . . 252

8.6.3 Stat and File Inode . . . . . . . . . . . . . . . . . . . . 253

8.6.4 File Type and Permissions . . . . . . . . . . . . . . . 254

8.6.5 Opendir-Readdir Functions . . . . . . . . . . . . . . 255

8.6.6 Readlink Function . . . . . . . . . . . . . . . . . . . . . 256

8.6.7 The ls Program . . . . . . . . . . . . . . . . . . . . . . . 256

8.7 open-close-lseek System Calls . . . . . . . . . . . . . . . . . . . 258

8.7.1 Open File and File Descriptor . . . . . . . . . . . . . 258

8.7.2 Close File Descriptor . . . . . . . . . . . . . . . . . . . 259

8.7.3 lseek File Descriptor . . . . . . . . . . . . . . . . . . . 259

8.8 Read() System Call . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

8.9 Write() System Call . . . . . . . . . . . . . . . . . . . . . . . . . . . 260

8.10 File Operation Example Programs . . . . . . . . . . . . . . . . . 260

8.10.1 Display File Contents . . . . . . . . . . . . . . . . . . 260

8.10.2 Copy Files . . . . . . . . . . . . . . . . . . . . . . . . . . 261

8.10.3 Selective File Copy . . . . . . . . . . . . . . . . . . . . 262

8.11 Programming Project: Recursive Copy Files

using System Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264

Contents xv

8.11.1 Hints and Helps . . . . . . . . . . . . . . . . . . . . . . . 264

8.11.2 Sample Solution . . . . . . . . . . . . . . . . . . . . . . 264

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

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265

9 Library I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267

9.1 Library I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 267

9.2 Library I/O Functions vs. System Calls . . . . . . . . . . . . . 267

9.3 Algorithms of Library I/O Functions . . . . . . . . . . . . . . . 271

9.3.1 Algorithm of fread . . . . . . . . . . . . . . . . . . . . . 271

9.3.2 Algorithm of fwrite . . . . . . . . . . . . . . . . . . . . 271

9.3.3 Algorithm of fclose . . . . . . . . . . . . . . . . . . . . 271

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

9.5 Library I/O Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272

9.5.1 Char Mode I/O . . . . . . . . . . . . . . . . . . . . . . . 272

9.5.2 Line mode I/O . . . . . . . . . . . . . . . . . . . . . . . . 273

9.5.3 Formatted I/O . . . . . . . . . . . . . . . . . . . . . . . . 274

9.5.4 In-memory Conversion Functions . . . . . . . . . . 274

9.5.5 Other Library I/O Functions . . . . . . . . . . . . . . 274

9.5.6 Restriction on Mixed fread-fwrite . . . . . . . . . . 275

9.6 File Stream Buffering . . . . . . . . . . . . . . . . . . . . . . . . . . 275

9.7 Functions with Varying Parameters . . . . . . . . . . . . . . . . 276

9.8 Programming Project: Printf-like Function . . . . . . . . . . . 277

9.8.1 Project Specification . . . . . . . . . . . . . . . . . . . 278

9.8.2 Base Code of Project . . . . . . . . . . . . . . . . . . . 278

9.8.3 Algorithm of myprintf() . . . . . . . . . . . . . . . . . 279

9.8.4 Project Refinements . . . . . . . . . . . . . . . . . . . . 279

9.8.5 Project Demonstration and Sample

Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

9.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

10 Sh Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283

10.1 sh Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283

10.2 sh Scripts vs. C Programs . . . . . . . . . . . . . . . . . . . . . . . 284

10.3 Command-line parameters . . . . . . . . . . . . . . . . . . . . . . 284

10.4 Sh Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285

10.5 Quotes in sh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285

10.6 sh Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286

10.7 sh Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286

10.7.1 Built-in Commands . . . . . . . . . . . . . . . . . . . . 286

10.7.2 Linux Commands . . . . . . . . . . . . . . . . . . . . . 287

10.8 Command Substitution . . . . . . . . . . . . . . . . . . . . . . . . . 288

10.9 Sh Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . 288

10.9.1 if-else-fi statement . . . . . . . . . . . . . . . . . . . . . 288

10.9.2 for Statement . . . . . . . . . . . . . . . . . . . . . . . . . 290

10.9.3 while Statement . . . . . . . . . . . . . . . . . . . . . . . 291

10.9.4 until-do Statement . . . . . . . . . . . . . . . . . . . . . 291

xvi Contents

Tải ngay đi em, còn do dự, trời tối mất!