This document discusses various file control (fcntl) functions in Linux including fcntl, dup, and dup2. It provides examples of using fcntl to get and set file status and descriptor flags, duplicate file descriptors with dup and dup2, and close file descriptors. It notes that dup and dup2 create new file descriptors that reference the same open file, but have separate descriptor flags, and dup2 first closes an already open descriptor before duplicating.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
50 views18 pages
L13-File Management
This document discusses various file control (fcntl) functions in Linux including fcntl, dup, and dup2. It provides examples of using fcntl to get and set file status and descriptor flags, duplicate file descriptors with dup and dup2, and close file descriptors. It notes that dup and dup2 create new file descriptors that reference the same open file, but have separate descriptor flags, and dup2 first closes an already open descriptor before duplicating.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18
L13-File Management
Prof. Jibi Abraham
College of Engineering Pune (COEP)
Forerunners in Technical Education fcntl function int fcntl( int fd, int cmd ); int fcntl( int fd, int cmd, long arg ); int fcntl( int fd, int cmd, struct lock *ldata )
• Performs operations pertaining to fd, like
– Duplicate an existing descriptor (cmd = F_DUPFD) – Get/set file status flags (cmd = F_GETFL or F_SETFL) – Get/set file descriptor flags (cmd = F_GETFD or F_SETFD) – Get/set record locks (cmd = F_GETLK, F_SETLK, or F_SETLKW) • Specific operation depends on cmd • returns –1 on an error or some other value if OK depending on cmd fcntl: cmd with F_GETFL • int return_value = fcntl(fd, F_GETFL); • Returns the current file status flags as set by open() ie. O_RDONLY, O_WRONLY, O_RDWR, O_APPEND, O_NONBLOCK • Access mode can be extracted from AND’ing the return value – return_value & O_ACCMODE • e.g. O_WRONLY F_GETFL Example fcntl: cmd with F_SETFL • Sets the file status flags associated with fd • Only O_APPEND and O_NONBLOCK may be set. Other flags are unaffected • Change a file to non-blocking and write append mode int flag1 = fcntl(fd, F_GETFL); fcntl(fd, F_SETFL, flag1| O_APPEND| O_NONBLOCK); File Descriptor Flags • Structure of the file descriptor table is as follows: struct ufd { struct file *fp; int flags; } *u_ufd • File descriptor flags are miscellaneous attributes of a file descriptor – FD flags are associated with particular file descriptors, so that if duplicate file descriptors are created from a single opening of a file, each descriptor has its own set of flags • Currently there is just one file descriptor flag: FD_CLOEXEC, which causes the descriptor to be closed if you use any of the exec… functions fcntl: cmd with F_GETFD, F_SETFD • F_GETFD returns the file descriptor flags for fd – Currently, only FD_CLOEXEC flag is defined – Close-on-exec flag of a file specifies that if a process owning the fd calls exec function, file descriptor should be closed by the kernel before the new program runs if the flag is on – By default, the flag is off for a file – Returns zero if the flag is off • F_SETFD sets the file descriptor flags for fd – New flag value is set from the third argument, as 0 to be off and 1 to set Example • Reports the current value of Close-on-exec flag of a file specified by file descriptor fd and sets it to on afterwards
int val = fcntl(fd, F_GETFD);
if (val) printf(“set”); else { printf (“Not set and setting now”); fcntl(fd, F_SETFD, 1); } Parent-Child Example int main(int argc, char **argv) { int fd, pid; fd = open("xyz", O_RDWR); //Modify by adding before fork write(fd, “abcd”, 4); fcntl(fd, F_SETFD, 1); pid = fork(); if(pid == 0) { Poll Question What will happen? printf("%d", fd); A. Display “buf =abcd” execvp("child", "child“, NULL); } B. read will fail and display }//child “buf=“ main ( ){ int fd; char buf [1024]; scanf(“%d”, &fd); read (fd,buf,100); printf(“ buf = %s\n", buf); return 0; } fcntl: cmd with F_DUPFD • new_fd = fcntl (old, F_DUPFD, new) • Duplicates the file descriptor, allocate another file descriptor new that refers to the same open file as the original old • Returns the new file descriptor. It is the lowest- numbered descriptor that is not already open, that is greater than or equal to the third argument new fcntl: cmd with F_DUPFD • Duplicate and original descriptors share one file table entry and one set of file status flags • But the new_fd has its own set of file descriptor flags and its FD_CLOEXEC flag is cleared • return value of -1 indicates an error – EBADF The old argument is invalid. – EINVAL The new argument is invalid. – EMFILE There are no more file descriptors available dup() dup () It copies the file descriptor into the first free slot of the process file descriptor table and returns a new file descriptor to the user. Syntax int dup(int fd); int dup2(int fd1, int fd2); return: new file descriptor if OK, –1 on error • #define dup(fd) fcntl(fd, F_DUPFD,0); Kernel Data structures after dup • fd1 = open(“/etc/passwd”,…); • fd2 = open(“/etc/passwd”,…); • fd3 = open(“local”,…); • fd4 = dup(fd1); dup Example int main(int argc,char *argv[]) { int i, j; char buf1[10],buf2[10]; i = open(argv[1],O_CREAT|O_RDWR,0777); j = dup(i); //j = dup2(i, j); $ ./a.out new printf("i=%d j=%d\n",i , j); i=3 j=4 write(i,”COEP",4); write(j,"_PUNE",5); $ cat new } COEP_PUNE Example • Redirecting Standard Output to a File
• Closes standard output for the current processes, re-
assigns standard output to go to the file referenced by pfd and closes the original file descriptor to clean up dup2 • int dup2(int fd1, int fd2);
fd2); • dup2 is an atomic operation, • Alternate form involves two function calls. It is possible in the latter case to have a signal catcher called between the close and the fcntl that could modify the file descriptors Poll Question • Which one of the following statements is NOT correct with respect to dup and dup2 calls? A. The new file descriptor returned by dup is guaranteed to be the lowest-numbered available file descriptor B. With dup2, one specifies the value of the new descriptor with the fd2 argument C. With dup2, if fd2 is already open, it is first closed D. With dup2, if fd2 is already open, it will generate FD_CLOEXEC to close the file and then open it E. With dup2, if fd equals fd2, then dup2 returns fd2 without closing it. Thank You