为什么cout会阻止后续代码在这里运行
Why does cout prevent subsequent code from running here?
我正在开发一个基本的shell,但在下面的循环中,程序没有运行过标记的行(而是立即循环)。当我注释掉它时,整个块在再次循环之前完成。这是怎么回事?
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main(int argc, char *argv[]) {
string input;
const char *EOF="exit";
string prompt=getenv("USER");
prompt.append("@ash>");
while(true) {
int parent=fork();
if ( !parent ) {
cout << prompt; //The program never gets past this point
getline(cin,input);
if (!input.compare(EOF))
exit(0);
cout << input << 'n';
execlp("ls", "-l", NULL);
return 0;
}
else
wait();
}
}
添加这些#include
s:
#include <sys/types.h>
#include <sys/wait.h>
则正确调用wait(2)
:
int status;
wait(&status);
您的代码wait()
不会调用wait(2)
系统调用。相反,它声明了一个类型为union wait
的临时对象。如果您#include
stdlib.h
而不是sys/wait.h
,那么您只能得到类型声明,而不能得到函数声明。
顺便说一句,如果您检查了wait
调用的返回值:int result = wait()
,您会收到一条信息性错误消息:
xsh.cc:26:错误:无法在初始化中将"wait"转换为"int"
相关文章:
- 努力将整数转换为链表。不知道我在这里做错了什么
- 代码在main()中运行,但在函数中出现错误
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 为什么我的代码在输出中增加了93天
- 为什么使用__LINE_的代码在发布模式下在MSVC下编译,而不是在调试模式下
- 这行代码在C++类中意味着什么
- 当我从下面的代码中删除关键字 virtual 时,它可以正常工作,否则会出现错误。在这里"virtual"字的意义是什么?
- 我的C++合并排序代码不起作用。我在这里错过了什么?
- 试图美化这个Arduino代码[初学者在这里]
- 改进我的代码以应对C++的挑战,新手在这里
- 在这里,我编写了一个使用 2 维数组并打算执行行和的 C++ 代码,但它没有给出正确的输出
- 菜鸟在这里,有人可以向我解释这段代码吗?
- 此代码的输出是什么?我在这里错过了什么吗?
- 将C 节俭代码汇编为接口Cassandra会导致以下错误.我们在这里缺少什么
- *&在这里做什么?如何更改它以获取代码作为 void binaryTree::insert(binaryNode *root,string value)?可能吗?
- C++我可以在这里使用数组来缩短代码吗
- AFAIK,下面的代码不应该编译,但它可以在 clang 和 GCC 中编译。我在这里错过了什么?
- 代码块 int main 之前在这里定义
- 为什么cout会阻止后续代码在这里运行
- 朋友操作符的代码示例?c++头文件,朋友在这里做什么