regarding lseek and fread

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.
 
W

Walter Roberson

venkat said:
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,

lseek() and fork() and unistd.h are not defined by the C language.
You should ask your question in a newsgroup that deals with your
OS internals.

printf("in child before read fseek is %d , lseek is %d \n",
ftell(fp), lseek(fp->_file ,0, 1));

<OT>
lseek is defined as returning off_t which might well not be
an int (and usually is not), so using a %d format for the
output of the result of lseek() is not correct.

By the way, for portability you should not be assuming the
existance of a _file member of the FILE structure. That's
an implementation aspect that is subject to change. You should
use the fileno() macro.

But for the rest of your question, after making the above fixes,
you need to consult a unix newsgroup.
 
M

Martin Ambuhl

venkat said:
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.

You have aseveral simple errors as far as the C programming language is
concerned. ftell() returns a long and you incorrectly try to print that
value with the designator "%d". In addition, you attempt illegally to
buf[10] ='\0';
but there is no such buf[10]. This should cause in segfault in most
varieties of Unix. Further, you make no effor to check that "vikas" was
successfully opened for read. You combine the incompatible errors of
using an implicit int return type for main (allowed only before C99) and
omitting the explicit return (or exit()) from main (allowed only in C99
and later).

As far as this newsgroup is concerned, the difference between lseek and
ftell is simple: there is a standard C function called ftell, there is
not such a standard C function called lseek. Since C I/O identifies
streams through pointers-to-FILE and the most common definition of lseek
(POSIX, but in the programmer's namespace in C, so it could be anything)
identifies the stream with an integer "file descriptor", you have a big
hint that lseek is not a standard C function. The fact that you need to
include the non-standard <unistd.h> header is another big hint.
Additionally, form(), wait(), and sleep() are not standard C functions.

What this means is that you need to ask your question in a newsgroup
where lseek is topical. That means one concerned with POSIX or some
near-relative of Unix.


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

fread/fwrite 2 18
fread() 6
A question of fork() function 3
URGENT 1
Linux: using "clone3" and "waitid" 0
An exercise in fread optimisation 10
fread() function and reading binary files 2
Help with EXT3 Filesystem work 1

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top