HP Rp7410 BSD Sockets Interface Programmer's Guide - Page 73

Synchronous I/O Multiplexing with Select

Page 73 highlights

Advanced Topics for Stream Sockets Synchronous I/O Multiplexing with Select The following example illustrates the select system call. Since it is possible for a process to have more than 32 open file descriptors, the bit masks used by select are interpreted as arrays of integers. The header file sys/types.h contains some useful macros to be used with the select() system call, some of which are reproduced below. /* * These macros are used with select(). select() uses bit masks of * file descriptors in long integers. These macros manipulate such * bit fields (the file system macros use chars). FD_SETSIZE may * be defined by the user, but must be = u.u_highestfd + 1. Since * the absolute limit on the number of per process open files is * 2048, FD_SETSIZE must be large enough to accommodate this many * file descriptors. * Unless the user has this many files opened, FD_SETSIZE should * be redefined to a smaller number. */ typedef long fd_mask #define NFDBITS (sizeof (fd_mask) * 8 /* 8 bits per byte #define howmany (x,y) (((x)+((y)-1))/(y)) typedef struct fd_set { fd_mask fds_bits [howmany (FD_SETSIZE, NFDBITS)]; */ } fd_set; #define FD_SET(n,p) ((p)->fds_bits[(n)/NFDBITS] #|= (1 fds_bits[(n)/NFDBITS] #&= ˜(1 fds_bits[(n)/NFDBITS] #& (1

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196

Chapter 3
73
Advanced Topics for Stream Sockets
Synchronous I/O Multiplexing with Select
The following example illustrates the
select
system call. Since it is
possible for a process to have more than 32 open file descriptors, the bit
masks used by
select
are interpreted as arrays of integers. The header
file
sys/types.h
contains some useful macros to be used with the
select()
system call, some of which are reproduced below.
/*
* These macros are used with select(). select() uses bit masks of
* file descriptors in long integers.
These macros manipulate such
* bit fields (the file system macros use chars).
FD_SETSIZE may
* be defined by the user, but must be = u.u_highestfd + 1. Since
* the absolute limit on the number of per process open files is
* 2048, FD_SETSIZE must be large enough to accommodate this many
* file descriptors.
* Unless the user has this many files opened, FD_SETSIZE should
* be redefined to a smaller number.
*/
typedef long fd_mask
#define NFDBITS (sizeof (fd_mask) * 8
/* 8 bits per byte
#define howmany (x,y)
(((x)+((y)-1))/(y))
typedef struct fd_set
{
fd_mask fds_bits [howmany (FD_SETSIZE, NFDBITS)]; */
}
fd_set;
#define FD_SET(n,p) ((p)->fds_bits[(n)/NFDBITS]
#|= (1 << ((n) % NFDBITS)))
#define FD_CLR(n,p)
((p)->fds_bits[(n)/NFDBITS]
#&= ˜(1 << ((n) % NFDBITS)))
#define FD_ISSET(n,p) ((p) ->fds_bits[(n)/NFDBITS]
#& (1 << ((n) % NFDBITS)))
#define FD_ZERO(p)
memset((char *) (p), (char) 0, sizeof (*(p)))
do_select(s)
int s;
/* socket to select on, initialized */
{
struct fd_set read_mask, write_mask; /* bit masks */
int nfds;
/* number to select on */
int nfd;
/* number found */
for (;;) {
/* for example... */
FD_ZERO(&read_mask);
/* select will overwrite on return */
FD_ZERO(&write_mask);
FD_SET(s, &read_mask);
/* we care only about the socket */
FD_SET(s, &write_mask);
nfds = s+1;
/* select descriptors 0 through s */
nfd = select(nfds, &read_mask, &write_mask, (int *) 0,
(struct timeval *) 0);/* will block */
if (nfd == -1) {
perror( “select: unexpected condition” );