Document of c-modernization-kit (porter) 1.0.0
Loading...
Searching...
No Matches
tcpServer_linux.c File Reference

TCP サーバーサンプル Linux 実装。 More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <signal.h>
#include <errno.h>
#include "tcpServer.h"
Include dependency graph for tcpServer_linux.c:

Go to the source code of this file.

Macros

#define MAX_EPOLL_EVENTS   64
 epoll_wait で一度に取得する最大イベント数。

Functions

static void sigchld_handler (int sig)
 SIGCHLD シグナルハンドラ。
static void shutdown_handler (int sig)
 SIGINT / SIGTERM シグナルハンドラ。
static int create_listen_socket (int port)
 listen ソケットを作成してバインドし、待ち受けを開始します。
static void worker_loop (int server_fd, int worker_id, int conns_per_worker)
 ワーカープロセスのメインループ (prefork モード用)。
void platform_init (ClientSessionFn session_fn)
 プラットフォーム初期化 (Windows: WSAStartup / Linux: no-op)。
void platform_cleanup (void)
 プラットフォーム後処理 (Windows: WSACleanup / Linux: no-op)。
int dispatch_internal_args (int argc, char *argv[])
 内部起動引数を処理します。
void run_fork_server (int port)
 fork モードのサーバーを起動します。
void run_prefork_server (int port, int num_workers, int conns_per_worker)
 prefork モードのサーバーを起動します。

Variables

static volatile sig_atomic_t running = 1

Detailed Description

TCP サーバーサンプル Linux 実装。

Author
c-modernization-kit sample team
Date
2026/03/17
Version
1.0.0

Linux 固有のサーバー処理を実装します。

  • fork() を使った接続ごとプロセス生成 (run_fork_server)
  • fork() を使ったプリフォーク (run_prefork_server)
  • プラットフォームフック (platform_init / platform_cleanup / dispatch_internal_args)

main() / handle_client_session() / parse_args()tcpServer.c に実装します。 g_session_fn の実体は tcpServer_common.c に定義されます。

Definition in file tcpServer_linux.c.

Macro Definition Documentation

◆ MAX_EPOLL_EVENTS

#define MAX_EPOLL_EVENTS   64

epoll_wait で一度に取得する最大イベント数。

Definition at line 36 of file tcpServer_linux.c.

Referenced by worker_loop().

Function Documentation

◆ sigchld_handler()

void sigchld_handler ( int sig)
static

SIGCHLD シグナルハンドラ。

Parameters
[in]sigシグナル番号 (未使用)。

waitpid() でゾンビプロセスを回収します。

Definition at line 54 of file tcpServer_linux.c.

Referenced by run_fork_server(), and run_prefork_server().

Here is the caller graph for this function:

◆ shutdown_handler()

void shutdown_handler ( int sig)
static

SIGINT / SIGTERM シグナルハンドラ。

Parameters
[in]sigシグナル番号 (未使用)。

running フラグを 0 にセットして prefork のメインループを終了させます。

Definition at line 67 of file tcpServer_linux.c.

References running.

Referenced by run_prefork_server().

Here is the caller graph for this function:

◆ create_listen_socket()

int create_listen_socket ( int port)
static

listen ソケットを作成してバインドし、待ち受けを開始します。

Parameters
[in]port待ち受けポート番号。
Returns
listen ソケットのファイルディスクリプタ。
Attention
失敗した場合は exit() で終了します。

Definition at line 85 of file tcpServer_linux.c.

Referenced by run_fork_server(), and run_prefork_server().

Here is the caller graph for this function:

◆ worker_loop()

void worker_loop ( int server_fd,
int worker_id,
int conns_per_worker )
static

ワーカープロセスのメインループ (prefork モード用)。

Parameters
[in]server_fdサーバーソケットのファイルディスクリプタ。
[in]worker_idワーカーの識別番号。
[in]conns_per_worker同時接続数。1 の場合は逐次処理、2 以上は epoll 多重処理。

conns_per_worker == 1 の場合: accept() → g_session_fn() を繰り返す従来の逐次処理。

conns_per_worker > 1 の場合: epoll を使ったイベントループで最大 conns_per_worker 本のコネクションを シングルスレッドで同時処理します。容量に達すると server_fd を epoll から 除去して新規 accept を止め、空きが生じると再登録して受け付けを再開します。

Definition at line 132 of file tcpServer_linux.c.

References BUFFER_SIZE, client_close, client_recv, client_send, g_session_fn, get_pid, MAX_EPOLL_EVENTS, and running.

Referenced by run_prefork_server().

Here is the caller graph for this function:

◆ platform_init()

void platform_init ( ClientSessionFn session_fn)

プラットフォーム初期化 (Windows: WSAStartup / Linux: no-op)。

Parameters
[in]session_fnセッション処理関数。g_session_fn に保存されます。

Definition at line 254 of file tcpServer_linux.c.

References g_session_fn.

Referenced by main().

Here is the caller graph for this function:

◆ platform_cleanup()

void platform_cleanup ( void )

プラットフォーム後処理 (Windows: WSACleanup / Linux: no-op)。

Definition at line 259 of file tcpServer_linux.c.

Referenced by main().

Here is the caller graph for this function:

◆ dispatch_internal_args()

int dispatch_internal_args ( int argc,
char * argv[] )

内部起動引数を処理します。

Parameters
[in]argcコマンドライン引数の数。
[in]argvコマンドライン引数の配列。
Returns
内部起動引数を処理した場合は 1、通常起動の場合は 0 を返します。

Windows では --child <handle> / --worker <pipe> を検出して処理します。 Linux では常に 0 を返します。

Definition at line 264 of file tcpServer_linux.c.

Referenced by main().

Here is the caller graph for this function:

◆ run_fork_server()

void run_fork_server ( int port)

fork モードのサーバーを起動します。

Parameters
[in]port待ち受けポート番号。

Definition at line 275 of file tcpServer_linux.c.

References create_listen_socket(), g_session_fn, get_pid, and sigchld_handler().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ run_prefork_server()

void run_prefork_server ( int port,
int num_workers,
int conns_per_worker )

prefork モードのサーバーを起動します。

Parameters
[in]port待ち受けポート番号。
[in]num_workers事前生成するワーカープロセス数。
[in]conns_per_worker1 ワーカーあたりの同時接続数。 1 の場合は従来の逐次処理。 2 以上の場合はイベント駆動型の多重接続処理。

Definition at line 316 of file tcpServer_linux.c.

References create_listen_socket(), get_pid, running, shutdown_handler(), sigchld_handler(), and worker_loop().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ running

volatile sig_atomic_t running = 1
static