Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

932 Solutions to Selected Exercises Appendix C


"write lock is pending\n");
printf("killing child 1...\n");
kill(pid1, SIGINT);
printf("killing child 2...\n");
kill(pid2, SIGINT);
printf("killing child 3...\n");
kill(pid3, SIGINT);
exit(0);
}

Figure C.15 Determine record-locking behavior

On FreeBSD 8.0, Linux 3.2.0, and Mac OS X 10.6.8, the behavior is the same:
additional readers can starve pending writers. Running the program gives us
child 1: obtained read lock on file
child 2: obtained read lock on file
child 3: can’t set write lock: Resource temporarily unavailable
child 3 about to block in write-lock...
parent: obtained additional read lock while write lock is pending
killing child 1...
child 1: exit after pause
killing child 2...
child 2: exit after pause
killing child 3...
child 3: can’t write-lock file: Interrupted system call
On Solaris 10, readers don’t starve waiting writers. In this case, the parent is
unable to obtain a read lock because there is a process waiting for a write lock.
14.2 Most systems define thefd_setdata type to be a structurethat contains a single
member: an array of long integers. One bit in this array corresponds to each
descriptor.The fourFD_macros then manipulate this array of longs, turning
specific bits on and offand testing specific bits.
One reason that the data type is defined to be a structurecontaining an array and
not simply an array is to allow variables of typefd_setto be assigned to one
another with the C assignment statement.
14.3 In the good ol’ days, most systems allowed us to define the constant
FD_SETSIZEbeforeincluding the header<sys/select.h>.For example, we
could write
#define FD_SETSIZE 2048
#include <sys/select.h>
to define the fd_set data type to accommodate 2,048 descriptors.
Unfortunately,things aren’t that simple anymore. Touse this technique with
contemporary systems, we need to do several things:


  1. Before we include any header files, we need to define whatever symbol
    prevents us from including<sys/select.h>.Some systems might protect

Free download pdf