关于构建shell时的fork()
About fork() while building a shell
这是我的代码。
pid_t fpid=fork();
if(fpid > 0){
wait(&fpid);
}
else{
do_command();
}
问题是,函数do_command()只执行一行,所以我更改了do_command:
else{
execlp("/bin/ls","ls","-al",NULL);
cout<<"ntest<<endl;
}
此外,ls命令已执行,但cout命令丢失。。
我的代码出了什么问题?
请原谅我英语不好,帮帮我。
这是我的do_command()函数声明:
void do_command(const char *command) {
//all commands with arguments
const char *kernel_address = "/bin/";
char *kernel_command;
strcpy(kernel_command, kernel_address);
strcat(kernel_command, command);
cout << "nCommand is:" << kernel_command << "n" << endl;
execlp(kernel_command, command, NULL);
}
此外,在子进程中调用函数时没有任何输出
下一行不会打印,因为exec系列命令只在出现错误时返回。如果没有错误,它将替换您的程序,并执行要在exec中执行的任何命令。因此,cout不工作
来自execlp:的手册
exec()函数族用新的进程映像替换当前进程映像。
一旦调用了execlp
,程序就不再运行。它已被ls
取代。因此,它永远不会到达cout
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- ostream过载时的缓冲区冲洗
- 具有未知值时的时间复杂性
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 当比特(而不是字节)的顺序至关重要时的持久性
- 旋转模型矩阵时的形状失真
- 讨论 - 创建矩阵时的数组与向量的向量 - 什么是最实用的选择
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 打印第二列时的2d字符矢量打印空间
- 读取文件时运行时的未知行为
- posix_spawn():使用posix_scawn()时的错误处理问题
- 查找最近配对时的OpenMP竞赛条件
- 如何修复valgrind启动时的致命错误(与libc6-dbg和libc6-dbg:i386连接)
- 在 c++ 中在开关情况下使用和不使用"break"时的不同输出
- 求和 int 乘以双倍时的欠/过近似
- 打包可变参数模板具有零元素时的递归
- 在Qt5中使用QTextSteam时的"使用已删除功能"
- 为什么在Shell输出中运行的可执行操作到stdout,而不是崩溃时的stderr
- 关于构建shell时的fork()
- 在c++中使用unix shell中的文本文件时,如何请求用户输入