So I recently had to do mounting and unmounting of DMG files from the command line using perl. The steps I used were:
(a) Convert image to UDRW (so I could modify the image contents)(b) Mount the image(c) Modify contents(d) Unmount the image(e) Convert to UDCO (compressed image format, so I could save space and bandwidth)
So between (d) and (e) I was getting a "Resource Temporarily Unavailable" error, with code 35. Looked this up on the internet, and it means that there is a lock on the resource. I ran a "ps -e" command and saw a bunch of diskimage-helper processes, just standing there with a -post-exec parameter. I killed all of the processes related to this and conversion worked fine. So I ran the process again and monitored processes. Again, the process was left running, even after unmounting the image.
Solution: It seems that even though the hdiutil tool, which I used to mount and unmount, has what appear to be equivalent command parameters; they are not equivalent. Changing hdiutil unmount to hdiutil detach solved the problem.
Figured i'd let you know this helped me. I think it might be possible (haven't confirmed) to use:
hdiutil eject /dev/disk4 (where disk4 is the original virtual device for the dmg).
$ hdiutil mount Pablo0.sparseimage
/dev/disk4s2 Apple_HFS /Volumes/Pablo0
hdiutil detach /Volumes/Pablo0