tags:

views:

391

answers:

3

I use Linux's mount(2) function in a single-threaded process. But mounting of devices devices like CD-ROM may take a while (worst I've seen is 40 seconds!), as it will ponder a little, spin up the disk, and only then will mount the filesystem. This may block the process from processing other events for considerable time.

I cannot seem to find a way to mount a filesystem in a non-blocking way. Is there a way to mount a filesystem asynchronously without multi-threading or forking?

Knowing whether the action is complete is not an issue for me as I already read kernel uevents in the same thread.

+11  A: 

No. Without firing up another thread or fork()ing, you have to wait for mount() to return.

Sean Bright
Right... there is no way to do anything asynchronously without multithreading or forking. (+1)
David Zaslavsky
@David: well, there is AIO (asynchronous I/O - and on Linux that may involve a thread being started, though I think not any more, if it ever did), but it doesn't help when mounting a file system. But that's quibbling - your main point is accurate.
Jonathan Leffler
Lots of things can be done asynchronously, but I don't think mount is one of them.
MarkR
+1  A: 

If you want to do it in a single threaded manner, you can manually execute the mount command and background it and poll for completion using select() or something. However, this is hackish and not very different from forking and calling mount() within your program.

Also worth noting is that I've experienced mount() blocking an entire process (and associated threads), so for true asynchronous behavior, forking is probably the way to go.

codelogic
+1  A: 

You you can let the mounting process run in the background. Insted of running somthing like:

system("mount -a ");

Do

system("mount -a &");

This will let the mouning complete in the background for you.

But after looking a bit closer, this solution does not use the C interface but the system interface

eaanon01