Is an Operating System Kernel an Interpeter for All Other Programs

Is an operating system kernel an interpeter for all other programs?

They run on the "bare metal", but they do contain operating system-specific things. An executable file will typically provide some instructions to the kernel (which are, arguably, "interpreted") as to how the program should be loaded into memory, and the file's code will provide ways for it to "hook" in to the running operating system, such as by an operating system's API or via device drivers. Once such a non-interpreted program is loaded into memory, it runs on the bare metal but continues to communicate with the operating system, which is also running on the bare metal.

In the days of single-process operating systems, it was common for executables to essentially "seize" control of the entire computer and communicate with hardware directly. Computers like the Apple ][ and the Commodore 64 work like that. In a modern multitasking operating system like Windows or Linux, applications and the operating system share use of the CPU via a complex multitasking arrangement, and applications access the hardware via a set of abstractions built in to the operating system's API and its device drivers. Take a course in Operating System design if you are interested in learning lots of details.

Bouncing off Junaid's answer, the way that the kernel blocks a program from doing something "funny" is by controlling the allocation and usage of memory. The kernel requires that memory be requested and accessed through it via its API, and thus protects the computer from "unauthorized" access. In the days of single-process operating systems, applications had much more freedom to access memory and other things directly, without involving the operating system. An application running on an old Apple ][ can read to or write to any address in RAM that it wants to on the entire computer.

One of the reasons why a compiled application won't just "run" on another operating system is that these "hooks" are different for different operating systems. For example, an application that knows how to request the allocation of RAM from Windows might not have any idea how to request it from Linux or the Mac OS. As Disk Crasher mentioned, these low level access instructions are inserted by the compiler.

Do interpreted languages need an operating system to work?

Of course you can write an interpreter that runs on bare-metal, it is just that if the platform does not have an OS any run-time support the language needs must be part of the interpreter. To the extent in some cases that such an interpreter might essentially be an OS. That is if it provides the services to operate a system, it could be called an operating system.

It is not perhaps as simple as interpreted vs compiled. Java for example runs on a virtual machine and is "compiled" to bytecode. The bytecode is interpreted (or just-in-time compiled in some cases), rather then the Java source directly. In an embedded system, it is possible that you would deploy cross-compiled bytecode on the target rather then the source. Certainly however JVMs exist for bare-metal. Some support multi-threading through a third party RTOS, others either have that support built-in or do not support threading at all.

There are interpreters for cut-down subsets of JavaScript and Python that run on bare-metal microcontrollers. I am not sure about full implementations, but it is technically possible given sufficient run-time support even if not explicitly implemented. To fully support some of these languages along with all the standard and third-party libraries and frameworks a developer might expect, may require so much run-time support and resource that it is simpler to deploy and OS, so implementations for resource constrained systems are often subsets or have restricted libraries.

Write OS in interpreted language

Microsoft Research has created an OS, called Singularity.

It IS a research project though, and I think they needed some low level code to initiate the boot process (at some point an OS needs to talk to the hardware).

Wikipedia says:

The lowest-level x86 interrupt dispatch code is written in assembly language and C. Once this code has done its job, it invokes the kernel, whose runtime and garbage collector are written in Sing# (an extended version of Spec#, itself an extension of C#) and runs in unprotected mode. The hardware abstraction layer is written in C++ and runs in protected mode. There is also some C code to handle debugging. The computer's BIOS is invoked during the 16-bit real mode bootstrap stage; once in 32-bit mode, Singularity never invokes the BIOS again, but invokes device drivers written in Sing#. During installation, Common Intermediate Language (CIL) opcodes are compiled into x86 opcodes using the Bartok compiler.

role of command interpreter in operating systems

Traditionally, at least, the phrase "command interpreter" means only command-line interpreters such as cmd.exe and if you interpret the phrase that way, the claim is false. When the interface is graphical, such as in Windows, we usually call it a "shell" instead.

The Windows shell is called Explorer, explorer.exe in Task Manager.

So clicking on the GUI elements equals writing a command in cmd.exe?

They are equivalent, in the sense that they both serve broadly the same function (allowing the user to interface with the operating system) but not identical.

In particular, note that Explorer doesn't work by passing commands to cmd.exe or vice-versa.

Which programming languages can be used to make an operating system?

The lowest level of an operating system is the kernel, it runs on the CPU, so the languages used can't depend on virtual machines or interpreters. Compilers create machine code from the language, and generally package the output in modules with well defined formats. Those modules can be used to create libraries, applications, or an operating system kernel. You need a language that lets you specify the module contents with a fair amount of control, a language like C is fairly easy, a language like C++ makes it much harder, so is not used for the low level of an operating system.

At the lowest level, you need complete control over the output, because it has to match the hardware, not a module format, so you use assembly language for that.

Above the kernel, there's a lot of stuff that uses higher level interfaces, so doesn't need to be a specific binary module, and can use an interpreter or virtual machine. Those levels can be in Java like Android is.

The original MacOS was written in a version of Pascal. Some IBM mainframe OSes used PL/1. Those are no longer popular, but both compiled into modules like C does.

Can a whole operating system be written without using even one line of C/C++ code?

Yes and no.

First of all, it's important to remember that whatever your language of choices, in the end the compiled product is in assembly language (or more accurately, machine code). Even interpreters (such as the cpython interpreter) are ultimately translating your scripts into machine code.

But that's probably being overly technical and missing the heart of your question:

"Can I write an operating system in a higher-level language?"

The answers to this are both personal and technical.

First, the personal side: if don't already know how to write an operating system in a mix of assembly language and C then you have absolutely no business trying your hand at OS design.

Often those new to programming have these sorts of questions because they want to do something as cool as writing a new OS without all the learning required to even attempt such a project. They wonder if higher-level languages can be a way to bypass all that messy study.

So if, in your heart-of-hearts, this is what you're after, stop now. Stop, stop, stop. Becoming good at something is hard work. There are no shortcuts. Be ready to roll up your sleeves and get some carpal tunnel syndrome.

That doesn't preclude following a path to eventual OS design! If that's your passion, then start at the top and work your way down. Get books on networking protocols, memory management, threading, etc, tackle each major subsystem and get to know it well. You can't write a new one if you can't use an old one!

Then read books on operating system design and implementation until you dream about process management methodologies.

Just bear in mind, the amount of knowledge necessary (not just of computer operations but of social constructs like APIs) is immense. This is a long journey and a rewarding one. If you truly love this craft like I do, you'll be glad you took the time even if you never actually write an OS.

Now, the technical answer. You're going to need a bootloader, and that must be written in assembly language. After all, your processor doesn't know C#. Past the bootloader phase, you can write your OS code in any language you want and it'll run, assuming your language can compile to machine code binaries (and not bytecode!)

However, even in our current "glut of cycles" computing environment, an OS must be lean and efficient and that's nearly impossible to achieve in a higher level language, even more so in an interpreted language.

Chances are, you'll need to write your own compiler/interpreter of that given language as a core component of your OS. That core compiler would likely allow only a restricted subset of the language (and you'd bootstrap by writing a more robust compiler in the restricted sub-language). Otherwise the performance will be abysmal.

But all of this is horribly complex and a real discussion of the issues requires a depth of knowledge you probably currently lack. But if you have the drive to do so, you can easily change that, and then I'd happily debate approaches all day!

If it makes you feel any better, I do know enough to write an operating system, and still I sit around daydreaming, trying to figure out how much of an OS could I get away with writing in python. ;)

Memory management in Operating System

Where does the MMU fit what does it do since the address translation and page-in / page out is done by CPU and OS?

The MMU is part of a modern CPU. The virtual to physical address translation is done by the MMU / CPU hardware.

The Operating System kernel (software) is responsible for making sure that that the MMU is correctly configured (so that the correct pages are mapped into user-space processes address spaces), and for handling the page fault interrupts generated by the MMU. It also deals with the higher level functions such as:

  • swapping pages in and out,
  • keeping track of which pages are clean,
  • ensuring that there is a pool of clean pages that can be evicted if there is a page fault,
  • dealing with scheduling issues such as killing processes that are causing thrashing
  • and so on.

How is the Virtual address space assign in interpreted lang?

It is the same as for a non-interpreted programming language. In the interpreted case, the interpreter is a user-space (non-privileged) program. The code that it is interpreting is ... data for the interpreter.

Related Topics

Leave a reply