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).