File read/ write operation


 
Thread Tools Search this Thread
Operating Systems Linux File read/ write operation
# 1  
Old 05-27-2010
File read/ write operation

Hi,

I am creating a progress bar for file upload for which I have CGI script which copies the data and depending on certain bytes it increments the progress bar. Here, I am writing the incremented value to a file which is read by Ajax at server end. However, here I want to ask that, is it possible for two different processes to write and read data simultaneously from the same file?

If not is there any other way by which I can write and read simultaneously?


Thanks
# 2  
Old 05-27-2010
Short answer, yes, it is possible.

In order to keep the reader consistent you have to employ some kind of IPC - sockets, shared memory, mutex, file locking. Example: the reader tries to read whenever it feels like it, it will probably hit an EOF, which is not the real EOF, because reader did not wait for writer to finish.

Check out the flock() (advisory lock) call.
edit -
My bad - you are writing perl (probably). A better perler than me will have to advise about flock.
# 3  
Old 05-27-2010
Well I do not wish to use IPC at this time.
So without that it is not possible, right?

Code:
 while (cgiFormFileRead(file, b, sizeof(b), &got_count) == cgiFormSuccess)
        {
        fptr = fopen("progress_bar.txt", "w");
        fwrite(b,sizeof(char),got_count,output);
        i++;
                if(i == inc && j<=100)
                {
                fprintf(fptr, "%d" ,j);
                fseek(fptr, 0, SEEK_SET);
                i = 0;
                //fflush(fptr);
                fclose(fptr);
                j++;
                }
        }

So, my ajax reads the text file after every 1 sec or so, however the data is not available until whole file is written by the CGI. Any suggestion?

---------- Post updated at 01:24 PM ---------- Previous update was at 01:11 PM ----------

No I am using C.

Last edited by Scott; 05-27-2010 at 03:17 PM.. Reason: Fixed code tag
# 4  
Old 05-27-2010
Quote:
Originally Posted by xs2punit
Well I do not wish to use IPC at this time.
So without that it is not possible, right?
Without some form of communication or synchronization between the reader and the writer the reader will not be able to tell the difference between the real end of the file, a temporary underrun, a broken socket, etc, etc.
# 5  
Old 05-27-2010
Well the ajax reads the data synchronously ie

Code:
function send_request()
{
request.onreadystatechange = request_handler;   
request.open("GET","progress_bar.txt",true);    //true means that Ajax will get data asynchronously
request.send(null);
}
function request_handler() //function to check if data is available for read
{
if (request.readyState == 4 && request.status == 200)
        {
        document.getElementById("progress").innerHTML= request.responseText + '%';
        document.getElementById("progress").style.width = request.responseText + '%';
        document.getElementById("progress").style.backgroundColor = "green";
         }
}

So, here the send_request() method is called after every 500ms(changeable). It can read the data only when request.readyState = 4 and this gets true only when whole file is written NOT at each data write.

I hope you understood what I mean to say.

Last edited by Scott; 05-27-2010 at 08:15 PM..
# 6  
Old 05-27-2010
Is the object request.readyState in shared memory - can both reader and writer process see exactly the same object? If not then there needs to be a way using IPC to get the other process to see request.readyState.

If this is so and you don't like IPC:
You have to use advisory file locking - period. Since you're in C it ain't hard.
# 7  
Old 05-27-2010
I think I get what you're saying, it doesn't change the problem. The AJAX can't tell it the difference between an EOF before the file is finished and the real EOF.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read and write in the file

Hello Guys, How all are doing? I have an issue in Unix and want help from all of you I have a file in UNIX which it read by line by line , If at the end of line '0' is written the it should fetch that line into another file and change '0' to '1' and If at the end of line '1' is written then it... (10 Replies)
Discussion started by: adisky123
10 Replies

2. Shell Programming and Scripting

File Read and Write

I have got a file in following format: AAAAAAA BBBBBBBB CCCCCCC DDDDDDD I am trying to read this file and out put it in following format: AAAAAAA,BBBBBBB,CCCCCCC,DDDDDD Preferred method is shell or Perl. Any help appreciated. (11 Replies)
Discussion started by: Araoki
11 Replies

3. Shell Programming and Scripting

Grab data between 2 keywords any do an array operation and write the file intact

Hi Unix Gurus, I need to grep for a block that is between a start and end keyword and then in between I need to find and replace a keyword. for eg: I need to search between Test = 000; and Test = 000; and find K9 and replace with M9 INPUT FILE Define { Replace = K9; Test =... (6 Replies)
Discussion started by: naveen@
6 Replies

4. Shell Programming and Scripting

Transferring file write operation to another host

I have a process running on two hosts in Active/Standby mode. Process running on active machine is handling the traffic while process running on Standby machine is sitting idle. We have a debugging mechanism in which logs are generated on local machine. When logging is enabled then process running... (1 Reply)
Discussion started by: akhilonnet
1 Replies

5. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

6. Solaris

monitoring multipath IO for read and write operation

Hi Folks, I would like to monitor multipath IO on solaris for write and read operations. Does "sar -d" include multipath IO information along with other block devices ? Thanks, Faizan. (0 Replies)
Discussion started by: sifaizan
0 Replies

7. UNIX for Dummies Questions & Answers

Write/read to file descriptors

Is it possible to write to file descriptor 0 and read from 1 or 2? How could this be implemented? (3 Replies)
Discussion started by: machshev
3 Replies

8. UNIX for Dummies Questions & Answers

Simultaneous file read operation

Hi All, I need information on file handling in UNIX enviornment. If in UNIX enviornment I have two process.. P1 P2 Both accessing the same file simultaneosuly(read operation).. Suppose P1 opened the file in read mode first and started reading the file.. Then P2 opens the same file in... (5 Replies)
Discussion started by: chibob
5 Replies

9. Shell Programming and Scripting

read and write from a file

I have tried to show the file name whose size is greater than 200 byte in current directory. Please help me. ls -l | tr -s " " " " | cut -f 5,9 -d " " >out.txt #set -a x `cat out.txt` i=0 `cat out.txt` | while do read x echo $x #re=200 j=0 if }" < "200" ] then echo $j j=`expr $j... (2 Replies)
Discussion started by: rinku
2 Replies

10. IP Networking

how do you get to know if the write operation was succesful.

how do you get to know if the write operation for writing to a socket was succesful was succesful. (3 Replies)
Discussion started by: arjunjag
3 Replies
Login or Register to Ask a Question