閱讀nginx原始碼_win32

WBOY
發布: 2016-08-08 09:31:57
原創
1101 人瀏覽過

本來打算週末好好看看nginx原始碼的,卻玩了兩天的遊戲。

還沒開始編譯nginx,cygwin還沒裝好,mirror不給力。過了一遍http://blog.csdn.net/kenbinzhang/article/category/603177關於nginx系列的文章,對nginx工程結構有了大致的印象。

int ngx_cdecl main(int argc, char *const *argv)
{
    ngx_int_t         i;
    ngx_log_t        *log;
    ngx_cycle_t      *cycle, init_cycle;
    ngx_core_conf_t  *ccf;

    ngx_debug_init();

    ngx_strerror_init();

    ngx_get_options(argc, argv);	// 解析程序参数,设置对应的全局变量

    if (ngx_show_version) {
		// 输出nginx版本

        if (ngx_show_help) {
			// 输出程序帮助
        }

        if (ngx_show_configure) {
			// 输出nginx编译配置
        }

        if (!ngx_test_config) {
            return 0;
        }
    }

	/* 计算了一下每种日期格式的字符串长度,并调用ngx_time_update。
	每调用一次ngx_time_update,会更新一个日期字符串cache对应slot里面的日期字符
	更新顺序为0~63 0~63 …… Question:它这个Cache有什么用?*/
    ngx_time_init();	// TODO

#if (NGX_PCRE)
    ngx_regex_init();	// 初始化正则表达式库
#endif

    ngx_pid = ngx_getpid();
	/*初始化日志文件。ngx_prefix是在ngx_get_options函数中解析出来的,
	如果没有制定-p参数,那么它就是NULL,此时,程序采用NGX_PREFIX作为日志存储目录。
	*/
    log = ngx_log_init(ngx_prefix);

    /* STUB */
#if (NGX_OPENSSL)
    ngx_ssl_init(log);
#endif

    ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
    init_cycle.log = log;
    ngx_cycle = &init_cycle;
    init_cycle.pool = ngx_create_pool(1024, log);
	
	/*argc存到了ngx_argc变量中,argv存到了ngx_os_argv变量中、每个命令行参数
	存到了ngx_argv变量中,ngx_os_environ记录了程序环境变量。*/
    ngx_save_argv(&init_cycle, argc, argv);
	
	/*初始化了init_cycle中关于conf文件的一些配置*/
    ngx_process_options(&init_cycle);
	
	/*获取操作系统信息。这个链接到的是ngx_win32_init.c中的函数。
	这个函数里面调用了WSAStartup来初始化winsock。同时,这个函数里面还通过
	WSAIoctl的SIO_GET_EXTENSION_FUNCTION_POINTER选项获取socket的扩展API*/
    ngx_os_init(log);

    ngx_crc32_table_init();
	// Question: 没看明白
    ngx_add_inherited_sockets(&init_cycle);

    ngx_max_module = 0;
	// 网上说ngx_modules变量是在运行auto/configure脚本的时候生成的
    for (i = 0; ngx_modules[i]; i++) {
        ngx_modules[i]->index = ngx_max_module++;
    }

	/* 这个函数有点长,粗略看了一下,包含初始化init_cycle中一些重要的成员
	配置解析、配置文件解析等*/
    cycle = ngx_init_cycle(&init_cycle);

    if (ngx_test_config) {
        return 0;
    }

    if (ngx_signal) {
        return ngx_signal_process(cycle, ngx_signal);
    }

    ngx_os_status(cycle->log);

    ngx_cycle = cycle;

    ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);

    if (ccf->master && ngx_process == NGX_PROCESS_SINGLE) {
        ngx_process = NGX_PROCESS_MASTER;
    }

    ngx_create_pidfile(&ccf->pid, cycle->log);

    ngx_log_redirect_stderr(cycle);

    if (log->file->fd != ngx_stderr) {
        if (ngx_close_file(log->file->fd) == NGX_FILE_ERROR) {
            ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
                          ngx_close_file_n " built-in log failed");
        }
    }

    ngx_use_stderr = 0;

    if (ngx_process == NGX_PROCESS_SINGLE) {
        ngx_single_process_cycle(cycle);
    } else {
        ngx_master_process_cycle(cycle);
    }

    return 0;
}
登入後複製
有點睏了,改天再繼續看吧。 。

以上就介紹了閱讀nginx原始碼_win32,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!