Wow.Learned something new thanks dude.I am learning fd.I have another little question:
The following code use dup() to duplicate fd but my fd1=3, when I did fd2=dup(fd1) then why fd2 is not 3. My output shows fd2=4:
dup() duplicates an existing file descriptor (in this case fd=3) and returns
the new descriptor to the calling process. It does not close the existing
fiel descriptor.
The new descriptor returned by the call to dup() is the lowest numbered
descriptor currently not in use by the process (in this case fd=4)
Two open fd's never share the same value. When you dup(), you just assure they'll read the same file. fd1 != fd2, but read(fd1, buf, sizeof (buf)) will return the same as read(fd2, buf, sizeof (buf)).