S E C O N D E D I T I O N W. RICHARD STEVENS I Interprocess Communications Well-implemented interprocess communications (IPC) are key to the performance of virtually every non-trivial UNIX program. In UNIX Network Programming, Udnme 2, Seeond Edition, legendary UNIX expert W . Richard Stevens presents a comprehensive guide to every form of IPC. including message passing. synchronization. you'll ever nee& sh-are d memory, and Remote Procedure Calls (RPG. Stevens heglns with a basic: introduction to IPC and the problems it is intended to solve b Slep-by-step you'll learn how to maximize both System V IPC and the new Posix standards. which offer dramatic improvements in convenience and performance. You'll find extensive coverage of Pthreads. with many examples rrflecting multiple threads instead of multiple processes. Alongthe way. you'll master every current IPC technique and technology. including: Pipes and FlFOs POSIXan d System V Semaphores Posix and System V Message Queues Poslx and System V Shared Me St b-Ci aris Doors and Sun RPC performance Measurements of IPC Techn~ques of UNlX Network Programming, 1 of five! You won't just learn about IPC "from the outside." You'll actually create implemkntatiom of Posix message queues. read-write locks, and semaphores, gaining an in-de@h understanding of these capabilities you simply can't get awwhere else. The book contains extensive new source code--all caqfully optimized and available on the Web. You'll even find a complete guide to mesuring IPC performance with message passing bandwidth and laterfcy progrMns, and thread and process synchronization programs. The better you understand IPC. the better your UNIX software will run. This book contains all you need to know. I ABOUT THE AUTHOR W. RlCHAR D STEVENS is author of UNIX Network Programming. Firs widely recognized as the classic text in UNIX networking and UNIX Net Programming, Wume 1. Second Edition. He is also author of Advanced in the UNIX Environment and the TCP/IP IIIustrated Series. Stevens is an acknowledged UNIX and networking expert. sought-after instructor, and occamnal consultant. PRENTICE HALL Upper Saddle River. NJ 07458 Function prototype Page boolLt clnt-control( CLIENT *d, unsigned int request, char *ptr); 418 CLIENT *clnt-create (const char *host, unsigned long prognum, unsigned long versnum, const char *protocol); void clnt-destroy(CL1ENT *c!); 420 int door-bind ( int fd) ; 390 int door-call ( int fd, door-arg-t *argp); 361 int door-create( Door-serverqroc *proc, void *cookie, u-int attr) ; 363 int door-cred (door-cred-t *cred); 365 int door-info( int fd, door-in£ o-t *info); 365 int dooryeturn( char *dataptr, size-t datasize, door-desc-t *descptr. size-t ndesc) ; 365 int door-revoke (int fd) ; 390 Door-createqroc *door~server~create(Door~createqro*pcr Oc); 384 int door-unbind(void); 390 void err-durn (const char *fit, . . . ) ; void err-msg (const char *fit, . . . ) ; void err-quit (const char *fit, . . . ) ; void err-rot (const char *fit, . . . ) ; void err-sys (const char *fit, . . . ) ; int fcntl( int fd, int cmd, . . . /* struct flock *arg */ ) ; int fstat(int fd, struct stat *buf ); key-t ftok(const char *pathname, int id); int ftruncate( int fd, of f-t length) ; int qclose( mqd-t mqdes); int qgetattr (mqd-t mqdes, struct mpattr *attr); int -notify (mqd-t mqdes, const struct sigevent *notification) ; mqd-t qopen(const char *name, int oflag, . . . /* mode-t mode, struct mq-attr *attr */ ). int -unlink (const char *name) ; 77 int msgctl(int msqid, int cmd, struct msqid-ds *buff); int msgget( key-t key, int oflag); FILE *popen (const char *command, const char *type) ; 52 Function prototype Page int pthread-cancel (pthread-t tid); 187 void pthread-c leanupgop ( int execute) ; 187 void pthread-cleanupgush(void (*function)( void *), void *a%); 187 int pthreahcreate( pthread-t *tid, const pthread-attr-t *attr, void * (*fu?tc() void *), void *a%); 502 int pthread-detach (pthread-t tid); 504 void pthread-exit (void *status); 504 int pthread-join(pthread-t tid, void **status) ; 503 pthread-t pthread-self(void); 503 int pthread-condattr-destroy(pthread-condattr-t *attr); 172 int pthread-condattr-getpshared(const pthread-condattr-t 'attr, int *valptr); 173 int pthread-condattr-init(pthread-condattr-t *attr); 172 int pthread-condattr-setpshared(pthread-condattr-t *attr, int value); 173 int pthread-cond-broadcast(pthread-cond-t *cptr); 171 int pthreahcond-destroy(pthread-cond-t *cptr); 172 int pthread-cond-init(pthread-cond-t *cptr, const pthread-condattr-t *attr); 172 int pthread-cond-signal(pthread-cond-t *cptr); 167 int pthread-cond-timedwait(pthread-cond-t *cptr, pthread-mutex-t *mptr, const struct timespec *abstime); 171 int pthread-cond-wait(pthread~cond-t *cptr, pthread-mutex-t *mptr); 167 int pthread-mutaxattr-destroy(pthread-mutexattr-t *attr); int pthread-mutaxattr-getpsharrd ( const pthread-mutexattr-t *attr, int "valptr); int pthread-mutaxattr-init (pthread-mutexattr-t *attr); int pthread-mutaxattr-setpshared(pthread-mutexattr-t *attr, int value); int pthread-mutex-destroy(pthread-mutex-t *mptr); int pthread-mutex-init(pthread-mutex-t *mptr, const pthread-mutexattr-t *attr); int pthread~mutex~lock(pthread~mutex*~mtp tr) ; int pthrea~mutex~trylock(pthread~mutex*~mtp tr); int pthread~mutex~unlock(pthread~mutex*~mtp tr); int pthread~rwlockattr~destroy(pthread~rwlockatt*ra~tttr ); int pthread-rwlockattr-getpshared(const pthread-rwlockattr-t *attr, int *Va!ptr); int pthread-rwlockattr-init(pthread-rwlockattr-t *attr); int pthread-rwlockattr-setpshared (pthread-rwlockattr-t *attr, int value); int pthread-rwlock_destroy(pthread-rwlock-t *noptr); int pthread-rwlock-init (pthread-rwlock-t *noptr, const pthread-rwlockattr-t *attr); int pthread~rwlock~rdlock(pthread~rwlock*~nto ptr); int pthread~rwlock~tryrdlock(pthread~rwlock*~nto ptr); int pthread~rwlock~trywr1ock(pthread~rwloc*kn~otp tr); int pthrea~rwlock~unlock(pthread~rwloc*kn~ut@ '); int pthread~rw1ock~wrlock(pthread~rwlo*~qk~ttr ); UNIX Network Programming Volume 2 Second Edition lnterprocess Communications by W. Richard Stevens ISBN 0-23-082082-9 Prentice Hall PTR Upper Saddle River, NJ 07458 www.phptr.com Library of Congress Cataloging-in-Publication Data Stevens, W. Richard. UNIX network programming I by W. Richard Stevens. -- 2nd ed. v. <l > : ill. : 25 cm. Includes bibliographical references and index. Contents: v. 1. Networking APIs : sockets and XTI. ISBN 0-13-490012-X (v. 1) 1. UNIX (Computer file) 2. Computer networks. 3. Internet programming. I. Title. QA76.76.063S755 1998 005.7' 12768--DC21 97-31 761 Editorial/production supervision: Patti Guerrieri Cover design director: Jerry Votta Cover designer: Scott Weiss Manufacturing manager: Alexis R. Heydl Marketing manager: Miles Williams Acquisitions editor: Mary Franz Editorial assistant: Noreen Regina 01999 by Prentice Hall PTR Prentice-Hall, Inc. A Simon & Schuster Company Upper Saddle River, NJ 07458 Prentice Hall books are widely used by corporations and government agencies for training, marketing, and resale. The publisher offers discounts on this book when ordered in bulk quantities. For more information, contact: Corporate Sales Department, Phone: 800-382-3419; Fax: 201-236-7141; E-mail: [email protected]; or write: Prentice Hall PTR, Corp. Sales Dept., One Lake Street, Upper Saddle River, NJ 07458. All products or services mentioned in this book are the trademarks or service marks of their respective companies or organizations. All rights reserved. No part of this book may be reproduced, in any form or by any means, without permission in writing from the publisher. Printed in the United States of America 1 0 9 8 7 6 5 4 3 2 ISBN 0-1 3-0810 81 -9 Prentice-Hall lntemational (UK) Limited, London Prentice-Hall of Australia Pty. Limited, Sydney Prentice-Hall Canada Inc., Toronto Prentice-Hall Hispanoamericana, S.A., Mexico Prentice-Hall of India Private Limited, New Delhi Prentice-Hall of Japan, Inc., Tokyo Simon & Schuster Asia Pte. Ltd., Singapore Editora Prentice-Hall do Brasil, Ltda., Rio de Janeiro To the Usenet community; for many questions answered, and many FAQs provided. Abbreviated Table of Contents Part 1. Introduction 1 Chapter 1. lntroduction Chapter 2. Posix IPC Chapter 3. System V IPC Part 2. Message Passing 41 Chapter 4. Pipes and FlFOs 43 Chapter 5. Posix Message Queues 75 Chapter 6. System V Message Queues 12 9 Part 3. Synchronization 157 Chapter 7. Mutexes and Condition Variables 159 Chapter 8. Read-Write Locks 17 7 Chapter 9. Record Locking 193 Chapter 10. Posix Semaphores 219 Chapter 11. System V Semaphores 28 1 Part 4. Shared Memory 301 Chapter 12. Shared Memory Introduction 303 Chapter 13. Posix Shared Memory 325 Chapter 14. System V Shared Memory 343 Part 5. Remote Procedure Calls 353 Chapter 15. Doors 355 Chapter 16. Sun RPC 399 Appendix A. Performance Measurements Appendix 6. A Threads Primer Appendix C. Miscellaneous Source Code Appendix D. Solutions to Selected Exercises Table of Contents Preface xiii Part 1. lntroduction 1 Chapter 1. lntroduction 1.1 lntroduction 3 1.2 Processes, Threads, and the Sharing of Information 5 1.3 Persistence of IPC Objects 6 1.4 Name Spaces 7 1.5 Effect of fork, exec, and exit on IPC Objects 9 1.6 Error Handling: Wrapper Functions 11 1.7 Unix Standards 13 1.8 Road Map to IPC Examples in the Text 15 1.9 Summary 16 Chapter 2. Posix IPC 2.1 lntroduction 19 2.2 IPC Names 19 2.3 Creating and Opening IPC Channels 22 2.4 IPC Permissions 25 2.5 Summary 26 vii viii UNIX Network Programming Contents Chapter 3. System V IPC 3.1 lntroduction 27 3.2 key-t Keys and ftok Function 28 3.3 i p c q e r m Structure 30 3.4 Creating and Opening IPC Channels 30 3.5 IPC Permissions 32 3.6 Identifier Reuse 34 3.7 ipcs and ipcrm Programs 36 3.8 Kernel Limits 36 3.9 Summary 38 Part 2. Message Passing 41 Chapter 4. Pipes and FlFOs 4.1 lntroduction 43 4.2 A Simple Client-Server Example 43 4.3 Pipes 44 4.4 Full-Duplex Pipes 50 4.5 popen and pclose Functions 52 4.6 FlFOs 54 4.7 Additional Properties of Pipes and FlFOs 58 4.8 One Server, Multiple Clients 60 4.9 Iterative versus Concurrent Servers 66 4.10 Streams and Messages 67 4.1 1 Pipe and FIFO Limits 72 4.1 2 Summary 73 Chapter 5. Posix Message Queues 5.1 lntroduction 75 5.2 maopen, maclose, and mxunlink Functions 76 5.3 m a g e t a t t r and m a s e t a t t r Functions 79 5.4 masend and mareceive Functions 82 5.5 Message Queue Limits 86 5.6 m a n o ti fy Function 87 5.7 Posix Realtime Signals 98 5.8 Implementation Using Memory-Mapped 110 106 5.9 Summary 126 Chapter 6. System V Message Queues 6.1 lntroduction 129 6.2 msgget Function 130 6.3 msgsnd Function 131 6.4 msgrcv Function 132 6.5 msgctl Function 134 6.6 Simple Programs 135 6.7 Client-Server Example 140 6.8 Multiplexing Messages 142