/*
* 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;
}
}