- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下两个简单的程序:
bye.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”,预期 msg 打印到控制台。
$ ./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”,那么 sleep 就会执行。
谢谢,艾哈迈德。
最佳答案
根据 the spec ,execl
的参数列表必须由 NULL 指针终止(即 (char *)0
,而不是 ""
)。
更改附近的代码只是更改调用 execl
时堆栈上的内容。如所写,程序的行为是未定义的。
附言始终检查库例程的返回值是否有错误。
关于c++ - execl 不运行编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28469803/
假设我 putenv 一个环境变量 ABC,然后执行 execl,或者我执行 execle 并将 ABC 添加到 envp 数组中我传递给 execle 的指针。 如果有的话,有区别吗? 最佳答案 p
我试图在 C++ 中运行 execl(),但它失败了,我不确定原因。 string fileName = "test.txt"; string computerName = "jssoile@
POSIX 系统公开了一系列 exec 函数,这些函数允许将可能不同的东西加载到当前进程中,保持打开的文件描述符、进程标识符等。 这可以出于多种原因完成,在我的例子中,这是自举——我想更改我自己进程的
我正在使用 c 中的 find 命令进行编程,但当我想对文件使用 exec 时,我被阻止了。 如果它是一个目录,那么很简单,我只使用 chdir(path) 但在文件上我有错误不是目录,所以我不能在上
if (fork_return==0) { printf("FROM THE CHILD PROCESS \n"); //print process id printf("The chil
我真的很喜欢这方面的一些调试帮助。我从早上到凌晨 4 点就一直在做这件事。 (我预计会在 7 小时内交付此内容 [上午 11 点]) main.c 中的所有内容都有效,但是当我创建一些子进程来使用 e
我试图理解这个系统调用(execle()),但我不知道它是如何工作的。我不知道如何使用 char* envp[],我们必须将其作为参数传递。我已经尝试过,但它不起作用: #include #incl
我有一个程序可以读取文件,对其进行处理并将结果放入输出文件中。当我有一个参数(输入文件)时,我创建输出文件并写入内容。 我创建了一个 fork() 以便将 stdout 重定向为 write() 内容
#include #include #include #define MAXLINE 512 main(int argc,char* argv[]){ int k; for (k
程序从配置文件中读取一些值,有些已定义,有些未定义,有些值为 0,有些处于事件状态。 我有以下代码: char *arg1="", *arg1_value="", *arg2="", *arg
你能帮我解决这个问题吗?execl 调用的程序“exam”和“students”执行了多少次?我认为正确的答案是程序“考试”有 8 个运行时间,“学生”有 0 个运行时间,因为在前两个 fork 中将
我正在制作一个 C 程序,该程序使用 execl 来执行 ls 命令来列出文件。如果在没有任何命令行参数的情况下调用程序,则会列出当前目录,如果用户将文件目录指定为命令行参数,则会列出该目录。 exe
我使用 TCC 编译并运行了以下程序在 Windows 7 下出现应用程序崩溃: #include int main(void) { if (execlp("c:\\windows\\sys
我正在使用 cywin 在 Windows 上编译 crashme 源代码,我遇到了一些编译错误。 错误: crashme.c: In function 'vfork_main': crashme.c
在UNIX环境下的系统编程上下文中,在使用C++编程语言时,在我的理解中,execl()会传入它要运行的程序的路径,以及一个vector。当传入该 vector 时,我将其理解为传入入口点,通常是 m
我有以下两个简单的程序: bye.cc #include int main() { std::cout #include #include #include using namespace
这个 execl 语句可能有什么问题?当我尝试运行它时,接收可执行文件提示 argc 小于 3。当我打印 argv 内容时,我得到以下信息: argv[1] = -1076146944 argv[2]
我的目标是学习如何执行 execle() 函数。我在 Head First C 书中找到了这段代码。 这是主进程代码(diner_info.c): #include #include int ma
在一个项目上工作,当我调用 execl() 时它不工作。它在 fork 之后调用,应该重新执行当前文件。(参数在文件的前面声明): argument = argv[0]; int err =execl
我正在尝试用 C 语言重现此命令: ls | wc > output.txt 因此,为此,我编写了以下程序: #include #include #include #include #incl
我是一名优秀的程序员,十分优秀!