Exemple #1
0
        private CompareResult CompareAnswerBySpj(string input, string output, string userOutput)
        {
            string inputPath      = Path.Combine(SpjJudgeContext.TempDirectory, SPJ_INPUT_NAME);
            string outputPath     = Path.Combine(SpjJudgeContext.TempDirectory, SPJ_OUTPUT_NAME);
            string userOutputPath = Path.Combine(SpjJudgeContext.TempDirectory, SPJ_USER_OUTPUT_NAME);

            File.WriteAllText(inputPath, input);
            File.WriteAllText(outputPath, output);
            File.WriteAllText(userOutputPath, userOutput);


            int            exitCode;
            RuntimeMonitor monitor;

            using (ProcessRunner runner = CreateSpecialJudgeProcessRunner())
            {
                runner.ProcessorAffinity = SpjTask.ProcessorAffinity;

                // 创建监视器
                monitor = new RuntimeMonitor(runner.Process, ConfigManager.Config.MonitorInterval)
                {
                    TimeLimit      = SpjTask.TimeLimit,
                    TotalTimeLimit = SpjTask.TimeLimit * TOTAL_TIME_LIMIT_RATIO,
                    MemoryLimit    = SpjTask.MemoryLimit
                };
                monitor.Start();

                runner.OutputLimit = SpjLangConfig.OutputLimit;
                exitCode           = runner.Run("", out string _, out string _, ProcessPriorityClass.RealTime);
                monitor.Dispose();
            }


            File.Delete(inputPath);
            File.Delete(outputPath);
            File.Delete(userOutputPath);

            if (monitor.LimitExceed)
            {
                if (monitor.MemoryCost == monitor.MemoryLimit)
                {
                    throw new JudgeException("Special judge program memory limit exceed");
                }

                throw new JudgeException("Special judge program time limit exceed");
            }

            switch (exitCode)
            {
            case 0:
                return(CompareResult.Accepted);

            case 2:
                return(CompareResult.PresentationError);

            default:
                return(CompareResult.WrongAnswer);
            }
        }
Exemple #2
0
        /// <summary>
        /// 编译评测任务的代码
        /// </summary>
        /// <returns></returns>
        public string Compile()
        {
            using (ProcessRunner runner = CreateProcessRunner())
            {
                runner.ProcessorAffinity = JudgeTask.ProcessorAffinity;
                if (LangConfig.UseUTF8)
                {
                    runner.Encoding = Encoding.UTF8;
                }

                RuntimeMonitor monitor = new RuntimeMonitor(runner.Process, 50)
                {
                    TimeLimit      = LangConfig.MaxCompileTime,
                    TotalTimeLimit = LangConfig.MaxCompileTime
                };

                monitor.Start();

                int    exitcode;
                string output, error;

                try
                {
                    exitcode = runner.Run("", out output, out error);
                }
                catch (Exception ex)
                {
                    return(ex.ToString());
                }
                finally
                {
                    monitor.Dispose();
                }

                if (exitcode != 0)
                {
                    if (monitor.LimitExceed)
                    {
                        return("Compile timeout.");
                    }

                    if (string.IsNullOrEmpty(error))
                    {
                        return("Compiler runtime error.");
                    }

                    return(error);
                }

                return("");
            }
        }
Exemple #3
0
        public string Compile(string args)
        {
            using (ProcessRunner runner = new ProcessRunner(CompilerPath, CompilerWorkDirectory, args))
            {
                runner.ProcessorAffinity = ProcessorAffinity;

                RuntimeMonitor monitor = new RuntimeMonitor(runner.Process);
                monitor.TimeLimit = TimeLimit;

                monitor.Start();

                int    exitcode;
                string output, error;

                try
                {
                    exitcode = runner.Run("", out output, out error);
                }
                catch (Exception e)
                {
                    return(e.ToString());
                }
                finally
                {
                    monitor.Dispose();
                }

                if (!string.IsNullOrEmpty(error)) //存在编译错误
                {
                    return(error);
                }

                if (exitcode != 0)
                {
                    if (monitor.LimitExceed)
                    {
                        return("Compile timeout.");
                    }
                    return("Compiler runtime error.");
                }

                return("");
            }
        }
Exemple #4
0
        public SingleJudgeResult Judge(string input, string output)
        {
            string         userOutput = "";
            string         userError  = "";
            int            exitcode   = 0;
            RuntimeMonitor monitor;

            using (ProcessRunner runner = new ProcessRunner(RunnerPath, RunnerWorkDirectory, RunnerArgs))
            {
                runner.ProcessorAffinity = ProcessorAffinity;
                monitor = new RuntimeMonitor(runner.Process)//创建监视器
                {
                    TimeLimit      = TimeLimit,
                    TotalTimeLimit = TimeLimit * TOTAL_TIME_LIMIT_TUPLING,
                    MemoryLimit    = MemoryLimit
                };
                monitor.Start();

                runner.OutputLimit = OutputLimit;
                exitcode           = runner.Run(input, out userOutput, out userError, ProcessPriorityClass.RealTime);
                monitor.Dispose();
            }

            SingleJudgeResult result = new SingleJudgeResult
            {
                TimeCost    = monitor.TimeCost,
                MemoryCost  = monitor.MemoryCost,
                JudgeDetail = userError,
                ResultCode  = JudgeResultCode.Accepted
            };

            if ((userOutput.Length >= OutputLimit || userError.Length >= OutputLimit))
            {
                result.ResultCode = JudgeResultCode.OutputLimitExceed;
                return(result);
            }

            if (monitor.LimitExceed)
            {
                result.ResultCode = (TimeLimit == monitor.TimeCost) ? JudgeResultCode.TimeLimitExceed : JudgeResultCode.MemoryLimitExceed;
                return(result);
            }

            if (exitcode != 0)//判断是否运行错误
            {
                result.ResultCode = JudgeResultCode.RuntimeError;
                return(result);
            }

            CompareResult cmpResult = CompareAnswer(output, userOutput);//对比答案输出

            if (cmpResult == CompareResult.Accepted)
            {
                result.ResultCode = JudgeResultCode.Accepted;
            }
            else if (cmpResult == CompareResult.PresentationError)
            {
                result.ResultCode = JudgeResultCode.PresentationError;
            }
            else
            {
                result.ResultCode = JudgeResultCode.WrongAnswer;
            }

            return(result);
        }
        public SingleJudgeResult Judge(string input, string output)
        {
            string         userOutput = "";
            string         userError  = "";
            int            exitCode   = 0;
            RuntimeMonitor monitor;

            using (ProcessRunner runner = CreateProcessRunner())
            {
                runner.ProcessorAffinity = JudgeTask.ProcessorAffinity;
                if (LangConfig.UseUtf8)
                {
                    runner.Encoding = Encoding.UTF8;
                }

                // 创建监视器
                int totalTimeLimit = Math.Max(JudgeTask.TimeLimit * TOTAL_TIME_LIMIT_RATIO, MIN_TOTAL_TIME_LIMIT);
                monitor = new RuntimeMonitor(runner.Process, ConfigManager.Config.MonitorInterval,
                                             LangConfig.RunningInVm)
                {
                    TimeLimit      = JudgeTask.TimeLimit,
                    TotalTimeLimit = totalTimeLimit,
                    MemoryLimit    = JudgeTask.MemoryLimit
                };
                monitor.Start();

                runner.OutputLimit = LangConfig.OutputLimit;
                exitCode           = runner.Run(input, out userOutput, out userError,
                                                ProcessPriorityClass.RealTime, ConfigManager.Config.MonitorInterval * 2);

                monitor.Dispose();
            }

            SingleJudgeResult result = new SingleJudgeResult
            {
                TimeCost    = monitor.TimeCost,
                MemoryCost  = monitor.MemoryCost,
                JudgeDetail = userError,
                ResultCode  = JudgeResultCode.Accepted
            };

            if (userOutput.Length >= LangConfig.OutputLimit ||
                userError.Length >= LangConfig.OutputLimit)
            {
                result.ResultCode = JudgeResultCode.OutputLimitExceed;
                return(result);
            }

            if (monitor.LimitExceed)
            {
                result.ResultCode = JudgeTask.TimeLimit == monitor.TimeCost
                    ? JudgeResultCode.TimeLimitExceed
                    : JudgeResultCode.MemoryLimitExceed;
                return(result);
            }

            if (exitCode != 0) // 判断是否运行错误
            {
                result.ResultCode = JudgeResultCode.RuntimeError;
                return(result);
            }

            CompareResult cmpResult = CompareAnswer(output, userOutput); // 对比答案输出

            if (cmpResult == CompareResult.Accepted)
            {
                result.ResultCode = JudgeResultCode.Accepted;
            }
            else if (cmpResult == CompareResult.PresentationError)
            {
                result.ResultCode = JudgeResultCode.PresentationError;
            }
            else
            {
                result.ResultCode = JudgeResultCode.WrongAnswer;
            }

            return(result);
        }