file-locking

How can I delete a file that is in use by another process?

天大地大妈咪最大 提交于 2019-11-30 07:30:12
问题 When I try to delete a file occurs the following exception: The process cannot access the file '' because it is being used by another process. My code looks like: string[] files = Directory.GetFiles(@"C:\SEDocumentConverter\SOURCE"); foreach (string file in files) { File.Delete(file); } How can I solve this problem? 回答1: There is no way to delete a file that's currently being used by another process. You have to close whatever program has that file open first, before you can delete it. If you

Is a move operation in Unix atomic?

房东的猫 提交于 2019-11-30 06:47:00
Suppose there are 2 processes P1 and P2, and they access a shared file Foo.txt . Suppose P2 is reading from Foo.txt . I don't want P1 to write to Foo.txt while P2 is reading it. So I thought I could make P1 write to Foo.tmp and as a last step, rename Foo.tmp to Foo.txt . My programming language is Java So my question is, would this ensure that P2 reads the correct data from Foo.txt ? Would the rename operation be committed once P2 completes reading the file? EDIT I tried to recreate this scenario as follows: My P1 code is something like this: File tempFile = new File(path1); File realFile =

PHP check if file locked with flock()?

旧城冷巷雨未停 提交于 2019-11-30 00:57:47
问题 Will fopen() fail if a file exists, but is currently locked with LOCK_EX ? Or do I have to open it, and then try and set a lock, in order to determine if one already exists? I've also read that flock() will; pause [the script] untill you get the lock for indefinite amount of time or till your script times out http://www.php.net/manual/en/function.flock.php#95257 If so, is it true this 'pause' can be by-passed with; if (!flock($f, LOCK_SH | LOCK_NB)) { // file locked, do something else } 回答1:

File.Copy locks source file after completion

佐手、 提交于 2019-11-30 00:27:29
问题 We are trying to copy a file from a server, down to a local machine in a .NET 2.0 application (C#), but keep having the source file unnecessarily locked. We suspect it is something configured on the file server that is causing this behaviour, but are not sure what ... can you help? After the file copy operation, the file server (Windows 2K3 R2) reports that the source file is being held with a read lock, even though no further operation is done with the file on the server. The lock is

How to make php scripts run in parallel?

与世无争的帅哥 提交于 2019-11-29 11:39:28
script1.php <?php session_start(); sleep(10); script2.php <?php session_start(); I run script1.php in the browser and immediately after script2.php in another browser window. session_start() in script2.php can't execute until script1.php is not finished executing. Why it happened and how to make php scripts run in parallel? Try <?php session_start(); session_write_close(); sleep(10); Related: Thoughts on PHP sessions 来源: https://stackoverflow.com/questions/6618026/how-to-make-php-scripts-run-in-parallel

Is std::ifstream thread-safe & lock-free?

孤人 提交于 2019-11-29 08:25:40
I intend to perform opening for reading a single file from many threads using std::ifstream. My concern is if std::ifstream is thread-safe & lock-free? More details: I use g++ 4.4 on Ubuntu & Windows XP, 4.0 on Leopard. Each thread creates its own instance of std::ifstream Thanks in advance! That is implementation defined. Standard C++ says absolutely nothing about threading, and therefore any assumptions about threads inherently invoke unspecified or implementation defined behavior. We need the platform you are using to be more specific, but it's probably unreasonable to assume ifstream is

Test if file is locked

蓝咒 提交于 2019-11-28 09:27:24
In PHP, how can I test if a file has already been locked with flock ? For example, if another running script has called the following: $fp = fopen('thefile.txt', 'w'); flock($fp, LOCK_EX); Prateek if (!flock($fp, LOCK_EX|LOCK_NB, $wouldblock)) { if ($wouldblock) { // another process holds the lock } else { // couldn't lock for another reason, e.g. no such file } } else { // lock obtained } As described in the docs , use LOCK_NB to make a non-blocking attempt to obtain the lock, and on failure check the $wouldblock argument to see if something else holds the lock. Check it like this: if (!flock

Java FileLock for Reading and Writing

[亡魂溺海] 提交于 2019-11-28 07:27:31
I have a process that will be called rather frequently from cron to read a file that has certain move related commands in it. My process needs to read and write to this data file - and keep it locked to prevent other processes from touching it during this time. A completely separate process can be executed by a user to (potential) write/append to this same data file. I want these two processes to play nice and only access the file one at a time. The nio FileLock seemed to be what I needed (short of writing my own semaphore type files), but I'm having trouble locking it for reading. I can lock

How can I unlock a file that is locked by a process in .NET [duplicate]

对着背影说爱祢 提交于 2019-11-28 06:56:10
This question already has an answer here: Unlock Files for deletion 2 answers I want my application to clean all the temp files it used, the problem is that not all the temp files are under my control, so I just want to "brutally" unlock them in order to delete them programatically. Take a look at this article. I think that you'll struggle to do this in C# natively, even using interop, but writing a C++/CLI wrapper assembly may be a good compromise. Note also that the user needs to have the SE_DEBUG privilege for this to work. I've struggled with this as well, and ended up just shelling out to

How to make php scripts run in parallel?

无人久伴 提交于 2019-11-28 05:27:55
问题 script1.php <?php session_start(); sleep(10); script2.php <?php session_start(); I run script1.php in the browser and immediately after script2.php in another browser window. session_start() in script2.php can't execute until script1.php is not finished executing. Why it happened and how to make php scripts run in parallel? 回答1: Try <?php session_start(); session_write_close(); sleep(10); Related: Thoughts on PHP sessions 来源: https://stackoverflow.com/questions/6618026/how-to-make-php-scripts