{"id":353,"date":"2025-06-22T22:39:15","date_gmt":"2025-06-22T14:39:15","guid":{"rendered":"http:\/\/www.tgwttt.xyz\/?p=353"},"modified":"2025-11-24T16:43:00","modified_gmt":"2025-11-24T08:43:00","slug":"%e8%b4%9f%e8%bd%bd%e5%9d%87%e8%a1%a1%e5%bc%8f%e7%9a%84%e5%9c%a8%e7%ba%bfoj","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=353","title":{"rendered":"\u8d1f\u8f7d\u5747\u8861\u5f0f\u7684\u5728\u7ebfOJ"},"content":{"rendered":"\n<p>gitee\u5730\u5740\uff1a<a href=\"https:\/\/gitee.com\/tgwTTT\/load-balancing-online-judge\">https:\/\/gitee.com\/tgwTTT\/load-balancing-online-judge<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u9879\u76ee\u6982\u8ff0<\/h2>\n\n\n\n<p>\u5728\u7ebf\u5224\u9898\u7cfb\u7edf\uff08Online Judge\uff0c\u7b80\u79f0OJ\uff09\u662f\u4e00\u4e2a\u5141\u8bb8\u7528\u6237\u5728\u7ebf\u7f16\u7a0b\u3001\u63d0\u4ea4\u4ee3\u7801\u5e76\u5b9e\u65f6\u83b7\u53d6\u8bc4\u6d4b\u7ed3\u679c\u7684\u5e73\u53f0\u3002\u672c\u9879\u76ee\u91c7\u7528\u6a21\u5757\u5316\u8bbe\u8ba1\uff0c\u5c06\u7cfb\u7edf\u5206\u4e3a\u4e09\u4e2a\u6838\u5fc3\u6a21\u5757\uff0c\u5b9e\u73b0\u4e86\u4ee3\u7801\u7f16\u8bd1\u3001\u9898\u76ee\u7ba1\u7406\u548c\u8d1f\u8f7d\u5747\u8861\u7b49\u5173\u952e\u529f\u80fd\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7cfb\u7edf\u67b6\u6784\u8bbe\u8ba1<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u6a21\u5757\u5212\u5206<\/h3>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>comm\u516c\u5171\u6a21\u5757<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u63d0\u4f9b\u7cfb\u7edf\u901a\u7528\u7684\u5de5\u5177\u7c7b\u548c\u51fd\u6570<\/li>\n\n\n\n<li>\u5305\u542b\u65e5\u5fd7\u8bb0\u5f55\u3001\u914d\u7f6e\u8bfb\u53d6\u3001\u516c\u5171\u6570\u636e\u7ed3\u6784\u7b49\u57fa\u7840\u7ec4\u4ef6<\/li>\n\n\n\n<li>\u4e3a\u5176\u4ed6\u6a21\u5757\u63d0\u4f9b\u7edf\u4e00\u7684\u57fa\u7840\u670d\u52a1\u652f\u6301<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>compile_server\u7f16\u8bd1\u670d\u52a1\u5668\u6a21\u5757<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u8d1f\u8d23\u7528\u6237\u63d0\u4ea4\u4ee3\u7801\u7684\u7f16\u8bd1\u548c\u6267\u884c<\/li>\n\n\n\n<li>\u5b9e\u73b0\u5b89\u5168\u6c99\u7bb1\u73af\u5883\uff0c\u9632\u6b62\u6076\u610f\u4ee3\u7801\u5bf9\u7cfb\u7edf\u9020\u6210\u7834\u574f<\/li>\n\n\n\n<li>\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\uff08C\/C++\u3001Java\u3001Python\u7b49\uff09\u7684\u7f16\u8bd1\u8fd0\u884c<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>oj_server\u6838\u5fc3\u4e1a\u52a1\u6a21\u5757<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u9898\u76ee\u5217\u8868\u83b7\u53d6\u548c\u5c55\u793a<\/li>\n\n\n\n<li>\u9898\u76ee\u7f16\u8f91\u548c\u63d0\u4ea4\u754c\u9762<\/li>\n\n\n\n<li>\u8d1f\u8f7d\u5747\u8861\u529f\u80fd\uff0c\u5408\u7406\u5206\u914d\u7f16\u8bd1\u4efb\u52a1<\/li>\n\n\n\n<li>\u7528\u6237\u7ba1\u7406\u3001\u63d0\u4ea4\u8bb0\u5f55\u3001\u6392\u540d\u7b49\u4e1a\u52a1\u529f\u80fd(\u62d3\u5c55)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>temp\u4e34\u65f6\u6587\u4ef6\u76ee\u5f55<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u5b58\u50a8\u7528\u6237\u63d0\u4ea4\u7684\u4ee3\u7801\u6587\u4ef6<\/li>\n\n\n\n<li>\u4fdd\u5b58\u7f16\u8bd1\u8fc7\u7a0b\u4e2d\u7684\u4e34\u65f6\u6587\u4ef6<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p><strong>\u6838\u5fc3\u6280\u672f\u5b9e\u73b0<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u524d\u7aef<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. HTML\u6a21\u677f\u6e32\u67d3\u6280\u672f<\/h3>\n\n\n\n<p>\u9879\u76ee\u4f7f\u7528ctemplate\u5e93\u5b9e\u73b0\u52a8\u6001\u7f51\u9875\u751f\u6210\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include <iostream>\n#include <string>\n#include <ctemplate\/template.h>\n\nint main()\n{\n    std::string in_html = \".\/test.html\";\n    std::string value = \"\u6bd4\u7279\u5c31\u4e1a\u8bfe\";\n    \n    \/\/ \u5f62\u6210\u6570\u636e\u5b57\u5178\n    ctemplate::TemplateDictionary root(\"test\");\n    root.SetValue(\"key\", value);\n    \n    \/\/ \u83b7\u53d6\u88ab\u6e32\u67d3\u7f51\u9875\u5bf9\u8c61\n    ctemplate::Template *tpl = ctemplate::Template::GetTemplate(in_html, ctemplate::DO_NOT_STRIP);\n    \n    \/\/ \u6dfb\u52a0\u5b57\u5178\u6570\u636e\u5230\u7f51\u9875\u4e2d\n    std::string out_html;\n    tpl->Expand(&out_html, &root);\n    \n    std::cout << out_html << std::endl;\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"972\" height=\"930\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-33.png\" alt=\"\" class=\"wp-image-356\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-33.png 972w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-33-300x287.png 300w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-33-768x735.png 768w\" sizes=\"auto, (max-width: 972px) 100vw, 972px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"825\" height=\"405\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-34.png\" alt=\"\" class=\"wp-image-357\" style=\"width:840px;height:auto\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-34.png 825w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-34-300x147.png 300w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-34-768x377.png 768w\" sizes=\"auto, (max-width: 825px) 100vw, 825px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"807\" height=\"861\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-35.png\" alt=\"\" class=\"wp-image-358\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-35.png 807w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-35-281x300.png 281w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/09\/image-35-768x819.png 768w\" sizes=\"auto, (max-width: 807px) 100vw, 807px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u540e\u7aef<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u540e\u7aef\u6846\u67b6<\/strong>: C++\u4e3a\u4e3b\uff0c\u7ed3\u5408\u591a\u79cd\u7cfb\u7edf\u8c03\u7528<\/li>\n\n\n\n<li><strong>\u5b57\u7b26\u4e32\u5904\u7406<\/strong>: Boost Algorithm\u5e93<\/li>\n\n\n\n<li><strong>\u6570\u636e\u5e8f\u5217\u5316<\/strong>: JSON\u683c\u5f0f\u7528\u4e8e\u6570\u636e\u4f20\u8f93<\/li>\n\n\n\n<li><strong>\u5b89\u5168\u673a\u5236<\/strong>: Linux\u8d44\u6e90\u9650\u5236(rlimit)\u548c\u4fe1\u53f7\u5904\u7406<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u516c\u5171\u6a21\u5757\uff08comm\uff09<\/h3>\n\n\n\n<p>\u516c\u5171\u6a21\u5757<\/p>\n\n\n\n<p>\u4e3a\u6574\u4e2a\u7cfb\u7edf\u63d0\u4f9b\u57fa\u7840\u652f\u6301\uff0c\u5305\u542b\u4ee5\u4e0b\u6838\u5fc3\u7ec4\u4ef6\uff1a<\/p>\n\n\n\n<p>1.<strong>\u65e5\u5fd7\u7cfb\u7edf<\/strong><\/p>\n\n\n\n<p>2.<strong>\u914d\u7f6e\u7ba1\u7406<\/strong><\/p>\n\n\n\n<p>3.<strong>\u516c\u5171\u6570\u636e\u7ed3\u6784<\/strong><\/p>\n\n\n\n<p><strong>2. \u7f16\u8bd1\u670d\u52a1\u5668\u6a21\u5757\uff08compile_server\uff09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class CompileServer {\npublic:\n    \/\/ \u7f16\u8bd1\u4ee3\u7801\n    CompileResult CompileCode(const std::string& code, \n                             const std::string& language,\n                             const std::string& work_dir);\n    \n    \/\/ \u6267\u884c\u7a0b\u5e8f\n    ExecuteResult ExecuteProgram(const std::string& executable_path,\n                                const std::string& input,\n                                int time_limit,\n                                int memory_limit);\n    \n    \/\/ \u6e05\u7406\u5de5\u4f5c\u76ee\u5f55\n    void CleanWorkDir(const std::string& work_dir);\n};<\/code><\/pre>\n\n\n\n<p>\u5b89\u5168\u6c99\u7bb1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class SecuritySandbox {\nprivate:\n    \/\/ \u8bbe\u7f6e\u8d44\u6e90\u9650\u5236\n    void SetResourceLimit(int time_limit, int memory_limit);\n    \n    \/\/ \u8bbe\u7f6e\u5b89\u5168\u7b56\u7565\n    void SetSecurityPolicy();\n    \npublic:\n    \/\/ \u5728\u6c99\u7bb1\u4e2d\u8fd0\u884c\u7a0b\u5e8f\n    SandboxResult RunInSandbox(const std::string& command,\n                              const std::string& input,\n                              int time_limit,\n                              int memory_limit);\n};<\/code><\/pre>\n\n\n\n<p><strong>3. \u4e1a\u52a1\u903b\u8f91\u670d\u52a1\u5668\u6a21\u5757\uff08oj_server\uff09<\/strong><\/p>\n\n\n\n<p><strong>HTTP\u8bf7\u6c42\u5904\u7406<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class HttpServer {\npublic:\n    \/\/ \u542f\u52a8\u670d\u52a1\u5668\n    void Start(int port);\n    \n    \/\/ \u8def\u7531\u6ce8\u518c\n    void AddRoute(const std::string& path, \n                 std::function<void(const HttpRequest&#038;, HttpResponse&#038;)> handler);\n    \nprivate:\n    \/\/ \u8bf7\u6c42\u8def\u7531\n    void RouteRequest(const HttpRequest& req, HttpResponse& resp);\n};<\/code><\/pre>\n\n\n\n<p><strong>\u9898\u76ee\u7ba1\u7406<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ProblemManager {\nprivate:\n    std::vector<Problem> problems_;\n    std::string problems_file_;\n    \npublic:\n    bool LoadProblems();\n    Problem GetProblem(int problem_id);\n    std::vector<Problem> GetAllProblems();\n    bool AddProblem(const Problem& problem);\n};<\/code><\/pre>\n\n\n\n<p><strong>\u8d1f\u8f7d\u5747\u8861\u5668<\/strong><\/p>\n\n\n\n<p>class LoadBalancer {<br>private:<br>std::vector servers_;<br>std::atomic current_index_{0};<\/p>\n\n\n\n<p>public:<br>\/\/ \u6dfb\u52a0\u7f16\u8bd1\u670d\u52a1\u5668<br>void AddServer(const CompileServerInfo& server);<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u9009\u62e9\u670d\u52a1\u5668\uff08\u8f6e\u8be2\u7b97\u6cd5\uff09\nCompileServerInfo SelectServer();\n\n\/\/ \u5065\u5eb7\u68c0\u67e5\nvoid HealthCheck();<\/code><\/pre>\n\n\n\n<p>};<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u5c31\u662f\u7b80\u7565\u7684\u9879\u76ee\u4ecb\u7ecd\u4e86\uff0c\u5b8c\u6574\u4ee3\u7801\u8bf7\u53c2\u9760gitee\u4ed3\u5e93\u4e2d\u7684\u4ee3\u7801<\/p>\n\n\n\n<p>\u4eca\u5929\u7684\u66f4\u65b0\u5c31\u5230\u8fd9\u5566\uff01\uff01\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>gitee\u5730\u5740\uff1ahttps:\/\/gitee.com\/tgwTTT\/load-balancing-online-&#8230;<\/p>\n","protected":false},"author":1,"featured_media":356,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-353","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=353"}],"version-history":[{"count":4,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/353\/revisions"}],"predecessor-version":[{"id":887,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/353\/revisions\/887"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/media\/356"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}