• 对pgpooII的pool_process_context的 proc_id 的理解


    已开始,我以为: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。

  • 相关阅读:
    jenkins+jmeter结合使用
    Bean的前身今世&处理器&Aware
    Spring的profile属性
    XML的验证模式
    org.springframework.beans包
    packge-info.java
    字节码解释执行引擎
    invokedynamic指令
    多态方法调用的解析和分派
    运行时栈帧结构
  • 原文地址:https://www.cnblogs.com/gaojian/p/2631881.html
Copyright © 2020-2023  润新知