Tuesday, July 28, 2009

C programming problem related to lseek and dup2?

I just start learning to program in Linux and have problem like this.





1.


The following code is used to append 10 bytes to a file:


if (lseek(fd,0,SEEK_END) %26lt; 0 || write(fd, buf, 10) != 10)


{


opps("Seek/Write failed");


}


Assuming that none of the functions calls fail, buf is big enough, we have write permis-


sion, and fd refers to a file opened for writing then does the above code always works,


never works or sometimes works? Why?





2.


Consider the following C code:


int fd1, fd2;


char buf;


fd1 = open("file",O_RDONLY); /*open for reading */


fd2 = open("file",O_RDONLY); /*open for reading */


fd2 = dup2(fd1,fd2); /* make fd2 be the copy of fd1 */


close(fd1); /* we do not need fd1 anymore */


Assuming that none of the above function calls fail, explain in detail what happens with


the following line of C code.


read(fd2, %26amp;buf, 1);








Can someone help me,please!

C programming problem related to lseek and dup2?
how about doing your own homework?
Reply:wat


No comments:

Post a Comment