//create worker child proccess static int swFactoryProcess_manager_start(swFactory *factory) { swFactoryProcess *object = factory->object; int i, pid, ret; int writer_pti; swServer *serv = factory->ptr; #if SW_WORKER_IPC_MODE == 2 #define _SW_PATH_BUF_LEN 128 //这里使用ftok来获取消息队列的key char path_buf[_SW_PATH_BUF_LEN]; char *path_ptr = getcwd(path_buf, _SW_PATH_BUF_LEN); //读数据队列 if(swQueueMsg_create(&object->rd_queue, 1, ftok(path_ptr, 1), 1) < 0) { swError("[Master] swPipeMsg_create[In] fail. Error: %s [%d]", strerror(errno), errno); return SW_ERR; } //为TCP创建写队列 if (serv->have_tcp_sock == 1) { //写数据队列 if(swQueueMsg_create(&object->wt_queue, 1, ftok(path_ptr, 2), 1) < 0) { swError("[Master] swPipeMsg_create[out] fail. Error: %s [%d]", strerror(errno), errno); return SW_ERR; } } #else object->pipes = sw_calloc(object->worker_num, sizeof(swPipe)); if (object->pipes == NULL) { swError("malloc[worker_pipes] fail. Error: %s [%d]", strerror(errno), errno); return SW_ERR; } //worker进程的pipes for (i = 0; i < object->worker_num; i++) { if (swPipeUnsock_create(&object->pipes[i], 1, SOCK_DGRAM) < 0) { swError("create unix socket[1] fail"); return SW_ERR; } object->workers[i].pipe_master = object->pipes[i].getFd(&object->pipes[i], 1); object->workers[i].pipe_worker = object->pipes[i].getFd(&object->pipes[i], 0); } #endif if (serv->task_worker_num > 0) { if (swProcessPool_create(&SwooleG.task_workers, serv->task_worker_num, serv->max_request)< 0) { swWarn("[Master] create task_workers fail"); return SW_ERR; } //设置指针和回调函数 SwooleG.task_workers.ptr = serv; SwooleG.task_workers.onTask = swTaskWorker_onTask; } pid = fork(); switch (pid) { //创建manager进程 case 0: for (i = 0; i < object->worker_num; i++) { // close(worker_pipes[i].pipes[0]); writer_pti = (i % object->writer_num); object->workers[i].writer_id = writer_pti; pid = swFactoryProcess_worker_spawn(factory, i); if (pid < 0) { swError("Fork worker process fail"); return SW_ERR; } else { object->workers[i].pid = pid; } } //创建task_worker进程 if (serv->task_worker_num > 0) { swProcessPool_start(&SwooleG.task_workers); } //标识为管理进程 SwooleG.process_type = SW_PROCESS_MANAGER; ret = swFactoryProcess_manager_loop(factory); exit(ret); break; //主进程 default: object->manager_pid = pid; //TCP需要writer线程 if (serv->have_tcp_sock == 1) { int ret = swFactoryProcess_writer_start(factory); if (ret < 0) { return SW_ERR; } } #if SW_WORKER_IPC_MODE != 2 for (i = 0; i < object->worker_num; i++) { writer_pti = (i % object->writer_num); object->workers[i].writer_id = writer_pti; if (serv->have_tcp_sock == 1) { //将写pipe设置到writer的reactor中 object->writers[writer_pti].reactor.add(&(object->writers[writer_pti].reactor), object->workers[i].pipe_master, SW_FD_PIPE); } } #endif break; case -1: swError("[swFactoryProcess_worker_start]fork manager process fail\n"); return SW_ERR; } return SW_OK; }
//create worker child proccess static int swFactoryProcess_manager_start(swFactory *factory) { swFactoryProcess *object = factory->object; int i, pid, ret; int reactor_pti; swServer *serv = factory->ptr; if (serv->ipc_mode == SW_IPC_MSGQUEUE) { //读数据队列 if (swQueueMsg_create(&object->rd_queue, 1, serv->message_queue_key, 1) < 0) { swError("[Master] swPipeMsg_create[In] fail. Error: %s [%d]", strerror(errno), errno); return SW_ERR; } //为TCP创建写队列 if (serv->have_tcp_sock == 1) { //写数据队列 if (swQueueMsg_create(&object->wt_queue, 1, serv->message_queue_key + 1, 1) < 0) { swError("[Master] swPipeMsg_create[out] fail. Error: %s [%d]", strerror(errno), errno); return SW_ERR; } } } else { object->pipes = sw_calloc(serv->worker_num, sizeof(swPipe)); if (object->pipes == NULL) { swError("malloc[worker_pipes] fail. Error: %s [%d]", strerror(errno), errno); return SW_ERR; } //worker进程的pipes for (i = 0; i < serv->worker_num; i++) { if (swPipeUnsock_create(&object->pipes[i], 1, SOCK_DGRAM) < 0) { swError("create unix socket[1] fail"); return SW_ERR; } object->workers[i].pipe_master = object->pipes[i].getFd(&object->pipes[i], 1); object->workers[i].pipe_worker = object->pipes[i].getFd(&object->pipes[i], 0); } } if (SwooleG.task_worker_num > 0) { key_t msgqueue_key = 0; if (SwooleG.task_ipc_mode > 0) { msgqueue_key = serv->message_queue_key + 2; } if (swProcessPool_create(&SwooleG.task_workers, SwooleG.task_worker_num, serv->max_request, msgqueue_key)< 0) { swWarn("[Master] create task_workers fail"); return SW_ERR; } //设置指针和回调函数 SwooleG.task_workers.ptr = serv; SwooleG.task_workers.onTask = swTaskWorker_onTask; if (serv->onWorkerStart != NULL) { SwooleG.task_workers.onWorkerStart = swTaskWorker_onWorkerStart; } } pid = fork(); switch (pid) { //创建manager进程 case 0: //创建子进程 for (i = 0; i < serv->worker_num; i++) { //close(worker_pipes[i].pipes[0]); reactor_pti = (i % serv->writer_num); object->workers[i].reactor_id = reactor_pti; pid = swFactoryProcess_worker_spawn(factory, i); if (pid < 0) { swError("Fork worker process fail"); return SW_ERR; } else { object->workers[i].pid = pid; } } /** * create task worker pool */ if (SwooleG.task_worker_num > 0) { swProcessPool_start(&SwooleG.task_workers); } //标识为管理进程 SwooleG.process_type = SW_PROCESS_MANAGER; ret = swFactoryProcess_manager_loop(factory); exit(ret); break; //主进程 default: SwooleGS->manager_pid = pid; break; case -1: swError("[swFactoryProcess_worker_start]fork manager process fail"); return SW_ERR; } return SW_OK; }