Advanced Programming in the UNIX Environment W. Richard Stevens

Similar documents
UNIT I Linux Utilities

UNIT I Linux Utilities and Working with Bash

Building blocks for Unix power tools

CptS 360 (System Programming) Unit 6: Files and Directories

Overview. Over the next four weeks, we will look at these topics: Building Blocks. Advanced Authentication Issues.

System BIOS for IBM PCs, Compatibles, and EISA Computers, Second Edition

Win32 Network Programming

St. MARTIN S ENGINEERING COLLEGE Dhulapally,Secunderabad DEPARTMENT OF INFORMATION TECHNOLOGY Academic year

CL020 - Advanced Linux and UNIX Programming

Files and Directories

Mathematica for Scientists and Engineers

INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad

Linux is obsolete 2.0

Introductory logic and sets for Computer scientists

Shell Execution of Programs. Process Groups, Session and Signals 1

Department of Computer Science and Technology, UTU 2014

An Introduction to Object-Oriented Programming

INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad

CS 33. Signals Part 2. CS33 Intro to Computer Systems XXIV 1 Copyright 2016 Thomas W. Doeppner. All rights reserved.

Programming with POSIX Threads

INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) DUNDIGAL, HYDERABAD

Chp1 Introduction. Introduction. Objective. Logging In. Shell. Briefly describe services provided by various versions of the UNIX operating system.

UNIX Network Programming

CONTENTS IN DETAIL 1 HISTORY AND STANDARDS 1

4. System Functions and Subroutines

Secure Software Programming and Vulnerability Analysis

RTEMS Filesystem Design Guide

Real-Time Systems and Programming Languages

CHETTINAD COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF MCA QUESTION BANK UNIT 1

History of FreeBSD. FreeBSD Kernel Facilities

UNIT I INTRODUCTION TO UNIX & FILE SYSTEM

read(2) There can be several cases where read returns less than the number of bytes requested:

INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad

Designing the User Interface

Noorul Islam College Of Engineering, Kumaracoil MCA Degree Model Examination (October 2007) 5 th Semester MC1642 UNIX Internals 2 mark Questions

Contents. Part 1. Introduction and TCP/IP 1. Foreword Preface. xix. I ntroduction 31

VALLIAMMAI ENGINEERING COLLEGE. SRM Nagar, Kattankulathur QUESTION BANK

Programming. In Ada JOHN BARNES TT ADDISON-WESLEY

Design Overview of the FreeBSD Kernel CIS 657

File System User API

Lecture 17. Log into Linux. Copy two subdirectories in /home/hwang/cs375/lecture17/ $ cp r /home/hwang/cs375/lecture17/*.

Dynamic Detection and Prevention of Race Conditions in File Accesses

(In columns, of course.)

The Design and Implementation of the 4.4BSD Operating System. Marshall Kirk McKusick Keith Bostic Michael J. Karels John S.

Reconstructive Software Archaeology Warren Toomey School of IT, Bond Uni

Design Overview of the FreeBSD Kernel. Organization of the Kernel. What Code is Machine Independent?

NATURAL LANGUAGE PROCESSING

DATABASE SYSTEM CONCEPTS

Data Structures in C++ Using the Standard Template Library

CSC209 Review. Yeah! We made it!

Files and Directories

Babu Madhav Institute of Information Technology, UTU

II/IV B.Tech. DEGREE EXAMINATIONS, NOV/DEC-2017

Advanced Unix Concepts. Satyajit Rai

Automatic Text Processing

Introduction to Unix. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Application Programming

COMPUTER AND ROBOT VISION

ADVANCED OPERATING SYSTEMS

Mastering Linux. Paul S. Wang. CRC Press. Taylor & Francis Group. Taylor & Francis Croup an informa business. A CHAPMAN St HALL BOOK

The Unified Modeling Language User Guide

File I/O - Filesystems from a user s perspective

Architectural Support for Operating Systems. Jinkyu Jeong ( Computer Systems Laboratory Sungkyunkwan University

Virtual File System. Don Porter CSE 306

An Introduction to Search Engines and Web Navigation

Sistemas Operativos /2016 Support Document N o 1. Files, Pipes, FIFOs, I/O Redirection, and Unix Sockets

Files and Directories Filesystems from a user s perspective

OPERATING SYSTEMS OVERVIEW. Operating Systems 2015 Spring by Euiseong Seo

Architectural Support for OS

Architectural Support for Operating Systems. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Operating Systems 2010/2011

Dr. Rafiq Zakaria Campus. Maulana Azad College of Arts, Science & Commerce, Aurangabad. Department of Computer Science. Academic Year

Overview. Last Lecture. This Lecture. Daemon processes and advanced I/O functions

Process. Program Vs. process. During execution, the process may be in one of the following states

RCU. ò Dozens of supported file systems. ò Independent layer from backing storage. ò And, of course, networked file system support

Unix System Programming - Chapter 8

Contents. * All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

11/3/71 SYS BREAK (II)

Chapter 8: I/O functions & socket options

Standards / Extensions C or C++ Dependencies POSIX.1 XPG4 XPG4.2 Single UNIX Specification, Version 3

CS631 - Advanced Programming in the UNIX Environment

Mastering OSF/Motif Widgets

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

Integrated Approach. Operating Systems COMPUTER SYSTEMS. LEAHY, Jr. Georgia Institute of Technology. Umakishore RAMACHANDRAN. William D.

Interprocess Communication. Originally multiple approaches Today more standard some differences between distributions still exist

Subject: Operating System (BTCOC403) Class: S.Y.B.Tech. (Computer Engineering)

INTRODUCTION TO THE UNIX FILE SYSTEM 1)

Reconstructive Software

Unix Systems Programming In a Nutshell

CS631 - Advanced Programming in the UNIX Environment. Dæmon processes, System Logging, Advanced I/O

CSCI 4500/8506 Operating Systems Some UNIX System Calls, Library, and PThreads Functions

for Operating Systems Computer Systems Laboratory Sungkyunkwan University

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

Appendix A: FreeBSD. Operating System Concepts 9 th Edition

VI SEMESTER. MANAGEMENT AND ENTREPRENEURSHIP (Common to All Branches)

Computers as Components Principles of Embedded Computing System Design

Advanced Unix/Linux System Program. Instructor: William W.Y. Hsu

What is an Operating System? A Whirlwind Tour of Operating Systems. How did OS evolve? How did OS evolve?

Basic POSIX signal concepts

Transcription:

Advanced Programming in the UNIX Environment W. Richard Stevens ADDISON-WESLEY PUBLISHING COMPANY Reading, Massachusetts Menlo Park, California New York Don Mills, Ontario Wokingham, England Amsterdam Bonn Sydney Singapore Tokyo Madrid San Juan Paris Seoul Milan Mexico City Taipei

Preface Chapter 1. 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 1.10 1.11 1.12 Chapter 2. 2.1 2.2 2.3 Introduction Introduction 1 Logging In 1 Files and Directories 3 Input and Output 6 Programs and Processes 9 ANSI C Features 12 Error Handling 14 User Identification 16 Signals 17 Unix Time Values 19 System Calls and Library Functions 20 Summary 23 Unix Standardization and Implementations Introduction 25 Unix Standardization 25 2.2.1 ANSI C 25 2.2.2 IEEE POSIX 26 2.2.3 X/Open XPG3 28 2.2.4 FIPS 28 Unix Implementations 28 2.3.1 System V Release 4 2.3.2 4.3+BSD 29 29 xv 1 25 Vll

V1U 2.4 Relationship of Standards and Implementations 30 2.5 Limits 30 2.5.1 ANSI C Limits 31 2.5.2 POSIX Limits 32 2.5.3 XPG3 Limits 34 2.5.4 sysconf, pathconf, and fpathconf Functions 34 2.5.5 FIPS 151-1 Requirements 39 2.5.6 Summary of Limits 41 2.5.7 Indeterminate Run-Time Limits 41 2.6 Feature Test Macros 44 2.7 Primitive System Data Types 44 2.8 Conflicts Between Standards 45 2.9 Summary 46 Chapter 3. 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 File I/O Introduction 47 File Descriptors open Function creat Function close Function lseek Function read Function write Function I/O Efficiency File Sharing 47 48 50 51 51 54 55 55 56 Atomic Operations 60 dup and dup2 Functions fcntl Function 63 ioctl Function 67 /dev/fd 69 Summary 70 61 47 Chapter 4. Files and Directories 4.1 Introduction 73 4.2 stat, fstat, and lstat Functions 4.3 File Types 74 4.4 Set-User-ID and Set-Group-ID 77 4.5 File Access Permissions 78 4.6 Ownership of New Files and Directories 4.7 access Function 82 4.8 umask Function 83 4.9 chmod and fchmod Functions 85 4.10 Sticky Bit 88 4.11 chown, fchown, and lchown Functions 73 81 89 73

4.12 4.13 4.14 4.15 4.16 4.17 4.18 4.19 4.20 4.21 4.22 4.23 4.24 4.25 4.26 File Size 90 File Truncation 91 Filesystems 92 link, unlink, remove, and rename Functions Symbolic Links 99 symlink and readlink Functions 102 File Times 102 utime Function 103 mkdir and rmdir Functions 106 Reading Directories 107 chdir, fchdir, and getcwd Functions 112 Special Device Files 114 sync and fsync Functions 116 Summary of File Access Permission Bits Summary 118 117 95 Chapter 5. 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 5.10 5.11 5.12 5.13 5.14 5.15 Chapter 6. 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 6.10 Standard I/O Library Introduction 121 Streams and FILE Objects 121 Standard Input, Standard Output, and Standard Error Buffering 122 Opening a Stream 125 Reading and Writing a Stream 127 Line-at-a-Time I/O 130 Standard I/O Efficiency 131 Binary I/O 133 Positioning a Stream 135 Formatted I/O 136 Implementation Details 138 Temporary Files 140 Alternatives to Standard I/O 143 Summary 143 System Data Files and Information Introduction 145 Password File 145 Shadow Passwords 148 Group File 149 Supplementary Group IDs 150 Other Data Files 152 Login Accounting 153 System Identification 154 Time and Date Routines 155 Summary 159 121 122 145

Chapter 7. Chapter 8. Chapter 9. The Environment of a Unix Process 7.1 Introduction 161 7.2 main Function 161 7.3 Process Termination 162 7.4 Command-Line Arguments 165 7.5 Environment List 166 7.6 Memory Layout of a C Program 167 7.7 Shared Libraries 169 7.8 Memory Allocation 169 7.9 Environment Variables 172 7.10 setjmp and longjmp Functions 174 7.11 getrlimit and setrlimit Functions 180 7.12 Summary 184 Process Control 8.1 Introduction 187 8.2 Process Identifiers 187 8.3 fork Function 188 8.4 vfork Function 193 8.5 exit Functions 195 8.6 wait and waitpid Functions 197 8.7 wait3 and wait4 Functions 202 8.8 Race Conditions 203 8.9 exec Functions 207 8.10 Changing User IDs and Group IDs 213 8.11 Interpreter Files 217 8.12 system Function 221 8.13 Process Accounting 226 8.14 User Identification 231 8.15 Process Times 232 8.16 Summary 235 Process Relationships 9.1 Introduction 237 9.2 Terminal Logins 237 9.3 Network Logins 241 9.4 Process Groups 243 9.5 Sessions 244 9.6 Controlling Terminal 246 9.7 tcgetpgrp and tcsetpgrp Functions 247 9.8 Job Control 248 9.9 Shell Execution of Programs 252 161 187 237

XI 9.10 Orphaned Process Groups 256 9.11 4.3+BSD Implementation 259 9.12 Summary 261 Chapter 10. Signals 263 10.1 Introduction 263 10.2 Signal Concepts 263 10.3 signal Function 270 10.4 Unreliable Signals 274 10.5 Interrupted System Calls 275 10.6 Reentrant Functions 278 10.7 SIGCLD Semantics 279 10.8 Reliable Signal Terminology and Semantics 282 10.9 kill and raise Functions 283 10.10 alarm and pause Functions 285 10.11 Signal Sets 291 10.12 sigprocmask Function 292 10.13 sigpending Function 293 10.14 sigaction Function 296 10.15 sigsetjmp and siglongjmp Functions 299 10.16 sigsuspend Function 303 10.17 abort Function 309 10.18 system Function 310 10.19 sleep Function 317 10.20 Job-Control Signals 319 10.21 Additional Features 320 10.22 Summary 323 Chapter 11. Terminal I/O 325 11.1 Introduction 325 11.2 Overview 325 11.3 Special Input Characters 331 11.4 Getting and Setting Terminal Attributes 335 11.5 Terminal Option Flags 336 11.6 stty Command 342 11.7 Baud Rate Functions 343 11.8 Line Control Functions 344 11.9 Terminal Identification 345 11.10 Canonical Mode 349 11.11 Noncanonical Mode 352 11.12 Terminal Window Size 358 11.13 termcap, terminfo, and curses 360 11.14 Summary 360

XU Chapter 12. Advanced I/O 363 12.1 12.2 12.3 12.4 12.5 12.6 12.7 12.8 12.9 12.10 Introduction 363 Nonblocking I/O 363 Record Locking 367 Streams 383 I/O Multiplexing 394 12.5.1 select Function 396 12.5.2 poll Function 400 Asynchronous I/O 402 12.6.1 System V Release 4 403 12.6.2 4.3+BSD 403 readv and writev Functions 404 readn and writen Functions 406 Memory Mapped I/O 407 Summary 413 Chapter 13. 13.1 13.2 13.3 13.4 13.5 13.6 Daemon Processes Introduction 415 Daemon Characteristics 415 Coding Rules 417 Error Logging 418 13.4.1 SVR4 Streams log Driver 13.4.2 4.3+BSD syslog Facility Client-Server Model 424 Summary 424 419 421 415 Chapter 14. Interprocess Communication 14.1 Introduction 427 14.2 Pipes 428 14.3 popen and pclose Functions 435 14.4 Coprocesses 441 14.5 FIFOs 445 14.6 System V IPC 449 14.6.1 Identifiers and Keys 449 14.6.2 Permission Structure 450 14.6.3 Configuration Limits 451 14.6.4 Advantages and Disadvantages 14.7 Message Queues 453 14.8 Semaphores 457 14.9 Shared Memory 463 14.10 Client-Server Properties 470 14.11 Summary 472 451 427

xiu Chapter 15. Chapter 16. Chapter 17. Chapter 18. Advanced Interprocess Communication 15.1 Introduction 475 15.2 Stream Pipes 475 15.3 Passing File Descriptors 479 15.3.1 System V Release 4 481 15.3.2 4.3BSD 484 15.3.3 4.3+BSD 487 15.4 An Open Server, Version 1 490 15.5 Client-Server Connection Functions 496 15.5.1 System V Release 4 497 15.5.2 4.3+BSD 501 15.6 An Open Server, Version 2 505 15.7 Summary 514 A Database Library 16.1 Introduction 515 16.2 History 515 16.3 The Library 516 16.4 Implementation Overview 518 16.5 Centralized or Decentralized? 521 16.6 Concurrency 522 16.7 Source Code 524 16.8 Performance 545 16.9 Summary 550 Communicating with a PostScript Printer 17.1 Introduction 551 17.2 PostScript Communication Dynamics 551 17.3 Printer Spooling 554 17.4 Source Code 556 17.5 Summary 578 A Modem Dialer 18.1 Introduction 579 18.2 History 579 18.3 Program Design 580 18.4 Data Files 582 18.5 Server Design 584 18.6 Server Source Code 586 18.7 Client Design 615 18.8 Client Source Code 617 18.9 Summary 629 475 515 551 579

XIV Chapter 19. 19.1 19.2 19.3 19.4 19.5 19.6 19.7 19.8 Appendix A. Appendix B. B.1 B.2 Appendix C. Bibliography Index Pseudo Terminals Introduction 631 Overview 631 Opening Pseudo-Terminal Devices 636 19.3.1 System V Release 4 638 19.3.2 4.3+BSD 640 pty_fork Function 641 pty Program 644 Using the pty Program 648 Advanced Features 655 Summary 656 Function Prototypes Miscellaneous Source Code Our Header File 679 Standard Error Routines 681 Solutions to Selected Exercises 631 659 679 687 713 719