如何通过每次分支新的子进程来多次运行同一个jar文件
how to run same jar file multiple times by forking new child process every time?
我正在尝试写一个c++程序。该程序需要运行一些jar文件,每个jar文件运行两次。问题是程序只正确运行每个文件一次。在第二次我得到了一个错误的消息,好像虚拟机得到了一个错误的命令。但是,第一次运行jar文件的命令是相同的!!
这是我得到的红色错误消息的前几行。
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
-client to select the "client" VM
-server to select the "server" VM
这是我的代码。这是主文件:
#include <iostream>
#include <fstream>
#include <signal.h>
#include <string.h>
#include <time.h>
#include "StringParser.h"
#include "ShellCore.h"
using namespace std;
int main() {
while (1) {
char checkers_command[] = "java -jar /home/samer/Downloads/chkr.jar";
char castle_command[] = "java -jar /home/samer/Downloads/cas.jar";
int child_status;
bool wait_bool = true;
cout << "Run Batch Shell> ";
cin.getline(input_command_line, 256);
if (strcasecmp("exit", input_command_line) == 0) {
cout << "The shell program will terminaten";
exit(0);
} else {
char* commands;
for (int var = 0; var < 4; ++var) {
if (var % 2 == 0) {
commands = checkers_command;
} else if (var % 2 == 1) {
commands = castle_command;
}
char* arg_list_tokened[50];
parse_command_line(arg_list_tokened, commands);
spawn(arg_list_tokened[0], arg_list_tokened);
if (wait_bool) {
// The parent wait until the child finishes.
cout << "The parent will wait for " << arg_list_tokened[0] << endl;
wait(&child_status);
if (WIFEXITED(child_status)) {
printf("The child process exited normally with exit code %dn",
WEXITSTATUS(child_status));
} else {
printf("The child process exited abnormallyn");
}
}
}
cout << "donen";
}
}
return 0;
}
这是Shellcore.cpp文件中的"spawn"方法:Shellcore.h包含了Shellcore.cpp所需的include。
#include "ShellCore.h"
pid_t child_pid;
int spawn(char* program_name, char** args) {
child_pid = fork();
if (child_pid != 0) {
return child_pid;
} else {
execvp(program_name, args);
// If the execvp return, this indicate that an error occurred.
printf("From the spawn function in the parent process, execvp ERRORn");
abort();
}
}
很抱歉问题很长,代码也很长。谢谢预支:)
解析函数:void parse_command_line(char* output_list[], char* command_line) {
char * pch;
int i = 0;
pch = strtok(command_line, " &"");
output_list[0] = pch;
while (pch != NULL) {
i++;
pch = strtok(NULL, " &"");
output_list[i] = pch;
}
output_list[++i] = NULL;
}
strtok()在调用时改变字符串。您将指针"命令"设置为checkers_command或castle_command,但strtok只是要覆盖指向后者的每个内存。如果你复制checkers/castle,而不是在调用解析函数之前指向它,那么每次通过循环,你都会得到一个新的checkers/castle副本。
有比strdup更好的方法,但为了简洁起见,您可以简单地这样做。如果您保留这种复制字符串的方法(不建议),请记住在完成后对返回的指针调用free(),否则将发生内存泄漏。
改成这样:
for (int var = 0; var < 4; ++var)
{
if (var % 2 == 0)
{
//commands = checkers_command;
commands = strdup(checkers_command);
}
else
if (var % 2 == 1)
{
//commands = castle_command;
commands = strdup(castle_command);
}
char* arg_list_tokened[50];
parse_command_line(arg_list_tokened, commands);
spawn(arg_list_tokened[0], arg_list_tokened);
//............
}
相关文章:
- 运行同一解决方案的另一个项目的项目
- 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程序
- 为什么std::async使用同一个线程运行函数
- 创建了三个线程来实例化同一个类.但很快只有一个线程继续运行
- openmp是否可以在同一个cpu(核心)上运行不同的线程?
- 多个WT应用程序可以在同一个网页上运行吗?
- c++ CreateProcess()与第一个运行在同一个窗口中
- 在c++中使用不同的参数同时运行同一个exe
- 为什么在同一个文本文件上多次运行的程序会有不同的输出
- 在两个不同的输入数据集上运行同一个C/ c++程序的两个实例
- 如何通过每次分支新的子进程来多次运行同一个jar文件