0

I've designed a program to run off flash drives. My idea was to put an Eject button on the program so you can easily 'safely remove' the drive. However on Mac, you can't unmount the drive whilst the application is still running unless you do a force unmount. My question is, should I be doing this?

Isn't a force unmount similar to just pulling the drive out? Is it safe to make this option easily available?

Also is there any alternative?

Kara
  • 6,115
  • 16
  • 50
  • 57
Andy
  • 3,600
  • 12
  • 53
  • 84

1 Answers1

0

I take it your application is running from the disk itself, yes?

Rather than force-eject the drive, you should spawn a separate process. Basically you write a little helper program that waits for your main program to quit, then ejects the disk, and finally quits itself. When you spawn the helper process, you will have to do so "without waiting." The terminology may be different depending on the language you're using ("in a new thread" or "detaching a process") but the basic idea is that you must launch a helper program to take over from your main program in such a way that your main program can quit.

It's bad form to force eject - you can't be sure the drive is not in use. On a Mac, for example, you have Spotlight reading/writing to external disks at all sorts of times.

Community
  • 1
  • 1
original_username
  • 2,398
  • 20
  • 24
  • You are correct in that the application runs from the drive I wish to eject. So basically, I could write a temporary script from the main program on to another drive, that waits for the program to quit and then ejects the drive and deletes the program. The only thing is if the drive can't be unmounted for another reason my program will have still quit. Is there anyway to determine whether it could be unmounted before trying? – Andy Oct 05 '13 at 10:55
  • Probably the 'lsof' command has an option to do that. You could also set a timer, and after a few seconds either "give up" or "force eject". – original_username Oct 05 '13 at 11:01
  • Yes, I see how I can use `lsof`. Thanks for your answer. Its pointed me in the right direction – Andy Oct 05 '13 at 12:06
  • How would I wait for my main program to exit though? – Andy Oct 05 '13 at 12:15
  • You can pass your main program's PID to the helper (or even just use your main program's name), and then use 'ps' to see if it is still running. – original_username Oct 05 '13 at 12:17
  • When I type `ps` in the terminal I only get one process though, the command for which is `-bash` – Andy Oct 05 '13 at 13:03
  • i use "ps aux" sometimes to see what's running, but there's probably a more concise option. see http://unixhelp.ed.ac.uk/CGI/man-cgi?ps – original_username Oct 05 '13 at 13:16
  • Thanks for that. I've found that I can check if a program is running by using `ps -p $PID`, so I should be set now – Andy Oct 05 '13 at 15:10