LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-01-2013, 03:11 AM   #1
SIG_SEGV
Member
 
Registered: Jul 2012
Location: Banglore, INDIA
Distribution: Fedora-Core
Posts: 70

Rep: Reputation: 11
Red face buffer allocation for single file opened twice in different modes


hi guys,
I have got bit of a confusion after going through buffer allocation algorithms from Maurice J Bach. Following Hash queues, It says that if a file is opened already in some process and another attempt is made to open the same file again by another process, then a common buffer of file data is shared b/w 2 procs.

So, to test this i tried for an example.

PHP Code:
Open a same file in 2 diff process (1 in read and other in write mode). 
If 
a common buffer was allocated to both the processthe whatever i write into file in write modethe same should be reflected for the other process that is reading the fileButthis didnt happenCan any one elaborate the actual concept please:(???? 
 
Old 04-01-2013, 04:06 AM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,600

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
The file being open for write gets a separate buffer to hold the new data.

The shared buffers only occur if both processes have the file open in read mode.

You also have some odd formatting causing everthing to be long lines without wrap.
It makes it hard to read.
 
1 members found this post helpful.
Old 04-01-2013, 04:22 AM   #3
SIG_SEGV
Member
 
Registered: Jul 2012
Location: Banglore, INDIA
Distribution: Fedora-Core
Posts: 70

Original Poster
Rep: Reputation: 11
Well, thanx for the answer. but how to solve this odd formatting problem of my threads.
 
Old 04-01-2013, 06:01 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,600

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
I suspect it is indirectly caused by the "PHP" coding...

Usually used for fixed format, but you put a long line instead.

It is intended for use in preserving the format of code blocks, rather than quoting.
 
Old 04-02-2013, 12:40 AM   #5
SIG_SEGV
Member
 
Registered: Jul 2012
Location: Banglore, INDIA
Distribution: Fedora-Core
Posts: 70

Original Poster
Rep: Reputation: 11
@ipollard

Sorry bro, I cant accept your answer about the buffer allocn concept after realizing by going through some tests.
Its not like as you said:
Quote:
The file being open for write gets a separate buffer to hold the new data.

The shared buffers only occur if both processes have the file open in read mode.
when a read or write sys call is going to operate, it first locks the file inode (shared by 2 procs) and then proceeds,
so that no other process changes the ref buffer's data. once the inode is released after read operation the write process
can change buffer's data no matter what old data was. After this write if read process continues its operation, it ends up reading
unexpected data. So, to say finally

"Inodes are locked during any I/O operations and they are left unlocked b/w 2 I/O operations. At this time other process can acquire lock and
can change shared buffer data, resulting in unexpected O/P for the read process. So, for any file open in the O.S whether for READ or WRITE, its
clear that ONE AND ONLY ONE BUFFER IS ALLOCATED FOR IT. NO MATTER HOW MANY PROCESS USE/MANIPULATE it."
 
Old 04-02-2013, 07:39 AM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,600

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
It can't lock the inode - multiple parallel read/writes are perfectly permissible.

Now fcntl can lock entire stretches of bytes, and multiple reads/writes carried out on different parts of the file... even with
locks, as the locks are only advisory, UNLESS the file is marked for mandatory locking.

http://www.thegeekstuff.com/2012/04/...locking-types/

The file block buffer itself is under a RCU lock if I remember right, thus the write gets (temporarily)
a separate block during actual transfer of bytes, and permitting both read and write simultaneously.
 
Old 04-03-2013, 01:10 AM   #7
SIG_SEGV
Member
 
Registered: Jul 2012
Location: Banglore, INDIA
Distribution: Fedora-Core
Posts: 70

Original Poster
Rep: Reputation: 11
M just trying to ensure you that:

Quote:
For any file open in the O.S whether for READ or WRITE, its
clear that ONE AND ONLY ONE BUFFER IS ALLOCATED FOR IT. NO MATTER HOW MANY PROCESS USE/MANIPULATE it."
To get convinced try below example:
Run the below 2 procs simultaneously and just feed them with required key hit alternatively and YOU will
feel the buffer manipulation that is COMMON for both the procs.

process 1: read

Quote:
int main ()
{
int fd; char buff[1];
fd = open ("./num", O_RDONLY); // num is a text file that contains "1234567890"
while (1)
{
getchar ();
read (fd, buff, 1);
putchar ( *buff );
}
return 0;
}
process 2: write

Quote:
int main ()
{
int fd;
char buff[1];buff[0] = 'A';
fd = open ("./num", O_WRONLY);
while (1)
{
getchar ();
write (fd, buff, 1);
}
return 0;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
allocation of page_cgroup failed on a single node system BenjaminLost Linux - Kernel 0 04-07-2011 07:53 AM
getting SDL to use a single frame buffer knobby67 Programming 1 08-14-2010 03:33 PM
Processor priority level for Algorithm for Buffer Allocation manavendra Programming 1 01-09-2010 07:21 PM
gvim - double quote char always replaced with two single quotes in special modes JazzItSelf Linux - Software 4 09-16-2008 12:26 PM
Allocation of memory for single bit mailsrinu28 Linux - Software 4 09-07-2006 04:10 PM


All times are GMT -5. The time now is 03:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration