作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这个c++程序的目的是理解操作系统中的并发进程机制。下面的代码是针对一个进程的子函数。子进程有他们的编号,NO.5 和 NO.6。
我正在尝试在 NO.6 进程中执行 a.out 文件。我正在尝试这样做。
void ChildFunction_For_ProcessNO.4(int i){
switch(i){
case(5):
cout << "This is process five, and the ID for this process is " << getpid() << '\n'
<< "and the ID for the parent process is " << getppid() << '\n';
CreateThreads_Five();
cout << "Process five has ended.\n" << '\n';
break;
case(6):
cout << "This is process six, and the ID for this process is " << getpid() << '\n'
<< "and the ID for the parent process is " << getppid() << '\n';
execl("./a.out", "a.out", NULL);
//and I also tried this way
execl("Home/CLionProjects/Project_1/a.out", "a.out", NULL);
char buf[100];
cout << "getcwd: " << getcwd(buf, sizeof(buf))) << endl;
cout << "Process six has ended.\n";
break;
}
getcwd 的输出是这样的
getcwd: /home/chengxuyuan/CLionProjects/Project_1/cmake-build-debug
a.out文件已经和c++程序一起放在文件夹中了。 the screenshot of the working directory编译进行得很顺利,但是 a.out 文件中没有应该是 Hello world
的输出。我怎么解决这个问题。非常感谢!
最佳答案
getcwd
输出显示您必须将文件 a.out
放入
/home/chengxuyuan/CLionProjects/Project_1/cmake-build-debug
你的尝试
execl("Home/CLionProjects/Project_1/a.out", "a.out", NULL);
错误,这不是完整路径。你将不得不使用
execl("/home/chengxuyuan/CLionProjects/Project_1/a.out", "a.out", NULL);
顺便说一句:您应该指定与要运行的程序使用的参数 0 相同的值,即
execl("/home/chengxuyuan/CLionProjects/Project_1/a.out", "/home/chengxuyuan/CLionProjects/Project_1/a.out", NULL);
或
execl("./a.out", "./a.out", NULL);
这是 shell 会做的,也是大多数程序所期望的。
关于c++ - 如何在 Linux 中使用 exec 函数族执行 a.out 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58411950/
在C中,protocol family被提及为第一个参数。 例如: ipv4 的套接字(AF_INET,,) socket(AF_PACKET,,) 用于数据包嗅探 Python 支持三个地址族:AF
根据 vfork() 手册页,如果 vfork() 在调用 _exit 或 exec 之前修改除 pid_t 之外的任何数据,则行为未定义系统调用系列。 由此我了解到,如果vfork()创建的子进程调
我是一名优秀的程序员,十分优秀!