Skip to content

Commit

Permalink
use read lock for the parent
Browse files Browse the repository at this point in the history
  • Loading branch information
vladak committed Dec 9, 2024
1 parent 781dd8a commit ed6eb27
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion file-locking/fcntl-locking.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ lock(int fd, struct flock *fl)
err(1, "fcntl");
}

static void
lockr(int fd, struct flock *fl)
{
fl->l_type = F_RDLCK;
if (fcntl(fd, F_SETLKW, fl) == -1)
err(1, "fcntl");
}

static void
unlock(int fd, struct flock *fl)
{
Expand Down Expand Up @@ -141,7 +149,7 @@ dump_file(char *filename, bool locking)
while (1) {
/* Lock only the 2nd half of the file. */
if (locking)
lock(fd, &fl);
lockr(fd, &fl);

(void) lseek(fd, SEEK_SET, 0);
memset(buf, 0, sizeof (buf));
Expand Down

0 comments on commit ed6eb27

Please sign in to comment.