"struct file" - per process per file?

"struct file" - per process per file?

am 29.03.2007 18:41:48 von Rajat Jain

Hi,

This has reference to LDD3 chap3 - Char drivers (Pg 60).

The following is my understanding which seems to be incorrect. Can
somebody please point out what am I missing?

LDD3 states that the fops->open() and fops->release() methods are
called only when "struct file" is created / destroyed. It further says
that "struct file" is created ONLY when a process calls open() system
all on a file. To give an example, it also says that if process 1
opens file A and then forks another process B, then A and B will share
the same struct file??

This seems quite strange to me as I had an understanding that two
processes working on the same file will always have different file
structure. Also, this would mean that a read by one of the processes
on the file would affect the file position pointer in the other
process?

I am sure I am missing something here. What is it?

Thanks,

Rajat

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ

RE: "struct file" - per process per file?

am 30.03.2007 03:51:52 von Gaurav Dhiman

This is a multi-part message in MIME format.

------_=_NextPart_001_01C7726D.FD1747A5
Content-Type: text/plain;
charset="iso-2022-jp"
Content-Transfer-Encoding: 7bit

Hi Rajat,

Find my inline response, marked wih "GD>".
As my mail client does not support text response, so sorry for such a marking tag.


From: kernelnewbies-bounce@nl.linux.org 代理 Rajat Jain
Sent: 3/29/2007 (木) 10:11 午後
To: kernelnewbies; newbie
Subject: "struct file" - per process per file?



Hi,

This has reference to LDD3 chap3 - Char drivers (Pg 60).

The following is my understanding which seems to be incorrect. Can
somebody please point out what am I missing?

LDD3 states that the fops->open() and fops->release() methods are
called only when "struct file" is created / destroyed. It further says
that "struct file" is created ONLY when a process calls open() system
all on a file. To give an example, it also says that if process 1
opens file A and then forks another process B, then A and B will share
the same struct file??


GD> This is correct. whenever a process opens the file, corresponding "struct file" structure is created in kernel. As you might be knowing that this is just a virtual file in kernel which keeps track of attributes of actual file, like where to read/write, pointer to file operation structures etc. Theseattributes makes sense when a file is opened, till the file is not closed, at which time fops->release() function is called.


This seems quite strange to me as I had an understanding that two
processes working on the same file will always have different file
structure. Also, this would mean that a read by one of the processes
on the file would affect the file position pointer in the other
process?


GD> Wherever a process forks another process all the "struct file" strucutres are shared between both processes unless and untill any one of those processes does not write to it at which time a seperate copy is created, this is also know as COW (Copy On Write). When a process is forked, no other "struct file" structure is created, rather the refference count for the "struct file" structures are incremented and there pointers are also saved in new process's file table (struct file_struct).


I am sure I am missing something here. What is it?

Thanks,

Rajat

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ




------_=_NextPart_001_01C7726D.FD1747A5
Content-Type: text/html;
charset="iso-2022-jp"
Content-Transfer-Encoding: 7bit







"struct file" - per process per file?



Hi Rajat,

 

Find my inline response, marked wih
"GD>".

As my mail client does not support
text response, so sorry for such a  marking tag.

 

 

From:
kernelnewbies-bounce@nl.linux.org 代理 Rajat Jain
Sent: 3/29/2007 (木)
10:11 午後
To: kernelnewbies; newbie
Subject: "struct file" -
per process per file?



Hi,

This has reference to LDD3 chap3 - Char drivers (Pg
60).

The following is my understanding which seems to be incorrect.
Can
somebody please point out what am I missing?

LDD3 states that the
fops->open() and fops->release() methods are
called only when "struct
file" is created / destroyed. It further says
that "struct file" is created
ONLY when a process calls open() system
all on a file. To give an example, it
also says that if process 1
opens file A and then forks another process B,
then A and B will share
the same struct file??


GD> This is correct. whenever a process opens the file,
corresponding "struct file" structure is created in kernel. As you might be
knowing that this is just a virtual file in kernel which keeps track of
attributes of actual file, like where to read/write, pointer to file operation
structures etc. Theseattributes makes sense when a file is opened, till the file
is not closed, at which time fops->release() function is
called.



This seems quite strange to me as I had an understanding that
two
processes working on the same file will always have different
file
structure. Also, this would mean that a read by one of the
processes
on the file would affect the file position pointer in the
other
process?


GD> Wherever a process forks another process all the "struct file"
strucutres are shared between both processes unless and untill any one of those
processes does not write to it at which time a seperate copy is created, this is
also know as COW (Copy On Write). When a process is forked, no other "struct
file" structure is created, rather the refference count for the "struct file"
structures are incremented  and there pointers are also saved in new
process's file table (struct file_struct).


 
I am sure I am missing something here. What is
it?

Thanks,

Rajat

--
To unsubscribe from this list: send
an email with
"unsubscribe kernelnewbies" to ecartis@nl.linux.org
Please
read the FAQ at href="http://kernelnewbies.org/FAQ">http://kernelnewbies.org /FAQ





------_=_NextPart_001_01C7726D.FD1747A5--


--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ

Re: "struct file" - per process per file?

am 30.03.2007 04:34:02 von Rajat Jain

> This has reference to LDD3 chap3 - Char drivers (Pg 60).
>
> The following is my understanding which seems to be incorrect. Can
> somebody please point out what am I missing?
>
> LDD3 states that the fops->open() and fops->release() methods are
> called only when "struct file" is created / destroyed. It further says
> that "struct file" is created ONLY when a process calls open() system
> all on a file. To give an example, it also says that if process 1
> opens file A and then forks another process B, then A and B will share
> the same struct file??
>
>
> GD> This is correct. whenever a process opens the file, corresponding
> "struct file" structure is created in kernel. As you might be knowing that
> this is just a virtual file in kernel which keeps track of attributes of
> actual file, like where to read/write, pointer to file operation structures
> etc. Theseattributes makes sense when a file is opened, till the file is not
> closed, at which time fops->release() function is called.
>
>
> This seems quite strange to me as I had an understanding that two
> processes working on the same file will always have different file
> structure. Also, this would mean that a read by one of the processes
> on the file would affect the file position pointer in the other
> process?
>
>
> GD> Wherever a process forks another process all the "struct file"
> strucutres are shared between both processes unless and untill any one of
> those processes does not write to it at which time a seperate copy is
> created, this is also know as COW (Copy On Write). When a process is forked,
> no other "struct file" structure is created, rather the refference count for
> the "struct file" structures are incremented and there pointers are also
> saved in new process's file table (struct file_struct).
>

Hi Gaurav,

Yes I understand the CoW procedure. So what you are syaing that when
the process A forks process B, both of them point to same "struct
file" (with a ref count of 2). What I am wondering is that what
happens once both the processes actually start writing to those files.

1) In case a seperate copy is created (CoW) at this point for the
forked thread, is open() method of the driver called?

2) In case both the processes continue to use the same "struct file"
wouldn't reads of one file affect the reads of another (since the
location ptr in file will be affected)?

Thanks,

Rajat
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs