分享下HUSTOJ的源碼

純代碼乾貨系列

/*
 * Copyright 2008 sempr <[email protected]>
 *
 * Refacted and modified by zhblue<[email protected]> 
 * Bug report email [email protected]
 * 
 * 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
#define LOCKFILE "/var/run/judged.pid"
#define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
#define STD_MB 1048576

#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);
	vsprintf(buffer, fmt, ap);
	fprintf(fp, "%s\n", buffer);
	if (DEBUG)
		printf("%s\n", buffer);
	va_end(ap);
	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);
	}
}


//當有代評測提交,並且進程數允許的情況下,創建新的子進程調用該評測函數
//輸入:代評測提交的solution_id, 子進程在ID[]中的保存位置 i  
void run_client(int runid, int clientid) {
	char buf[BUFFER_SIZE], runidstr[BUFFER_SIZE];
	//在Linux系統中,Resouce limit指在一個進程的執行過程中,它所能得到的資源的限制,
	//比如進程的core file的最大值,虛擬內存的最大值等 ,這是運行時間,內存大小實現的關鍵 
	/*
	結構體中 rlim_cur是要取得或設置的資源軟限制的值,rlim_max是硬限制
	這兩個值的設置有一個小的約束:	
	1) 任何進程可以將軟限制改爲小於或等於硬限制
	2)任何進程都可以將硬限制降低,但普通用戶降低了就無法提高,該值必須等於或大於軟限制
	3) 只有超級用戶可以提高硬限制
	
	setrlimit(int resource,const struct rlimit rlptr);返回:若成功爲0,出錯爲非0	
	RLIMIT_CPU:CPU時間的最大量值(秒),當超過此軟限制時向該進程發送SIGXCPU信號
	RLIMIT_FSIZE:可以創建的文件的最大字節長度,當超過此軟限制時向進程發送SIGXFSZ
	*/ 
	struct rlimit LIM;
	LIM.rlim_max = 800;
	LIM.rlim_cur = 800;
	setrlimit(RLIMIT_CPU, &LIM);//cpu運行時間限制 

	LIM.rlim_max = 80 * STD_MB;
	LIM.rlim_cur = 80 * STD_MB;
	setrlimit(RLIMIT_FSIZE, &LIM);//可文件大小限制,防止惡意程序的嗎? 

	LIM.rlim_max = STD_MB << 11;//左移11 STD_MB是2^20 MB 2^11MB 2GB機器起碼的2GB虛擬內存? 
	LIM.rlim_cur = STD_MB << 11;
	setrlimit(RLIMIT_AS, &LIM);//最大運行的虛擬內存大小限制 

	LIM.rlim_cur = LIM.rlim_max = 200;
	setrlimit(RLIMIT_NPROC, &LIM);//每個實際用戶ID所擁有的最大子進程數,這些都是爲了防止惡意程序的吧?? 

	//buf[0]=clientid+'0'; buf[1]=0;
	sprintf(runidstr, "%d", runid);//轉換成字符?還是字符串? 
	sprintf(buf, "%d", clientid);

	//write_log("sid=%s\tclient=%s\toj_home=%s\n",runidstr,buf,oj_home);
	//sprintf(err,"%s/run%d/error.out",oj_home,clientid);
	//freopen(err,"a+",stderr);

	if (!DEBUG)
		execl("/usr/bin/judge_client", "/usr/bin/judge_client", runidstr, buf,
				oj_home, (char *) NULL);
	else
	
	//返回值:如果執行成功則函數不會返回, 執行失敗則直接返回-1, 失敗原因存於errno 中. 
	//execl()其中後綴"l"代表list也就是參數列表的意思,第一參數path字符指針所指向要執行的文件路徑, 
	//接下來的參數代表執行該文件時傳遞的參數列表:argv[0],argv[1]... 最後一個參數須用空指針NULL作結束。 
//	執行/bin目錄下的ls, 第一參數爲程序名ls, 第二個參數爲"-al", 第三個參數爲"/etc/passwd"
//execl("/bin/ls", "ls", "-al", "/etc/passwd", (char *) 0);
//這裏第一個參數爲程序名稱judge_client,第二個參數爲代評測題目id, 第三個爲本進程pid保存位置,第四個參數爲oj目錄
//默認/home/judge,第五個參數爲“debug” 
		execl("/usr/bin/judge_client", "/usr/bin/judge_client", runidstr, buf,
				oj_home, "debug", (char *) NULL);

	//exit(0);
}
//執行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;
		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 {
		return executesql("set names utf8");
	}
}
FILE * read_cmd_output(const char * fmt, ...) {
	char cmd[BUFFER_SIZE];

	FILE * ret = NULL;
	va_list ap;

	va_start(ap, fmt);
	vsprintf(cmd, fmt, ap);
	va_end(ap);
	//if(DEBUG) printf("%s\n",cmd);
	ret = popen(cmd, "r");

	return ret;
}
int read_int_http(FILE * f) {
	char buf[BUFFER_SIZE];
	fgets(buf, BUFFER_SIZE - 1, f);
	return atoi(buf);
}
bool check_login() {
	const char * cmd =
			"wget --post-data=\"checklogin=1\" --load-cookies=cookie --save-cookies=cookie --keep-session-cookies -q -O - \"%s/admin/problem_judge.php\"";
	int ret = 0;

	FILE * fjobs = read_cmd_output(cmd, http_baseurl);
	ret = read_int_http(fjobs);
	pclose(fjobs);

	return ret > 0;
}
void login() {
	if (!check_login()) {
		char cmd[BUFFER_SIZE];
		sprintf(cmd,
				"wget --post-data=\"user_id=%s&password=%s\" --load-cookies=cookie --save-cookies=cookie --keep-session-cookies -q -O - \"%s/login.php\"",
				http_username, http_password, http_baseurl);
		system(cmd);
	}

}
int _get_jobs_http(int * jobs) {
	login();
	int ret = 0;
	int i = 0;
	char buf[BUFFER_SIZE];
	const char * cmd =
			"wget --post-data=\"getpending=1&oj_lang_set=%s&max_running=%d\" --load-cookies=cookie --save-cookies=cookie --keep-session-cookies -q -O - \"%s/admin/problem_judge.php\"";
	FILE * fjobs = read_cmd_output(cmd, oj_lang_set, max_running, http_baseurl);
	while (fscanf(fjobs, "%s", buf) != EOF) {
		//puts(buf);
		int sid = atoi(buf);
		if (sid > 0)
			jobs[i++] = sid;
		//i++;
	}
	pclose(fjobs);
	ret = i;
	while (i <= max_running * 2)
		jobs[i++] = 0;
	return ret;
	return ret;
}
//功能:取得待評測題目信息到jobs數組
//輸入:int * jobs :保存solution_id/runid
//輸出:如果查詢成功則返回:要評測題目數量 
//如果查詢待判題目不成功則返回0

int _get_jobs_mysql(int * jobs) {
	//mysql.h
	//如果查詢數據包括二進制或者更快速度 用這個
	//如果執行成功,返回0;不成功非0
	if (mysql_real_query(conn, query, strlen(query))) {
		if (DEBUG)
			write_log("%s", mysql_error(conn));
		sleep(20);
		return 0;
	}
	
	//mysql.h
	//返回具有多個結果的MYSQL_RES結果集合。如果出現錯誤,返回NULL
	//具體參見百度
	res = mysql_store_result(conn);
	int i = 0;
	int ret = 0;
	//遍歷結果集mysql_fetch_row()
	while ((row = mysql_fetch_row(res)) != NULL) {
		jobs[i++] = atoi(row[0]);
	}
	ret = i; //要評測jobs末端 如 0 1 2 有數據,則i=3代表數據
	while (i <= max_running * 2)
		jobs[i++] = 0; //設定的最大工作數目爲max_running*2,將0-8置位0共9個 max_running*2+1數組開這麼大 
	return ret;
	return ret;
}
int get_jobs(int * jobs) {
	if (http_judge) {	//web和core默認連接方式:數據庫,web插入solution,core輪訓/更新solution-result,web輪訓solution-result
		return _get_jobs_http(jobs);
	} else
		return _get_jobs_mysql(jobs);//讀取要判題的任務數量

}

//更新初始化solution表格
//更新成功返回1;否則0
// 疑問:OJ_CI爲2,and result < 2這句怎麼都是不成立,這個Sql語句怎麼都不會執行成功纔對啊 
//用limit 1加了一層保障。避免where 條件出現異常時,錯誤更新影響太多。 
//不知道php初始寫多少,但是調用給的參數爲2啊,不懂!!!! 
bool _check_out_mysql(int solution_id, int result) {
	char sql[BUFFER_SIZE]; //sql語句保存 
	sprintf(sql,
			"UPDATE solution SET result=%d,time=0,memory=0,judgetime=NOW() WHERE solution_id=%d and result<2 LIMIT 1",
			result, solution_id);
//執行sql語句,成功返回0;否則非0 
	if (mysql_real_query(conn, sql, strlen(sql))) {
		syslog(LOG_ERR | LOG_DAEMON, "%s", mysql_error(conn));
		return false;
	} else {
		//影響行數,更新數大於0,執行成功,返回1,否則0 
		if (mysql_affected_rows(conn) > 0ul)
			return true;
		else
			return false;
	}

}

bool _check_out_http(int solution_id, int result) {
	login();
	const char * cmd =
			"wget --post-data=\"checkout=1&sid=%d&result=%d\" --load-cookies=cookie --save-cookies=cookie --keep-session-cookies -q -O - \"%s/admin/problem_judge.php\"";
	int ret = 0;
	FILE * fjobs = read_cmd_output(cmd, solution_id, result, http_baseurl);
	fscanf(fjobs, "%d", &ret);
	pclose(fjobs);

	return ret;
}

//初始更新solution表
//依據參數不同執行不同的更新函數 
bool check_out(int solution_id, int result) {

	if (http_judge) {
		return _check_out_http(solution_id, result);
	} else
		return _check_out_mysql(solution_id, result);

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

	//把本次輪訓到的代評測題目全部投入評測後 
	//在非掛起等待子進程的結束,如果有子進程評測完成結束 
	//在上個的for裏,當可用進程沒有的時候,那麼就必須等其中一個進程結束,那麼才能繼續執行,哪怕在for裏已經有 
	// 子進程結束是殭屍進程了,只要workcnt<max_running,那麼就也不處理子殭屍進程的回收問題,而是優先投入新的子進程
	//進行評測
	//那麼子殭屍進程 誰來回收,何時回收,怎麼回收,總不能等可用的全成了殭屍進程,在for裏用到的時候在進行回收吧 
	//如果可用進程數開的特別大,而一直沒有用戶提交,那豈不是,運行一段時間後,系統肯定會一直有max_running個進程的
	//資源被佔用,而且大大大99%部分都是死的子殭屍進程,for只是用幾個收幾個,而不管也沒法管其他的,因爲for只有當
	//有評測任務的時候纔會執行到,大部分沒有用戶提交程序評測的輪詢時間段裏,不順手回收下,豈不可惜!!! 
	 
	//所以就是while()要完成的任務,父進程執行到這裏的時候,掃一眼是否有待回收子殭屍進程,有就 順手回收一個;
	// 因爲不知道有多少待回收的,什麼時候要回收;所以只且只能在這個輪詢時間段裏回收一個 
	//這個while,純粹是順手牽羊行爲,當然也有更新評測完成數量的重要任務~~~ 
	/*
	如果使用了WNOHANG參數調用waitpid,即使沒有子進程退出,它也會立即返回,不會像wait那樣永遠等下去
	1、當正常返回的時候,waitpid返回收集到的子進程的進程ID;

          2、如果設置了選項WNOHANG,而調用中waitpid發現沒有已退出的子進程可收集,則返回0;

          3、如果調用中出錯,則返回-1,這時errno會被設置成相應的值以指示錯誤所在;
	*/
	while ((tmp_pid = waitpid(-1, NULL, WNOHANG)) > 0) {
		workcnt--;
		retcnt++;
		for (i = 0; i < max_running; i++)     // get the client id
			if (ID[i] == tmp_pid)
				break; // got the client id
		ID[i] = 0;
		printf("tmp_pid = %d\n", tmp_pid);
	}
	//釋放數據庫資源 
	//這裏commit的調用,不知道是爲了關閉conn,還是數據庫不支持自動commit
	//還是徹底縮小日誌,不給機會rollback,待學習?????? 
	if (!http_judge) {
		mysql_free_result(res);                         // free the memory
		executesql("commit");
	}
	if (DEBUG && retcnt)
		write_log("<<%ddone!>>", retcnt);
	//free(ID);
	//free(jobs);
	//返回已經評測的次數 
	return retcnt;
}

int lockfile(int fd) {
	struct flock fl;
	fl.l_type = F_WRLCK;
	fl.l_start = 0;
	fl.l_whence = SEEK_SET;
	fl.l_len = 0;
	return (fcntl(fd, F_SETLK, &fl));
}

int already_running() {
	int fd;
	char buf[16];
	fd = open(LOCKFILE, O_RDWR | O_CREAT, LOCKMODE);
	if (fd < 0) {
		syslog(LOG_ERR | LOG_DAEMON, "can't open %s: %s", LOCKFILE,
				strerror(errno));
		exit(1);
	}
	if (lockfile(fd) < 0) {
		if (errno == EACCES || errno == EAGAIN) {
			close(fd);
			return 1;
		}
		syslog(LOG_ERR | LOG_DAEMON, "can't lock %s: %s", LOCKFILE,
				strerror(errno));
		exit(1);
	}
	ftruncate(fd, 0);
	sprintf(buf, "%d", getpid());
	write(fd, buf, strlen(buf) + 1);
	return (0);
}
int daemon_init(void)

{
	pid_t pid;

	if ((pid = fork()) < 0)
		return (-1);

	else if (pid != 0)
		exit(0); /* parent exit */

	/* child continues */

	setsid(); /* become session leader */

	chdir(oj_home); /* change working directory */

	umask(0); /* clear file mode creation mask */

	close(0); /* close stdin */

	close(1); /* close stdout */

	close(2); /* close stderr */

	return (0);
}

int main(int argc, char** argv) {
	DEBUG = (argc > 2);
	if (argc > 1)
		strcpy(oj_home, argv[1]);
	else
		strcpy(oj_home, "/home/judge");
	chdir(oj_home);    // change the dir

	if (!DEBUG)
		daemon_init();//創建一個daemon守護進程 
	if (strcmp(oj_home, "/home/judge") == 0 && already_running()) {
		syslog(LOG_ERR | LOG_DAEMON,
				"This daemon program is already running!\n");
		return 1;
	}
//	struct timespec final_sleep;
//	final_sleep.tv_sec=0;
//	final_sleep.tv_nsec=500000000;
	init_mysql_conf();	// set the database info
	signal(SIGQUIT, call_for_exit);
	signal(SIGKILL, call_for_exit);
	signal(SIGTERM, call_for_exit);
	int j = 1;
	while (1) {			// start to run
		//這個while的好處在於,只要一有任務就抓緊佔用系統優先把所以任務處理完成,哪怕會空循環幾次的可能存在
		//但是沒有任務後,就會進入到“懶散”的 休息sleep(time)後再輪詢是不是有任務,釋放系統的資源,避免Damon一直
		//死循環佔用系統 
		while (j && (http_judge || !init_mysql())) {

			j = work();//如果讀取失敗或者沒有要評測的數據,那麼返回0,利用那麼有限的幾個進程來評測無限的任務量 

		}
		sleep(sleep_time);
		j = 1;
	}
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章