Let us learn manipulating file descriptor using fcntl system call in Linux. fcntl system call works on open file descriptors and manipulates it based on the command supplied in the fcntl(int fd, int cmd, ../*arg*/).

Syntax of fcntl() system call

  • fcntl requires a valid file descriptor

ubuntu@seco-desk-ubuntu:~$ gcc main.c

ubuntu@seco-desk-ubuntu:~$ ./a.out

fcntl: : Bad file descriptor -1 The return value is -1 , it means fcntl() fails , becuase the first argument -1 to the fcntl() system call is not a valid file descriptor.

What is the use of fcntl system call in c?

  • To duplicate an existing descriptor (cmd = F_DUPFD)

  • get/set file descriptor flags (cmd = F_GETFD or F_SETFD),
  • get/set asynchronous I/O ownership (cmd = F_GETOWN or F_SETOWN),
  • get/set record locks (cmd = F_GETLK, F_SETLK, or F_SETLKW).



Related Contents to follow