简体   繁体   English

Linux中的非阻塞安装

[英]Non-blocking mount in Linux

I use Linux's mount(2) function in a single-threaded process. 我在单线程进程中使用Linux的mount(2)函数。 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. 但是安装像CD-ROM这样的设备可能需要一段时间(最糟糕的是我看到的是40秒!),因为它会稍微思考,旋转磁盘,然后才会挂载文件系统。 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. 知道动作是否完整对我来说不是问题,因为我已经在同一个线程中阅读了内核uevents。

不。不启动另一个线程或fork() ,你必须等待mount()返回。

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. 如果您想以单线程方式执行此操作,则可以手动执行mount命令并对其进行后台处理,并使用select()或其他方式轮询完成。 However, this is hackish and not very different from forking and calling mount() within your program. 但是,这是hackish,并且与在程序中分叉和调用mount()没有太大区别。

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. 另外值得注意的是,我经历过mount()阻塞整个进程(以及关联的线程),因此对于真正的异步行为,分叉可能是要走的路。

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. 这将让mouning在后台为您完成。

But after looking a bit closer, this solution does not use the C interface but the system interface 但仔细观察后,此解决方案不使用C接口,而是使用系统接口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM