Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

To check if there are any open file descriptors on a text file  RSS feed

 
mayank gupta
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a perl script that writes records into a flat file. Another java program reads the records from the flat file. The java program explodes into a couple of threads and polls the flat file to see if there are any new records in there and reads the flat file.
The problem here is that I have to replace my flat file when the size exceeds some 60k records. How can I move it when java threads are accessing it?
I had thought of including some functionality in my perl script as follows: Check within the perl script if there are any open file descriptors on the flat file. If not then replace the flat file.

How should I check if there are any open file descriptors on my flat file? i have tried to search the web but they talk mostly about lsof. This doesnt suit my requirements.

Please suggest.

Mayank.
 
Joe Ess
Bartender
Posts: 9436
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mayank gupta wrote:Hi,
i have tried to search the web but they talk mostly about lsof.


lsof would be my knee-jerk reaction. Exactly what is the problem with it?
 
mayank gupta
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i run the java program which is accessing the text file "test.txt" and do lsof i get the following:
vim 4700 mayank 4u REG 8,7 12288 392895 /home/mayank/Documents/CreditModule/src/.test.swp (deleted)
java 4731 mayank 17r REG 8,7 21 390905 /home/mayank/Documents/CreditModule/src/test
java 22110 mayank 17r REG 8,7 292 394129 /home/mayank/Documents/CreditModule/src/test (deleted)
(I have no instance of 'test' opened, only this one program accesses 'test')

when i do end my program and do lsof again (/usr/sbin/lsof | grep test), i get:
vim 4700 mayank 4u REG 8,7 12288 392895 /home/mayank/Documents/CreditModule/src/.test.swp (deleted)
java 22110 mayank 17r REG 8,7 292 394129 /home/mayank/Documents/CreditModule/src/test (deleted)

So i cant use a wc on this. I understand i should not take into account the entries which have (deleted) marked.
How can i ignore those entries and get only the active ones?

Thank you,

Mayank
 
mayank gupta
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please suggest.
 
Joe Ess
Bartender
Posts: 9436
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put logic in your script to ignore lines that contain "deleted" or use "grep -v deleted".
 
mayank gupta
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I got it working.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!