Sponsored Content
Full Discussion: File read/ write operation
Operating Systems Linux File read/ write operation Post 302425274 by jim mcnamara on Thursday 27th of May 2010 02:07:23 PM
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.
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
FLOCK(3)								 1								  FLOCK(3)

flock - Portable advisory file locking

SYNOPSIS
bool flock (resource $handle, int $operation, [int &$wouldblock]) DESCRIPTION
flock(3) allows you to perform a simple reader/writer model which can be used on virtually every platform (including most Unix derivatives and even Windows). On versions of PHP before 5.3.2, the lock is released also by fclose(3) (which is also called automatically when script finished). PHP supports a portable way of locking complete files in an advisory way (which means all accessing programs have to use the same way of locking or it will not work). By default, this function will block until the requested lock is acquired; this may be controlled with the LOCK_NB option documented below. PARAMETERS
o $handle -A file system pointer resource that is typically created using fopen(3). o $operation -$operation is one of the following: o LOCK_SH to acquire a shared lock (reader). o LOCK_EX to acquire an exclusive lock (writer). o LOCK_UN to release a lock (shared or exclusive). It is also possible to add LOCK_NB as a bitmask to one of the above operations if you don't want flock(3) to block while locking. o $wouldblock - The optional third argument is set to 1 if the lock would block (EWOULDBLOCK errno condition). RETURN VALUES
Returns TRUE on success or FALSE on failure. CHANGELOG
+--------------+---------------------------------------------------+ | Version | | | | | | | Description | | | | +--------------+---------------------------------------------------+ |5.5.22, 5.6.6 | | | | | | | Added support for the $wouldblock parameter on | | | Windows. | | | | | 5.3.2 | | | | | | | The automatic unlocking when the file's resource | | | handle is closed was removed. Unlocking now | | | always has to be done manually. | | | | +--------------+---------------------------------------------------+ EXAMPLES
Example #1 flock(3) example <?php $fp = fopen("/tmp/lock.txt", "r+"); if (flock($fp, LOCK_EX)) { // acquire an exclusive lock ftruncate($fp, 0); // truncate file fwrite($fp, "Write something here "); fflush($fp); // flush output before releasing the lock flock($fp, LOCK_UN); // release the lock } else { echo "Couldn't get the lock!"; } fclose($fp); ?> Example #2 flock(3) using the LOCK_NB option <?php $fp = fopen('/tmp/lock.txt', 'r+'); /* Activate the LOCK_NB option on an LOCK_EX operation */ if(!flock($fp, LOCK_EX | LOCK_NB)) { echo 'Unable to obtain lock'; exit(-1); } /* ... */ fclose($fp); ?> NOTES
Note flock(3) uses mandatory locking instead of advisory locking on Windows. Mandatory locking is also supported on Linux and System V based operating systems via the usual mechanism supported by the fcntl() system call: that is, if the file in question has the set- gid permission bit set and the group execution bit cleared. On Linux, the file system will also need to be mounted with the mand option for this to work. Note Because flock(3) requires a file pointer, you may have to use a special lock file to protect access to a file that you intend to truncate by opening it in write mode (with a "w" or "w+" argument to fopen(3)). Note May only be used on file pointers returned by fopen(3) for local files, or file pointers pointing to userspace streams that imple- ment the streamWrapper.stream_lock(3) method. Warning Assigning another value to $handle argument in subsequent code will release the lock. Warning On some operating systems flock(3) is implemented at the process level. When using a multithreaded server API like ISAPI you may not be able to rely on flock(3) to protect files against other PHP scripts running in parallel threads of the same server instance! flock(3) is not supported on antiquated filesystems like FAT and its derivates and will therefore always return FALSE under this environments (this is especially true for Windows 98 users). PHP Documentation Group FLOCK(3)
All times are GMT -4. The time now is 03:38 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy