my_judged.cc 涉及到的一些系统函数

fork()
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);
syslog(LOG_ERR | LOG_DAEMON, "This daemon program is already running!\n"); // 写入系统日志
FILE *fp = fopen(...);
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);
signal(SIGQUIT, call_for_exit); // 输入Quit Key的时候(CTRL+\)发送给所有Foreground Group的进程
signal(SIGKILL, call_for_exit); // 无法处理和忽略。中止某个进程
signal(SIGTERM, call_for_exit); // 请求中止进程,kill命令缺省发送
execl() // 执行文件
execl()函数声明如下:
extern int execl(_const char *_path,const char *_argv[],...,NULL)
//函数execl()返回值定义为整形,如果执行成功将不返回!执行失败返回-1。参数列表中char *_path为所要执行的文件的绝对路径,从第二个参数argv开始为执行新的文件所需的参数,最后一个参数必须是控指针(我为了简便用NULL代替)。
waitpid() //...
--------------------------------------------------------------
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
定义函数 int fcntl(int fd, int cmd); 
  int fcntl(int fd, int cmd, long arg); 
  int fcntl(int fd, int cmd, struct flock *lock);
    fcntl()针对(文件)描述符提供控制.参数fd 是被参数cmd操作(如下面的描述)的描述符.
  针对cmd的值,fcntl能够接受第三个参数int arg
ftruncate(fd, 0); // 以写模式打开的文件,就理解为清空文件吧!先!
-----------------------------------------------------------------
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);
va_end(ap);
fclose(fp);
}


原文地址:https://www.cnblogs.com/robbychan/p/3786697.html