Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between dup and open on same file.

 
Ankur Jain Kothari
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic