SIGSEGV 使用 fork 运行 geant4
SIGSEGV using fork to run geant4
当我运行使用 fork 创建子 Geant4 进程的代码时,我遇到了一个问题,但前提是我使用函数。 我可以连续多次调用我的函数而不会出现问题,因此我认为问题必须在 main 终止之前发生,因此我关心的所有内容都成功完成了。 我认为忽略错误不是好的编程实践,即使它们不影响结果,所以我想知道是什么导致了错误。
这是我没有函数的代码(没有SIGSEGV):
#include <iostream>
#include <unistd.h>
#include <string>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
using namespace std;
string runFile;
cout << "Enter run macro file name: ";
cin >> runFile;
cout << "Running './disk " << runFile << "'" << endl;
const char* file = runFile.c_str();
const char* programPath =
"/home/fred/Documents/DIRC_Research/disk-build/disk";
pid_t pid = fork();
switch (pid)
{
case -1:
std::cerr << "fork() failed.n";
exit(1);
case 0:
execl(programPath, "disk", file, NULL);
std::cerr << "execl() failed!";
exit(1);
default:
std::cout << "Process created with pid " << pid << std::endl;
int* status;
waitpid(pid, status, 0);
std::cout << "Process exited with " << WEXITSTATUS(status) << std::endl;
}
}
这是它使用函数
#include <iostream>
#include <unistd.h>
#include <string>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
void runDisk(const char*);
int main()
{
using namespace std;
string runFile;
cout << "Enter run macro file name: ";
cin >> runFile;
cout << "Running './disk " << runFile << "'" << endl;
const char* file = runFile.c_str();
runDisk(file);
return 0;
}
// Function to run disk Geant4 simulation with a run macro file
void runDisk(const char* runFile)
{
const char* programPath =
"/home/fred/Documents/DIRC_Research/disk-build/disk";
pid_t pid = fork();
switch (pid)
{
case -1:
std::cerr << "fork() failed.n";
exit(1);
case 0:
execl(programPath, "disk", runFile, NULL);
std::cerr << "execl() failed!";
exit(1);
default:
std::cout << "Process created with pid " << pid << std::endl;
int* status;
waitpid(pid, status, 0);
std::cout << "Process exited with " << WEXITSTATUS(status) << std::endl;
}
}
使用函数的代码在终止之前给出了一个 SIGSEGV,即使我使函数内联。我真的很想知道发生了什么。
我想
至少就函数用法而言,我已经弄清楚了。 我把
int status = 0;
waitpid(pid, &status, 0);
std::cout << "Process exited with " << WEXITSTATUS(status) << std::endl;
而不是
int* status;
waitpid(pid, status, 0);
std::cout << "Process exited with " << WEXITSTATUS(status) << std::endl;
现在我不再被SIGSEGV困扰。至于它在不在函数中时工作的原因对我来说仍然是一个谜,也许它与函数删除status
有关。
相关文章:
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 代码在main()中运行,但在函数中出现错误
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何在运行中期切换GTK CSS style_context
- 如何在MS Visual Studio 2019中运行QT UI
- 如何通过cpp程序运行shell脚本
- IPC使用多个管道和分支进程来运行Python程序
- 删除指向指针的指针是运行时错误吗
- 如何用参数值调用函数(仅在运行时已知)
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- c++中的指针和运行时错误
- 在C应用程序中运行C++(带有STL)函数
- 运行程序时出现问题
- 控制允许动态运行c++的并发操作数
- SIGSEGV 使用 fork 运行 geant4