把之前slurm_server变成daemon

 
 
#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>

#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>

#include <signal.h>
#include <syslog.h>

#include "socket_server.h"

static void sig_term_func(int SIG)
{
	if(SIG == SIGTERM){
		syslog(LOG_INFO, "terminated.");
		closelog();
		exit(0);
		// do some clean work, e.g., close file/socket
	}
}

static int daemon_init(void)
{
	pid_t pid;

	if((pid = fork()) < 0)
		exit(-1);
	else if(pid != 0) //as to parent, fork will return child's pid
		exit (0);   //parent exit
	else if (pid == 0) {   	//child continues
		setsid();      //become session leader
		umask(0);      //clear file mode creation mask
		close(0);  //close stdin, stdout, stderr
		close(1);
		close(2);

		openlog("slurm server daemon:", LOG_PID, LOG_USER);
		syslog(LOG_INFO, "started.");
		signal(SIGTERM, sig_term_func);

		running(); //real work in child process
	}
	return 0;
}


int main (int argc, char *argv[])
{
//	allocate_noblock();
//	puts("=====================================");
//	allocate_block();
//	puts("=====================================");
//	get_node_info();
//	puts("=====================================");
//	get_pratition_info();
//	puts("=====================================");

	if(daemon_init() == -1){
		printf("can not fork self");
		exit(0);
	}

	return 0;
}


你可能感兴趣的:(把之前slurm_server变成daemon)