This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Linux / UNIX and the fly likes difference between dup and open on same file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "difference between dup and open on same file. " Watch "difference between dup and open on same file. " New topic
Author

difference between dup and open on same file.

Ankur Jain Kothari
Ranch Hand

Joined: Feb 08, 2010
Posts: 154
Can anyone explain to me the diference between these two?

int fd1 = open("cricket.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
int fd2 = open("cricket.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);

and

int fd1 = open("cricket.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
int fd2 = dup(fd1);

Thanks a ton

Cheers


Do What You Wanna Be....Taking Things The Way They Come
scjp 1.6 91 percent, scmad 90 percent(rounded off to nearest integer)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

If you use dup(), the two descriptors are somewhat connected: the second file descriptor uses the same underlying file description. That means, for example, if you use lseek() with an offset of 100 on one descriptor, and then read from the other one, you'll read starting from offset 100.

If you open() two separate descriptors, however, a seek on one descriptor won't affect the other one at all, and you can read from offset 100 in one file and offset 200 in the other file, without interference.


[Jess in Action][AskingGoodQuestions]
 
jQuery in Action, 2nd edition
 
subject: difference between dup and open on same file.
 
Similar Threads
clone() mehtod
how come this doesn't work java?
Problems with debugging servlets (JDevloper 9i)
How to check given field whether primitive or user defined
My certification status not updated yet