Judge Core部分(分函数)

/*
 * Copyright 2008 sempr <iamsempr@gmail.com>
 *
 * Refacted and modified by zhblue<newsclan@gmail.com> 
 * Bug report email newsclan@gmail.com
 * 
 * This file is part of HUSTOJ.
 *
 * HUSTOJ is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * HUSTOJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with HUSTOJ. if not, see <http://www.gnu.org/licenses/>.
 */
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <mysql/mysql.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <signal.h>
#include <sys/resource.h>
static int DEBUG = 0; //是否启用调试,来查看日志运行记录,默认0,不启用
#define BUFFER_SIZE 1024
// (1) pid文件的内容:pid文件为文本文件,内容只有一行, 记录了该进程的ID。
// 用cat命令可以看到。
// (2) pid文件的作用:防止进程启动多个副本。只有获得pid文件(固定路径固定文件名)写入权限(F_WRLCK)的进程才能正常启动并把自身的PID写入该文件中。
// 其它同一个程序的多余进程则自动退出。
#define LOCKFILE "/var/run/judged.pid" //新版HUSTOJ将状态文件的目录修改到了/home/judge/etc
#define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) //读写权限被占,因此是锁模式
#define STD_MB 1048576
//Judge Status
#define OJ_WT0 0
#define OJ_WT1 1
#define OJ_CI 2
#define OJ_RI 3
#define OJ_AC 4
#define OJ_PE 5
#define OJ_WA 6
#define OJ_TL 7
#define OJ_ML 8
#define OJ_OL 9
#define OJ_RE 10
#define OJ_CE 11
#define OJ_CO 12

static char host_name[BUFFER_SIZE];
static char user_name[BUFFER_SIZE];
static char password[BUFFER_SIZE];
static char db_name[BUFFER_SIZE];
static char oj_home[BUFFER_SIZE];
static char oj_lang_set[BUFFER_SIZE];
static int port_number;
static int max_running;
static int sleep_time;
static int sleep_tmp;
static int oj_tot;
static int oj_mod;
static int http_judge = 0;
static char http_baseurl[BUFFER_SIZE];
static char http_username[BUFFER_SIZE];
static char http_password[BUFFER_SIZE];

static bool STOP = false;

static MYSQL *conn;
static MYSQL_RES *res;  //mysql读取结果集,在_get_http/mysql_jobs()中被更新
static MYSQL_ROW row;
//static FILE *fp_log;
static char query[BUFFER_SIZE];//在init_mysql_conf中更新,固定取2倍最大判题客户端的待评判题目solution_id

void call_for_exit(int s) {
  STOP = true;
  printf("Stopping judged...\n");
}

void write_log(const char *fmt, ...) {
  va_list ap;     //初始化指向可变参数列表的指针
  char buffer[4096];
//  time_t          t = time(NULL);
//  int             l;
  sprintf(buffer, "%s/log/client.log", oj_home);
  FILE *fp = fopen(buffer, "a+");
  if (fp == NULL) {
    fprintf(stderr, "openfile error!\n");
    system("pwd");
  }
  va_start(ap, fmt);  //ap指向可变参数列表的开始
  vsprintf(buffer, fmt, ap);//将fmt和ap中指向的参数转换成格式化的字符串,放到buffer中。
                            //用法和sprintf一样,vsprintf参数对应的位置也是一样的。
  fprintf(fp, "%s\n", buffer);//转换后的内容输出到文件中
  if (DEBUG)
    printf("%s\n", buffer);
  va_end(ap);//ap修改为0,使程序更健壮
  fclose(fp);
}

int after_equal(char * c) {
  int i = 0;
  for (; c[i] != '\0' && c[i] != '='; i++)
    ;
  return ++i;
}
void trim(char * c) {
  char buf[BUFFER_SIZE];
  char * start, *end;
  strcpy(buf, c);
  start = buf;
  while (isspace(*start))
    start++;
  end = start;
  while (!isspace(*end))
    end++;
  *end = '\0';
  strcpy(c, start);
}
bool read_buf(char * buf, const char * key, char * value) {
  if (strncmp(buf, key, strlen(key)) == 0) {
    strcpy(value, buf + after_equal(buf));
    trim(value);
    if (DEBUG)
      printf("%s\n", value);
    return 1;
  }
  return 0;
}
void read_int(char * buf, const char * key, int * value) {
  char buf2[BUFFER_SIZE];
  if (read_buf(buf, key, buf2))
    sscanf(buf2, "%d", value);

}
// read the configue file
void init_mysql_conf() {
  FILE *fp = NULL;
  char buf[BUFFER_SIZE];
  host_name[0] = 0;
  user_name[0] = 0;
  password[0] = 0;
  db_name[0] = 0;
  port_number = 3306;
  max_running = 3;
  sleep_time = 1;
  oj_tot = 1;
  oj_mod = 0;
  strcpy(oj_lang_set, "0,1,2,3,4,5,6,7,8,9,10");
  fp = fopen("./etc/judge.conf", "r");
  if (fp != NULL) {
    while (fgets(buf, BUFFER_SIZE - 1, fp)) {
      read_buf(buf, "OJ_HOST_NAME", host_name);
      read_buf(buf, "OJ_USER_NAME", user_name);
      read_buf(buf, "OJ_PASSWORD", password);
      read_buf(buf, "OJ_DB_NAME", db_name);
      read_int(buf, "OJ_PORT_NUMBER", &port_number);
      read_int(buf, "OJ_RUNNING", &max_running);
      read_int(buf, "OJ_SLEEP_TIME", &sleep_time);
      read_int(buf, "OJ_TOTAL", &oj_tot);

      read_int(buf, "OJ_MOD", &oj_mod);

      read_int(buf, "OJ_HTTP_JUDGE", &http_judge);
      read_buf(buf, "OJ_HTTP_BASEURL", http_baseurl);
      read_buf(buf, "OJ_HTTP_USERNAME", http_username);
      read_buf(buf, "OJ_HTTP_PASSWORD", http_password);
      read_buf(buf, "OJ_LANG_SET", oj_lang_set);

    }
    sprintf(query,
        "SELECT solution_id FROM solution WHERE language in (%s) and result<2 and MOD(solution_id,%d)=%d ORDER BY result ASC,solution_id ASC limit %d",
        oj_lang_set, oj_tot, oj_mod, max_running * 2);
    sleep_tmp = sleep_time;
    //  fclose(fp);
  }
}

上面一段代码是读取配置文件./etc/judge.conf,查看了一下judge.conf的内容,大致是如下:

OJ_HOST_NAME=127.0.0.1
OJ_USER_NAME=kiraioj
OJ_PASSWORD=xwd
OJ_DB_NAME=jol
OJ_PORT_NUMBER=3306
OJ_RUNNING=4
OJ_SLEEP_TIME=1
OJ_TOTAL=1
OJ_MOD=0
OJ_JAVA_TIME_BONUS=2
OJ_JAVA_MEMORY_BONUS=64
OJ_JAVA_XMS=-Xms64M
OJ_JAVA_XMX=-Xmx128M
OJ_SIM_ENABLE=0
OJ_HTTP_JUDGE=0
OJ_HTTP_BASEURL=http://127.0.0.1/JudgeOnline
OJ_HTTP_USERNAME=admin
OJ_HTTP_PASSWORD=admin
OJ_OI_MODE=0
OJ_SHM_RUN=1
OJ_USE_MAX_TIME=1
OJ_LANG_SET=0,1,2,3,4,5,6,7,8,9,10,11

处理手段很干净,这样处理配置文件不必担心配置项与项之间的位置关系,因为每一条配置项都会拿去判断,内容少,根本不用考虑效率,值得学习。

//执行sql语句成功返回1,否则返回0 
//并且关闭是否conn,它在init里初始化开始的 
int executesql(const char * sql) {

  if (mysql_real_query(conn, sql, strlen(sql))) {
    if (DEBUG)
      write_log("%s", mysql_error(conn));
    sleep(20);
    conn = NULL;  //执行失败关闭conn,下次查询的时候重新init
    return 1;
  } else
    return 0;
}

int init_mysql() {
  if (conn == NULL) {
    conn = mysql_init(NULL);    // init the database connection
    /* connect the database */
    const char timeout = 30;
    mysql_options(conn, MYSQL_OPT_CONNECT_TIMEOUT, &timeout); //设置数据库查询超时时间

    if (!mysql_real_connect(conn, host_name, user_name, password, db_name, port_number, 0, 0)) {
      if (DEBUG)
        write_log("%s", mysql_error(conn));
      sleep(2);
      return 1;
    } else {
      return 0;
    }
  } else {  //set names utf8告诉服务器将来从这个客户端传来的信息采用字符集utf8
            //每次这样做可以看成是一步保证字符集不出错的查询。
    return executesql("set names utf8");
  }
}

前面的东西有点看不懂了,特别是两种方式去查询待评测题目信息中,发post请求这个。有点乱,所以先看了一眼work(),发现是有两种查询方式,根据不同环境使用不同的查询方式。

int work() {
//      char buf[1024];
  static int retcnt = 0;//统计 已经 完成评测次数  
  int i = 0;
  static pid_t ID[100];  //short类型的宏定义,进程表中的索引项,进程号;保存正在执行的子进程pid 
  static int workcnt = 0;//统计 现用 judge_client进程数量 
  int runid = 0;      //solution_id,测试运行编号
  int jobs[max_running * 2 + 1];//max_running 从judge.conf获取,一般为4,这里设置为工作目录:9
  pid_t tmp_pid = 0;

  //for(i=0;i<max_running;i++){
  //      ID[i]=0;
  //}

  //sleep_time=sleep_tmp;
  /* get the database info */
  if (!get_jobs(jobs)) //如果读取失败或者要评测题目数量为0,jobs[]被置为:1001,1002,0,...0;默认9位 
    retcnt = 0;
  /* exec the submit *///遍历评测每个solution_id的题目,只负责把所以题目全部投入到新的评判进程里
  //不管是否评测完成 
  for (int j = 0; jobs[j] > 0; j++) {
    runid = jobs[j]; //读取solution_id,待评测提交题目id 
    //老式并发处理中,默认oj_tot 为 1 oj_mod 为0,在init_sql_conf中设置 所以无用 
    if (runid % oj_tot != oj_mod)  
      continue;
    if (DEBUG) //调试用默认0 无用 
      write_log("Judging solution %d", runid);
    //workcnt 为static 变量,相当于死锁,统计现用run_client进程 数目 
    //本if 等待可用 子进程,并且用 i 腾出保存 新子进程的位置 
    if (workcnt >= max_running) {           // if no more client can running
        //如果达到了可用最大进程数目,那么等待一个子进程结束
      //waitpid,参考linux 下 c 语言编程下的 进程管理 
      //waitpid()会暂时停止目前进程的执行,直到有信号来到或子进程结束
      //pid_t waitpid(pid_t pid,int * status,int options);
      //pid=-1 代表任意子进程;status 取回子进程识别码,这里不需要所以NULL; 
      //参数options提供了一些额外的选项来控制waitpid,比如不等待继续执行,这里0代表不使用,进程挂起
      //如果 有子进程已经结束,那么执行到这里的时候会直接跳过,子进程也会由僵尸进程释放  
      //返回结束的子进程pid  
      tmp_pid = waitpid(-1, NULL, 0);     // wait 4 one child exit
      workcnt--;//子进程结束了个,那么现用judge_client数量减一  
      retcnt++;//评测完成数加1 
      //清除保存在 ID[]里的已经结束的子进程信息 
      for (i = 0; i < max_running; i++)     // get the client id
        if (ID[i] == tmp_pid)
          break; // got the client id
      ID[i] = 0;
    } else {                                             // have free client

      for (i = 0; i < max_running; i++)     // find the client id
        if (ID[i] == 0)
          break;    // got the client id
    }
    
    //其实这里worknct<max_running 一定成立,除非waitpid()出错 
    //check_out:更新初始化表,但是怎么都不该执行成功才对的啊,为什么还能成功呢
    //如果可以开始新的子进程进行评测 
    if (workcnt < max_running && check_out(runid, OJ_CI)) {
      workcnt++;//正运行子进程数目加1----这里是不是太早了,子进程创建一定能成功?????
            //应该在子进程里更新这个数值吧 
      ID[i] = fork();   //创建子进程 ,将子进程pid返回给父进程,将0返回给子进程  // start to fork
                      //这句写的觉得难理解,父进程会将其更新为新进程pid
              //子进程呢,创建之初会更新为0,那到底是多少???????
              //按照程序,子进程会完整复制父进程的代码,数据,堆栈
              //那么如果是父进程在执行那么ID[i] 不为0而是子进程pid
              //如果是子进程的在执行,那么数据段又是ID[i]为0????
              //那static 的作用呢 
      if (ID[i] == 0) {//如果成立,那么代表是在执行子进程代码,执行run_judge_client 
        if (DEBUG)
          write_log("<<=sid=%d===clientid=%d==>>\n", runid, i);
        run_client(runid, i);  //在子进程里更新ID[0]=pid  // if the process is the son, run it
        exit(0);//子进程执行完毕退出0,父进程不会执行这段if ,在run_client里进程会跳转到execl(judge_client)
                //执行成功不返回,不成功返回非0,保存在erro里,那么这里又是怎么执行到的,子进程如何退出的?????? 
      }

    } else {//理论上,在上个if里已经保证了这里为ID[i] = 0,这里估计是为了进一步保证 
      ID[i] = 0;
    }
  }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容