execl没有运行编程
execl not running programming
我有以下两个简单的程序:
再见.cc
#include <iostream>
int main()
{ std::cout << "Bye bye bye world" << std::endl; }
你好.cc
#include <cstdlib>
#include <unistd.h>
#include <sys/wait.h>
#include <iostream>
using namespace std;
int main()
{
int status;
cout << "Hello world" << endl;
int pid = fork();
if (pid != 0) {
cout << "I am parent - " << pid << endl;
// wait for child to finish up......
cout << "Waiting for child to finish" << endl;
wait(&status);
cout << "Child finished, status " << status << endl;
} else {
cout << "--- I am child - " << pid << endl; // **Note**
execl("bye", "");
cout << "--- I am sleeping" << endl;
sleep(3);
exit(11);
}
}
在hello.cc中,如果标记为"Note"的行被启用(未注释),我会得到预期的行为,sleep(3)不会执行,"bye"会执行,预期的消息会打印到控制台。
$ ./hello
Hello world
I am parent - 27318
Waiting for child to finish
--- I am child - 0
Bye bye bye world
Child finished, status 0
然而,当标记为"Note"的行被注释时,"bye"不会被执行,并且执行sleep(3)。
$ ./hello
Hello world
I am parent - 27350
Waiting for child to finish
--- I am sleeping
Child finished, status 2816
有人能帮我理解可能发生了什么吗?我发现很奇怪的是,如果我用printf()代替"cout",那么睡眠就完成了。
谢谢,艾哈迈德。
根据规范,execl
的参数列表必须由NULL指针终止(即(char *)0
,而不是""
)。
更改附近的代码只是更改调用execl
时堆栈上发生的内容。正如所写的,程序的行为是未定义的。
p.S.始终检查库例程的返回值是否存在错误。
exec函数家族,如果成功,则不返回。这就是为什么在执行execl()时看不到sleep注释的原因。
相关文章:
- 所有的openMp c++编程都是使用在GPU上运行的-fopenmp编译的吗
- 以编程方式在运行时检测 CPU 体系结构
- C 编程:在循环时运行 2 并从 loop1 获取数据的随机结果
- 为什么以下C++元编程代码在 N=100 时给出编译错误,但对于小 N 运行良好
- C++ Qt - 检查 Steam 游戏是否以编程方式运行
- 用于优化存储/运行时算法的元编程,C++
- execl没有运行编程
- 如何使用 C 编程在运行时在 SQL 语句中传递文件路径
- 如何以编程方式运行make&CMake
- 如何以编程方式启动程序并在 Linux 上使用 root 权限运行它
- 竞争性编程:解决方案运行速度太慢
- 通过命令行以编程方式运行程序
- 在运行时确定缓冲区的大小?(套接字编程)
- 如何通过Qt中的root特权以编程方式运行打印命令lpr-p
- 使用gtest框架在运行时以编程方式重复确定的测试子集
- OpenGL可编程管道运行速度较慢
- C/ c++ -可编程运行Makefile
- 如何以编程方式获取服务的状态(运行/停止)
- 我的Qt5应用程序如何以编程方式知道哪些窗口和对话框正在运行
- 在Windows中,您可以通过编程(c++)检测其他正在运行的程序加载了哪些dll吗?