🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1496

Re: Inversion-of-control and dependency-injection in C

#1496
From: Ross Finlayson
Date: Thu, 12 Nov 2026 21:44
296 lines
11145 bytes
On 11/12/2026 12:55 PM, Richard Harnden wrote:
> On 12/11/2026 20:11, Lew Pitcher wrote:
>> On Thu, 12 Nov 2026 09:41:04 -0800, Ross Finlayson wrote:
>>
>>> Hello, dear C experts, I'm hoping you can tell me a story about
>>> modularity and composability, about the concerns of inversion-of-control
>>> and dependency-injection in C, basically about wiring
>>> test-time and run-time targets, for example about conditional-includes
>>> and various build-targets, probably mostly about function pointers.
>>
>> Pardon my impertinence, but this sounds like a homework problem to me.
>>
>> Could you rephrase your question without the academic wording?
>>
>
> Or get an AI do to your homework?
>
> Be careful to remove the white-on-white-very-small-font gotcha your
> professor doubtlessly put in.
>
>


When I use "A.I." it's framed as a dialogue and I just copy the
transcripts directly, with the attributions.

I.e., I've never submitted anything to Usenet written by a bot
on my behalf.


I'm not sure whether I'd call it "distance learning", or,
"distance learning", say, ....




Like say for example I'm looking at something like this that I tapped
up this morning, and I want to figure out then how to make it so that
the guts of the workers have a nice real quality of covering all the
bases of covering all the cases, when for example according to the C
host as it were or POSIX there's signals and traps to be figured out
in accounts of "asynchronous concurrency".






Outline of "I/O-handle-bound process model"

The "I/O-handle-bound process model", is an account of a process group
in a process tree,
that works up to the bounds of 1024 many file-descriptors that a
pselect() call can poll,
with up to 1000-many file-descriptors in up to 1000-many worker
processes, for up to
1,000,000-many open connections.

The "1000-many" is basically derived from FD_SETSIZE having a limit of
1024,
and that numbered file-descriptors that can participate in non-blocking I/O
with pselect() notification of readiness, must be less than FD_SETSIZE
their value.

The basic idea is that there's a main or master process, that being also
the process group leader,
that has a "process-protocol" convention, that the master/leader, has
domain sockets
to its child processes, with up to 1000-many worker processes, then
other processes
as the listener, which accepts connections, and a logger and a metrics
process.

The listener is its own process that does accept and management (figured
IP address listing and perhaps a tiny bit of TLS business). It's figured
that it's
an Internet protocol server, and "connection-oriented", or as mostly
about TCP/IP,
and furthermore "association-oriented", about SCTP, then about UDP or
datagram
"protocols", the listener demultiplexes the packets as "connections" to
the workers.

The file-descriptors in a process are numbered, non-negative integers.

The file-descriptors 0, 1, 2 are ubiqitously stdin, stdout, stderr,
"standard input-output-error".

The file-descriptor 3 is reserved as it may be a convention for passing
open connections to created processes.

The file-descriptors 4, 5, 6 are reserved as "stdlog, stdmet, stddmp",
"logging-metrics-dump outputs".

The file-descriptors 21-1020 are reserved for "1000-many connections".

The leader uses the "1000-many connections" for domain sockets to its
"1000-many workers".

Each follower (listener, worker, ...) has a reserved file-descriptor
(11) to connect with the leader.

The leader uses file-descriptor 11 to connect to the listener.
The leader uses file-descriptor 12 to connect to an optional console.
The leader uses file-descriptor 13 to connect to an optional logger.
The leader uses file-descriptor 14 to connect to an optional metrics.

launcher

leader

logger
metrics
console

listener
worker

The model of operation describes how a process is created, according to
fork and execve.

The file-descriptors 0-10 are "standard", about stdin/stdout/stderr and
these
named stdlog/stdmet/stddmp. Usually stdin is un-read, or set to /dev/null.
Usually stdout and stderr are inherited (by all child processes) from
leader.

The stdlog and stdmet and stddmp are inherited from leader. A usual idea is
that these as file-handles may be passed to the leader for example to
the shell,
or that usually according to options, the first child processes of the
leader are
the logger and metrics, with the logger and metrics receiving all the
messages
on those handles, then the leader and all other processes sharing those
handles,
and all writing the logs and metrics to those same handles. Then, the
dumper
is an abstract output stream, outside of logging and metrics and
standard I/O.

The file-descriptors 11-20 are in the "process-protocol".

The file-descriptors 21-1020 of the leader are in the "process-protocol".
The file-descriptors 21-1020 of the listener are in the "process-protocol".
The file-descriptors 21-1020 of the workers are in the
"application-protocol".

The file-descriptor 3 in the listener is the listening socket.
The file-descriptor 3 may be passed to the leader to be passed to the
listener,
the listening socket.

leader:
4 -> stdlog
5 -> stdmet
6 -> stddmp
11 <-> logger
12 <-> metrics
13 <-> listener
14 <-> console
21-1020 <-> workers

logger:
4 <- stdlog
6 <- stddmp
11 <-> logger (leader)
12 <-> metrics

metrics:
5 <- stdmet
11 <-> logger
12 <-> metrics (leader)


listener:
4 -> stdlog
5 -> stdmet
6 -> stddmp
11 <-> logger
12 <-> metrics
13 <-> listener (leader)
14-20 <-> listening socket(s)
21-1020 <-> workers

worker:
4 -> stdlog
5 -> stdmet
6 -> stddmp
11 <-> leader
13 <-> listener
21-1020 -> connections (front-end and back-end)

Each of these has their own "main()" source code and executable,
with that each is created by leader with fork() then setting the handles
respectively, then execve() thus that the child processes start with
their open handles in the process-protocol (11-20) and stdio (0-10)
already opened and directed.

Then, the process-protocol is according to use-cases, as driven by
events, about that the domain-sockets that are under the handles
of the process-protocol, make for passing data, and also live handles,
as are the connections.


The main use-case is that the listener is bound to a listening socket,
as connections are accepted with connection-management happening
in the listener, listener passes the connection to leader, which finds or
create a worker, and passes the connection to worker, which then
proceeds to service the connection according to the application-protocol.

listener: accepts connection

listener -> leader: connection-accepted !
leader -> worker: connection-ready ?
worker -> leader: connection-ready !
leader -> listener: pass-ready !
listener -> leader: connection-pass !
leader -> worker: connection-pass !

When a connection is closed, according to the application-protocol
or if the connection drops or is severed, then worker advises leader,
then leader advises listener.

worker -> leader: connection-closed
leader -> listener: connection-closed

Then, when there are process-level events, the leader makes
commands to the workers, about graceful exit.

leader -> worker: drain connections
leader -> worker: close connections
leader -> worker: exit

Since connections and their attachments & associations
may be short-lived or long-lived, and the goal is that
there only needs be as many workers as fulfill servicing
the connections, then is that there's a process-protocol
about migrating associations (and their connections)
among workers, usually from "sparse" workers to "dense"
workers, where sparse means "not-very-full" and dense
means "not-very-empty", or from "short-lived" workers
to "long-lived" workers when connections are long-lived,
that "association migration" is in the process-protocol.

leader -> worker: emigrate-association
worker -> leader: migrate-association
leader -> worker: immigrate-association


The threading model in the process model

The basic idea of the threading model is that workers
are multi-threaded, the leader is single-threaded, and
that the threads "park" when there is no activity,
yet that the workers have one "sweeper" thread
to account for time-outs, then that as the workers
close their connections, they exit, so that under no
traffic, there is no activity, with all processes "parked".

The idea of "parking" is blocking on the inputs to the
protocols, or the event-driven pumps in the workers.

The threads basically block in pselect() waiting for
ready reading, writing, or other events, or, waiting
on a queue item, pop().

The leader parks in pselect() on 11-1024, the process-protocol.

The listener parks in pselect() about
accept() and pselect() on the listening socket and
the process-protocol sockets.

The worker has four or more threads.

reader-thread: parks in pselect() with read-interest
executor-thread: parks in pop() of application-protocol tasks
writer-thread: parks in pop() of application-protocol responses, then
blocks in pselect() while writing, then parks
sweeper-thread: according to time/alarm, runs on a frequency eg 500
milliseconds, 5 seconds, 5 minutes

When the worker is empty, after its connections close, it exits.

The logger and metrics park in pselect() on the read-side of
stdlog/stdmet/stddmp.

Thusly, at idle, with no connections, the entire process-group is
"parked" (0% CPU, "acquiescent").






Technical Details
Message-ID: <YS2dnRFBYqynrmv3nZ2dnZfqn_SdnZ2d@giganews.com>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-1.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail
References: <yQOdnQ-qnKiiZ2j3nZ2dnZfqn_udnZ2d@giganews.com> <11d56gl$3ud5q$1@dont-email.me> <11d594d$9qh8$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-1.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 12 Nov 2026 21:44:26 +0000
Subject: Re: Inversion-of-control and dependency-injection in C
Newsgroups: comp.lang.c
References: <yQOdnQ-qnKiiZ2j3nZ2dnZfqn_udnZ2d@giganews.com>
 <11d56gl$3ud5q$1@dont-email.me> <11d594d$9qh8$1@dont-email.me>
From: Ross Finlayson <ross.a.finlayson@gmail.com>
Date: Thu, 12 Nov 2026 13:44:15 -0800
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101
 Thunderbird/38.6.0
MIME-Version: 1.0
In-Reply-To: <11d594d$9qh8$1@dont-email.me>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <YS2dnRFBYqynrmv3nZ2dnZfqn_SdnZ2d@giganews.com>
Lines: 296
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-oxYa1PdHmoYRoekOdXK744jst2hcbXnyP4OAxxfPuMgeArQnoXBOCNmJKnzqhlxyuhGA5ygIRebh9uK!TELH0KeKMF4zaFU0lHjDoSopBzlt27wJPHVmxPSGKcTz08vXNBEPijb+KcP99MNWNNKrxwAAJf13
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40