Example #1
0
int cpServer_start()
{
    int i, pid, ret, ping_pid, sock;
    if (CPGC.daemonize > 0)
    {
        if (daemon(0, 0) < 0)
        {
            return FAILURE;
        }
    }
    if ((sock = cpListen()) < 0)
    {
        cpLog("listen[1] fail");
        return FAILURE;
    }

    CPGS->master_pid = getpid();
    CPGL.process_type = CP_PROCESS_MASTER;
    cpList_create();

    pid = fork();
    switch (pid)
    {
            //创建manager进程
        case 0:
            for (i = 0; i < CPGC.worker_min; i++)
            {
                //alloc了max个 但是只启动min个
                ret = cpCreate_worker_mem(i);
                pid = cpFork_one_worker(i);
                if (pid < 0 || ret < 0)
                {
                    cpLog("Fork worker process fail");
                    return FAILURE;
                }
                else
                {
                    CPGS->workers[i].pid = pid;
                    CPGS->workers_status[i] = CP_WORKER_IDLE;
                }
            }


            //数据库坏连接检测恢复进程
            ret = cpCreate_ping_worker_mem();
            ping_pid = cpFork_ping_worker();
            if (ping_pid < 0 || ret < 0)
            {
                cpLog("Fork ping  process fail");
                return FAILURE;
            }
            CPGS->ping_workers->pid = ping_pid;


            //标识为管理进程
            CPGL.process_type = CP_PROCESS_MANAGER;
            CPGS->worker_num = CPGC.worker_min; //初始为min个worker
            ret = cpWorker_manager_loop();
            exit(ret);
            break;
            //主进程
        default:
            CPGS->manager_pid = pid;
            break;
        case -1:
        {
            cpLog("fork manager process fail");
            return FAILURE;
        }
    }

    cpSignalInit();
    if (cpReactor_start(sock) < 0)
    {
        cpLog("Reactor_start[1] fail");
        return FAILURE;
    }
    return SUCCESS;
}
Example #2
0
int cpServer_start()
{
    int w, pid, ret, sock, g;
    if (CPGC.daemonize > 0)
    {
        if (daemon(0, 0) < 0)
        {
            return FAILURE;
        }
    }
    if ((sock = cpListen()) < 0)
    {
        cpLog("listen[1] fail");
        return FAILURE;
    }

    CPGS->master_pid = getpid();
    CPGL.process_type = CP_PROCESS_MASTER;

    pid = fork();
    switch (pid)
    {
            //创建manager进程
        case 0:
            for (g = 0; g < CPGS->group_num; g++)
            {
                for (w = 0; w < CPGS->G[g].worker_min; w++)
                {
                    //alloc了max个 但是只启动min个
                    ret = cpCreate_worker_mem(w, g);
                    pid = cpFork_one_worker(w, g);
                    if (pid < 0 || ret < 0)
                    {
                        cpLog("Fork worker process fail");
                        return FAILURE;
                    }
                    else
                    {
                        CPGS->G[g].workers[w].pid = pid;
                        CPGS->G[g].workers_status[w] = CP_WORKER_IDLE;
                    }
                }
            }
            //数据库坏连接检测恢复进程
            //            ret = cpCreate_ping_worker_mem();
            //            ping_pid = cpFork_ping_worker();
            //            if (ping_pid < 0 || ret < 0)
            //            {
            //                cpLog("Fork ping  process fail");
            //                return FAILURE;
            //            }
            //            CPGS->ping_workers->pid = ping_pid;

            //标识为管理进程
            CPGL.process_type = CP_PROCESS_MANAGER;
            ret = cpWorker_manager_loop();
            exit(ret);
            break;
            //主进程
        default:
            CPGS->manager_pid = pid;
            break;
        case -1:
        {
            cpLog("fork manager process fail");
            return FAILURE;
        }
    }

    cpSignalInit();
    if (cpReactor_start(sock) < 0)
    {
        cpLog("Reactor_start[1] fail");
        return FAILURE;
    }
    return SUCCESS;
}