Linux – Is it dangerous to have several parallel jobs create the same directory at the same time

gridlinuxmkdir

Is it dangerous to have several parallel jobs create the same directory using mkdir -p? (This is under Linux.)

In my case, I send many jobs to a SUN grid to process them in parallel, and some of these jobs start by creating a certain directory foo. So, the execution of the different mkdir commands might happen at exactly the same time …

Best Answer

A simple mkdir is atomic (if you are using NTFS, there are chances it is not atomic, need some check).

By deduction, the mkdir -p folder1/folder2/ starts by creating folder1 which is atomic. I f at the same time another process tries to create folder1 also, it will see that folder1 is created so it will try to create folder2 which will either fail (if the first process already created folder2) or succeed and the first process will fail.

This should not be a problem if this is properly handled (i.e. good error handling).

Related Topic