top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

OS: How does file descriptor duping works in linux operating system ?

+1 vote
362 views
OS: How does file descriptor duping works in linux operating system ?
posted Apr 11, 2016 by Vikram Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

dup() system call in linux systems copies a file descriptor into the first free slot in the private file descriptor table and then returns the new file descriptor to the user. It works for all the file types. The syntax is :

  newfd = dup(fd);

Here fd is the file descriptor being duped and newfd is returned to the user.dup() system call doesnt create a separate entry in the global file table like the open() system call, instead it just increments the count field of the entry pointed to by the given input file descriptor in the global file table.
How it works: Consider an example where fd 0, 1 and 2 are by default engaged to the standard input/output and error. Then if the user opens a file "/var/file1" (fd - 3), then he opens file "/var/file2" (fd - 4) and again he opened "/var/file1" (fd - 5). And now, if he does a dup(3), kernel would follow the pointer from the user file descriptive table for the fd entry '3', and increments the count value in the global file table. Then, it searches for the next available free entry in file descriptor table and returns that value to the user (6 in this case).
enter image description here

answer May 23, 2016 by Shivam Kumar Pandey
Similar Questions
0 votes

It is very basic query but clearing the doubt always make your knowledge stronger.

+1 vote

What happens after you write “a.out” and press enter. What are the functionality performed by the OS after executable file is created of your code.

...