C语言 “阻塞系统调用”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19309136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:41:21  来源:igfitidea点击:

What is meant by "blocking system call"?

cmultithreadingoperating-systemsystem-calls

提问by sam

What is the meaning of "blocking system call"?

“阻塞系统调用”是什么意思?

In my operating systems course, we are studying multithreaded programming. I'm unsure what is meant when I read in my textbook "it can allow another thread to run when a thread make a blocking system call"

在我的操作系统课程中,我们正在学习多线程编程。当我在我的教科书中读到“当一个线程进行阻塞系统调用时它可以允许另一个线程运行”时,我不确定是什么意思

回答by Crowman

A blocking system call is one that must wait until the action can be completed. read()would be a good example - if no input is ready, it'll sit there and wait until some is (provided you haven't set it to non-blocking, of course, in which case it wouldn't be a blocking system call). Obviously, while one thread is waiting on a blocking system call, another thread can be off doing something else.

阻塞系统调用是必须等到操作完成的系统调用。read()将是一个很好的例子 - 如果没有准备好输入,它会坐在那里等待一些输入(当然,前提是你没有将它设置为非阻塞,在这种情况下它不会是阻塞系统调用)。显然,当一个线程正在等待阻塞系统调用时,另一个线程可能会停止做其他事情。

回答by Brendan

For a blocking system call, the caller can't do anything until the system call returns. If the system call may be lengthy (e.g. involve file IO or networking IO) this can be a bad thing (e.g. imagine a frustrated user hammering a "Cancel" button in an application that doesn't respond because that thread is blocked waiting for a packet from the network that isn't arriving). To get around that problem (to do useful work while you wait for a blocking system call to return) you can use threads - while one thread is blocked the other thread/s can continue doing useful work.

对于阻塞系统调用,在系统调用返回之前调用者不能做任何事情。如果系统调用可能很长(例如,涉及文件 IO 或网络 IO),这可能是一件坏事(例如,想象一个沮丧的用户在应用程序中敲击“取消”按钮而没有响应,因为该线程被阻塞等待来自未到达的网络的数据包)。为了解决这个问题(在等待阻塞系统调用返回时做有用的工作),您可以使用线程 - 当一个线程被阻塞时,另一个线程可以继续做有用的工作。

The alternative is non-blocking system calls. In this case the system call returns (almost) immediately. For lengthy system calls the result of the system call is either sent to the caller later (e.g. as some sort of event or message or signal) or polled by the caller later. This allows you to have a single thread waiting for many different lengthy system calls to complete at the same time; and avoids the hassle of threads (and locking, race conditions, the overhead of thread switches, etc). However, it also increases the hassle involved with getting and handling the system call's results.

另一种方法是非阻塞系统调用。在这种情况下,系统调用(几乎)立即返回。对于冗长的系统调用,系统调用的结果要么稍后发送给调用者(例如作为某种事件、消息或信号),要么稍后由调用者轮询。这允许您让单个线程同时等待许多不同的冗长系统调用完成;并避免线程的麻烦(以及锁定、竞争条件、线程切换的开销等)。然而,它也增加了获取和处理系统调用结果的麻烦。

It is (almost always) possible to write a non-blocking wrapper around a blocking system call; where the wrapper spawns a thread and returns (almost) immediately, and the spawned thread does the blocking system call and either sends the system call's results to the original caller or stores them where the original caller can poll for them.

可以(几乎总是)围绕阻塞系统调用编写非阻塞包装器;包装器生成一个线程并(几乎)立即返回,生成的线程执行阻塞系统调用,并将系统调用的结果发送给原始调用者或将它们存储在原始调用者可以轮询它们的地方。

It is also (almost always) possible to write a blocking wrapper around a non-blocking system call; where the wrapper does the system call and waits for the results before it returns.

也(几乎总是)可以在非阻塞系统调用周围编写阻塞包装器;包装器执行系统调用并在返回之前等待结果。

回答by hartmut

I would suggest having a read on this very short text: http://files.mkgnu.net/files/upstare/UPSTARE_RELEASE_0-12-8/manual/html-multi/x755.htmlIn particular you can read there why blocking system calls can be a worry with threads, not just with concurrent processes:

我建议阅读这个非常短的文本:http: //files.mkgnu.net/files/upstare/UPSTARE_RELEASE_0-12-8/manual/html-multi/x755.html特别是你可以在那里阅读为什么阻止系统调用可能是线程的一个问题,而不仅仅是并发进程:

This is particularly problematic for multi-threaded applications since one thread blocking on a system call may indefinitely delay the update of the code of another thread.

这对于多线程应用程序尤其成问题,因为在系统调用上阻塞的一个线程可能会无限延迟另一个线程的代码更新。

Hope it helps.

希望能帮助到你。