104
104
main (int argc , char * * argv )
105
105
{
106
106
struct sigaction act ;
107
- int i , j , fd , locking = 0 ;
107
+ int i , j , fd ;
108
108
struct flock fl ;
109
109
char * progname = argv [0 ];
110
+ enum {
111
+ NO_LOCK = 0 ,
112
+ SHARED_FD ,
113
+ EXCLUSIVE_FD
114
+ } locking = NO_LOCK ;
110
115
111
116
act .sa_handler = sigint_handler ;
112
117
sigemptyset (& act .sa_mask );
@@ -118,11 +123,11 @@ main(int argc, char **argv)
118
123
switch (opt ) {
119
124
case 'l' :
120
125
(void ) printf ("Using locking with a shared fd.\n" );
121
- locking = 1 ;
126
+ locking = SHARED_FD ;
122
127
break ;
123
128
case 'L' :
124
129
(void ) printf ("Using locking with private fd's.\n" );
125
- locking = 2 ;
130
+ locking = EXCLUSIVE_FD ;
126
131
break ;
127
132
default :
128
133
usage (progname );
@@ -135,14 +140,14 @@ main(int argc, char **argv)
135
140
if (argc != 1 )
136
141
usage (progname );
137
142
138
- if (locking == 0 )
143
+ if (locking == NO_LOCK )
139
144
printf ("Not using locking.\n" );
140
145
141
146
/* Create the file. */
142
147
char * filename = argv [0 ];
143
148
if ((fd = open (filename , O_CREAT | O_TRUNC | O_WRONLY , 0666 )) == -1 )
144
149
err (1 , "open" );
145
- if (locking == 2 )
150
+ if (locking == EXCLUSIVE_FD )
146
151
close (fd );
147
152
148
153
/* extend the file to FILE_LEN bytes */
@@ -160,7 +165,7 @@ main(int argc, char **argv)
160
165
continue ;
161
166
162
167
/* child */
163
- if (locking == 2 ) {
168
+ if (locking == EXCLUSIVE_FD ) {
164
169
if ((fd = open (filename , O_WRONLY , 0666 )) == -1 )
165
170
err (1 , "open" );
166
171
}
@@ -169,7 +174,7 @@ main(int argc, char **argv)
169
174
while (1 ) {
170
175
/* Lock only the 2nd half of the file. */
171
176
if (j == FILE_LEN / 2 ) {
172
- if (locking > 0 ) {
177
+ if (locking > NO_LOCK ) {
173
178
fl .l_type = F_WRLCK ;
174
179
if (fcntl (fd , F_SETLKW , & fl ) == -1 )
175
180
err (1 , "fcntl" );
@@ -186,9 +191,9 @@ main(int argc, char **argv)
186
191
}
187
192
#endif
188
193
/*
189
- * NOTE: If locking is set to 1 , the file position
190
- * is shared among the NPROC processes so the
191
- * following could happen (timewise):
194
+ * NOTE: If locking is set to SHARED_FD , the file
195
+ * position is shared among the NPROC processes
196
+ * so the following could happen (timewise):
192
197
*
193
198
* 1) process in the upper half lseek()s to
194
199
* offset > FILE_LEN / 2
@@ -213,7 +218,7 @@ main(int argc, char **argv)
213
218
*/
214
219
if (j == FILE_LEN ) {
215
220
j = 0 ;
216
- if (locking > 0 ) {
221
+ if (locking > NO_LOCK ) {
217
222
fl .l_type = F_UNLCK ;
218
223
if (fcntl (fd , F_SETLKW , & fl ) == -1 )
219
224
err (1 , "fcntl" );
0 commit comments