What Is The Purpose Of Using Fcntl Function?

2 Answers

raaga Profile
raaga answered
This function is used for file descriptor manipulation. It is used for changing the attributes of an opened file. The syntax of this file is as follows:
int fcntl(int file_descriptor, int cmd, int arg)
The header files sys/types, unistd and fcntl have to be included to use this function.
It returns –1 in case of some error however the return on success depends on the value of second argument.
file_descriptor is the file descriptor on which some operation is to be performed.
cmd is some command code that specifies what operation is to be carried out.
Arg is the optional argument and its value depends on the command provided as th second argument. It default value is 0.
It is meant for five purposes.

1. Duplicating file descriptor
2. To obtain or set the file descriptor flags
3. To obtain or set file status flags
4. Get or Set I/O ownership
5. Get or Set record locks
Following are the command codes that are given as the second argument:
F_DUPED is used for duplicating the file descriptor provided as the first argument.
F_GETFD and F_SETFD are used to get/set the file descriptor flags.
F_GETFL and F_SETFL are used to get/set file status flags.
F_GETOWN and F_SETOWN are used to get or set I/O ownership
F_GETLK, F_STELK and F_SETLKW are used for locking records.
Shumaela Rana Profile
Shumaela Rana answered
The basic use of this function is that it offers the control of open file in many different ways for example it supports F_DUPFD etc.

Answer Question