yes, not a unix os but rather unix-like, and i want to program all of it on python, is that possible?? even the kernel, i want it all python. i know most kernels use c++ or c* but maybe python has a library to turn c* into python?? i’m still sort of a beginner but thanks and i would appreciate the answers
No, not if you want it to run on the hardware directly at least. If you want it to run as an emulator of sorts under Linux then you could.
Python needs an interpretor to run on and lacks direct memory/hardware access.
What OS will run the Python interpreter?
Can’t Python be translated into machine code and packaged into a binary? I swear I have no experience in OS development, just curious.
Can’t Python be translated into machine code
Yes, and that’s basically what the CPython interpreter does when you call a Python script. It sometimes even leaves the result laying in your filesystem, with the extension .pyc . This is the byte code (aka machine code) for CPython’s implementation of the Python Virtual Machine (PVM).
and packaged into a binary?
Almost. The .pyc file is meant to run with the appropriate PVM, not for x86 or ARM64, for example. But if you did copy that .pyc to another computer that has a CPython PVM, then you can run that byte code and the Python code should work.
To create an actual x86 or ARM64 binary, you might use a Python compiler like cython, which compiles to x86 or ARM64 by first translating to C, and then compiling that. The result is a very inefficient and slow binary, but it is functional. You probably shouldn’t do this though.
Yes, and that’s basically what the CPython interpreter does when you call a Python script. It sometimes even leaves the machine code laying in your filesystem, with the extension .pyc . This is the byte code (aka machine code) for CPython’s implementation of the Python Virtual Machine (PVM).
This is incorrect; the term “machine code” refers to code that can be run on a real machine, not to code that requires a virtual machine.
You might want to browse through this: https://wiki.osdev.org/Creating_an_Operating_System
Which should also help explain why doing the whole thing in python isn’t feasible
As it happens, this is strikingly similar to an interview question I sometimes ask: what parts of a multitasking OS cannot be written wholly in C. As one might expect, the question is intentionally open-ended so as to query a candidate’s understanding of the capabilities and limitations of the C language. Your question asks about Python, but I posit that some OS requirement which a low-level language like C cannot accomplish would be equally intractable for Python.
Cutting straight to the chase, C is insufficient for initializing the stack pointer. Sure, C itself might not technically require a working stack, but a multitasking operating system written in C must have a stack by the time it starts running user code. So most will do that initialization much earlier, so that the OS’s startup functions can utilize the stack.
Thjs is normally done by the bootloader code, which is typically written in assembly and runs when the CPU is taken out of reset, and then will jump into the OS’s C code. The C functions will allocate local variables on the stack, and everything will work just fine, even rewriting the stack pointer using intrinsics to cause a context switch (although this code is often – but not always – written in assembly too).
The crux of the issue is that the initial value of the stack pointer cannot be set using C code. Some hardware like the Cortex M0 family will initialize the stack pointer register by copying the value from 0x00 in program memory, but that doesn’t change the fact that C cannot set the stack pointer on its own, because invoking a C function may require a working stack in the first place.
In Python, I think it would be much the same: how could Python itself initialize the stack pointer necessary to start running Python code? You would need a hardware mechanism like with the Cortex M0 to overcome this same problem.
The reason the Cortex M0 added that feature is precisely to enable developers to never be forced to write assembly for that architecture. They can if they want to, but the architecture was designed to be developed with C exclusively, including interrupt handlers.
If you have hardware that natively executes Python bytecode, then your OS could work. But for x86 platforms or most other targets, I don’t think an all-Python, no-assembly OS is possible.
The essence of your answers is “yes, but…”. And the “but” is mostly about how slow Python is in contexts that need to be astonishingly fast.
It depends how complex the hardware is and how much time we’re willing to waste.
Technically, when I deploy a Python program to a BBC Microbit, that’s (more or less) what is happening. Pure Python code is making every decision, and is interacting directly with all available hardware.
We could still argue semantics - virtually no (modern) computer exists that isn’t running at least one tiny binary compatibility driver written in C.
I believe the compiled C binary on a BBC Microbit to bootstrap a pure Python OS is incredibly small, but my best guess is that it’s still present. The C library for Microbit needed to exist for other languages to use, and Python likes calling C binaries. So I don’t imagine anyone has recreated it in pure Python for fun (and slower results).
(Edit: As others have pointed out, I’m talking about MicroPython, which is, itself written in C. The Microbit is so simple it might not use MicroPython, but I can’t imagine the BBC Microbit team bothered to reinvent the wheel for this.)
Of course, if you don’t mind that the lowest level code has got to be binary, and very few people are crazy enough to create that code with Python, then…
It begs another interesting question: Just how much of an OS can we get away with writing in Python.
And that question is answered both by RedHat Linux and Debian Linux - and the answer is that both are built with an awful lot of Python.
In contrast, Android is mostly Java with
lots of Ca C Linux kernel. Windows is mostly C# and lots of C. iOS is mostly Objective C and lots of C.You can have an OS built with almost any language you want, as long as you also want parts of it built in C. (Edit: This is meant to amuse you, not be guidance for what is possible. Today, we love our C code. C didn’t always exist, and might someday no longer be our favorite hardware driving language.)
An interesting current development is discussion around rebuilding parts of the Linux Kernel with Rust, which can run just as fast as C. This would effectively cause RedHat, Debian and Android to replace some of their C code with Rust. To date, there’s been a lot of interest and discussion and not a lot of (any?) actual funding or work completed.
While I get your point that Python is often not the most appropriate language to write certain parts of an OS, I have to object to the supposed necessity of C. In particular, the bolded claim that an OS not written in C is still going to have C involved.
Such an OS could instead have written its non-native parts using assembly. And while C was intentionally designed to be similar to assembly, it is not synonymous with assembly. OS authors can and do write assembly when even the C language cannot do what they need, and I gave an example of this in my comment.
The primacy of C is not universal, and has a strong dependency on the CPU architecture. Indeed, there’s a history of building machines which are intended for a specific high-level language, with Lisp Machines being one of the most complex – since Lisp still has to be compiled down to some sort of hardware instructions. A modern example would be Java, which defines the programming language as well as the ISA and byte code: embedded Java processors were built, and thus there would have been zero need for C apart from legacy convenience.