The Linux Programmer’s Guide

  • View
    27

  • Download
    8

Embed Size (px)

DESCRIPTION

linux programming guide

Text of The Linux Programmer’s Guide

  • The Linux Programmers Guide

    Sven GoldtSven van der Meer

    Scott BurkettMatt Welsh

    Version 0.4March 1995

    0...Our continuing mission: to seek out knowledge of C, to explore strange unix

    commands, and to boldly code where no one has man page 4.

    As opposed to previous PDF versions of this guide, the present edition provides the convenience of hyperlinks and the readability of type 1 fonts. Special thanks to Han The Thanh for his pdfTeX program and to Sebastian Rahtz for his hyperref package! Unfortunately, I have not yet reviewed the entire document with regard to typos. Please feel free to send me your comments! - Ulrich Latzenhofer (latz@beer.com)

  • 2

  • Contents

    1 The Linux operating system 7

    2 The Linux kernel 9

    3 The Linux libc package 11

    4 System calls 13

    5 The swiss army knife ioctl 15

    6 Linux Interprocess Communications 176.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176.2 Half-duplex UNIX Pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

    6.2.1 Basic Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176.2.2 Creating Pipes in C . . . . . . . . . . . . . . . . . . . . . . . . . . 196.2.3 Pipes the Easy Way! . . . . . . . . . . . . . . . . . . . . . . . . . 236.2.4 Atomic Operations with Pipes . . . . . . . . . . . . . . . . . . . . 276.2.5 Notes on half-duplex pipes: . . . . . . . . . . . . . . . . . . . . . 27

    6.3 Named Pipes (FIFOs - First In First Out) . . . . . . . . . . . . . . . . . . . 276.3.1 Basic Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276.3.2 Creating a FIFO . . . . . . . . . . . . . . . . . . . . . . . . . . . 276.3.3 FIFO Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 286.3.4 Blocking Actions on a FIFO . . . . . . . . . . . . . . . . . . . . . 306.3.5 The Infamous SIGPIPE Signal . . . . . . . . . . . . . . . . . . . . 30

    6.4 System V IPC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306.4.1 Fundamental Concepts . . . . . . . . . . . . . . . . . . . . . . . . 306.4.2 Message Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . 326.4.3 Semaphores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466.4.4 Shared Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

    7 Sound Programming 697.1 Programming the internal speaker . . . . . . . . . . . . . . . . . . . . . . 697.2 Programming a sound card . . . . . . . . . . . . . . . . . . . . . . . . . . 69

    8 Character Cell Graphics 718.1 I/O Function in libc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

    8.1.1 Formatted Output . . . . . . . . . . . . . . . . . . . . . . . . . . . 728.1.2 Formatted Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

    8.2 The Termcap Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748.2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748.2.2 Find a Terminal Description . . . . . . . . . . . . . . . . . . . . . 758.2.3 Look at a Terminal Description . . . . . . . . . . . . . . . . . . . 758.2.4 Termcap Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . 76

    3

  • 4 CONTENTS

    8.3 Ncurses - Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808.4 Initializing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828.5 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828.6 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

    8.6.1 Formatted Output . . . . . . . . . . . . . . . . . . . . . . . . . . . 868.6.2 Insert Characters/Lines . . . . . . . . . . . . . . . . . . . . . . . . 868.6.3 Delete Characters/Lines . . . . . . . . . . . . . . . . . . . . . . . 868.6.4 Boxes and Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . 878.6.5 Background Character . . . . . . . . . . . . . . . . . . . . . . . . 88

    8.7 Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 888.7.1 Formated Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

    8.8 Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 898.8.1 Input Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 908.8.2 Terminal Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . 918.8.3 Use Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

    8.9 Clear Window and Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . 938.10 Updating the Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948.11 Video Attributes and Color . . . . . . . . . . . . . . . . . . . . . . . . . . 958.12 Cursor and Window Coordinates . . . . . . . . . . . . . . . . . . . . . . . 988.13 Scrolling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 998.14 Pads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1008.15 Soft-labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1018.16 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1018.17 Low-level Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1028.18 Screen Dump . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1028.19 Termcap Emulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1028.20 Terminfo Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1038.21 Debug Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1048.22 Terminfo Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

    8.22.1 Boolean Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . 1048.22.2 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1058.22.3 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

    8.23 [N]Curses Function Overview . . . . . . . . . . . . . . . . . . . . . . . . 112

    9 Programming I/O ports 1159.1 Mouse Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1169.2 Modem Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1179.3 Printer Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1179.4 Joystick Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

    10 Porting Applications to Linux 11910.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11910.2 Signal handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

    10.2.1 Signals under SVR4, BSD, and POSIX.1 . . . . . . . . . . . . . . 12010.2.2 Linux signal options . . . . . . . . . . . . . . . . . . . . . . . . . 12010.2.3 signal under Linux . . . . . . . . . . . . . . . . . . . . . . . . . . 12110.2.4 Signals supported by Linux . . . . . . . . . . . . . . . . . . . . . 121

    10.3 Terminal I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12110.4 Process information and control . . . . . . . . . . . . . . . . . . . . . . . 122

    10.4.1 kvm routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12210.4.2 ptrace and the /proc filesystem . . . . . . . . . . . . . . . . . . . . 12210.4.3 Process control under Linux . . . . . . . . . . . . . . . . . . . . . 122

    10.5 Portable conditional compilation . . . . . . . . . . . . . . . . . . . . . . . 12310.6 Additional Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

  • CONTENTS 5

    11 Systemcalls in alphabetical order 125

    12 Abbreviations 131

  • CopyrightThe Linux Programmers Guide is c 1994, 1995 by Sven GoldtSven Goldt, Sachsendamm 47b, 10829 Berlin, Germany< goldt@math.tu berlin.de > .Chapter 8 is c 1994, 1995 by Sven van der Meer< vdmeer@cs.tuberlin.de > .Chapter 6 is c 1995 Scott Burkett < scottb@IntNet.net > .Chapter 10 is c 1994, 1995 Matt Welsh < mdw@cs.cornell.edu > .Special thanks goes to John D. Harper < jharper@uiuc.edu > for proofreadingthis guide.Permission to reproduce this document in whole or in part is subject to the followingconditions:

    1. The copyright notice remains intact and is included.2. If you make money with it the authors want a share.3. The authors are not responsible for any harm that might arise by the use of it.

    PrefaceThis guide is far from being complete.The first release started at version 0.1 in September 1994. It concentrated on systemcalls because of lack of manpower and information. Planned are the description oflibrary functions and major kernel changes as well as excursions into important areaslike networking, sound, graphics and asynchronous I/O. Maybe some hints abouthow to build shared libraries and pointers to useful toolkits will later be included.This guide will only be a success with generous help in the form of information orperhaps even submission of whole chapters.

    IntroductionOnce upon a time I installed Linux on my PC to learn more about system administra-tion. I tried to install a slip server but it didnt work with shadow and mgetty. I hadto patch sliplogin and it worked until the new Linux 1.1 releases. No one could tellme what had happened. There was no documentation about changes since the 0.99kernel except the kernel change summaries from Russ Nelson, but they didnt helpme very much in solving problems.The Linux Programmers Guide is meant to do what the name implies It is to helpLinux programmers understand the peculiarities of Linux. By its nature, this alsomeans that it should be useful when porting programs from other operating systemsto Linux. Therefore, this guide must describe the system calls and the major kernelchanges which have effects on older programs like serial I/O and networking.

    Sven Goldt The L

Recommended

View more >