ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
vivek@vivek-friend:~/v/syspro$ ./a.out
After child read file pointer ftell-> 11 AAAAAAAAAA
Initially in parent process file pointer 4096 '
After read file pointer 4107 BBBBBBBBBB
vivek@vivek-friend:~/v/syspro$ In child process after parent read file pointer ftell->11 AAAAAAAAAA
After child 2nd time read file pointer 22 AAAAAAAAAA
Q 1. "In child process after parent read file pointer ftell->11 AAAAAAAAAA"
In the above line of the output, from the book i am reading it should be
"In child process after parent read file pointer ftell->4107 AAAAAAAAAA"
As per the book it's saying that "If the temporary buffer into which the value read using fread(), had not been full. Remember, when an fread() is executed, by default 4096 characters are read into a temporary buffer. from there, 10 characters are transferred to our array buff. Now it's because this buffer is full that the ftell() show 4096(but im my case its showing 22).because fread() reads only when the buffer is empty.
Q 2. what is difference between logical file pointer and physical file pointer?
I haven't yet tried to follow all of your code, but I just recognized that you missed to use the return value of fopen(3). Though, you're still operating on the old, invalid (after fclose(3)) file pointer.
now output coming as
After child read file pointer ftell-> 11 AAAAAAAAAA
Initially in parent process file pointer 4096 ��
After read file pointer 4107 BBBBBBBBBB
vivek@vivek-friend:~/v/syspro$ In child process after parent read file pointer ftell->11 AAAAAAAAAA
After child 2nd time read file pointer 22 AAAAAAAAAA
no chanege....
I am not understanding wats going on after the child process execute after sleep()
First caveat - using the same file descriptor in multiple processes is going to cause problems.
Second caveat - both the parent and the child process use the same descriptor at the very start of their code. As it happens, the child is executed first, but this order is not defined.
Anyway, on to the behaviour of your example. Although the file descriptors are shared after the fork, the file pointer structure is not. This discrepancy is what causes the interesting behaviour:
1. The child process reads 11 bytes. Underneath, it has buffered in a block of 4096 bytes from the file.
2. The parent process then checks the file position. Because the underlying file has advanced by 4096 bytes, it reports 4096. It then reads in 11 bytes. Underneath, it has buffered in a second block of 4096 bytes from the file.
3. The child process then reads another 11 bytes. Because it still has bytes in the buffer, it simply grabs them from the buffer. It does not recheck the underlying file pointer, because it already has the cached value (even though the underlying file pointer is different). So it reports that it has reached position 22.
Even more interesting, you will find that if the child process continues reading past the end of the block of 'A's (the end of the first buffer), it will then read in the 'next' block containing 'C's (actually the third block), yet will report wrongly that the file position is only in the 4000s.
This behaviour may vary between systems, because it depends on the implementation of FILE and of fread. Your book may be assuming a different (older) implementation, or it may be assuming that the parent process ran first. It doesn't actually matter, because you are not intended to use the same file handle in two processes.
Last edited by neonsignal; 02-27-2010 at 06:58 AM.
else
{
printf("Initially in parent process befor fread: ftell-> %ld lseek->%ld buff->%s\n",ftell(fp),lseek(fp->fd,01,SEEK_CUR),buff);
fread(buff,sizeof(buff),1,fp);
buff[10]='\0';
printf("In parent process after fread :ftell-> %ld lseek->%ld,buff-> %s\n",ftell(fp),lseek(fp->fd,01,SEEK_CUR),buff);
}
}
fread2.c: In function ‘main’:
fread2.c:29: error: ‘FILE’ has no member named ‘fd’
fread2.c:33: error: ‘FILE’ has no member named ‘fd’
fread2.c:38: error: ‘FILE’ has no member named ‘fd’
fread2.c:43: error: ‘FILE’ has no member named ‘fd’
fread2.c:46: error: ‘FILE’ has no member named ‘fd’
As ftell() just tells value of logical file pointer position, so as to make my doubt more clear, i have used lseek to get the physical file pointer position also, but while running m getting error as
again when i have tried to replace the fd with _file
then in that case getting error as
fread2.c: In function ‘main’:
fread2.c:29: error: ‘FILE’ has no member named ‘_file’
fread2.c:33: error: ‘FILE’ has no member named ‘_file’
fread2.c:38: error: ‘FILE’ has no member named ‘_file’
fread2.c:43: error: ‘FILE’ has no member named ‘_file’
fread2.c:46: error: ‘FILE’ has no member named ‘_file’
I wants to know where in my computer i can see the FILE structure definition ?????
yeah m getting u a bit that we cant see the definition o FILE structureas it may be already compliled in the lirary or somwthing like this.
what can i do for the highlighted below, so that my lseek) execute properly.
printf("After child 2nd time read file pointer ftell->%ld lseek->%ld buff->%s\n",ftell(fp),lseek(fp->fd,01, SEEK_CUR), buff);
Even better, use a zero offset so that it just reports the current position:
Code:
lseek(fileno(fp), 0, SEEK_CUR)
Your stdio.h may be different (it will vary between operating systems), but on mine the contents of the struct are deliberately obscured. Even if you can access the contents, you probably shouldn't, because the resultant code will not be portable between different systems.
Last edited by neonsignal; 03-02-2010 at 05:37 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.