- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如果我有一个文件描述符,它已使用 open (2)
打开,随后使用 fcntl (2)
设置为 O_NONBLOCK
使用FFI(外部函数接口(interface))。
这意味着几乎每次调用read(2)
都会返回-1,errno
设置为EAGAIN
,这意味着这次有没有可用数据。
遗憾的是,我正在使用的 FFI 无法访问 errno
变量,因此我无法确定返回 -1 值是因为没有数据还是因为文件句柄不再有效。
所以我试图以某种方式确定文件描述符是否仍然有效,而无需读取 errno
。我 already tried all the answers with fcntl
from this question ,但它们不起作用并且永远不会返回 -1。
也许这是因为我正在读取设备文件:/dev/input/js0
?
我可以调用另一个函数来告诉我文件描述符是否无效吗? (在上面的问题中有人提到了poll
,但我不确定这是什么意思。)
我正在使用 Squeak FFI,不允许添加任何自定义 C 包装器。我正在尝试访问游戏 handle 并从中读取按钮信息,这是一项可选任务。
fcntl
的 FFI(在类 Gamepad
中定义):
manipulateFileHandle: fileHandle command: command
< cdecl: long 'fcntl' ( long long ) module: 'libc.so.6' >
^ self externalCallFailed
然后在另一种方法中,我称它为:
| handleTest |
handleTest := Gamepad manipulateFileHandle: externalFileHandle command: 1.
Transcript show: handleTest; cr.
命令
1 是F_GETFD
,读取文件描述符标志。但是 handleTest
永远不会是 -1,即使在拔下游戏 handle 之后也是如此。
最佳答案
在 GNU/Linux 系统上,libc
包含函数 __errno_location()
,它返回 errno
变量的地址。您的 FFI 应该能够调用该函数,然后取消引用表示 int *
的地址。
在 GCC 发明线程局部变量的语法之前,允许多线程代码安全操作 errno
的技术是将其定义为:
#define errno (*__errno_location ())
该函数将返回一个线程本地地址。在您的 Linux 系统上,如果您对使用 errno
的 C 代码使用 gcc -E
,您将看到扩展。
As explained by Barmar, the
fcntl()
techniques work. The reason it never returned-1
was because you were still holding a valid file descriptor. A file descriptor does not become invalid until it is closed.
关于c - 检测带有 O_NONBLOCK 的文件描述符在 FFI 场景中是否仍然有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37061993/
我正在努力通过接受 void 的 FFI 传递结构并在另一端读回它。 有问题的库是 libtsm,一个终端状态机。它允许您提供输入,然后找出输入后终端将处于哪种状态。 它将其绘制函数声明为: pub
只是为了测试目的,我在 delphi 中创建了一个小的 DLL。代码为: library MyDll; uses SysUtils, Classes, Vcl.Dialogs;
这是我一直好奇的事情:我想知道 LuaJIT 的 FFI 模块如何设法使用正确的调用约定来调用外部 native 函数,而无需在用户原型(prototype)中进行任何声明。 我尝试阅读源代码以自己解
我有一个带有函数的 C 库,在一个不透明的结构上运行,定义如下: Foo* makeFoo(); // create a new Foo Foo* dupFoo(const Foo* orig); /
我正在尝试从 purescript 调用 navigator.geolocation.getCurrentPosition javascript 函数,但遇到了两个问题。 在 javascript 中
ruby 版本 2.2.4p230 RubyGem 版本 2.7.2 已安装 Ruby-devel 和 lib64ffi-devel。 使用 64 位 OpenMandriva 3.0、urpmi 和
我正在尝试使用 FFI 将以下 JavaScript 函数导入 PureScript: function getGreeting() { return "Hi, welcome to the sh
我对 Haskell 中的 FFI 有一些疑问 我知道我必须使用语言编译指示 {-# LANGUAGE ForeignFunctionInterface #-}但是当我使用 {-# LANGUAGE
当我尝试构建 Flutter项目至 IOS 它向我显示了这个错误,我尝试清理并尝试更改项目的目录。 Launching lib/main.dart on iPhone 12 Pro Max in de
完整堆栈跟踪: /Users/galharth/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencie
我试过运行 pod install在我的 Xcode 项目中,但出现以下错误 /System/Library/Frameworks/Ruby.framework/Versions/2.6/us
我需要在节点 Electron 项目中使用模块“ffi”。我可以使用“gyp”重建它并在节点中使用库,但我不能使用“electron-rebuild”重建它并在 Electron 中使用它。 我跑了:
以下警告是什么意思,我该如何解决它的原因? Warning: Unimplemented primitive used:removeEventListener 在 [@bs.val] external
谁能告诉我一个使用带有可变参数的 C 函数(例如 printf )和 Haskell 的外部函数接口(interface)的示例?我尝试搜索 HaskellWiki,但没有找到这样的示例。 谢谢! 最
我使用 FFI 是为了在 C 中使用一个函数,该函数接受一个结构并返回相同的结构。我看到的引用说我必须使用指向这些结构的指针才能将其导入 Haskell。所以,例如。 data Bar = Bar {
我想在带有 FFI 的 Rust 中包含一个动态 C 库。 该库实际上也是用 Rust 构建的,但公开了一个 C 接口(interface),因此它也可以从其他语言中使用。当我用 cargo 构建库(
我试图在winapi上写一个包装器。我想包装接受回调函数指针的函数。 例如,考虑以下情况: // The unsafe callback type the FFI function accepts t
我正在使用 rust-bindgen 从 Rust 访问 C 库。一些函数返回指向结构的可空指针,bindgen 表示为 extern "C" { pub fn get_some_data()
介绍 我正在用 inline-c 包装一个 C 数值库;一些函数可以将回调传递给步骤例程,考虑 ODE 的优化或时间积分。 特别是在原生 C 中,使用回调可以对连续数组进行操作,通过指针修改它们,并将
fn main() { let val = 0; unsafe { foo(&val) } } extern "C" { pub fn foo(val: *const u32)
我是一名优秀的程序员,十分优秀!