Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-blocking mount in Linux

Tags:

c

linux

mount

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.

like image 704
Alex B Avatar asked Dec 03 '22 16:12

Alex B


2 Answers

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

like image 153
Sean Bright Avatar answered Dec 11 '22 17:12

Sean Bright


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.

like image 43
codelogic Avatar answered Dec 11 '22 17:12

codelogic