初次提交
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(dcapp)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
|
||||
include_directories(./)
|
||||
|
||||
aux_source_directory(./datacenter DC_SRC)
|
||||
aux_source_directory(./dataservice DS_SRC)
|
||||
|
||||
add_executable(dcapp main.cpp ${DC_SRC} ${DS_SRC})
|
||||
@@ -0,0 +1,3 @@
|
||||
#include "datacenter.h"
|
||||
|
||||
REGISTER_SERVICE(DataCenter);
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef _DCAPP_DATACENTER_DATACENTER_H_
|
||||
#define _DCAPP_DATACENTER_DATACENTER_H_
|
||||
|
||||
#include <unistd.h>
|
||||
#include "service.h"
|
||||
|
||||
class DataCenter : public IService {
|
||||
public:
|
||||
DataCenter(){
|
||||
printf(".1.\n");
|
||||
};
|
||||
~DataCenter(){
|
||||
printf(".2.\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* 服务初始化
|
||||
*/
|
||||
virtual int Init(void *pData) {
|
||||
printf("---DataCenter --Init.....\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行业务(业务循环) while(1){doSomeThings...}
|
||||
*/
|
||||
virtual void Exec() {
|
||||
while(1){
|
||||
printf("--- DataCenter doThins...\n");
|
||||
sleep(2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
virtual const char* getServiceName(){
|
||||
return "DataCenter";
|
||||
}
|
||||
};
|
||||
|
||||
#endif // _DCAPP_DATACENTER_DATACENTER_H_
|
||||
@@ -0,0 +1,3 @@
|
||||
#include "dataservice.h"
|
||||
|
||||
REGISTER_SERVICE(DataService);
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef _DCAPP_DATASERVICE_DATASERVICE_H_
|
||||
#define _DCAPP_DATASERVICE_DATASERVICE_H_
|
||||
|
||||
#include <unistd.h>
|
||||
#include "service.h"
|
||||
|
||||
class DataService :public IService {
|
||||
public:
|
||||
DataService() {
|
||||
printf(".1.\n");
|
||||
}
|
||||
~DataService(){
|
||||
printf(".2.\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* 服务初始化
|
||||
*/
|
||||
virtual int Init(void *pData) {
|
||||
printf("--- DataService Init.....\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行业务(业务循环) while(1){doSomeThings...}
|
||||
*/
|
||||
virtual void Exec() {
|
||||
while(1){
|
||||
printf("-- DataService doThins...\n");
|
||||
sleep(2);
|
||||
}
|
||||
}
|
||||
|
||||
virtual const char* getServiceName(){
|
||||
return "DataService";
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // _DCAPP_DATASERVICE_DATASERVICE_H_
|
||||
@@ -0,0 +1,97 @@
|
||||
#include <sys/wait.h> // 包含 wait 函数
|
||||
|
||||
#include <sched.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <map>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "service.h"
|
||||
|
||||
#define LOCK_FILE "/tmp/dcapp.lock"
|
||||
|
||||
typedef void (*spawn_proc_pt) (void *data);
|
||||
|
||||
static int isProRun(){
|
||||
int fd = open(LOCK_FILE,O_CREAT | O_RDWR,0666);
|
||||
if(fd <0)
|
||||
return 1;
|
||||
struct flock fl;
|
||||
fl.l_type = F_WRLCK; // 写锁
|
||||
fl.l_whence = SEEK_SET;
|
||||
fl.l_start = 0;
|
||||
fl.l_len = 0;
|
||||
if (fcntl(fd, F_SETLK, &fl) == -1) {
|
||||
close(fd);
|
||||
return 1; // 或者可以选择退出程序,因为已经有实例在运行了
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
//返回类型是pid_t
|
||||
static pid_t spawn_process(spawn_proc_pt proc, void *data,const char *name){
|
||||
|
||||
pid_t pid;
|
||||
//创建进程
|
||||
pid = fork();
|
||||
|
||||
switch(pid){
|
||||
case -1:
|
||||
fprintf(stderr,"fork() failed while spawning \"%s\"\n",name);
|
||||
return -1;
|
||||
case 0:
|
||||
prctl(PR_SET_PDEATHSIG,SIGKILL);
|
||||
proc(data);
|
||||
return 0;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
printf("start %s %ld\n",name,(long int)pid);
|
||||
return pid;
|
||||
}
|
||||
|
||||
static void worker_process_init(){
|
||||
cpu_set_t cpu_affinity;
|
||||
CPU_ZERO(&cpu_affinity);
|
||||
CPU_SET(random() % CPU_SETSIZE,&cpu_affinity);
|
||||
//sched_setaffinity
|
||||
if(sched_setaffinity(0,sizeof(cpu_set_t),&cpu_affinity) == -1){
|
||||
fprintf(stderr,"sched_setaffinity() failed\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void worker_process(void *data){
|
||||
if(data == NULL){
|
||||
return ;
|
||||
}
|
||||
IService *pService = (IService*) data;
|
||||
prctl(PR_SET_NAME,pService->getServiceName(),0,0,0);
|
||||
//初始化
|
||||
worker_process_init();
|
||||
pService->Init(NULL);
|
||||
//干活
|
||||
pService->Exec();
|
||||
}
|
||||
|
||||
void start_worker_processes(void *pCfg){
|
||||
std::map<std::string, IService*> map = ServiceFactovy::getServiceMap();
|
||||
for (std::map<std::string, IService*>::iterator itor = map.begin(); itor != map.end(); itor++) {
|
||||
spawn_process(worker_process,itor->second, itor->first.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc,char **argv){
|
||||
|
||||
sigset_t set, oset;
|
||||
sigemptyset(&set);
|
||||
sigaddset(&set, SIGUSR1);
|
||||
sigprocmask(SIG_SETMASK, &set, &oset);
|
||||
|
||||
if(isProRun())
|
||||
exit(0);
|
||||
start_worker_processes(NULL);
|
||||
wait(NULL);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
#ifndef _DCAPP_SERVICE_H_
|
||||
#define _DCAPP_SERVICE_H_
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
class IService{
|
||||
public:
|
||||
/**
|
||||
* 服务初始化
|
||||
*/
|
||||
virtual int Init(void *pData) = 0;
|
||||
|
||||
/**
|
||||
* 执行业务(业务循环) while(1){doSomeThings...}
|
||||
*/
|
||||
virtual void Exec() = 0;
|
||||
|
||||
/**
|
||||
* 获取服务名称
|
||||
* @return
|
||||
*/
|
||||
virtual const char* getServiceName() = 0;
|
||||
};
|
||||
|
||||
|
||||
class ServiceFactovy{
|
||||
public:
|
||||
static std::map<std::string, IService*>& getServiceMap(){
|
||||
static std::map<std::string, IService*> serviceMap;
|
||||
return serviceMap;
|
||||
}
|
||||
};
|
||||
|
||||
#define REGISTER_SERVICE(className) \
|
||||
struct className##Register { \
|
||||
className##Register() { \
|
||||
ServiceFactovy::getServiceMap()[#className] = new className(); \
|
||||
} \
|
||||
} className##RegisterInstance;
|
||||
|
||||
#endif // _DCAPP_SERVICE_H_
|
||||
Reference in New Issue
Block a user