V
venkat
Hi,
I am learing Unix internals. I have come across a problem where
i am not able to understand what is happening. As i gone through the
book i found that lseek will give the physical descriptor and ftell
will give about logical descriptor, but when i read the text using
fread the ftell and lseek should give diffrent values. but when new
process comes into to existence ftell should use lseek value. but is
not happening the output line given below and the code is given
below.
#include<stdio.h>
#include <unistd.h>
main()
{
FILE *fp;
char buf[10];
int pid , dip;
fp = fopen("vikas", "r");
pid = fork();
if (pid == 0) {
printf("in child before read fseek is %d , lseek is %d \n",
ftell(fp), lseek(fp->_file ,0, 1));
fread(buf, sizeof buf , 1, fp);
buf[10] ='\0';
printf("after child read file pointer ftell is %d, lseek is %d
%s\n", ftell(fp), lseek(fp->_file ,0, 1)
, buf);
sleep(5);
fread(buf, sizeof buf, 1, fp);
buf[10] = '\0';
printf("after child 2nd time read file pointer is %d , lseek is
%d %s\n", ftell(fp), lseek(fp->_file, 0
, 1), buf);
}
else {
wait(0);
printf("intially in parent file ponte %d, lseek is %d %s\n",
ftell(fp), lseek(fp->_file, 0, 1), buf);
fread(buf, sizeof buf, 1, fp);
buf[10] ='\0';
printf("after parent read file pointer is %d ,lseek is %d %s
\n", ftell(fp), lseek(fp->_file, 0, 1), bu
f);
}
}
output is
in child before read fseek is 0 , lseek is 0
after child read file pointer ftell is 10, lseek is 4096 AAAAAAAAAA
after child 2nd time read file pointer is 20 , lseek is 4096
AAAAAAAAAA
intially in parent file ponte 20, lseek is 20
after parent read file pointer is 30 ,lseek is 4096 AAAAAAAAAA
i am not able to undersand the parent portion. why the ftell is using
20 as for a new process it should use lseek value, and how come the
lseek value has been changed?.
Appreciate your help in this read.
Thanks,
Venkat.
I am learing Unix internals. I have come across a problem where
i am not able to understand what is happening. As i gone through the
book i found that lseek will give the physical descriptor and ftell
will give about logical descriptor, but when i read the text using
fread the ftell and lseek should give diffrent values. but when new
process comes into to existence ftell should use lseek value. but is
not happening the output line given below and the code is given
below.
#include<stdio.h>
#include <unistd.h>
main()
{
FILE *fp;
char buf[10];
int pid , dip;
fp = fopen("vikas", "r");
pid = fork();
if (pid == 0) {
printf("in child before read fseek is %d , lseek is %d \n",
ftell(fp), lseek(fp->_file ,0, 1));
fread(buf, sizeof buf , 1, fp);
buf[10] ='\0';
printf("after child read file pointer ftell is %d, lseek is %d
%s\n", ftell(fp), lseek(fp->_file ,0, 1)
, buf);
sleep(5);
fread(buf, sizeof buf, 1, fp);
buf[10] = '\0';
printf("after child 2nd time read file pointer is %d , lseek is
%d %s\n", ftell(fp), lseek(fp->_file, 0
, 1), buf);
}
else {
wait(0);
printf("intially in parent file ponte %d, lseek is %d %s\n",
ftell(fp), lseek(fp->_file, 0, 1), buf);
fread(buf, sizeof buf, 1, fp);
buf[10] ='\0';
printf("after parent read file pointer is %d ,lseek is %d %s
\n", ftell(fp), lseek(fp->_file, 0, 1), bu
f);
}
}
output is
in child before read fseek is 0 , lseek is 0
after child read file pointer ftell is 10, lseek is 4096 AAAAAAAAAA
after child 2nd time read file pointer is 20 , lseek is 4096
AAAAAAAAAA
intially in parent file ponte 20, lseek is 20
after parent read file pointer is 30 ,lseek is 4096 AAAAAAAAAA
i am not able to undersand the parent portion. why the ftell is using
20 as for a new process it should use lseek value, and how come the
lseek value has been changed?.
Appreciate your help in this read.
Thanks,
Venkat.