4

Today I tried to move my Microsoft Word document file from one place to another when it was open in Microsoft Word. But then it said it is unable to move since it is open in Microsoft Word. Similarly same thing happens when we try to move MP3 files when it is playing in media player. I think there are many other cases like this.

But why it can't move files when they are opened in any programs? What is the underlying idea behind it?

3
  • 6
    If you are trying to "copy and paste", it simply pastes without any error. But if you are trying "cut and paste", it does not. So recheck your question.
    – Damon
    Dec 5, 2014 at 4:42
  • @AaKASH What OS is this? Are the files local or remote? Can you show the actual error message? Oh yeah, it's not a move right, but a copy? Dec 5, 2014 at 4:43
  • @Louis It is Microsoft Windows. Dec 5, 2014 at 6:04

2 Answers 2

18

The concept you are asking about is called Concurrency and Synchronization control, and refers to a set of techniques to prevent simultaneous (and likely conflicting) changes to a piece of data accessible by multiple processes.

the textbook example of the concept is a married couple with a joint debit card, and 1000$ in their account. If they are at ATMs on opposite sides of town, and both withdraw 1000$ at the exact same instant, and there were no concurrency controls, the bank would dish out a total of 2000$, which obviously wouldn't be right. Its even likely that both ATMs would reduce the amount to 0, rather than subtracting 1000 each, so the bank might not even notice that they got ripped off.

More specifically, Concurrency refers to situations where multiple things happen at the same time (and the problems that can ensue), and Synchronization is a set of techniques to avoid concurrency problems, often by controlling who can change data at any given time, using locks.

To have a concurrency problem, you need a couple components:

  1. the system must implement multiple processes or threads (often to support multiple users, but that is not a strict requirement.)
  2. the processes/threads/users must have access to a shared bit of data which they can change.

In this case, the file is the data, and the two processes are the copy and the editor/player that are accessing the file.

Filesystems deal with file concurrency as an essential feature of any modern OS. MS filesystem APIs (.dlls) deal with this scenario by creating synchronization locks on the files when a process opens them. The locks prevent other operations on the file while one or the other process is changing the data. for normal content files, when the first process opens the file for Read/Write, it establishes a write lock which prevents all other processes from changing the file. Additionally, when that file is being written to actively, a read lock is established so that if another process attempts to read the data, it won't get part of it from before the change and part of it from after. these read locks are released almost immediately after the file is written, but the write locks persist until the file is closed. Read locks are also established when a process reads data, to prevent file changes while the read is in progress. The general idea is that you can't write while a read lock exists, and at most one write lock can exist at any given time, so a file can only be written if there are no locks or just one write lock owned by the process doing the writing.

Usually content files are accessed using what MS calls "single-write multiple-read semantics", which would usually allow the files in question to be copied, because the copy process would not require a write lock. I'm not certain exactly why your MP3 player would lock the mp3, but my hunch is that it has to do with Metadata editors built into the player. Either way, its important to note that the process itself has some control over what locks are established, and there is no good way to tell what the programmer was thinking.

Some MS Office documents complicate the issue more, because they create temporary files for editing (word and power-point primarily), which may allow multiple editors to change their own copies simultaneously, and use special techniques to try to prevent the User2 from saving the document, and overwriting changes User1 saved after User2 opened the file. Excel and Access however may block write access to the User2, and pop up a window asking if you wish to be notified when User1 has exited the file, or allow the file to open in read only mode for User2. MS Office versions handle these problems differently. For instance, with office '07, I had to close a document before attaching it to an email. in 2010/2013, I don't have to. It also makes a difference as to whether the document is accessed locally or over a network share.

This is an enormously complex set of topics, which have huge impact on multi-process/multi-user systems, in innumerable ways, so the tactics used for each situation are myriad. I have not even scratched the surface in this post. it gets really fun when dealing with multithreaded IO algorithms and database concurrency control. The links herein should give you a good starting point to explore the concepts to whatever level of depth is appropriate for your needs and curisoity.

Good luck

2
  • In Windows, moving is prohibited so long as there is an active file handle, regardless if it's read-only. Many programs such as text editors will open the file handle, read the file, then close. When saving, it will open the file, write the data, then close. However other programs, such as an mp3 player, will keep the file handle open. The mp3 player will do this because it only reads a bit of data at a time, not all at once. That's why some of my multi-gigabyte FLACs don't crash my 4GB PC.
    – BlueBuddy
    Dec 5, 2014 at 20:44
  • 1
    Ahh, I see that the op has updated their post to indicate that they attempted to Move teh file, not Copy it as originally stated. Yes you are quite correct, you cannot move a file while it is open, regardless of any kind of lock; a handle is sufficient to block the operation. Dec 5, 2014 at 21:13
0

Frank's answer is very complete but long. Here is the simple reason. You cannot move the file because the program (Office or MP3 Player) is using it. Moving a file copies it and then deletes the original. Deleting a file that is in use is not allowed.

Unix systems seem to allow you to delete files that are in use. The trick there is that the file is not actually deleted until all programs stop using it. The file is invisible but still on the disk. But Windows just says "No!"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .