c++ - 如何运行在后台从标准输入读取字符串的 C++ 程序?
问题描述
我有一个 C++ 程序,它在循环中从标准输入读取字符串,当我运行它时,它工作得很好。现在我想在后台运行它。
首先我尝试nohup ./myprog &
了,但是nohup.out
程序的输出(“”)会很快增加invalid order
。
然后我试了一下nohup ./myprog </dev/null &
,现象还是一样。
我想知道:
- 有什么技巧可以在 Linux 的后台运行我的程序吗?
nohup
见面的时候cin
,发生了什么?为什么我的程序输出invalid order
?
我的 C++ 代码如下:
#include <iostream>
#include <string>
#include <thread>
using namespace std;
#include <stdlib.h>
void domybusiness()
{
// do my business
}
int main()
{
thread t(domybusiness);
t.detach();
string order;
while (true)
{
cin >> order;
if(order == "exit")
{
exit(0);
}
else if(order == "other cmd")
{
// other process
}
else
{
cout << "invalid order" << endl;
}
}
return 0;
}
解决方案
推荐阅读
- docker - npm install can't find package.json
- excel - Office JS: Is there a way to find the last row with data in on an excel sheet so that i can then copy that information onto another sheet?
- javascript - Failed to execute 'pushState' on 'History'. Function could not be cloned
- c# - Why is Automapper causing a loading delay in Blazor WebAssembly?
- vue.js - Vue:Vue 路由器没有导航到任何路线
- python - 如何使 python discord bot 执行/使用命令
- jakarta-mail - 带有 OpenJDK 11 的 Jakarta Mail:java.util.ServiceConfigurationError: jakarta.mail.Provider: Provider com.sun.mail.imap.IMAPProvider not a subtype
- python - 在列表中查找每个系统出现一次的值
- python-3.x - grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC 终止于:status = StatusCode.UNAVAILABLE
- firebase - 在前端使用 Firebase Firestore 文档 ID 是不是一个坏主意?