Multiple choice technology operating systems

17 How do I `lock' a file?

  1. 1 Use filelock()

  2. 2 use lockfile()

  3. 3 Use lockfd()

  4. 4 use fcntl()

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

fcntl() is the Unix system call used for file locking operations. It provides both advisory and mandatory locking mechanisms through the F_SETLK and F_SETLKW commands. filelock(), lockfile(), and lockfd() are not standard POSIX or Unix functions.

AI explanation

fcntl() is correct. This is a classic Perl FAQ question ('How do I lock a file?'). Perl's built-in portable answer is flock(), but on systems where flock() isn't available or isn't suitable (e.g., NFS-mounted files, or systems needing finer-grained record locking), the FAQ's fallback recommendation is to use fcntl()-based locking via Perl's fcntl() call with F_SETLK/F_SETLKW. filelock(), lockfile(), and lockfd() are not real Perl core functions for this purpose — they're plausible-sounding distractors, not actual APIs, whereas fcntl() is a genuine, documented Perl/POSIX locking mechanism.