PostgreSQLserver端接收连接后fork进程

main->PostmasterMain->ServerLoop:
	for(;;){
		...
		//会阻塞在这,监视文件描述符的变化
		selres = select(nSockets, &rmask, NULL, NULL, &timeout);
		if (selres > 0){
			for (i = 0; i < MAXLISTEN; i++){
				//找一个有效的fd
				if (ListenSocket[i] == PGINVALID_SOCKET)
					break;
				if (FD_ISSET(ListenSocket[i], &rmask)){
					port = ConnCreate(ListenSocket[i]);->StreamConnection->accept
					BackendStartup(port);//fork进程
					|--	pid = fork_process();
					|	if (pid == 0){//child
					|		InitPostmasterChild();
					|		ClosePostmasterPorts(false);
					|		BackendInitialize(port);
					|		//该函数不会返回
					|		BackendRun(port);->PostgresMain:
					|		|--	for (;;){
					|		|		if (send_ready_for_query){
					|		|			...
					|		|			ReadyForQuery(whereToSendOutput);->pq_flush();->internal_flush->secure_write->secure_raw_write->send
					|		|			send_ready_for_query = false;
					|		|		}
					|		|		firstchar = ReadCommand(&input_message);
					|		|		switch(firstchar):{
					|		|			case 'Q':exec_simple_query(query_string);
					|		|			...
					|		|		}
					|		|--	}
					|	}
					|	...father
					|--	return STATUS_OK	
					//关闭father socket
					StreamClose(port->sock);
					ConnFree(port);
				}
			}
		}
	}

标题名称:PostgreSQLserver端接收连接后fork进程
地址分享:http://hbruida.cn/article/pgoipp.html