S E C O N D E D I T I O N
W. RICHARD STEVENS
Interprocess C o m m u n i c a t i o n s
I
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.
shared memory, and Remote Procedure Calls (RPG.
Stevens heglns with a basic: introduction to IPC and the problems it is intended to solve
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:
-
you'll ever nee&
b
Pipes and FlFOs
Posix and System V Message Queues
POSIX and System V Semaphores
Poslx and System V Shared Me
Sbiaris Doors and Sun RPC
t -C
performance Measurements
of IPC Techn~ques
of UNlX Network Programming,
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.
1
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 S T E V E N S 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
boolLt clnt-control (CLIENT *d, unsigned int request, char *ptr) ;
CLIENT *clnt-create (const char *host, unsigned long prognum,
unsigned long versnum, const char *protocol) ;
void clnt-destroy(CL1ENT * c ! ) ;
Page
418
420
int door-bind ( int f d ) ;
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~createqroc *prOc);
384
390
int door-unbind(void);
void err-durn (const char * f i t , . . . ) ;
void err-msg (const char * f i t , . . . ) ;
void err-quit (const char * f i t , . . . ) ;
void err-rot (const char * f i t , . . . ) ;
void err-sys (const char * f i t , . . . ) ;
int fcntl (int f d , int cmd, . . . / * struct flock *arg * / ) ;
int fstat(int fd, struct stat *buf ) ;
key-t ftok(const char *pathname, int i d ) ;
int ftruncate (int f d , of f-t length) ;
int q c l o s e (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) ;
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) ;
77
52
Function prototype
int pthread-cancel (pthread-t tid) ;
void pthread-c leanupgop ( int execute) ;
void pthread-cleanupgush(void (*function) (void * ) , void *a%) ;
int pthreahcreate (pthread-t *tid, const pthread-attr-t *attr,
void * (*fu?tc) (void * ) , void *a%) ;
int pthread-detach (pthread-t tid) ;
void pthread-exit (void *status) ;
int pthread-join(pthread-t tid, void **status) ;
pthread-t pthread-self(void);
int pthread-condattr-destroy(pthread-condattr-t *attr);
int pthread-condattr-getpshared(const pthread-condattr-t 'attr, int *valptr) ;
int pthread-condattr-init(pthread-condattr-t *attr);
int pthread-condattr-setpshared(pthread-condattr-t *attr, int value);
int pthread-cond-broadcast(pthread-cond-t *cptr);
int pthreahcond-destroy(pthread-cond-t *cptr);
int pthread-cond-init(pthread-cond-t *cptr, const pthread-condattr-t *attr);
int pthread-cond-signal(pthread-cond-t
int pthread-cond-timedwait(pthread-cond-t *cptr, pthread-mutex-t *mptr,
*cptr);
const struct timespec *abstime) ;
int pthread-cond-wait(pthread~cond-t *cptr, pthread-mutex-t *mptr);
Page
187
187
187
502
504
504
503
503
172
173
172
173
171
172
172
167
171
167
int
int
int
int
int
int
int
int
int
int
int
int
int
int
int
int
int
int
int
int
pthread-mutaxattr-destroy(pthread-mutexattr-t *attr);
pthread-mutaxattr-getpsharrd ( const pthread-mutexattr-t *attr, int "valptr) ;
pthread-mutaxattr-init (pthread-mutexattr-t *attr) ;
pthread-mutaxattr-setpshared(pthread-mutexattr-t *attr, int value);
pthread-mutex-destroy(pthread-mutex-t *mptr);
pthread-mutex-init(pthread-mutex-t *mptr, const pthread-mutexattr-t *attr);
pthread~mutex~lock(pthread~mutex~t
*mptr) ;
*mptr);
pthrea~mutex~trylock(pthread~mutex~t
pthread~mutex~unlock(pthread~mutex~t *mptr);
pthread~rwlockattr~destroy(pthread~rwlockattr~t
pthread-rwlockattr-getpshared(const pthread-rwlockattr-t *attr, int *Va!ptr) ;
pthread-rwlockattr-init(pthread-rwlockattr-t *attr);
pthread-rwlockattr-setpshared (pthread-rwlockattr-t *attr, int value) ;
pthread-rwlock_destroy(pthread-rwlock-t *noptr);
pthread-rwlock-init (pthread-rwlock-t *noptr,
*attr);
const pthread-rwlockattr-t *attr);
pthread~rwlock~rdlock(pthread~rwlock~t
*noptr);
pthread~rwlock~tryrdlock(pthread~rwlock~t
pthrea~rwlock~unlock(pthread~rwlock~t
pthread~rwlock~trywr1ock(pthread~rwlock~t
*nu@');
* q t r ) ;
pthread~rw1ock~wrlock(pthread~rwlo~k~t
*noptr);
*noptr);
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. : 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.
QA76.76.063S755 1998
005.7' 12768--DC2 1
I. Title.
97-3 176 1
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: corpsales@prenhall.com; 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-081 081 -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
lntroduction
Chapter 1.
Posix IPC
Chapter 2.
Chapter 3.
System V IPC
Part 2. Message Passing
Chapter 4. Pipes and FlFOs
Chapter 5.
Chapter 6.
Posix Message Queues
System V Message Queues
Part 3. Synchronization
Chapter 7. Mutexes and Condition Variables
Chapter 8. Read-Write Locks
Chapter 9. Record Locking
Chapter 10.
Chapter 11.
Posix Semaphores
System V Semaphores
Part 4. Shared Memory
Chapter 12.
Chapter 13.
Chapter 14.
Shared Memory Introduction
Posix Shared Memory
System V Shared Memory
Part 5. Remote Procedure Calls
Chapter 15. Doors
Chapter 16.
Appendix A. Performance Measurements
Appendix 6. A Threads Primer
Appendix C. Miscellaneous Source Code
Appendix D.
Sun RPC
Solutions to Selected Exercises
1
41
43
75
1 29
157
159
1 77
193
21 9
28 1
301
303
325
343
353
355
399