Judge Core部分(分函數(shù))

/*
 * 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; //是否啟用調(diào)試,來(lái)查看日志運(yùn)行記錄,默認(rèn)0,不啟用
#define BUFFER_SIZE 1024
// (1) pid文件的內(nèi)容:pid文件為文本文件,內(nèi)容只有一行, 記錄了該進(jìn)程的ID。
// 用cat命令可以看到。
// (2) pid文件的作用:防止進(jìn)程啟動(dòng)多個(gè)副本。只有獲得pid文件(固定路徑固定文件名)寫入權(quán)限(F_WRLCK)的進(jìn)程才能正常啟動(dòng)并把自身的PID寫入該文件中。
// 其它同一個(gè)程序的多余進(jìn)程則自動(dòng)退出。
#define LOCKFILE "/var/run/judged.pid" //新版HUSTOJ將狀態(tài)文件的目錄修改到了/home/judge/etc
#define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) //讀寫權(quán)限被占,因此是鎖模式
#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讀取結(jié)果集,在_get_http/mysql_jobs()中被更新
static MYSQL_ROW row;
//static FILE *fp_log;
static char query[BUFFER_SIZE];//在init_mysql_conf中更新,固定取2倍最大判題客戶端的待評(píng)判題目solution_id

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

void write_log(const char *fmt, ...) {
  va_list ap;     //初始化指向可變參數(shù)列表的指針
  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指向可變參數(shù)列表的開始
  vsprintf(buffer, fmt, ap);//將fmt和ap中指向的參數(shù)轉(zhuǎn)換成格式化的字符串,放到buffer中。
                            //用法和sprintf一樣,vsprintf參數(shù)對(duì)應(yīng)的位置也是一樣的。
  fprintf(fp, "%s\n", buffer);//轉(zhuǎn)換后的內(nèi)容輸出到文件中
  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的內(nèi)容,大致是如下:

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

處理手段很干凈,這樣處理配置文件不必?fù)?dān)心配置項(xiàng)與項(xiàng)之間的位置關(guān)系,因?yàn)槊恳粭l配置項(xiàng)都會(huì)拿去判斷,內(nèi)容少,根本不用考慮效率,值得學(xué)習(xí)。

//執(zhí)行sql語(yǔ)句成功返回1,否則返回0 
//并且關(guān)閉是否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;  //執(zhí)行失敗關(guān)閉conn,下次查詢的時(shí)候重新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); //設(shè)置數(shù)據(jù)庫(kù)查詢超時(shí)時(shí)間

    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告訴服務(wù)器將來(lái)從這個(gè)客戶端傳來(lái)的信息采用字符集utf8
            //每次這樣做可以看成是一步保證字符集不出錯(cuò)的查詢。
    return executesql("set names utf8");
  }
}

前面的東西有點(diǎn)看不懂了,特別是兩種方式去查詢待評(píng)測(cè)題目信息中,發(fā)post請(qǐng)求這個(gè)。有點(diǎn)亂,所以先看了一眼work(),發(fā)現(xiàn)是有兩種查詢方式,根據(jù)不同環(huán)境使用不同的查詢方式。

int work() {
//      char buf[1024];
  static int retcnt = 0;//統(tǒng)計(jì) 已經(jīng) 完成評(píng)測(cè)次數(shù)  
  int i = 0;
  static pid_t ID[100];  //short類型的宏定義,進(jìn)程表中的索引項(xiàng),進(jìn)程號(hào);保存正在執(zhí)行的子進(jìn)程pid 
  static int workcnt = 0;//統(tǒng)計(jì) 現(xiàn)用 judge_client進(jìn)程數(shù)量 
  int runid = 0;      //solution_id,測(cè)試運(yùn)行編號(hào)
  int jobs[max_running * 2 + 1];//max_running 從judge.conf獲取,一般為4,這里設(shè)置為工作目錄: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)) //如果讀取失敗或者要評(píng)測(cè)題目數(shù)量為0,jobs[]被置為:1001,1002,0,...0;默認(rèn)9位 
    retcnt = 0;
  /* exec the submit *///遍歷評(píng)測(cè)每個(gè)solution_id的題目,只負(fù)責(zé)把所以題目全部投入到新的評(píng)判進(jìn)程里
  //不管是否評(píng)測(cè)完成 
  for (int j = 0; jobs[j] > 0; j++) {
    runid = jobs[j]; //讀取solution_id,待評(píng)測(cè)提交題目id 
    //老式并發(fā)處理中,默認(rèn)oj_tot 為 1 oj_mod 為0,在init_sql_conf中設(shè)置 所以無(wú)用 
    if (runid % oj_tot != oj_mod)  
      continue;
    if (DEBUG) //調(diào)試用默認(rèn)0 無(wú)用 
      write_log("Judging solution %d", runid);
    //workcnt 為static 變量,相當(dāng)于死鎖,統(tǒng)計(jì)現(xiàn)用run_client進(jìn)程 數(shù)目 
    //本if 等待可用 子進(jìn)程,并且用 i 騰出保存 新子進(jìn)程的位置 
    if (workcnt >= max_running) {           // if no more client can running
        //如果達(dá)到了可用最大進(jìn)程數(shù)目,那么等待一個(gè)子進(jìn)程結(jié)束
      //waitpid,參考linux 下 c 語(yǔ)言編程下的 進(jìn)程管理 
      //waitpid()會(huì)暫時(shí)停止目前進(jìn)程的執(zhí)行,直到有信號(hào)來(lái)到或子進(jìn)程結(jié)束
      //pid_t waitpid(pid_t pid,int * status,int options);
      //pid=-1 代表任意子進(jìn)程;status 取回子進(jìn)程識(shí)別碼,這里不需要所以NULL; 
      //參數(shù)options提供了一些額外的選項(xiàng)來(lái)控制waitpid,比如不等待繼續(xù)執(zhí)行,這里0代表不使用,進(jìn)程掛起
      //如果 有子進(jìn)程已經(jīng)結(jié)束,那么執(zhí)行到這里的時(shí)候會(huì)直接跳過(guò),子進(jìn)程也會(huì)由僵尸進(jìn)程釋放  
      //返回結(jié)束的子進(jìn)程pid  
      tmp_pid = waitpid(-1, NULL, 0);     // wait 4 one child exit
      workcnt--;//子進(jìn)程結(jié)束了個(gè),那么現(xiàn)用judge_client數(shù)量減一  
      retcnt++;//評(píng)測(cè)完成數(shù)加1 
      //清除保存在 ID[]里的已經(jīng)結(jié)束的子進(jìn)程信息 
      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
    }
    
    //其實(shí)這里worknct<max_running 一定成立,除非waitpid()出錯(cuò) 
    //check_out:更新初始化表,但是怎么都不該執(zhí)行成功才對(duì)的啊,為什么還能成功呢
    //如果可以開始新的子進(jìn)程進(jìn)行評(píng)測(cè) 
    if (workcnt < max_running && check_out(runid, OJ_CI)) {
      workcnt++;//正運(yùn)行子進(jìn)程數(shù)目加1----這里是不是太早了,子進(jìn)程創(chuàng)建一定能成功?????
            //應(yīng)該在子進(jìn)程里更新這個(gè)數(shù)值吧 
      ID[i] = fork();   //創(chuàng)建子進(jìn)程 ,將子進(jìn)程pid返回給父進(jìn)程,將0返回給子進(jìn)程  // start to fork
                      //這句寫的覺得難理解,父進(jìn)程會(huì)將其更新為新進(jìn)程pid
              //子進(jìn)程呢,創(chuàng)建之初會(huì)更新為0,那到底是多少???????
              //按照程序,子進(jìn)程會(huì)完整復(fù)制父進(jìn)程的代碼,數(shù)據(jù),堆棧
              //那么如果是父進(jìn)程在執(zhí)行那么ID[i] 不為0而是子進(jìn)程pid
              //如果是子進(jìn)程的在執(zhí)行,那么數(shù)據(jù)段又是ID[i]為0????
              //那static 的作用呢 
      if (ID[i] == 0) {//如果成立,那么代表是在執(zhí)行子進(jìn)程代碼,執(zhí)行run_judge_client 
        if (DEBUG)
          write_log("<<=sid=%d===clientid=%d==>>\n", runid, i);
        run_client(runid, i);  //在子進(jìn)程里更新ID[0]=pid  // if the process is the son, run it
        exit(0);//子進(jìn)程執(zhí)行完畢退出0,父進(jìn)程不會(huì)執(zhí)行這段if ,在run_client里進(jìn)程會(huì)跳轉(zhuǎn)到execl(judge_client)
                //執(zhí)行成功不返回,不成功返回非0,保存在erro里,那么這里又是怎么執(zhí)行到的,子進(jìn)程如何退出的?????? 
      }

    } else {//理論上,在上個(gè)if里已經(jīng)保證了這里為ID[i] = 0,這里估計(jì)是為了進(jìn)一步保證 
      ID[i] = 0;
    }
  }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容