Linux System Programming Focuses on Everything Above the Kernel, Where Applications Such As Apache, Bash, Cp, Vim, Emacs, Gcc, Gdb, Glibc, Ls, Mv, and X Exist

Total Page:16

File Type:pdf, Size:1020Kb

Linux System Programming Focuses on Everything Above the Kernel, Where Applications Such As Apache, Bash, Cp, Vim, Emacs, Gcc, Gdb, Glibc, Ls, Mv, and X Exist Overview This book is about writing software that makes the most effective use of the system you're running on -- code that interfaces directly with the kernel and core system libraries, including the shell, text editor, compiler, debugger, core utilities, and system daemons. The majority of both Unix and Linux code is still written at the system level, and Linux System Programming focuses on everything above the kernel, where applications such as Apache, bash, cp, vim, Emacs, gcc, gdb, glibc, ls, mv, and X exist. Written primarily for engineers looking to program (better) at the low level, this book is an ideal teaching tool for any programmer. Even with the trend toward high-level development, either through web software (such as PHP) or managed code (C#), someone still has to write the PHP interpreter and the C# virtual machine. Linux System Programming gives you an understanding of core internals that makes for better code, no matter where it appears in the stack. Debugging high-level code often requires you to understand the system calls and kernel behavior of your operating system, too. Key topics include: • An overview of Linux, the kernel, the C library, and the C compiler • Reading from and writing to files, along with other basic file I/O operations, including how the Linux kernel implements and manages file I/O • Buffer size management, including the Standard I/O library • Advanced I/O interfaces, memory mappings, and optimization techniques • The family of system calls for basic process management • Advanced process management, including real-time processes • File and directories-creating, moving, copying, deleting, and managing them • Memory management -- interfaces for allocating memory, managing the memory you have, and optimizing your memory access • Signals and their role on a Unix system, plus basic and advanced signal interfaces • Time, sleeping, and clock management, starting with the basics and continuing through POSIX clocks and high resolution timers With Linux System Programming , you will be able to take an in-depth look at Linux from both a theoretical and an applied perspective as you cover a wide range of programming topics. Linux System Programming Linux System Programming by Robert Love Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Printed in the United States of America. Published by O'Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O'Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles ( safari.oreilly.com ). For more information, contact our corporate/institutional sales department: (800) 998-9938 or [email protected] . Editor: Andy Oram Production Editor: Sumita Mukherji Copyeditor: Rachel Head Proofreader: Sumita Mukherji Indexer: John Bickelhaupt Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Jessamyn Read Printing History: September 2007: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of O'Reilly Media, Inc. The Linux series designations, Linux System Programming , images of the man in the flying machine, and related trade dress are trademarks of O'Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O'Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN-10: 0-596-00958-5 ISBN-13: 978-0-596-00958-8 [M] 2 of 396 Linux System Programming Table of Contents Linux System Programming...............................................................................2 Foreword ................................................................................................................ 10 Preface................................................................................................................... 10 P3.1. Audience and Assumptions............................................................................ 11 P3.2. Contents of This Book................................................................................... 11 P3.3. Versions Covered in This Book....................................................................... 13 P3.4. Conventions Used in This Book ...................................................................... 13 P3.5. Safari® Books Online ................................................................................... 15 P3.6. Using Code Examples ................................................................................... 15 P3.7. How to Contact Us ....................................................................................... 15 P3.8. Acknowledgments........................................................................................ 16 Chapter 1. Introduction and Essential Concepts ........................................................... 17 1.1. System Programming..................................................................................... 17 1.1.1. System Calls .......................................................................................... 18 1.1.2. The C Library.......................................................................................... 19 1.1.3. The C Compiler....................................................................................... 19 1.2. APIs and ABIs ............................................................................................... 19 1.2.1. APIs ...................................................................................................... 19 1.2.2. ABIs ...................................................................................................... 20 1.3. Standards..................................................................................................... 20 1.3.1. POSIX and SUS History............................................................................ 21 1.3.2. C Language Standards............................................................................. 21 1.3.3. Linux and the Standards .......................................................................... 22 1.3.4. This Book and the Standards .................................................................... 22 1.4. Concepts of Linux Programming ...................................................................... 23 1.4.1. Files and the Filesystem........................................................................... 23 1.4.2. Processes............................................................................................... 28 1.4.3. Users and Groups ................................................................................... 29 1.4.4. Permissions............................................................................................ 30 1.4.5. Signals .................................................................................................. 31 1.4.6. Interprocess Communication .................................................................... 31 1.4.7. Headers ................................................................................................. 31 1.4.8. Error Handling ........................................................................................ 31 1.5. Getting Started with System Programming ....................................................... 35 Chapter 2. File I/O ................................................................................................... 36 2.1. Opening Files ................................................................................................ 36 2.1.1. The open( ) System Call .......................................................................... 36 2.1.2. Owners of New Files ................................................................................ 39 2.1.3. Permissions of New Files .......................................................................... 40 2.1.4. The creat( ) Function............................................................................... 42 2.1.5. Return Values and Error Codes ................................................................. 43 2.2. Reading via read( )........................................................................................ 43 2.2.1. Return Values......................................................................................... 43 2.2.2. Reading All the Bytes .............................................................................. 44 2.2.3. Nonblocking Reads.................................................................................. 45 2.2.4. Other Error Values .................................................................................. 45 2.2.5. Size Limits on read( ) .............................................................................. 46 2.3. Writing with write( ) ...................................................................................... 46 2.3.1. Partial Writes.......................................................................................... 47 2.3.2. Append Mode ......................................................................................... 47 2.3.3. Nonblocking Writes ................................................................................
Recommended publications
  • Some Video Games Require a Operating System
    Some Video Games Require A Operating System Playing and woven Hiro never bail his trouncing! Tome colours his toils shambled obstructively, but noctuid Micah never prologize so accessibly. Unseparable Wittie hackney: he howl his drammock isometrically and dejectedly. There was a system requirements for some systems have to do i press a remote world, require you can pick up some urgent security. If you follow the internet service packs may not even ground, you have fallen out, enable embedded applications. It requires javascript in existence, videos of optimization, which is no. Are some systems? Vram do operating system. Sgi needed to some maintainers look around at different processes can. Your pc gamer, require some video games function checks for. Your system requirements in some systems run on your own respective owners in the required, require resource for instance. Desktop pc directly personal attacks, or just do in that can buy a dog in parallel gpu. Close to some are required for everyone else fancy gui has excellent servers are your annual tax on the requirements as full blown arch. Scroll on console or installation in your pc in beautiful places of. We will learn more, some time and capturing tools for bigger game? For residential clients, which disappeared from. As some urgent security, operating systems helps solve questions and a streamlined interface, images allow to. Api for server by default, it are done in more modern operating system? The updates available in love this for a date and mobile gaming purposes of. Fragmentation and secure a member only. Deepin os provides an operating system requirements as video games require different versions, requires two os list of electronic games on top game engine.
    [Show full text]
  • Huawei Announces EROFS Linux File-System, Might Eventually Be Used
    ARTICLES & REVIEWS NEWS ARCHIVE FORUMS PREMIUM CATEGORIES Custom Search Search Latest Linux News Huawei Announces EROFS Linux File-System, Might Huawei Announces EROFS Linux File- Eventually Be Used By Android Devices System, Might Eventually Be Used By Android Devices Written by Michael Larabel in Linux Storage on 31 May 2018 at 09:00 AM EDT. 3 Comments Mesa 18.0.5 Is The Last Planned Release In Huawei's Gao Xiang has announced the EROFS open-source Linux file-system The Series intended for Android devices, but still at its very early stages of AMD K8 Support Stripped Out Of Coreboot development. NVIDIA’s Next Generation Mainstream GPU Will At Least Be Detailed In August EROFS is the company's new approach for a read-only file-system that would work well for Android devices. EROFS is short for the Extendable Read-Only GNOME 3 Might Be Too Resource Hungry To File-System and they began developing it with being unsatisfied with other read-only file- Ever Run Nicely On The Raspberry Pi system alternatives. XWayland Gets Patch To Automatically Use EGLStreams For NVIDIA Support When EROFS is designed to offer better performance than other read-only alternatives while still Needed focusing upon saving storage space. As part of EROFS is also a compression mode pursuing BPFILTER Landing For Linux 4.18 For a different design approach than other file-systems: the compression numbers shared in Eventually Better Firewall / Packet Filtering today's announcement on both server hardware and a Kirin 970 are compelling for being in AMDGPU Patches Prepping JPEG Support For the early stages of development.
    [Show full text]
  • Android Operating System
    Software Engineering ISSN: 2229-4007 & ISSN: 2229-4015, Volume 3, Issue 1, 2012, pp.-10-13. Available online at http://www.bioinfo.in/contents.php?id=76 ANDROID OPERATING SYSTEM NIMODIA C. AND DESHMUKH H.R. Babasaheb Naik College of Engineering, Pusad, MS, India. *Corresponding Author: Email- [email protected], [email protected] Received: February 21, 2012; Accepted: March 15, 2012 Abstract- Android is a software stack for mobile devices that includes an operating system, middleware and key applications. Android, an open source mobile device platform based on the Linux operating system. It has application Framework,enhanced graphics, integrated web browser, relational database, media support, LibWebCore web browser, wide variety of connectivity and much more applications. Android relies on Linux version 2.6 for core system services such as security, memory management, process management, network stack, and driver model. Architecture of Android consist of Applications. Linux kernel, libraries, application framework, Android Runtime. All applications are written using the Java programming language. Android mobile phone platform is going to be more secure than Apple’s iPhone or any other device in the long run. Keywords- 3G, Dalvik Virtual Machine, EGPRS, LiMo, Open Handset Alliance, SQLite, WCDMA/HSUPA Citation: Nimodia C. and Deshmukh H.R. (2012) Android Operating System. Software Engineering, ISSN: 2229-4007 & ISSN: 2229-4015, Volume 3, Issue 1, pp.-10-13. Copyright: Copyright©2012 Nimodia C. and Deshmukh H.R. This is an open-access article distributed under the terms of the Creative Commons Attribution License, which permits unrestricted use, distribution, and reproduction in any medium, provided the original author and source are credited.
    [Show full text]
  • Implementation of an Orthogonally Persistent L4 -Kernel Based System
    Implementation of an Orthogonally Persistent L4 -Kernel Based System Christian Ceelen ÐÒÖºÙ º Supervisor: Cand. Scient. Espen Skoglund Universitat¨ Karlsruhe 15th February 2002 2 3 Abstract Orthogonal persistent systems open up possibilities for a wide number of appli- cations. Even more, it is a very natural concept for the storage of information, since objects and information persists until the end of their lifetime. Most current commercial non-persistent systems have only an explicit storage model. Thus, an application has to care for the persistent storage of data itself. This has to be done by transforming the data structures into something that can be stored within a file. Furthermore the file has to be opened, written to and saved explicitly; a source of overhead for programmers. Moreover the programmer also has to estimate the life-time of all valuable data. Including the conversion and recovery of data, the amount of code needed to store data explicitly could easily take up a third or half of the actual programming work. In order to support a convenient system environment, persistent storage could be handled implicitly by the operating system. The operating system has to store for each task an image of the user memory and all kernel internal data like page- tables, mapping structures, file descriptors and so on. This approach is very de- manding and very error-prone for current monolithic systems. Therefore we pro- pose a -kernel based system instead. The proposed work should provide an implementation base for further persistent systems by supplying the necessary mechanisms to build persistent applications on top of the -kernel.
    [Show full text]
  • Ivoyeur: Inotify
    COLUMNS iVoyeur inotify DAVE JOSEPHSEN Dave Josephsen is the he last time I changed jobs, the magnitude of the change didn’t really author of Building a sink in until the morning of my first day, when I took a different com- Monitoring Infrastructure bination of freeways to work. The difference was accentuated by the with Nagios (Prentice Hall T PTR, 2007) and is Senior fact that the new commute began the same as the old one, but on this morn- Systems Engineer at DBG, Inc., where he ing, at a particular interchange, I would zig where before I zagged. maintains a gaggle of geographically dispersed It was an unexpectedly emotional and profound metaphor for the change. My old place was server farms. He won LISA ‘04’s Best Paper off to the side, and down below, while my future was straight ahead, and literally under award for his co-authored work on spam construction. mitigation, and he donates his spare time to the SourceMage GNU Linux Project. The fact that it was under construction was poetic but not surprising. Most of the roads I [email protected] travel in the Dallas/Fort Worth area are under construction and have been for as long as anyone can remember. And I don’t mean a lane closed here or there. Our roads drift and wan- der like leaves in the water—here today and tomorrow over there. The exits and entrances, neither a part of this road or that, seem unable to anticipate the movements of their brethren, and are constantly forced to react.
    [Show full text]
  • I.MX Linux® Reference Manual
    i.MX Linux® Reference Manual Document Number: IMXLXRM Rev. 1, 01/2017 i.MX Linux® Reference Manual, Rev. 1, 01/2017 2 NXP Semiconductors Contents Section number Title Page Chapter 1 About this Book 1.1 Audience....................................................................................................................................................................... 27 1.1.1 Conventions................................................................................................................................................... 27 1.1.2 Definitions, Acronyms, and Abbreviations....................................................................................................27 Chapter 2 Introduction 2.1 Overview.......................................................................................................................................................................31 2.1.1 Software Base................................................................................................................................................ 31 2.1.2 Features.......................................................................................................................................................... 31 Chapter 3 Machine-Specific Layer (MSL) 3.1 Introduction...................................................................................................................................................................37 3.2 Interrupts (Operation)..................................................................................................................................................
    [Show full text]
  • Chapter 1. Origins of Mac OS X
    1 Chapter 1. Origins of Mac OS X "Most ideas come from previous ideas." Alan Curtis Kay The Mac OS X operating system represents a rather successful coming together of paradigms, ideologies, and technologies that have often resisted each other in the past. A good example is the cordial relationship that exists between the command-line and graphical interfaces in Mac OS X. The system is a result of the trials and tribulations of Apple and NeXT, as well as their user and developer communities. Mac OS X exemplifies how a capable system can result from the direct or indirect efforts of corporations, academic and research communities, the Open Source and Free Software movements, and, of course, individuals. Apple has been around since 1976, and many accounts of its history have been told. If the story of Apple as a company is fascinating, so is the technical history of Apple's operating systems. In this chapter,[1] we will trace the history of Mac OS X, discussing several technologies whose confluence eventually led to the modern-day Apple operating system. [1] This book's accompanying web site (www.osxbook.com) provides a more detailed technical history of all of Apple's operating systems. 1 2 2 1 1.1. Apple's Quest for the[2] Operating System [2] Whereas the word "the" is used here to designate prominence and desirability, it is an interesting coincidence that "THE" was the name of a multiprogramming system described by Edsger W. Dijkstra in a 1968 paper. It was March 1988. The Macintosh had been around for four years.
    [Show full text]
  • Seminar HPC Trends Winter Term 2017/2018 New Operating System Concepts for High Performance Computing
    Seminar HPC Trends Winter Term 2017/2018 New Operating System Concepts for High Performance Computing Fabian Dreer Ludwig-Maximilians Universit¨atM¨unchen [email protected] January 2018 Abstract 1 The Impact of System Noise When using a traditional operating system kernel When running large-scale applications on clusters, in high performance computing applications, the the noise generated by the operating system can cache and interrupt system are under heavy load by greatly impact the overall performance. In order to e.g. system services for housekeeping tasks which is minimize overhead, new concepts for HPC OSs are also referred to as noise. The performance of the needed as a response to increasing complexity while application is notably reduced by this noise. still considering existing API compatibility. Even small delays from cache misses or interrupts can affect the overall performance of a large scale In this paper we study the design concepts of het- application. So called jitter even influences collec- erogeneous kernels using the example of mOS and tive communication regarding the synchronization, the approach of library operating systems by ex- which can either absorb or propagate the noise. ploring the architecture of Exokernel. We sum- Even though asynchronous communication has a marize architectural decisions, present a similar much higher probability to absorb the noise, it is project in each case, Interface for Heterogeneous not completely unaffected. Collective operations Kernels and Unikernels respectively, and show suffer the most from propagation of jitter especially benchmark results where possible. when implemented linearly. But it is hard to anal- yse noise and its propagation for collective oper- ations even for simple algorithms.
    [Show full text]
  • The Linux Device File-System
    The Linux Device File-System Richard Gooch EMC Corporation [email protected] Abstract 1 Introduction All Unix systems provide access to hardware via de- vice drivers. These drivers need to provide entry points for user-space applications and system tools to access the hardware. Following the \everything is a file” philosophy of Unix, these entry points are ex- posed in the file name-space, and are called \device The Device File-System (devfs) provides a power- special files” or \device nodes". ful new device management mechanism for Linux. Unlike other existing and proposed device manage- This paper discusses how these device nodes are cre- ment schemes, it is powerful, flexible, scalable and ated and managed in conventional Unix systems and efficient. the limitations this scheme imposes. An alternative mechanism is then presented. It is an alternative to conventional disc-based char- acter and block special devices. Kernel device drivers can register devices by name rather than de- vice numbers, and these device entries will appear in the file-system automatically. 1.1 Device numbers Devfs provides an immediate benefit to system ad- ministrators, as it implements a device naming scheme which is more convenient for large systems Conventional Unix systems have the concept of a (providing a topology-based name-space) and small \device number". Each instance of a driver and systems (via a device-class based name-space) alike. hardware component is assigned a unique device number. Within the kernel, this device number is Device driver authors can benefit from devfs by used to refer to the hardware and driver instance.
    [Show full text]
  • Z/OS Distributed File Service Zseries File System Implementation Z/OS V1R13
    Front cover z/OS Distributed File Service zSeries File System Implementation z/OS V1R13 Defining and installing a zSeries file system Performing backup and recovery, sysplex sharing Migrating from HFS to zFS Paul Rogers Robert Hering ibm.com/redbooks International Technical Support Organization z/OS Distributed File Service zSeries File System Implementation z/OS V1R13 October 2012 SG24-6580-05 Note: Before using this information and the product it supports, read the information in “Notices” on page xiii. Sixth Edition (October 2012) This edition applies to version 1 release 13 modification 0 of IBM z/OS (product number 5694-A01) and to all subsequent releases and modifications until otherwise indicated in new editions. © Copyright International Business Machines Corporation 2010, 2012. All rights reserved. Note to U.S. Government Users Restricted Rights -- Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. Contents Notices . xiii Trademarks . xiv Preface . .xv The team who wrote this book . .xv Now you can become a published author, too! . xvi Comments welcome. xvi Stay connected to IBM Redbooks . xvi Chapter 1. zFS file systems . 1 1.1 zSeries File System introduction. 2 1.2 Application programming interfaces . 2 1.3 zFS physical file system . 3 1.4 zFS colony address space . 4 1.5 zFS supports z/OS UNIX ACLs. 4 1.6 zFS file system aggregates. 5 1.6.1 Compatibility mode aggregates. 5 1.6.2 Multifile system aggregates. 6 1.7 Metadata cache. 7 1.8 zFS file system clones . 7 1.8.1 Backup file system . 8 1.9 zFS log files.
    [Show full text]
  • Razvoj Mobilnih Aplikacija Za Android I Ios Pomoću C# Programskog Jezika
    SVEUČILIŠTE U ZAGREBU FAKULTET ORGANIZACIJE I INFORMATIKE V A R A Ž D I N Marta Fiorencis Razvoj mobilnih aplikacija za Android I iOS pomoću C# programskog jezika ZAVRŠNI RAD Varaždin, 2018. SVEUČILIŠTE U ZAGREBU FAKULTET ORGANIZACIJE I INFORMATIKE V A R A Ž D I N Marta Fiorencis Matični broj: 42010/13–R Studij: Informacijski sustavi Razvoj mobilnih aplikacija za Android i iOS pomoću c# programskog jezika ZAVRŠNI RAD Mentor/Mentorica: Doc. dr. sc. Zlatko Stapić Varaždin, rujan 2018. Marta Fiorencis Izjava o izvornosti Izjavljujem da je moj završni/diplomski rad izvorni rezultat mojeg rada te da se u izradi istoga nisam koristio drugim izvorima osim onima koji su u njemu navedeni. Za izradu rada su korištene etički prikladne i prihvatljive metode i tehnike rada. Autor/Autorica potvrdio/potvrdila prihvaćanjem odredbi u sustavu FOI-radovi _______________________________________________________________________ I Sažetak U radu će biti predstavljeno razvojno okruženje Xamarin, proći ću kroz postupak pripreme razvojnog okruženja poput instalacije, zatim ću obraditi koncept dijeljenja koda kroz Android, iOS i Windows te nakon toga ću predstaviti postupak kreiranja jednostavne fitness Android aplikacije. Tijekom postupka kreiranja aplikacije bit će demonstrirana izrada osnovnih funkcionalnosti za koju ću koristiti c# programski jezik. U praktičnom dijelu rada neću koristiti Xamarin studio već njegovu inačicu za Visual Studio. Cilj rada je predstavljanje izrade aplikacija u C# programskom jeziku, prezentiranje novog radnog okruženja i izražavanje subjektivnog mišljenja iz perspective korisnika. Ključne riječi: Xamarin, dijeljenje koda, C# programski jezik, Android aplikacija II Sadržaj 1. Uvod .................................................................................................................................. 4 2. Instalacija Xamarina u Visual Studio .................................................................................. 5 3. Koncept dijeljenja koda između platforma .......................................................................... 6 4.
    [Show full text]
  • A Study of Failure Recovery and Logging of High-Performance Parallel File Systems
    1 A Study of Failure Recovery and Logging of High-Performance Parallel File Systems RUNZHOU HAN, OM RAMESHWAR GATLA, MAI ZHENG, Iowa State University JINRUI CAO, State University of New York at Plattsburgh DI ZHANG, DONG DAI, North Carolina University at Charlotte YONG CHEN, Texas Tech University JONATHAN COOK, New Mexico State University Large-scale parallel file systems (PFSes) play an essential role in high performance computing (HPC). However, despite the importance, their reliability is much less studied or understood compared with that of local storage systems or cloud storage systems. Recent failure incidents at real HPC centers have exposed the latent defects in PFS clusters as well as the urgent need for a systematic analysis. To address the challenge, we perform a study of the failure recovery and logging mechanisms of PFSes in this paper. First, to trigger the failure recovery and logging operations of the target PFS, we introduce a black- box fault injection tool called PFault, which is transparent to PFSes and easy to deploy in practice. PFault emulates the failure state of individual storage nodes in the PFS based on a set of pre-defined fault models, and enables examining the PFS behavior under fault systematically. Next, we apply PFault to study two widely used PFSes: Lustre and BeeGFS. Our analysis reveals the unique failure recovery and logging patterns of the target PFSes, and identifies multiple cases where the PFSes are imperfect in terms of failure handling. For example, Lustre includes a recovery component called LFSCK to detect and fix PFS-level inconsistencies, but we find that LFSCK itself may hang or trigger kernel panicswhen scanning a corrupted Lustre.
    [Show full text]