与 sys/select.h 和 termios.h 中定义的功能等效的 Windows 是什么 [英] What is the Windows equivalent to the capabilities defined in sys/select.h and termios.h

查看:37
本文介绍了与 sys/select.h 和 termios.h 中定义的功能等效的 Windows 是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在linux中有一个应用程序,编译成功.我想在 Windows 中运行相同的程序.

但是编译会产生以下与头文件相关的错误.

  1. 找不到 sys/select.h
  2. 找不到 termios.h

我该如何解决这个问题?

解决方案

Windows API 在结构和风格上与任何风格的 Unix 提供的系统调用和库例程的混合都非常不同.

termio.h

Windows 使用与任何 *nix 系统截然不同的模型进行终端 I/O.结果,确实没有与 termios.h 标头及其朋友直接等效的东西.

您想在 MSDN 上阅读有关 Windows 通信资源的信息.

需要了解的更多信息包括:

一般来说,你会发现你需要直接处理更多的 Windows API,因为 stdio 会在进行设备 I/O 时增加混乱.

选择.h

没有直接等价于 Unix select(2) 系统调用.

在 Windows 中,许多内核对象可以处于有信号或无信号状态,并且可以使用信号发送对象的行为来释放一个名为 WaitForMultipleObjects().当数据可用时,一些但不是所有的 HANDLE 对象都会发出信号.具体来说,我知道 WinSock 的 HANDLE 具有这种功能,但我不知道 Comm API.我知道 HANDLE 对打开的文件没有.

如果您需要在处理窗口消息的线程中等待事件,那么您可能应该使用 MsgWaitForMultipleObjects(),因为它会在线程被阻塞时正确传递消息.

阅读 MSDN 文章中的 Windows 同步原语使用同步.

但是,Windows 内置了几种异步 I/O,可以通过设计更改来代替对 select() 的需求.两者都需要大量使用无法与 C stdio 库结合使用的功能.

MSDN 有几篇关于 I/O 技术的文章,以及大量的例子:

请注意,有关 Windows 工作原理的大部分信息分散在 API 函数和结构参考材料的概述文章和备注部分中.这会给人一种第一次阅读时没有完全记录的印象.

使用 Cygwin 移植

另一种方法是使用 Cygwin 进行移植.它在 Windows API 上提供了大部分 POSIX 层.但是,除非您从他们那里购买商业使用许可,否则您最终会得到一个依赖于 GPL 的 Cygwin DLL 的应用程序.使用 Cygwin 来获得适用于没有 Unix 经验的 Windows 用户的应用程序可能很棘手,因为关于这两个系统的设置和使用方式的许多其他假设是不同的.

Cygwin 做了大量繁重的工作来构建 select() 的实现,该实现在 Windows 上运行,同时混合了不同的打开文件描述符.这项工作在用户指南中进行了描述.>

请注意,只有在 Cygwin 环境中完成时,才记录和支持针对 Cygwin 的构建.仅将 Cygwin 的 bin 放在 Windows PATH 上并在命令提示符下工作通常是不够的.您确实需要启动 Cygwin 的 bash 构建并从那里进行编译,以便所有内容都使用相同的 Cygwin 风格的挂载点和模拟的 Unix 文件结构.

将 Cygwin 头文件与第三方工具头文件混合使用肯定会导致疯狂.

我重新整理了一些,并添加了一些材料以回应评论.

I have an application in linux, which is compiled successfully. I want to run the same program in windows.

But compilation produces the following errors related to header files.

  1. Cannot find sys/select.h
  2. Cannot find termios.h

How can I fix this?

解决方案

The Windows API is structurally and stylistically very different from the blend of system calls and library routines provided by any flavor of Unix.

termio.h

Windows does terminal I/O with a very different model from any *nix system. As a result, there really is no direct equivalent to the termios.h header and its friends.

You want to read at MSDN about the Windows Communications Resources.

Some things to learn more about include:

In general, you will find that you need to deal a lot more with the Windows API directly because stdio will add to the confusion when doing device I/O.

select.h

There isn't a direct equivalent to the Unix select(2) system call.

In Windows, many kernel objects can be in either a signaled or non-signaled state, and the act of signalling the object can be used to release a thread that called WaitForMultipleObjects(). Some but not all HANDLE objects are signaled when data is available. Specifically, I know that HANDLEs from WinSock have that capability, but I don't know about the Comm API. I know that HANDLEs to an open file do not.

If you need to wait for an event in a thread that is processing window messages, then you should probably use MsgWaitForMultipleObjects() instead, since it will properly deliver messages while the thread is otherwise blocked.

Read about the Windows synchronization primitives at the MSDN article Using Synchronization.

However, there are several kinds of asynchronous I/O built into Windows that can replace the need for select() by a change of design. Both will require extensive use of features that cannot be used in combination with the C stdio library.

MSDN has several articles on I/O techniques, as well as numerous examples:

Note that much of the information on how Windows works is scattered among the overview articles and the remarks sections of the reference material for the API functions and structures. This can give the impression that nothing is completely documented on a first reading.

Porting with Cygwin

Another approach is to use Cygwin to do the port. It provides most of a POSIX layer over the Windows API. However, you will end up with an application that is dependent on the Cygwin DLL which is GPL unless you purchase a commercial use license from them. It can be tricky to use Cygwin to get an application that works well for a Windows user with no Unix experience also, since so many other assumptions about the way the two systems are setup and used differ.

Cygwin has done a fair amount of heavy lifting to build an implementation of select() that works on Windows given a mix of different open file descriptors. This effort is described in the User's Guide.

Do be aware that building against Cygwin is only documented and supported if done from within the Cygwin environment. It usually is not sufficient to just put Cygwin's bin on the Windows PATH and work from a command prompt. You really need to launch Cygwin's build of bash and compile from there so that everything is using the same Cygwin-style mount points and simulated Unix file structure.

Mixing Cygwin header files with third-party tool header files is a sure path to madness.

Edit: I've rearranged a bit, and added some material in response to comments.

这篇关于与 sys/select.h 和 termios.h 中定义的功能等效的 Windows 是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆