File read/delete synchronization in unix

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers File read/delete synchronization in unix
# 1  
Old 02-08-2008
Java File read/delete synchronization in unix


I have 2 processes that will be accessing the same file. First process first reads from the file, closes it and then opens it again to write to it (this is done sequentially). The second process may delete the file on some condition. These processes are independent of each other now.

What I'd like to know is whether unix handles the delete synchronously i.e. if the file is being read and the first process is holding on to the file handler, will the second process fail deletion, or will the deletion succeed but the read will fail. Similarly when the first process is doing the write.

As of now we wouldn't like to introduce any special synchronization between these two processes if the behavior is known. Please let me know where I can find more details about this.

# 2  
Old 02-08-2008
I'm not sure about what or why you're doing things...?

This is how unlink (remove() in C or rm in shell ) works: it deletes the file from the directory, but the file's data and any currently open file descriptors can see that data.
When the last file descriptor to the file close, then the data itself goes away.

This means process #1 can keep on reading the file. When it is done with the file -- and if it was deleted by process #2, then the file's data will no longer be accessible.

If process #2 opens another file with the same file name it will not affect process #1's data.

So, why are you doing this? The only reason I know of to delete a file that is open is to hide it from all other processes. It's a good security measure.

You can achieve synchronization using a lock file - another file that tells process #1 and process #2 when they can do something with the data - if you need it.
# 3  
Old 02-08-2008
Thanks for your input. So this means when process #1 is doing read while process #2 performs the delete on the file - both will succeed without any failure. Will this also be true when process #1 is writing to the file?

Yes. The reason we want to delete the file is to hide from any other process but don't want to fail a process that maybe currently having an open file descriptor. So long as we are guarenteed there will not be a failure, we'd like to not do any explicit synchronization.

Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read CSV file and delete hdfs, hive and hbase tables

I have a CSV file with hdfs directories, hive tables and hbase tables. 1. first column - hdfs directories 2. second column - hive tables 3. third column - hbase tables I have to check the csv file and look for the first column and delete the hdfs directory from the hdfs path, now... (2 Replies)
Discussion started by: shivamayam
2 Replies

2. UNIX for Beginners Questions & Answers

How to give permissions to read write but not delete the file?

i want to give users the ability to create write and read files in other user directory , but not to have option to delete the file after created ( sticky bit not going to work here ... ) for example : i have user : manager with directory repository i have user : worker1 that need to write... (4 Replies)
Discussion started by: umen
4 Replies

3. UNIX for Dummies Questions & Answers

How to read a file without opening the file and delete last line?

I have file called "text". The contents are as below : aaa bbb ccc ddd eee ffff ddd hhhh iiii I want to read this file without opening and and delete the last line. How can it be done? (4 Replies)
Discussion started by: the_hunter
4 Replies

4. Shell Programming and Scripting

Help with UNIX script --Read from one file and delete entries in other

Hi Guru's The script has to read an entry from one file and delete the set of lines form other file. Below is the format of the file. In the below example, script should read the entries from input file 2 and delete the entries from input file 1. Input file 1 cn: abc:... (7 Replies)
Discussion started by: Samingla
7 Replies

5. Shell Programming and Scripting

Read column from file and delete rows with some condition..

Hi.... I have a need of script to do delete row whenever condition is true.... 2.16 (3) 1 3 9999 0 (1) (0) 34.42 (4) 1 3 9999 37 (2) (3) 34.38 (4) 1 3 9999 64 (2) (3) 34.4 (4) 1 3 1 ... (13 Replies)
Discussion started by: nex_asp
13 Replies

6. Shell Programming and Scripting

echo ls to a file and then read file and selectively delete

I'm trying to write a script that will do an ls of a location, echo it into a file, and then read that file and selectively delete files/folders, so it would go something like this: cd $CLEAN_LOCN ls >>$TMP_FILE while read LINE do if LINE = $DONTDELETE skip elseif LINE =... (2 Replies)
Discussion started by: MaureenT
2 Replies

7. Solaris

Before I delete any file in Unix, How can I check no open file handle is pointing to that file?

I know how to check if any file has a unix process using a file by looking at 'lsof <fullpath/filename>' command. I think using lsof is very expensive. Also to make it accurate we need to inlcude fullpath of the file. Is there another command that can tell if a file has a truely active... (12 Replies)
Discussion started by: kchinnam
12 Replies

8. Shell Programming and Scripting

Delete files older than 3 months.(read date from the name of the file)

Guys, My log files stored in the date format format below(log_20080714072942): TIMESTAMP=`date +%Y%m%d%H%M%S` LOG=/log/log_${TIMESTAMP}.log I'm looking for a shell script which deletes all files which is older than 3 months from today. Regards, Bhagat (3 Replies)
Discussion started by: bhagat.singh-j
3 Replies

9. Shell Programming and Scripting

How to delete multiple space or tabs from a read only file

Hi, Actually I am want to cut the three fields of "file-nr" file. $ cat /proc/sys/fs/file-nr 638 219 52270 I want to assign these value to diffrent varibales as follow:- a=638 b=219 c=52270 I tried to use cut command for this purpose and also tried to squeeze all sapces... (6 Replies)
Discussion started by: bisla.yogender
6 Replies

10. UNIX for Dummies Questions & Answers

UNIX/Win passwd synchronization utility

I know I saw a couple posts about this a long time ago, but could not find them with search. Please refresh my memory. -Chuck (1 Reply)
Discussion started by: 98_1LE
1 Replies
Login or Register to Ask a Question