Rite logo
Solaris Systems Programming
Contents
Introduction
Fundamental Topics
Input/Output
Processes and Process
Control
Interprocess Communication
  Doors
Pseudo Terminals
  Index

Table of Contents



List of Programsxix

List of Figuresxxvii

Prefacexxxi


Part 1Introduction1

Chapter 1Introduction3
1.1Introduction3
1.2Logging In5
1.4Shells6
1.3Files, Directories, and File Systems7
1.5Input and Output12
1.6Programs, Processes, and Threads16
1.7Error Handling21
1.8User Identification22
1.9Signals24
1.10UNIX Time Values27
1.11System Calls and Library Functions28
1.12Introduction to 64-Bit Programming31
1.12.1    Writing 64-Bit Clean Programs32
1.12.2    Compiling and Installing 64-Bit Programs38
1.12.3    The Large File Compilation Environment39
1.12.4    The Transitional Large File Compilation Environment41
1.13Summary42

Chapter 2A Brief History of Solaris45
2.1Introduction45
2.2The Early Days: SunOS45
2.3Beyond SunOS: Solaris46
2.4Standards47
2.4.1    ANSI/ISO/IEC C47
2.4.2    System V Interface Definition48
2.4.3    IEEE POSIX48
2.4.4    The Open Group's XPG450
2.4.5    The Single UNIX Specification50
2.5Solaris 2.552
2.5.1    Solaris 2.5.152
2.6Solaris 2.653
2.7Solaris 753
2.8Solaris 853
2.9Solaris 954
2.10Solaris Standards Compliance54
2.11Compiling Standards Conforming Applications55
2.12Summary55


Part 2Fundamental Topics57

Chapter 3Utility Functions59
3.1Introduction59
3.2Manipulating Character Classes60
3.2.1    Testing Character Class Membership60
3.2.2    Changing Character Class Membership63
3.2.3    Summary of Character Classes64
3.3Manipulating Character Strings65
3.3.1    Finding the Length of a String66
3.3.2    Comparing Strings67
3.3.3    String Concatenation71
3.3.4    Copying Strings73
3.3.5    String Searching Functions74
3.3.6    Duplicating Strings80
3.3.7    Splitting a String into Tokens81
3.3.8    Functions for Transforming Strings84
3.3.9    Converting Strings to Numbers87
3.3.10  Converting Numbers to Strings91
3.4Manipulating Byte Arrays91
3.4.1    Comparing Byte Arrays92
3.4.2    Copying Byte Arrays92
3.4.3    Searching Byte Arrays93
3.4.4    Initializing Byte Arrays94
3.5Dynamic Memory94
3.5.1    Memory Alignment94
3.5.2    Allocating Dynamic Memory96
3.5.3    Freeing Dynamic Memory99
3.6Dynamic Memory100
3.6.1    The malloc Library100
3.6.2    The bsdmalloc Library102
3.6.3    The mapmalloc Library103
3.6.4    The watchmalloc Shared Object103
3.6.5    Comparing the malloc Libraries106
3.7Temporary Files108
3.7.1    Generating Temporary Filenames108
3.7.2    Creating Temporary Files109
3.8Parsing Command Line Arguments110
3.9Error Reporting117
3.10Suspending a Process119
3.11Summary120

Chapter 4Basic File I/O123
4.1Introduction123
4.2File Descriptors123
4.3The open Function124
4.4The creat Function126
4.5The close and closefrom Functions127
4.6The lseek and llseek Functions128
4.7The tell Function131
4.8The read and pread Functions131
4.9The write and pwrite Functions132
4.10The readn and writen Functions134
4.11I/O Efficiency135
4.12File Sharing136
4.13Atomic Operations139
4.14The dup and dup2 Functions140
4.15The fcntl Function142
4.16The ioctl Function153
4.17The fdwalk Function153
4.18Direct I/O155
4.19The /dev/fd File System156
4.20Summary158

Chapter 5The Standard I/O Library159
5.1Introduction159
5.2File Streams, Data Types, and Constants159
5.3Standard Input, Standard Output, and Standard Error160
5.4Opening a File Stream161
5.5Closing a File Stream163
5.6Reading and Writing163
5.6.1    Character Input Functions164
5.6.2    Character Output Functions165
5.6.3    Line Input Functions166
5.6.4    Line Output Functions167
5.6.5    Binary I/O167
5.7Stream Status167
5.8Formatted I/O170
5.8.1    Formatted Output170
5.8.2    Formatted Input172
5.8.3    Format Conversion Specifications173
5.8.4    C Language Escape Sequences183
5.9Positioning a Stream184
5.10File Stream Locking185
5.10.1  Unlocked File Stream I/O189
5.11Buffering191
5.12Standard I/O Efficiency194
5.13Summary199

Chapter 6Date and Time Operations201
6.1Introduction201
6.2The Complexities of Converting Time201
6.3Getting the Current Time203
6.3.1    The difftime Function205
6.4Setting the Current Time205
6.5Getting the Current Time Zone206
6.6Converting between UNIX Time and Calendar Time207
6.6.1    The localtime and localtime_r Functions209
6.6.2    The gmtime and gmtime_r Functions210
6.6.3    The mktime Function210
6.7Formatted Date I/O212
6.7.1    Converting a Date to a Formatted String213
6.7.2    Converting a Formatted String to a Date217
6.8Summary222

Chapter 7Users and Groups223
7.1Introduction223
7.2User Names223
7.3User IDs227
7.4Group IDs231
7.5Group Membership234
7.6The Password File237
7.7The Shadow Password File243
7.8Reading and Encrypting Passwords250
7.9The Group File256
7.10The utmpx and wtmpx Files261
7.11The utmp and wtmp Files268
7.12The lastlog File269
7.13The shells File270
7.14Summary273

Chapter 8System Information and Resource Limits275
8.1Introduction275
8.2System Information and Identification276
8.3System Resource Limits282
8.4Per-Process Resource Limits297
8.5The Resource Control Facility302
8.6Resource Control Examples311
8.7Resource Usage Information319
8.8Determining Resource Usage Using the /proc File System325
8.9Determining the System's Load Average334
8.10Summary335

Chapter 9Secure C Programming337
9.1Introduction337
9.2Buffer Overflows338
9.3The Program's Environment339
9.4Defensive Programming340
9.5The Principle of Least Privilege340
9.6Using chroot Jails344
9.7Tips For Writing Secure Programs346
9.8Summary349


Part 3Input/Ouput351

Chapter 10Files and Directories353
10.1Introduction353
10.2Pathname Components353
10.3The stat, fstat, and lstat Functions355
10.4File Types357
10.5Set-User-ID and Set-Group-ID360
10.6The Sticky Bit361
10.7File Access Permissions362
10.8The access Function365
10.9The umask Function367
10.10The chmod and fchmod Functions368
10.11The chown, fchown, and lchown Functions371
10.12File Size371
10.13File Truncation373
10.14File Systems374
10.15The link and unlink Functions377
10.16The remove and rename Functions380
10.17Symbolic Links381
10.18Resolving Paths that Might Contain Symbolic Links385
10.19The symlink and readlink Functions387
10.20File Times387
10.21Changing a File's Access and Modification Times390
10.22Creating and Removing Directories392
10.23Reading Directories393
10.24The chdir, fchdir, and getcwd Functions403
10.25The chroot and fchroot Functions406
10.26Special Files406
10.27The sync and fsync Functions409
10.28Putting It All Together410
10.29Summary416

Chapter 11Working with File Systems417
11.1Introduction417
11.2Disk Terminology417
11.3The Mounted File System Table418
11.4The mntfs File System ioctl Commands425
11.5File System Defaults427
11.6Mounting and Unmounting File Systems432
11.7Obtaining the Status of a File System438
11.8Reading File System Data Structures443
11.9Summary459

Chapter 12Terminal I/O461
12.1Introduction461
12.2Overview of Terminal I/O461
12.3Special Input Characters469
12.4Getting and Setting Terminal Attributes474
12.5Terminal Option Flags475
12.6Baud Rate Functions484
12.7Line Control Functions486
12.8Terminal Identification488
12.9Canonical Mode493
12.10Non-Canonical Mode496
12.11Terminal Window Size502
12.12Device-Independent Terminal Control503
12.13Summary505

Chapter 13Advanced I/O507
13.1Introduction507
13.2Nonblocking I/O507
13.3Record Locking511
13.4Record Locking Using fcntl513
13.5Record Locking Using lockf517
13.6Deadlock and Livelock517
13.7Lock Inheritance and Release519
13.8Mandatory Versus Advisory Locking521
13.9The STREAMS I/O Subsystem524
13.10STREAMS Messages526
13.11The putmsg and putpmsg Functions528
13.12The getmsg and getpmsg Functions529
13.13STREAMS ioctl Operations533
13.14STREAMS I/O Using read and write534
13.15I/O Multiplexing537
13.16The select Function539
13.17The poll Function543
13.18The /dev/poll Device Driver545
13.19Asynchronous I/O549
13.20Asynchronous I/O With STREAMS Device Files550
13.21Asynchronous I/O With Other Files551
13.22The readv and writev Functions553
13.23The sendfile and sendfilev Functions556
13.24Memory Mapped I/O562
13.25The mmap and munmap Functions562
13.26The mprotect Function570
13.27The madvise Function570
13.28The msync Function572
13.29Locking Pages in Memory573
13.30The memcntl Function577
13.31Summary of Memory Mapped I/O579
13.32Access Control Lists579
13.33The acl and facl Functions581
13.34The aclfromtext and acltotext Functions582
13.35The aclcheck Function584
13.36The aclfrommode and acltomode Functions588
13.37The aclsort Function589
13.38Extended File Attributes590
13.39The openat and attropen Functions591
13.40The fstatat Function594
13.41The unlinkat Function595
13.42The renameat Function595
13.43The fchownat Function596
13.44The futimesat Function596
13.45Changing Extended Attribute File Permissions597
13.46Summary599


Part 4Processes and Process Control601

Chapter 14The Environment of a UNIX Process603
14.1Introduction603
14.2Process Start-Up603
14.3Process Termination604
14.4Command Line Arguments608
14.5Environment Variables611
14.6The Memory Layout of a C Program613
14.7Shared Objects616
14.8Memory Allocation618
14.9The setjmp and longjmp Functions619
14.10Resource Limits626
14.11Summary626

Chapter 15Process Control629
15.1Introduction629
15.2Process Identifiers629
15.3The fork and fork1 Functions631
15.4The vfork Function637
15.5The exit and _exit Functions639
15.6The wait Function641
15.7The waitpid Function643
15.8The wait3 and wait4 Functions646
15.9The waitid Function647
15.10Race Conditions648
15.11The exec Functions652
15.12Interpreter Files662
15.13The system Function666
15.14Process Accounting670
15.15Summary676

Chapter 16Process Relationships677
16.1Introduction677
16.2Terminal Logins677
16.3Network Logins679
16.4Process Groups682
16.5Sessions684
16.6Controlling Terminal686
16.7The tcgetpgrp and tcsetpgrp Functions688
16.8The tcgetsid Function688
16.9Job Control689
16.10Shell Execution of Programs693
16.11Orphaned Process Groups697
16.12Summary700

Chapter 17Signals703
17.1Introduction703
17.2Signal Concepts703
17.3The signal Function713
17.4Unreliable Signals716
17.5Reliable Signals718
17.6The sigset Function719
17.7The pause Function720
17.8The sighold, sigrelse, sigignore, and sigpause Functions721
17.9Interrupted System Calls723
17.10Reentrant Functions725
17.11Comparing the SIGCHLD and SIGCLD Signals728
17.12The kill, killpg, raise, sigsend, and sigsendset Functions729
17.13The alarm Function734
17.14Interval Timers742
17.15POSIX Signals744
17.16Signal Sets746
17.17The sigprocmask Function747
17.18The sigpending Function748
17.19The sigaction Function752
17.20The sigfpe Function761
17.21The sigsetjmp and siglongjmp Functions762
17.22The sigsuspend Function768
17.23The sigwait Function773
17.24The abort Function775
17.25The system Function Revisited776
17.26The sleep Function Revisited784
17.27Job Control Signals787
17.28Software Signals790
17.29Alternate Signal Stacks792
17.30System Signal Messages797
17.31The sig2str and str2sig Functions798
17.32Summary802

Chapter 18Daemon Processes805
18.1Introduction805
18.2Characteristics of Daemons806
18.3Error Logging807
18.4The STREAMS log Driver807
18.5The syslog Facility812
18.6Becoming a Daemon817
18.7Starting Only One Copy of a Daemon821
18.8Summary825


Part 5Interprocess Communication827

Chapter 19Interprocess Communication Using Pipes and FIFOs829
19.1Introduction829
19.2Pipes829
19.3The popen and pclose Functions839
19.4Coprocesses848
19.5FIFOs854
19.6Iterative Versus Concurrent Servers863
19.7Summary865

Chapter 20The System V Interprocess Communication Facility867
20.1Introduction867
20.2System V IPC Concepts867
20.3System V Message Queues873
20.4System V Semaphores Sets890
20.5System V Shared Memory908
20.6Performance Comparisons919
20.7Summary924

Chapter 21Advanced Interprocess Communication927
21.1Introduction927
21.2Passing File Descriptors927
21.3An Open Server, Version 1931
21.4Client-Server Connection Functions938
21.5An Open Server, Version 2943
21.6Summary948

Chapter 22Doors951
22.1Introduction951
22.2Basic Door Functions953
22.3Door Information Functions968
22.4Advanced Door Facilities973
22.5Premature Termination of a Door Client or Server985
22.6Summary992


Part 6Pseudo Terminals995

Chapter 23Pseudo Terminals997
23.1Introduction997
23.2Overview997
23.3Opening a Pseudo Terminal Device1004
23.4The pty_fork Function1009
23.5The pty Program1011
23.6Using the pty Program1016
23.7Advanced Features1025
23.8Summary1033

Appendix AAn Internationalization and Localization Primer1035
A.1Introduction1035
A.2Locales1036
A.3The setlocale Function1037
A.4Message Catalogues1038
A.5Creating a Message Catalogue1039
A.6The bindtextdomain Function1041
A.7The gettext, dgettext, and dcgettext Functions1042
A.8The textdomain Function1042
A.9The strcoll and strxfrm Functions1044
A.10Checklist for Writing Internationalized Programs1045
A.11Summary1046

Appendix BThe BSD Source Compatibility Package1047
B.1Introduction1047
B.2Functions That Have Been Removed from the SCP1049
B.2.1    Obsolescent SCP-Originated Functions1049
B.2.2    SCP-Originated Functions That Are Not Obsolescent1053
B.3Summary1055

Appendix CFunction Summary1057
C.1Introduction1057
C.2Function Prototypes1057
C.3Function Availability1104

Appendix DMiscellaneous Source Code1117
D.1Our Header File, ssp.h1117
D.2Standard Error Functions1118
D.3File Status Flags Functions1121
D.4Section Locking Function1122
D.5Our readn and writen Functions1122
D.6Termination Status Function1124
D.7Our Version of snprintf1124

Appendix ESolutions to Selected Exercises1127

Bibliography1153

Index1159




List of Programs



Program 1.1List all the files in a directory9
Program 1.2Copy standard input to standard output using unbuffered I/O functions14
Program 1.3Copy standard input to standard output using standard I/O functions15
Program 1.4Print our process ID and parent process ID18
Program 1.5A very simple shell program19
Program 1.6Print our user and group IDs24
Program 1.7Our shell, modified to catch SIGINT26
Program 1.8Calculate the average system call latency29
Program 1.9Sign extension problems in 64-bit code38
Program 1.10Large file demonstration41
Program 3.1Showing the character class of a character65
Program 3.2Using strlen, strspn, and strcspn67
Program 3.3Bubble sort using strcmp69
Program 3.4Safe string concatenation72
Program 3.5Safe string copying75
Program 3.6Our highlight function76
Program 3.7Searching for the first occurrence of a character76
Program 3.8Find the first of several characters79
Program 3.9Searching for a substring80
Program 3.10Splitting lines into tokens82
Program 3.11Another method of splitting a line into tokens84
Program 3.12String encryption using rot1386
Program 3.13Converting numbers using strtol89
Program 3.14Comparing the different versions of malloc107
Program 3.15Using getopt and getsubopt114
Program 3.16Using an assertion to check for a NULL pointer119
Program 4.1Creating a sparse file130
Program 4.2Copy a file using read and write133
Program 4.3Print file status flags148
Program 4.4Print out the file access mode and status flags149
Program 4.5Print the extended and regular file descriptor flags150
Program 4.6A function to set file flags151
Program 4.7Copy a file using synchronous writes152
Program 4.8Implementing the closefrom function154
Program 4.9Investigating how direct I/O affects sequential file I/O157
Program 5.1Simple conversion specifications178
Program 5.2Using the alternative conversion introducer178
Program 5.3Adding precision modifiers179
Program 5.4Adding conversion specification flags181
Program 5.5Adding size modifiers183
Program 5.6Two threads writing a record without file locking188
Program 5.7Two threads writing a record with file locking190
Program 5.8Copy standard input to standard output using fgetc and fputc195
Program 5.9Copy standard input to standard output using fgets and fputs196
Program 6.1Determining a leap year202
Program 6.2Displaying information about the local time zone208
Program 6.3Printing login session length using gmtime211
Program 6.4Using mktime to determine what day of the week a given date is212
Program 6.5Using strftime to convert the time to various formats218
Program 7.1Using getlogin225
Program 7.2Using cuserid227
Program 7.3Getting and setting real and effective user IDs229
Program 7.4Getting and setting the current group set236
Program 7.5Searching by user name239
Program 7.6List all the users241
Program 7.7Searching the shadow password file by user name246
Program 7.8Listing all the passwords248
Program 7.9Traditional style password encryption252
Program 7.10New style password encryption254
Program 7.11Comparing passwords255
Program 7.12Searching by group name258
Program 7.13List all the groups260
Program 7.14Listing the logged-in users265
Program 7.15Printing users' last login times271
Program 7.16Listing the legal shells272
Program 8.1System information provided uname and sysinfo281
Program 8.2Displaying CPU and memory information292
Program 8.3Displaying some run time file limits295
Program 8.4Listing the supported user page sizes297
Program 8.5Print the current soft and hard resource limits301
Program 8.6Our implementation of the print_rctls function312
Program 8.7Our implementation of the print_rctl function313
Program 8.8Printing all the resource controls315
Program 8.9Modifying a resource control317
Program 8.10Time how long each command line argument takes to run321
Program 8.11Our implementation of getprusage329
Program 8.12Implementing getprusage using ioctl332
Program 8.13Displaying the system's load averages335
Program 9.1Using privilege bracketing342
Program 9.2Breaking out of a chroot jail345
Program 10.1Splitting a path into its component parts359
Program 10.2Print out the file type for each command line argument359
Program 10.3Demonstrating the access function366
Program 10.4Using the umask function368
Program 10.5Using the chmod function370
Program 10.6Unlinking a file379
Program 10.7Recursively list all the files in a directory384
Program 10.8Resolving symbolic links using realpath and resolvepath386
Program 10.9Display a file's access, modification, and inode modification times390
Program 10.10Changing a file's access and modification times392
Program 10.11Count the number of each type of file397
Program 10.12Recursively count the number of each type of file402
Program 10.13Change the current working directory404
Program 10.14Print the current working directory405
Program 10.15Print the st_dev and st_rdev fields of a file409
Program 10.16Print the entire contents of the stat structure412
Program 11.1Displaying information about mounted file systems422
Program 11.2Listing file systems mounted with a given option424
Program 11.3Setting a tag on a mounted file system427
Program 11.4Displaying information from /etc/vfstab430
Program 11.5Mounting a UFS file system436
Program 11.6Unmounting a file system438
Program 11.7Printing file system statistics441
Program 11.8Obtaining file system information using ustat443
Program 11.9Print the disk and inode use for each user455
Program 12.1Changing the INTR and KILL special input characters480
Program 12.2Using tcgetattr and tcsetattr to change the character size476
Program 12.3Changing the input baud rate486
Program 12.4Our implementation of isatty489
Program 12.5Our implementation of ttyname_r491
Program 12.6A function to read a user's password495
Program 12.7Set terminal to cbreak or raw mode499
Program 12.8Testing our raw and cbreak modes501
Program 12.9Print window size changes504
Program 13.1Using nonblocking I/O510
Program 13.2Our function to acquire or release a lock515
Program 13.3Our function to test for a lock516
Program 13.4Deadlock detection when file locking520
Program 13.5Copying a file that has a read lock on it523
Program 13.6Using isastream526
Program 13.7Copy a file using getmsg and write532
Program 13.8Listing the modules on a STREAMS device using ioctl535
Program 13.9Using fd_sets541
Program 13.10Using poll and nonblocking I/O to copy a file546
Program 13.11Using /dev/poll and nonblocking I/O to copy a file548
Program 13.12Using writev to write multiple buffers555
Program 13.13Copying a file using sendfile557
Program 13.14Writing a buffer using sendfile559
Program 13.15Copying a file and two buffers using sendfilev561
Program 13.16Copy a file using mmap and memcpy566
Program 13.17Sharing memory between related processes using /dev/zero569
Program 13.18Locking a file in memory576
Program 13.19Printing a file's ACL585
Program 13.20Setting a file's ACL587
Program 13.21Listing a file's extended attributes593
Program 13.22Changing permissions on an extended attribute file598
Program 14.1Example of how to use exit handlers608
Program 14.2Echo command line arguments to standard output609
Program 14.3Determining our executable's pathname610
Program 14.4Getting and setting environment variables613
Program 14.5The ubiquitous "hello world" program617
Program 14.6A program using nested functions620
Program 14.7Our example changed to use setjmp and longjmp622
Program 14.8The effect of longjmp on automatic, register, and volatile variables624
Program 14.9Incorrect usage of an automatic variable626
Program 15.1An example of the fork function634
Program 15.2An example of the vfork function638
Program 15.3Describe the termination status of a process642
Program 15.4Demonstrating different termination statuses643
Program 15.5Avoiding zombies by calling fork twice645
Program 15.6Program with a race condition650
Program 15.7Reimplementation of Program 15.6 avoiding race condition651
Program 15.8Demonstrating the exec functions658
Program 15.9Echo all the command line arguments and environment variables659
Program 15.10A program that execs an interpreter file663
Program 15.11An awk script as an interpreter file664
Program 15.12Our implementation of the system function (without signal handling)668
Program 15.13Testing our implementation of the system function669
Program 15.14Program to generate some process accounting entries673
Program 15.15Print selected fields from process accounting file675
Program 16.1Creating an orphaned process group698
Program 17.1Catching the signals SIGUSR1 and SIGUSR2 by using signal715
Program 17.2Catching the signals SIGUSR1 and SIGUSR2 by using sigset721
Program 17.3Calling non-reentrant functions from a signal handler727
Program 17.4A SIGCLD signal handler that can be problematic730
Program 17.5Our first attempt at implementing sleep736
Program 17.6Avoiding the race condition in our first version of sleep737
Program 17.7Calling ssp_sleep from a program that catches other signals738
Program 17.8Collecting user input with a timeout739
Program 17.9Collecting user input with a timeout, helped by setjmp and longjmp741
Program 17.10Collecting user input with an interval timer timeout745
Program 17.11Printing the current signal mask749
Program 17.12Example of signal sets, sigprocmask, and sigpending750
Program 17.13Our implementation of signal using sigaction759
Program 17.14Printing signal information in a signal handler760
Program 17.15Trapping invalid operation floating-point exceptions763
Program 17.16How sigsetjmp and siglongjmp interact with signal masks765
Program 17.17Protecting a critical region of code from a signal770
Program 17.18Waiting for a global variable to be set772
Program 17.19Functions that enable a child and parent to synchronize using signals775
Program 17.20Our implementation of the abort function777
Program 17.21Using ssp_system to invoke ex778
Program 17.22An implementation of system that handles signals correctly780
Program 17.23Execute the command line argument using system783
Program 17.24Our final implementation of sleep786
Program 17.25Handling SIGTSTP789
Program 17.26Using software signals791
Program 17.27Using an alternate stack for a signal handler794
Program 17.28Printing signal information using psignal and psiginfo799
Program 17.29Our implementation of the kill command801
Program 18.1Generating a log message810
Program 18.2Using the syslog facility816
Program 18.3Our daemon_init function818
Program 18.4Testing our daemon_init function821
Program 18.5Our one_copy function823
Program 18.6Testing our one_copy function824
Program 19.1Sending data from parent to child over a pipe833
Program 19.2Sending email through a pipe835
Program 19.3Functions that enable a child and parent to synchronize using pipes838
Program 19.4Sending email through a pipe using popen841
Program 19.5Our implementation of popen842
Program 19.6Our implementation of pclose844
Program 19.7Support functions for popen and pclose846
Program 19.8Our modified version of rot13848
Program 19.9Invoking rot13 using our version of popen and pclose849
Program 19.10A simple coprocess to calculate the square root of a number850
Program 19.11Program to drive the sqrt coprocess851
Program 19.12Another version of sqrt, using standard I/O853
Program 19.13FIFO server that can handle multiple clients859
Program 19.14FIFO client that works with the server in Program 19.13862
Program 20.1Obtain a System V IPC key based on a pathname870
Program 20.2Create a System V message queue879
Program 20.3Add a message to a System V message queue880
Program 20.4Remove a message from a System V message queue882
Program 20.5Remove a System V message queue883
Program 20.6List all messages currently in System V message queues887
Program 20.7Create a System V semaphore set897
Program 20.8Remove a System V semaphore set898
Program 20.9Set the values of semaphores in a semaphore set899
Program 20.10Get the values of semaphores in a semaphore set900
Program 20.11Perform operations on a System V semaphore set902
Program 20.12List all the System V semaphore sets905
Program 20.13Create a System V shared memory segment913
Program 20.14Write to a System V shared memory segment914
Program 20.15Read from a System V shared memory segment915
Program 20.16Remove a System V shared memory segment916
Program 20.17List all the System V shared memory segments918
Program 20.18Copy 100 MB using a System V message queue921
Program 20.19Copy 100 MB using a pipe922
Program 20.20Access a shared resource using a lock file923
Program 20.21Access a shared resource using a System V semaphore925
Program 21.1The send_fd and send_err functions930
Program 21.2The recv_fd function931
Program 21.3The client's main function933
Program 21.4The cs_open function934
Program 21.5The first server's main function935
Program 21.6The do_request function936
Program 21.7The buf2args and parse_args functions937
Program 21.8The srv_listen function941
Program 21.9The srv_accept function943
Program 21.10The cli_connect function943
Program 21.11The cs_open function944
Program 21.12Preamble for version 2 of our open server945
Program 21.13The second server's main function946
Program 21.14The loop function947
Program 21.15The add_entry and del_entry functions949
Program 22.1A simple door client958
Program 22.2A simple door server959
Program 22.3Door client modified to print address and size of result961
Program 22.4Door server that handles an unreferenced invocation963
Program 22.5The main function of our multiple unreferenced invocations server966
Program 22.6The server procedure of our multiple unreferenced invocations server967
Program 22.7Server procedure that prints the client's credentials969
Program 22.8Print information about a door972
Program 22.9Door client for descriptor passing example975
Program 22.10Door server procedure for descriptor passing example976
Program 22.11The main function of our server that manages its own threads980
Program 22.12The server creation function of our server that manages its own threads981
Program 22.13The thread start function of our server that manages its own threads983
Program 22.14The server procedure of our server that manages its own threads984
Program 22.15A server procedure that terminates itself986
Program 22.16Server procedure for idempotent server example987
Program 22.17Client that calls door_call again when SIGCHLD is received988
Program 22.18A client that terminates during a door_call990
Program 22.19A server procedure that detects premature client termination991
Program 23.1Function to open a pseudo terminal master device1007
Program 23.2Function to open a pseudo terminal slave device1008
Program 23.3Our pty_fork function1010
Program 23.4Our pty program's main function1012
Program 23.5The pty program's loop function1015
Program 23.6Implementing the script program using pty1019
Program 23.7The run_driver function for the pty program1024
Program 23.8Script to drive the passwd program1025
Program 23.9The pckt program's loop function1027
Program 23.10Program to test packet mode pseudo terminals1031
Program A.1An internationalized version of our greeting program1043
Program D.1Our header file, ssp.h1117
Program D.2Our standard error functions1119
Program D.3Our file status flag functions1121
Program D.4Our function to acquire or release a lock on a file section1122
Program D.5Our readn and writen functions1123
Program D.6Our function to print the termination status of a process1124
Program D.7Our version of snprintf1124
Program E.1Determine a file's size by using lseek1128
Program E.2Our function to convert an arbitrarily based number to a string1129
Program E.3Get a lastlog entry for a given user ID1132
Program E.4Writing to a file that is underneath a mount point1137
Program E.5Accessing a file after its file system has been unmounted1138
Program E.6Measuring the capacity of a pipe by using poll1140
Program E.7Incorrect use of vfork1142
Program E.8Create a zombie and look at it using ps1143
Program E.9Print a terminal's foreground process group ID1144
Program E.10Create a new session1145
Program E.11Print the number of the highest open file descriptor1147
Program E.12Portably opening a FIFO for reading and writing1149
Program E.13Passing a door descriptor in a cookie to avoid making it global1151
Program E.14Adding start and stop timestamps to our version of script1180




List of Figures



Figure 1.1Summary of the different file system types8
Figure 1.2System call latency on four architectures30
Figure 1.3Relationship of applications, library functions, and system calls30
Figure 1.4C data type sizes in bits for the ILP32 and LP64 data type models33
Figure 1.5The effect of the various compilation environments42
Figure 2.1Standards compliance of Solaris54
Figure 2.2Commands and flags required to build standards conforming applications55
Figure 3.1Alignment of various word sizes95
Figure 3.2Memory layout of our structure95
Figure 3.3Memory layout of our reordered structure96
Figure 3.4Red zones105
Figure 3.5Results from running Program 3.14106
Figure 4.1Availability of open flags127
Figure 4.2The effect of buffer size on file I/O135
Figure 4.3Kernel structures used for open files137
Figure 4.4Kernel structures for two processes opening the same file138
Figure 4.5Kernel structures after dup142
Figure 4.6Categories of fcntl cmd arguments143
Figure 4.7File access flags144
Figure 4.8Availability of fcntl cmds147
Figure 4.9The effect of O_DSYNC and O_SYNC on file copy times152
Figure 4.10Summary of how direct I/O affects sequential file I/O156
Figure 5.1The effect of the values of mode on fopen, freopen, and fdopen161
Figure 5.2Conversion characters177
Figure 5.3Precision modifiers179
Figure 5.4Conversion specification flag characters181
Figure 5.5Size modifiers182
Figure 5.6The effect of buffer size on standard (character) I/O197
Figure 5.7The effect of buffer size on standard (line and unlocked) I/O197
Figure 5.8Summary results from Programs 5.8 and 5.9198
Figure 5.9I/O buffer sizes for different values of buf_size198
Figure 6.1The relationship of the various time functions209
Figure 7.1Relationship between the functions that modify the different user IDs233
Figure 7.2The different ways to change the three user IDs234
Figure 8.1Availability of sysinfo commands280
Figure 8.2Variables that return -1 without setting errno291
Figure 8.3The effect of limits on implementation-defined constants300
Figure 8.4Resource controls available in Solaris 9303
Figure 10.1The effect of dirname and basename on different paths355
Figure 10.2File type macros358
Figure 10.3Masks to determine file access permissions364
Figure 10.4Other file access permission masks364
Figure 10.5Masks to determine file access permissions369
Figure 10.6Logical layout of file systems, cylinder groups, and inode lists374
Figure 10.7File system in more detail376
Figure 10.8File system detail after creating a test directory378
Figure 10.9How symbolic links are handled by various functions382
Figure 10.10The effect of functions on a file's access, update, and inode update times389
Figure 11.1Standard file system types433
Figure 11.2The standard mount options434
Figure 11.3Values of fs_clean449
Figure 11.4Constants that help with superblock I/O449
Figure 11.5UFS inode direct and indirect blocks453
Figure 11.6Macros for handling inode numbers453
Figure 12.1Terminal device input and output queues463
Figure 12.2Terminal line discipline464
Figure 12.3Terminal flag summary466
Figure 12.4Summary of terminal I/O functions468
Figure 12.5Relationship between the terminal device functions468
Figure 12.6Summary of special input characters469
Figure 12.7Availability of special input characters470
Figure 12.8The four cases for non-canonical input497
Figure 13.1The effect of O_NONBLOCK and O_NDELAY on a blocking read508
Figure 13.2The effect of O_NONBLOCK and O_NDELAY on a blocking write509
Figure 13.3The interoperability of different lock types512
Figure 13.4The effect of mandatory locking on I/O by other processes521
Figure 13.5Effect of mandatory locking on file I/O performance522
Figure 13.6Anatomy of a simple stream525
Figure 13.7STREAMS message type created by write, putmsg, and putpmsg529
Figure 13.8STREAMS message type retrieved by read, getmsg, and getpmsg530
Figure 13.9Overview of the talk program537
Figure 13.10The talk program using two processes538
Figure 13.11Visualization of the fd_set data type540
Figure 13.12The effect of poll and /dev/poll on nonblocking I/O efficiency549
Figure 13.13Details of the array of iovec structures used by readv and writev554
Figure 13.14Timing results for different methods of writing two buffers555
Figure 13.15Memory mapped file563
Figure 13.16Comparing the time to copy a file using Programs 4.2 and 13.16567
Figure 14.1Relationship of a C program's start-up and termination functions607
Figure 14.2An example environment variable list611
Figure 14.3Address space for 32-bit SPARC sun4u processes615
Figure 14.4Stack frames after do_foo has been called621
Figure 15.1Sharing of open files between parent and child636
Figure 15.2Summary of the six exec functions661
Figure 15.3Relationship of the six exec functions661
Figure 15.4Values for ac_flag in a process accounting record671
Figure 15.5Process hierarchy for Program 15.14674
Figure 16.1Arrangement of processes to allow terminal logins678
Figure 16.2Arrangement of processes once we've logged in from a terminal679
Figure 16.3Sequence of processes when running the TELNET server681
Figure 16.4Arrangement of processes once we've logged in from the network681
Figure 16.5Arrangement of a process and its offspring682
Figure 16.6Arrangement of processes in a process group683
Figure 16.7A session consisting of three process groups685
Figure 16.8A session with three process groups showing the controlling terminal687
Figure 16.9Summary of job control features692
Figure 16.10Processes in a pipeline when invoked by /bin/sh695
Figure 16.11Processes in a pipeline when invoked by /bin/ksh697
Figure 16.12Arrangement of processes from Program 16.1700
Figure 17.1Solaris signal summary706
Figure 17.2Features provided by the different signal handling functions724
Figure 17.3Reentrant functions that can be called from a signal handler726
Figure 17.4Similarities between the SVR4 and POSIX signal mechanisms746
Figure 17.5Values of si_code for system-generated signals758
Figure 17.6Time line for Program 17.16767
Figure 17.7Process groups for Program 17.21779
Figure 18.1Details of the STREAMS log facility808
Figure 18.2Details of the syslog facility812
Figure 19.1Conceptual representation of a pipe830
Figure 19.2Arrangement of SVR4 STREAMS pipes831
Figure 19.3Arrangement of a pipe after fork831
Figure 19.4Arrangement of a pipe between parent and child832
Figure 19.5Arrangement of pipes for synchronizing parent and child processes837
Figure 19.6Result of fp = popen (command, "r")839
Figure 19.7Result of fp = popen (command, "w")840
Figure 19.8Driving a coprocess by using two one-way pipes848
Figure 19.9Driving a coprocess by using a full-duplex pipe849
Figure 19.10Procedure that processes filtered data twice855
Figure 19.11Process arrangement when using a FIFO to connect two pipelines856
Figure 19.12Clients sending requests to a server via a well-known FIFO857
Figure 19.13Client-server communications using FIFOs857
Figure 20.1The effect of flags when creating or opening an IPC object871
Figure 20.2Summary of IPC permissions872
Figure 20.3System V message queue tunables874
Figure 20.4Message type retrieved by msgrcv for different values of msgtyp877
Figure 20.5System V semaphore tunables892
Figure 20.6System V shared memory tunables909
Figure 20.7Performance comparison of message queues and pipes920
Figure 20.8Performance comparison of semaphores and record locking924
Figure 21.1Passing a file descriptor from one process to another928
Figure 21.2Pipe after pushing the connld module onto one end942
Figure 21.3Client-server connections on a named pipe942
Figure 22.1The three types of function call scenarios952
Figure 23.1Arrangement of pseudo terminals998
Figure 23.2Arrangement of processes when using dtterm999
Figure 23.3Arrangement of processes when using sshd to provide network logins1000
Figure 23.4Arrangement of processes when running the script program1001
Figure 23.5Driving a coprocess via a pseudo terminal1002
Figure 23.6Monitoring the output of a long-running program using a pseudo terminal1003
Figure 23.7Arrangement of processes when running cat from our pty program1017
Figure 23.8Arrangement of processes when using our ssp_script shell script1020
Figure 23.9Running a coprocess with a pseudo terminal as its input and output1021
Figure 23.10Arrangement of processes when using a driver program with pty1023
Figure A.1Precedence of environment variables for setting locales1038
Figure B.1Obsolescent SCP-originated functions1053
Figure B.2SCP-originated functions that are not obsolescent1054
Figure C.1Function availability (from _exit to ascftime)1104
Figure C.2Function availability (from asctime to door_create)1105
Figure C.3Function availability (from door_cred to fgets)1106
Figure C.4Function availability (from fgetspent to getgrgid)1107
Figure C.5Function availability (from getgrgid_r to getutxent)1108
Figure C.6Function availability (from getutxid to malloc)1109
Figure C.7Function availability (from mallopt to psignal)1110
Figure C.8Function availability (from ptsname to rmdir)1111
Figure C.9Function availability (from sbrk to sigdelset)1112
Figure C.10Function availability (from sigemptyset to strncat)1113
Figure C.11Function availability (from strncmp to ttyname_r)1114
Figure C.12Function availability (from tzset to writev)1115
Figure D.1Summary of our standard error functions1119
Figure E.1Summary of running different versions of Program 1.101128
Figure E.2Minimum set of files required in a chroot jail1134
Figure E.3Stack frame for Program E.71141
Figure E.4Stack frames while Program 17.7 is executing1145


Send any questions or comments to: rich.teer@rite-group.com
Copyright © 1997-2006 by Rich Teer

Valid HTML 4.01!