Article View: comp.lang.c
Article #1535Re: Inversion-of-control and dependency-injection in C
From: Ross Finlayson
Date: Sat, 14 Nov 2026 17:05
Date: Sat, 14 Nov 2026 17:05
400 lines
16393 bytes
16393 bytes
On 11/12/2026 01:44 PM, Ross Finlayson wrote: > 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"). > > > > > > > So, this sort of account is example, for an idea, of the considerations. Why this may be considered "relevant to C", vis-a-vis, say, "relevant to platforms & frameworks aboove the system-platform", is because C is the ubiquitous relevant standard to systems programming. Of course, C is just ubiquitous itself in accounts like embedded-programming, here though in a world where all the distributed-programming mostly is defined by standards that, for example, as described in books like "TCP/IP Illustrated" and "Network Programming", which outline in detail the implementations of internetworking on the Internet, C is the milieu. Then, "POSIX" pretty much includes C by convention and practice, also defining a wider surface about the runtime. So, since also a subset of POSIX runs on anything, here it's considered reasonably relevant, unless you know a newsgroup about "modular programming of Internet client & server and peer-to-peer platforms & frameworks for the modern age in its bog-standard commodity monocultures of systems & infrastructure", where it may be more particularly apropos. So what this is then is an outline of a world where long-running server processes, or for example looking at it the other way a massively parallel multi-plexing client for doing jobs, that the process-model is in a real world with limited resources, albeit then with the guarantees given, the responsibilities & guarantees. As you can see from reading it, or, you know, to each their own, the idea is that these "leader", "listener", "worker" processes would be alike "nntpd-leader", "nntpd-listener", "nntpd-worker", with regards to "named and well-known protocols and their well-known ports according to practice and the IANA, or for example running protocols on 443 since that's the only port not firewalled". So, the idea is that a model of a _protocol_, basically has bi-directional or duplex communications, about communicating sequential processes, these then defined in terms of the abstractions of the platform, here "sockets" after "files", and that's the way it is, then about moving & copying the data, and interpreting and invoking the commands as logical, and establishing and maintaining the protocols, since convention and specification and standard and practice. Now, lots of people would say, "I can only look at one thing at one time, if it's a process it needs to have only one PID so that's it". Well, I think so too, it's involved, yet as well, that's not the way it is. So, here this "leader" process is a mostly tiny process whose role mostly is to get passed a privileged port or ports, according to TCP/UDP/SCTP, then that it can run as an un-privileged user otherwise with being able to bind the privileged port, then it's a tiny process so that as it fork()'s to exec/execv/execve or create processes in a group of processes so managed, it's only got one-deep ancestry, and about how in POSIX it must manage the masks and handlers of process signals after "raise", "signal" or "kill", all up and down the tree, and that's the way it is. (Another reason to have only one process is to keep its quotas & limits and monitoring & management a single number, yet that's why it's in a process-group, and multiple processes for the "I/O-handle-bound process model", which is the way it's possible to scale it up this way, about 500K connections.) So, that thusly being as of a sort of "library" routine, for example that "a shell launches it and redirects its I/O the whole process-group how it wants", then the innards or guts, is about, "protocol-modules". (The process-group has its own, "process-protocol", otherwise all, "application-protocols".) Then, there are communicating-sequential-processes it serves, the frontend and, also communicating-sequential-processes its dependencies, the back-end, then also accounts of the half-duplex and full-duplex what it serves, the client-server and peer-to-peer. So, then for "libraries", and, "think like a library author" and these kinds of things, is, "components", and, "think like a component framework provider". The implementors of protocols _need not care_ about the runtime of them, about "a component framework for definition of protocol", and, "a component framework of client & server in protocol", or, "com-peering in protocol", then about the re-usability of the code, in C and not ugly and hot & fast & safe & nice.
Technical Details
Message-ID:
<UKycnSLJ2_luCWX3nZ2dnZfqnPqdnZ2d@giganews.com>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!border-1.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-4.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mailReferences:
<yQOdnQ-qnKiiZ2j3nZ2dnZfqn_udnZ2d@giganews.com> <11d56gl$3ud5q$1@dont-email.me> <11d594d$9qh8$1@dont-email.me> <YS2dnRFBYqynrmv3nZ2dnZfqn_SdnZ2d@giganews.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!border-1.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-4.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Sat, 14 Nov 2026 17:05:55 +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> <YS2dnRFBYqynrmv3nZ2dnZfqn_SdnZ2d@giganews.com> From: Ross Finlayson <ross.a.finlayson@gmail.com> Date: Sat, 14 Nov 2026 09:05:46 -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: <YS2dnRFBYqynrmv3nZ2dnZfqn_SdnZ2d@giganews.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <UKycnSLJ2_luCWX3nZ2dnZfqnPqdnZ2d@giganews.com> Lines: 400 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-cEnI4wyOR+1JY0OBcXkC6RrK/2M1P1fqBbYVIUZ6zcKFeUJ3nE+SFHcWSp1CXiED6I9IcmATzveRs1W!asBu+aVBDIgHtSxZF7lmLtuUemGw13eeCgNEQu24zWpnCmg0VBITKRUWGBvh37fDukb7Adumi50m 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 X-Received-Bytes: 16169