English 中文(简体)
Qt 应用程序关闭 STDIN
原标题:Closing STDIN for a Qt Application

我正在研究一个拥有多个 C++ 执行程序的项目,该程序使用命名的管道进行通信。 主应用程序( App1) 生成其他应用程序。 当产卵时, 它关闭了给孩子使用的 < code> STDIN :

close(STDIN_FILENO);

它将 STDOUT STDERR 重定向到与子进程相关的其它文件。 这使得 App1 的输出成为 App1 的 < em> only , 没有孩子。 它还允许 App1 接受 < code> STDIN 的输入, 而不让它被子进程捕获 。

儿童进程之一是 Qt 应用程序。 产卵后, 它会尽可能多地使用CPU, 使我的计算机速度大大减慢。 如果我不关闭儿童进程 < code> STDIN , 这个行为会停止( 但是孩子们会捕捉到 < code> STDIN , 而不是我不希望的主要进程 ) 。

为什么会发生这种情况, 我怎样才能阻止 Qt 应用程序使用所有 CPU 周期?

最佳回答

我想我在确定另一个问题时已经了解了问题所在。 我正在关闭 < code> STDIN < /code> 文件描述符, 然后再重定向 < code> STDERR 和 < code> STDOUT < /code > 文件描述符。 这正在扰乱我使用 < code> freopen () 来重定向它们所用的索引 。

我将 close () STDIN () 移至重定向后,似乎不再有问题。

问题回答

也许给 Qt 应用程序它想要的东西? 在 < code> fork 之后使用 < code> > dup2 , 在 < code> exec 之前使用 < code> < exec ? < code> 将替换给定的文件描述符, 这样您就可以用文件替换 Stdin 。 快速示例 :

if(fork() == 0)
{
   int somefd = open("somefile", O_RDONLY);
   // replace stdin (0) with somefd before exec-ing
   if(dup2(somefd, 0) == -1)
   {
      // cunning plan failed
   }
   // exec Qt app here
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?