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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



>
> 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??

LDD3 is correct. Processes A and B will share the same struct file
in case of fork.

>
> 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?

It is not strange. It depends on how the two processes work on the same
file. If each one opens the file, then two struct file structures are created
in the kernel. This way, a read by one of the processes does not affect
the file position pointer in the other process. But it does affect in case
of fork.

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

Here is a small example to demonstrate the fork situation.
This small program reads data either from stdin or an input file.
In case of a file, it first opens the file for reading. It then forks.
Then both the parent and the child are reading. When you try it out,
you'll see that both affect each others read position.


Whooaa!! That is a shocking discovery. I had an understanding that 2
processes will never share any resource except when EXPLICITLY told
to. Thanks for clarifying my doubts.

However, I am wondering how is it going to work out in a scenario
where file locking (flock?) is implemented?

Thanks,

Rajat


#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
   #define SIZE    60
   char buf[SIZE];
   int fd=0;
   int st;
   int rd;

   if(argc==2)
       fd=open(argv[1], O_RDONLY);

   if(fork()){
       do{
           rd=read(fd, buf, SIZE);
           write(1, "\nParent:\n", sizeof("Parent: "));
           write(1, buf, rd);
           sleep(1);
       }while(rd);
       wait(&st);
   }
   else{
       do{
           rd=read(fd, buf, SIZE);
           write(1, "\nChild:\n", sizeof("Child: "));
           write(1, buf, rd);
           sleep(1);
       }while(rd);
   }
   return 0;
}


BlackHole

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



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


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux