为什么在 Linux 中使用 select

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14544621/
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-08-06 18:49:12  来源:igfitidea点击:

Why is select used in Linux

clinuxfile-descriptorselect-syscall

提问by user1667307

I was going through a serial program and I observed that they use select()before using read(). Why exactly is this required. Why cant we just directly call read()and check if it fails or not ? Also why do we have to increment the file descriptor by 1 and pass it while I am passing the file descriptor set already to select()?

我正在执行一个串行程序,我观察到他们select()在使用read(). 为什么需要这样做。为什么我们不能直接调用read()并检查它是否失败?另外,为什么我们必须将文件描述符增加 1 并在我传递已设置为 的文件描述符时传递它select()

Example:

例子:

r=select(fd+1, &fds, NULL, NULL, &timeout);where fds already has the value of fd

r=select(fd+1, &fds, NULL, NULL, &timeout);其中 fds 已经具有 fd 的值

采纳答案by Jonathan Leffler

The select()system call tells you whether there is any data to read on the file descriptors that you're interested in. Strictly, it is a question of whether a read operation on the file descriptor will block or not.

select()系统调用告诉你是否有任何数据的文件描述符你感兴趣的。严格来说,它是文件描述符的读操作是否会阻止或不是一个问题读取。

If you execute read()on a file descriptor — such as that connected to a serial port — and there is no data to read, then the call will hang until there is some data to read. Programs using select()do not wish to be blocked like that.

如果你read()在一个文件描述符上执行——比如连接到串行端口的那个——并且没有要读取的数据,那么调用将挂起,直到有一些数据要读取。使用的程序select()不希望像那样被阻止。

You also ask:

你还问:

Why do we have to increment the file descriptor by 1 and pass it while I am passing the file descriptor set already to select?

为什么我们必须将文件描述符增加 1 并在我传递已设置为 的文件描述符时传递它select

That's probably specifying the size of the FD_SET. The first argument to select()is known as nfdsand POSIX says:

这可能指定了 FD_SET 的大小。的第一个参数select()被称为nfds和 POSIX 说:

The nfdsargument specifies the range of descriptors to be tested. The first nfdsdescriptors shall be checked in each set; that is, the descriptors from zero through nfds-1in the descriptor sets shall be examined.

nfds参数指定要测试的描述符的范围。nfds应检查每组中的第一个描述符;也就是说,nfds-1应检查描述符集中从零到零的描述符。

So, to test a file descriptor n, the value in nfdsmust be at least n+1.

因此,要测试文件描述符n,中的值nfds必须至少为n+1

回答by Deepankar Bajpeyi

You use select call when you have to constantly monitor file descriptors until they get ready for some IO without blocking.

当您必须不断监视文件描述符直到它们为某些 IO 做好准备而不会阻塞时,您可以使用 select 调用。

Generally used when you want the IO (eg read() )non-blocking , read the :man page

通常在您希望 IO(例如 read() )非阻塞时使用,请阅读:手册页

Also read the related API's

另请阅读相关 API

回答by DigitalRoss

Programs that want to continue running while alsoreading interactive user input1need to be multithreaded orthey need to read input streams carefully and, specifically, conditionally.

希望继续运行,同时程序读交互式用户输入1需要是多线程或者他们需要读取输入流认真,具体地,有条件地。

Select(2)can be used to implement the second design pattern. It can determine whether input can be read without blocking the entire application.

Select(2)可用于实现第二种设计模式。它可以确定是否可以在不阻塞整个应用程序的情况下读取输入。



1. Or some other input that arrives unpredictably.

1. 或其他一些不可预测的输入。