5 ms·
If we invoke "OOP" here, why not invoke classes? Or rather "interfaces" in modern OOP-speak, or "traits" in other parts of the landscape. There's a common trai
by nine_k 8y ago
If we invoke "OOP" here, why not invoke classes? Or rather "interfaces" in modern OOP-speak, or "traits" in other parts of the landscape.
There's a common trait among many objects: you can read a sequence of bytes from it. Disk files, sockets, various input devices, random number generators, etc. There's another common trait, writing a stream of bytes. Another, even more common pair of traits, is "opening" and then "closing" something.
All of them can be described using interfaces / traits to clearly communicate which sets of operations are applicable to which objects.
This e.g. can be neatly used to describe the "many things are a file" idea:
File passwords_file = Filesystem.make("/etc/passwd");
Readable password_readable = passwords_file.openRead(); // Could be openWrite().
Process zcat_process = Processes.make("zcat /tmp/something.gz");
Readable zcat_stdout = zcat_process.stdout.openRead(); // Can only be openRead().
ClientSocket sock = Network.makeClientSocket(host, port, options); // Unlike a file.
Readable sock_readable = sock.openRead();
for (Readable r in [password_readable, zcat_stdout, sock_redable]) {
byte b = r.read(); // Read one byte from each, no matter what it is.
}
But this would require a very different language, way more powerful than C (but also not C++ please). Unlike in 1969, now we have languages like that, e.g. Rust and ATS-lang. Likely we need another 10-15 years for a viable, somehow widely used OS kernel written in such a language to emerge.
- aidanlister 8y agoYou are a level too high in the stack - what is happening behind the scenes when you execute any of those?
- comex 8y agoIf it were my design, IPC (auto-generated) to a corresponding method in the kernel.
- nine_k 8y agoAs long as this gets type-checked at compile time, the types info can be erased during compilation and be absent at runtime. Since traits / interfaces can't have their methods overridden, you don't need the dynamic dispatch, per-instance VMT, and such; you can have fixed offsets in a method table per class, both in userland and in kernel. (Some of the indirect calls can probably be made direct if the class is exactly known at compile time.)
- Zalastax 8y agoThe problem with classes is that an object's available methods can change over time. E.g. a file object should not have a write method after having been closed. Ongoing research is trying to fix this problem (see Scribble, mailbox types for unordered interaction), but until then I'd say it's probably best to stick with dynamic messaging.
- nine_k 8y agoAnother approach is that object's available methods cannot change over time, but this requires move semantics: `close(file)` consumes `file`, so you can't invoke anything else on it. Rust has this built-in (lifetimes and move / borrow semantics); I suppose C++ can model this, too.
- Zalastax 8y agoYes! Changing available methods/consuming data terms are virtually the same - the former can be modelled as having methods consume their object and returning a copy of it that has another type. My background is in distributed systems and there linearity has a big cost, which is why the research I mentioned needs to be done.
- Const-me 8y agoYou don’t need the OS to be written in that language. It’s enough to wrap the APIs into these interfaces. CPUs are much faster these days, for many tasks modern PCs are often IO bound, count of kernel calls rarely becomes the bottleneck. Windows does just that with their PowerShell. Here’s an example that copies a file remotely on the server: Invoke-Command -ComputerName SERVER -credential USERNAME -ScriptBlock { Copy-Item -Path C:\Temp\1.txt -Destination C:\Temp\2.txt } Copy-Item cmdlet doesn’t require arguments to be files, it can copy some other objects too, registry keys, installed certificates, etc.