• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

difference between dup and open on same file.

 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic