已开始,我以为:pool_process_context 里面的 proc_id是 其 进程ID。其实满不是那么回事:
看Source:
复制代码
/*
* Child process context:
* Manages per pgpool child process context
*/
typedef struct {
/*
* process start time, info on connection to backend etc.
*/
ProcessInfo *process_info;
int proc_id; /* Index to process table(ProcessInfo) (!= UNIX's PID) */
/*
* PostgreSQL server description. Placed on shared memory.
* Includes backend up/down info, hostname, data directory etc.
*/
BackendDesc *backend_desc;
int local_session_id; /* local session id */
} POOL_PROCESS_CONTEXT;
复制代码
再看 pool_init_process_context
复制代码
/*
* Initialize per process context
*/
void pool_init_process_context(void){
process_context = &process_context_d;
if (!process_info){
pool_error("pool_init_process_context: process_info is not set");
child_exit(1);
}
process_context->process_info = process_info;
if (!pool_config->backend_desc){
pool_error("pool_init_process_context: backend_desc is not set");
child_exit(1);
}
process_context->backend_desc = pool_config->backend_desc;
process_context->proc_id = my_proc_id;
process_context->local_session_id = 0; /* initialize local session counter */
}
复制代码
proc_id = my_proc_id。
而 my_proc_id 来自于 main.c 中生成子进程一段:
复制代码
int my_proc_id;
/*
* fork a child
*/
pid_t fork_a_child(int unix_fd, int inet_fd, int id){
pid_t pid;
pid = fork();
if (pid == 0){
/* Before we unconditionally closed pipe_fds[0] and pipe_fds[1]
* here, which is apparently wrong since in the start up of
* pgpool, pipe(2) is not called yet and it mistakenly closes
* fd 0. Now we check the fd > 0 before close(), expecting
* pipe returns fds greater than 0. Note that we cannot
* unconditionally remove close(2) calls since fork_a_child()
* may be called *after* pgpool starting up.
*/
if (pipe_fds[0] > 0){
close(pipe_fds[0]);
close(pipe_fds[1]);
}
myargv = save_ps_display_args(myargc, myargv);
/* call child main */
POOL_SETMASK(&UnBlockSig);
reload_config_request = 0;
my_proc_id = id;
run_as_pcp_child = false;
do_child(unix_fd, inet_fd);
}else if (pid == -1){
pool_error("fork() failed. reason: %s", strerror(errno));
myexit(1);
}
return pid;
}
复制代码
具体来说,在 main.c的代码中,有如下一段。
/* fork the children */
for (i=0;i<pool_config->num_init_children;i++){
process_info[i].pid = fork_a_child(unix_fd, inet_fd, i);
process_info[i].start_time = time(NULL);
}
所以,如果pgpool.conf中 num_init_children为128(缺省值),
那么,每个子进程的 my_proc_id 就会分别是:0,1,2...127。
也就是说,每个子进程的 process_context->proc_id 就是0,1,2,...127。
本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2012/08/10/2631881.html,如需转载请自行联系原作者