Unix and Linux Operating Systems

9
MODULE 2 Unix & Linux Operating Systems LuminUs Training for C and C++ on Unix/Linux Platforms LU01-C/C++/Unix/Linux-200511 Technology Services www.luminus.in

Transcript of Unix and Linux Operating Systems

MODULE 2

Unix & Linux Operating Systems

LuminUs Training for C and C++ on Unix/Linux Platforms LU01-C/C++/Unix/Linux-200511

Technology Services www.luminus.in

#12, Ramanuja Plaza, 3rd Floor, 5th Cross, Malleswaram Bangalore – 560003 Ph. +91-80-56979944 www.luminus.in

MODULE 1

Unix & Linux Operating Systems

LuminUs Training for C and C++ on Unix/Linux Platforms LU02-C/C++/Unix/Linux-200512

LuminUs Technology Services Committed to Succeed

iii

Table of Contents

Particulars Page No. Table of Contents iii. Preface ix. CHAPTER 1 Operating Systems and Unix Operating System 2 Hardware Management 2 Process Management 2 Introduction to the UNIX Operating System 3 History of UNIX 6 Present Scenario 9 What lies ahead? 9 CHAPTER 2 Linux and Open Source Software What is Linux? 12 The Linux kernel 12 Open Source Development Labs (OSDL) 13 The Open Source News for 2004 and 2005 15 Open Source Licenses 15 Linux Mark Institute 15 Standardization of the Unix and Linux Operating Systems 16 Different Dimensions Of Standardization 17 Desired Degree And Depth Of Standardization 17 POSIX Standard 17 The Open Group 18 POSIX: Types of Standards 18 POSIX Certification 19 Linux Standard Base (LSB) 19 Two-fold Organization of LSB Specifications 21 The File System Hierarchy Standard (FHS) 21 Online Resources 23 CHAPTER 3 UNIX and Linux Distributions Variants of Unix 26 Prominent Linux Distributions 29 Data Center Linux – DCL 29 Desktop Linux – DTL 32 Carrier Grade Linux – CGL (Embedded) 34 Live CD Versions – (Minimalist / Compact) 35 List of Linux Distributions 37

iv

CHAPTER 4 Standard Unix / Linux OS Structure Operating System Structure 46 Monolithic Systems 46 Layered Systems 46 Virtual Machines 47 Client-server Model 48 Unix / Linux Operating System Structure 49 Various parts of Unix / Linux 50 Micro kernels 51 Micro kernel vs. Monolithic System 52 Important parts of the kernel 53 Hierarchical File System 54 UNIX File System Organization 55 Unix / Linux Directory Structure 56 Unix Directories, Files and Inodes 57 Base Directories in UNIX 57 File systems supported by Unix / Linux 65 Listing File Systems 67 CHAPTER 5 Getting Started in the UNIX Environment Unix Installation – An overview 70 Before Beginning the Installation 70 Research Your Hardware Specifications 70 Choose an Installation Type 70 Planning Hard Disk Partition 71 The Boot Loader 71 Choosing How to Install 72 Step-by-Step Installation 72 Outline of Step by Step Installation of Fedora Linux 72 Starting the Install 73 Logging in 74 Major services in a UNIX or Linux system 74 Other Services on Unix / Linux Servers 77 Text Editors 77 The vi Text Editor 78 Using the vi Text Editor 79 CHAPTER 6 Navigation and Using Shell Commands Logging in 84 The login environment 85 Shells 85 Shell Commands and Scripts 86 Environment and shell variables 88 Wildcards 89 Regular expressions 90 Nested shell commands 91 Overview of UNIX / Linux Shell Commands 92 UNIX Commands with given Syntax 93 Other Commands 94 Important keys 95

v

Alternative shells 96 Window based terminal emulators 96 Remote shells and logins 96 Text editors 97 File handling commands 97 Ownership and granting access permission 98 Compilers 99 Miscellaneous 101 CHAPTER 7 C and C++ Programming on Unix / Linux Part 1 Introduction 104 Editing with Emacs 104 About Emacs 104 Opening a C or C++ Source File 104 Automatic Formatting 104 Syntax Highlighting 105 Compiling with GCC 105 Linking Object Files 107 Automating the Process with GNU Make 108 Debugging with GNU Debugger (GDB) 109 Running GDB 110 Finding More Information 111 Manuel Pages 111 Info 112 Header Files 113 Source Code 113 Summary 114

CHAPTER 8 C and C++ Programming on Unix / Linux Part 2 Introduction 116 Interaction With the Execution Environment 116 The Argument List 116 Unix/Linux Command-Line Conventions 117 Standard I/O 120 Program Exit Codes 121 The Environment 122 Using Temporary Files 123 Coding Defensively 125 System Call Failures 127 Writing and Using Libraries 131 Shared Libraries 133 Standard Libraries 134 Summary 138

CHAPTER 9 Advanced Network Programming Part 1 Introduction 140 A Simple Daytime Client 142 Protocol Independence 146 Error Handling: Wrapper Functions 147

vi

A Simple Daytime Server 149 OSI Model 152 BSD Networking History 153 Test Networks and Hosts 154 Unix Standards 157 64-Bit Architectures 160 Summary 170 CHAPTER 10 Advanced Network Programming Part 2 Introduction 172 The Big Picture 172 User Datagram Protocol (UDP) 175 Transmission Control Protocol (TCP) 175 Stream Control Transmission Protocol (SCTP) 176 TCP Connection Establishment and Termination 177 SCTP Association Establishment and Termination 189 Port Numbers 189 TCP Port Numbers and Concurrent Servers 191 Standard Internet Services 194 Protocol Usage by Common Internet Applications 195 Summary 196 CHAPTER 11 Graphical User Interfaces Part 1 Introduction 198 The X Window System 198 Anatomy of Your Desktop 199 Invoking X Window 200 The X Display Manager 200 Choosing a Window Manager 201 FVWM And Its Ancestors 201 The Wide World of Window Systems 202 The X Graphical Interfaces 202 The Flashy Window Managers 204 Working In X 206 What is XFree86®? 207 Overview of GNOME 208 The Gnome Development Framework 210 Non-Gnome Libraries 210 Other Libraries 213 Basics 214 Data Structures 222 Other Features 234 CHAPTER 12 Graphical User Interfaces Part 2 Introduction 236 A Complete Hello, World Application 236 Widgets (Window objects) 239 Signals and Callbacks 239 Containers And Widget Layout 242 Other Layout Widgets 253

vii

Widget Concepts 254 Realizing, Mapping, and Showing 256 Other Widget Concepts 258 The Main Loop 260 Quit Functions 261 Gnome Application Basics 263 Internationalization 264 Gnome Application Checklist 266 GNOME Standards and Resources 268

CHAPTER 13 Making Real World Linux Applications Overview 270 HTTP 271 Implementation 271 Common Functions 272 Loading Server Modules 274 The Server 275 The Main Program 280 Modules 283 Show Wall-Clock Time 283 Show the Linux Distribution 284 Show Free Disk Space 285 Summarize Running Processes 286 Using the Server 292 Building the Server 293 Running the Server 293 Finishing Up 294 Appendix A Standard Function Libraries in C & C++ C keywords 298 C++ keywords 298 ANSI standard library 299 POSIX library 299 ANSI Library Header Files and Functions 299 POSIX Standard Functions 304 Non ANSI Standard Unix Functions 306

Appendix B Other Development Tools Static Program Analysis 308 Finding Dynamic Memory Errors 309 A Program to Test Memory Allocation and Deallocation 310 Finding Memory Leaks Using mtrace 311 Using ccmalloc 312 Electric Fence 313 Choosing Among the Different Memory-Debugging Tools 314 Source Code for the Dynamic Memory Program 314 Profiling 316 A Simple Calculator 317 Collecting Profiling Information 318 Displaying Profiling Data 318 How gprof Collects Data 320 Source Code for the Calculator Program 320

viii

ix

PREFACE It usually takes a longer time to learn anything that is complex in nature, than something that seems like a child’s play. The learning curve is steeper in the former, and it takes longer to grasp the intricacies of the subject. Remembering a famous poem, “The road not taken”, by Robert Frost, which reads like, “TWO roads diverged in a yellow wood, And sorry I could not travel both …… I took the one less traveled by, And that has made all the difference.” The theme behind it is about taking one of the roads and he chooses the one less traveled by. The IT industry is vast and advancements in the same happen at a quick rate. The free software and open source movements though had started long time ago, its effect has had a presence across the globe, and now is being felt by a number of individuals and all levels of organizations. Various strategies have started being formulated based on open source, and the most prominent ones can be viewed and analyzed by looking at companies like Redhat, Novell and the like, where the primary domain areas of work for software engineers is Unix or more specifically the Linux operating system. Unix as an operating system has a highly interesting history of developments, and if taken as a subject, one may even write a complete thesis on it, since it has been prevalent across the globe since the decade of 1960’s and has become one the most important area of concern for various individuals and organizations. Some of the industry giants had developed Unix as a proprietary product with lots of legacy code developed for specific types of electronic and computer hardware. With the advent and establishment of Open Source, IT companies have been forced to contribute to it in some form or the other. With Redhat Linux being open source, Sun Microsystems took up the initiative of open sourcing its Solaris product, which was followed by IBM’s contribution of 1600 patents to the Open Source community. Recently Novell took up an initiative of building a community based development model for OpenSuse Linux distribution. For Unix and Linux Operating System and Software, there is development happening on all the aspects, be it software, hardware or networking. It is mostly upon the learner of these technologies; as to how deep one can go into learning the low level concepts. On the other hand, Microsoft and Windows have gained as much of popularity as criticism from rival competitors and partners. The debate of Microsoft vs. Linux has been a continuing debate in the industry for quite a long time now. Eventually it becomes a matter of acceptability and adoptability. This book forms Module II of training on “C and C++ on Unix / Linux Platforms”. The material in this book is an extension of Module I, with more emphasis on Unix and Linux operating environments and advanced programming techniques in C and C++. After going through this material, one is expected to be familiar with core Unix and Linux concepts, usage of shell commands, along with a firm understanding of network programming and graphical user interfaces.