开发运行模块
runner.hpp
#pragma once
#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include "../comm/log.hpp"
#include "../comm/util.hpp"
namespace ns_runner
{
using namespace ns_log;
using namespace ns_util;
class Runner
{
public:
Runner() {}
~Runner() {}
public:
// 指明文件名即可,不需要代理路径,不需要带后缀
/*******************************************
* 返回值 > 0: 程序异常了,退出时收到了信号,返回值就是对应的信号编号
* 返回值 == 0: 正常运行完毕的,结果保存到了对应的临时文件中
* 返回值 < 0: 内部错误
* *****************************************/
static int Run(const std::string &file_name)
{
/*********************************************
* 程序运行:
* 1. 代码跑完,结果正确
* 2. 代码跑完,结果不正确
* 3. 代码没跑完,异常了
* Run需要考虑代码跑完,结果正确与否吗??不考虑!
* 结果正确与否:是由我们的测试用例决定的!
* 我们只考虑:是否正确运行完毕
*
* 我们必须知道可执行程序是谁?
* 一个程序在默认启动的时候
* 标准输入: 不处理
* 标准输出: 程序运行完成,输出结果是什么
* 标准错误: 运行时错误信息
* *******************************************/
std::string _execute = PathUtil::Exe(file_name);
std::string _stdin = PathUtil::Stdin(file_name);
std::string _stdout = PathUtil::Stdout(file_name);
std::string _stderr = PathUtil::Stderr(file_name);
umask(0);
int _stdin_fd = open(_stdin.c_str(), O_CREAT | O_RDONLY, 0644);
int _stdout_fd = open(_stdout.c_str(), O_CREAT | O_WRONLY, 0644);
int _stderr_fd = open(_stderr.c_str(), O_CREAT | O_WRONLY, 0644);
if(_stdin_fd < 0 || _stdout_fd < 0 || _stderr_fd < 0){
return -1; //代表打开文件失败
}
pid_t pid = fork();
if(pid < 0){
close(_stdin_fd);
close(_stdout_fd);
close(_stderr_fd);
return -2; //代表创建子进程失败
}
else if(pid == 0)
{
dup2(_stdin_fd, 0);
dup2(_stdout_fd, 1);
dup2(_stderr_fd, 2);
execl(_execute.c_str()/*我要执行谁*/, _execute.c_str()/*我想在命令行上如何执行该程序*/, nullptr);
exit(1);
}
else
{
close(_stdin_fd);
close(_stdout_fd);
close(_stderr_fd);
int status = 0;
waitpid(pid, &status, 0);
// 程序运行异常,一定是因为收到了信号
return status & 0x7F;
}
}
};
}
util.hpp
#pragma once
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/unistd.h>
#include <sys/time.h>
namespace ns_util
{
const std::string temp_path = "./temp/";
class PathUtil
{
public:
static std::string AddSuffix(const std::string &file_name, const std::string &suffix)
{
std::string path_name = temp_path;
path_name += file_name;
path_name += suffix;
return path_name;
}
//编译时需要有的临时文件
//构建源文件路径+后缀的完整文件名
//1234 -> ./temp/1234.cpp
static std::string Src(const std::string &file_name)
{
return AddSuffix(file_name, ".cpp");
}
//构建可执行程序的完整路径+后缀名
static std::string Exe(const std::string &file_name)
{
return AddSuffix(file_name, ".exe");
}
static std::string CompilerError(const std::string &file_name)
{
return AddSuffix(file_name, ".compiler_error");
}
//运行时需要的临时文件
static std::string Stdin(const std::string &file_name)
{
return AddSuffix(file_name, ".stdin");
}
static std::string Stdout(const std::string &file_name)
{
return AddSuffix(file_name, ".stdout");
}
//构建该程序对应的标准错误完整路径+后缀名
static std::string Stderr(const std::string &file_name)
{
return AddSuffix(file_name, ".stderr");
}
};
class FileUtil {
public:
static bool IsFileExists(const std::string &path_name)
{
struct stat st;
if(stat(path_name.c_str(), &st) == 0){
//获取属性成功,文件已经存在
return true;
}
return false;
}
};
class TimeUtil
{
public:
static std::string GetTimeStamp()
{
struct timeval _time;
gettimeofday(&_time, nullptr);
return std::to_string(_time.tv_sec);
}
};
}
测试运行模块
compile_server.cc
#include "compiler.hpp"
#include "runner.hpp"
using namespace ns_compiler;
using namespace ns_runner;
int main()
{
std::string code = "code";
Compiler::Compile(code);
Runner::Run(code);
return 0;
}
加入日志
static int Run(const std::string &file_name)
{
/*********************************************
* 程序运行:
* 1. 代码跑完,结果正确
* 2. 代码跑完,结果不正确
* 3. 代码没跑完,异常了
* Run需要考虑代码跑完,结果正确与否吗??不考虑!
* 结果正确与否:是由我们的测试用例决定的!
* 我们只考虑:是否正确运行完毕
*
* 我们必须知道可执行程序是谁?
* 一个程序在默认启动的时候
* 标准输入: 不处理
* 标准输出: 程序运行完成,输出结果是什么
* 标准错误: 运行时错误信息
* *******************************************/
std::string _execute = PathUtil::Exe(file_name);
std::string _stdin = PathUtil::Stdin(file_name);
std::string _stdout = PathUtil::Stdout(file_name);
std::string _stderr = PathUtil::Stderr(file_name);
umask(0);
int _stdin_fd = open(_stdin.c_str(), O_CREAT | O_RDONLY, 0644);
int _stdout_fd = open(_stdout.c_str(), O_CREAT | O_WRONLY, 0644);
int _stderr_fd = open(_stderr.c_str(), O_CREAT | O_WRONLY, 0644);
if(_stdin_fd < 0 || _stdout_fd < 0 || _stderr_fd < 0){
LOG(ERROR) << "运行时打开标准文件失败" << "\n";
return -1; //代表打开文件失败
}
pid_t pid = fork();
if(pid < 0)
{
LOG(ERROR) << "运行时创建子进程失败" << "\n";
close(_stdin_fd);
close(_stdout_fd);
close(_stderr_fd);
return -2; //代表创建子进程失败
}
else if(pid == 0)
{
dup2(_stdin_fd, 0);
dup2(_stdout_fd, 1);
dup2(_stderr_fd, 2);
execl(_execute.c_str()/*我要执行谁*/, _execute.c_str()/*我想在命令行上如何执行该程序*/, nullptr);
exit(1);
}
else
{
close(_stdin_fd);
close(_stdout_fd);
close(_stderr_fd);
int status = 0;
waitpid(pid, &status, 0);
// 程序运行异常,一定是因为收到了信号
LOG(INFO) << "运行完毕,info:" << (status & 0x7F) << "\n";
return status & 0x7F;
}
}
code.cpp
#include <iostream>
int main()
{
std::cout << "hello world!" << std::endl;
std::cerr << "hello error" << std::endl;
return 0;
}
code.cpp
#include <iostream>
int main()
{
aaa
std::cout << "hello world!" << std::endl;
std::cerr << "hello error" << std::endl;
return 0;
}