Reimplenting Linux Kernel in Python

M

Maboroshi

Hello

My question has to do with python and linux - I was interested in
finding out what it would take to reimplement the Linux Kernel in python
basically just taking the source code from linux and rewriting it in
python Would this idea make sense to do - if so what would be the
benefits of doing this and in what way would this not be a good idea

Cheers
 
P

Peter L Hansen

Maboroshi said:
My question has to do with python and linux - I was interested in
finding out what it would take to reimplement the Linux Kernel in python
basically just taking the source code from linux and rewriting it in
python Would this idea make sense to do - if so what would be the
benefits of doing this and in what way would this not be a good idea

Um, you're the one suggesting a re-write. Doesn't that mean
it's sort of your job to know the benefits of doing it? There
are certainly endless reasons why it's not a good idea...

-Peter
 
M

Maboroshi

Peter said:
Um, you're the one suggesting a re-write. Doesn't that mean
it's sort of your job to know the benefits of doing it? There
are certainly endless reasons why it's not a good idea...

-Peter

Um sorry I should say in your opinion what would be the benefits and
also what would be the disbenefits
> Um, you're the one suggesting a re-write. Doesn't that mean
> it's sort of your job to know the benefits of doing it?

I understand some of the benefits but not all

Cheers
 
P

Peter L Hansen

Maboroshi said:
Um sorry I should say in your opinion what would be the benefits and
also what would be the disbenefits


I understand some of the benefits but not all

Let me be more explicit then. I think this is an idea that
is based on very little understanding of what is involved,
to the point that I think it would be pointless to discuss
it without knowing what are the things *you* perceive as
being benefits, and what disadvantages or potential road-blocks
*you* have already thought of.

-Peter
 
C

Carlos Ribeiro

Hello

My question has to do with python and linux - I was interested in
finding out what it would take to reimplement the Linux Kernel in python
basically just taking the source code from linux and rewriting it in
python Would this idea make sense to do - if so what would be the
benefits of doing this and in what way would this not be a good idea

To put it simply, it does not make sense, in its pure form. Linux is
written in C for a lot of good reasons: portability, need to access
the hardware, and performance comes to mind. Besides that, Linux is
Posix compliant and emulates Unix system calls to a large extend, and
that also makes C a natural choice. To do a similar implementation in
Python would be similar to try to use a knife to sculpt a marble piece
-- it's the wrong tool for the job.

Now, let me re-state your question in a broader sense. Is it possible
to write an OS using a language such as Python? Yes. But the resulting
OS will surely be different from Linux -- by design it would be a
completely different beast. Some parts would still be written in C and
assembler, because it needs to access the hardware, and also for
performance reasons. But an API could be devised to allow higher-level
functions to be written in Python, while calling the low-level
C+assembler stuff. But as I said, it's _so_ completely different from
anything else in use today as to make it pretty much a theorethical or
academic exercise with little practical use. I can't see a project
like that taking off, even if I would love to see it working.

--
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: (e-mail address removed)
mail: (e-mail address removed)
 
G

Grant Edwards

To put it simply, it does not make sense, in its pure form.
Linux is written in C for a lot of good reasons: portability,
need to access the hardware, and performance comes to mind.
Definitely.

Besides that, Linux is Posix compliant and emulates Unix
system calls to a large extend, and that also makes C a
natural choice.

Not sure I follow on that point. Any low level language that
can deal with a C "struct" and byte-addressible memory would be
fine.
 
P

Phil Frost

...

Now, let me re-state your question in a broader sense. Is it possible
to write an OS using a language such as Python? Yes. But the resulting
OS will surely be different from Linux -- by design it would be a
completely different beast. Some parts would still be written in C and
assembler, because it needs to access the hardware, and also for
performance reasons. But an API could be devised to allow higher-level
functions to be written in Python, while calling the low-level
C+assembler stuff. But as I said, it's _so_ completely different from
anything else in use today as to make it pretty much a theorethical or
academic exercise with little practical use. I can't see a project
like that taking off, even if I would love to see it working.

I can state from first-hand experience that this idea is not too
far-fetched. It is possible to enable Python to express device drivers
and low level OS functions like multitasking with an amazingly small
amount of C+asm; I'd say around 2000 lines, excluding the Python
runtime. If Pyrex counts as not-C, then I bet it could be done in 500.

The performance problems are a bit harder, but with a good JIT compiling
VM the runtime performance of Python could theoretically come very close
to C.

I do agree though that there is little point in rewriting Linux in
Python. The advantage in Python is its expressiveness, but by rewriting
an existing system you gain nothing.
 
C

Carlos Ribeiro

Besides that, Linux is Posix compliant and emulates Unix
Not sure I follow on that point. Any low level language that
can deal with a C "struct" and byte-addressible memory would be
fine.

Well, Unix itself was written in C, and a good part of the C standard
library maps naturally to (or at least helps to implement) functions
that are mandatory in a Posix-compliant OS. This does not meant that
other languages cant be used; it only means that C is a natural
choice.

Of course, you can provide a C-like interface for a Python program
(for example). But a OS written in pure Python would make a much
better use of the language if it provides a Pythonic interface. I
believe we can agree on this point, dont you think?

--
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: (e-mail address removed)
mail: (e-mail address removed)
 
F

Francis Lavoie

Phil said:
I can state from first-hand experience that this idea is not too
far-fetched. It is possible to enable Python to express device drivers
and low level OS functions like multitasking with an amazingly small
amount of C+asm; I'd say around 2000 lines, excluding the Python
runtime. If Pyrex counts as not-C, then I bet it could be done in 500.

The performance problems are a bit harder, but with a good JIT compiling
VM the runtime performance of Python could theoretically come very close
to C.

I do agree though that there is little point in rewriting Linux in
Python. The advantage in Python is its expressiveness, but by rewriting
an existing system you gain nothing.
unununium, except is name, look very amazing ... but we still have to
wait to see a product.
 
G

Grant Edwards

Well, Unix itself was written in C,

Originally it wasn't, but recent versions are.
and a good part of the C standard library maps naturally to
(or at least helps to implement) functions that are mandatory
in a Posix-compliant OS.

True, but that has nothing to do with writing a kernel. When
working in kernel space, you don't have access to Posix
services or the standard C library. Since you can't use the
standard C library when writing a kernel, it doesn't matter
what language it was designed for. Likewise for any of the
Posix standards that specify the API between an application and
the system libraries.
This does not meant that other languages cant be used; it only
means that C is a natural choice.

I still don't see why.
Of course, you can provide a C-like interface for a Python program
(for example). But a OS written in pure Python would make a much
better use of the language if it provides a Pythonic interface. I
believe we can agree on this point, dont you think?

Not really -- the interace between the kernel and user-space
isn't particulary C-oriented. IIRC on the x86 platform, you
make a system call by loading values into registers and
executing an interrupt instruction. You can't even make a
Linux system call on Linux using standard C. You have to use
assembly language wrappers that convert between the system call
API and the C API. It would be just as easy to make those
assembly language wrappers convert to a Pascal or Python API.
 
D

Dennis Lee Bieber

Hello

My question has to do with python and linux - I was interested in
finding out what it would take to reimplement the Linux Kernel in python
basically just taking the source code from linux and rewriting it in
python Would this idea make sense to do - if so what would be the
benefits of doing this and in what way would this not be a good idea

You'd have to name the result Ouroborous...

After all, common Python relies on the C-library making OS level
I/O calls... (and the C-library may be relying on assembly modules yet).


If it were possible, it would run VERY slowly; you'd be using a
byte-code interpreter to perform what is normally highly optimized
native code.

--
 
N

Neal D. Becker

Maboroshi said:
Hello

My question has to do with python and linux - I was interested in
finding out what it would take to reimplement the Linux Kernel in python
basically just taking the source code from linux and rewriting it in
python Would this idea make sense to do - if so what would be the
benefits of doing this and in what way would this not be a good idea

Cheers

Yeah, that's a great idea. Go for it. Let me know when you're done.
 
M

Michael Hobbs

Maboroshi said:
My question has to do with python and linux - I was interested in
finding out what it would take to reimplement the Linux Kernel in python
basically just taking the source code from linux and rewriting it in
python Would this idea make sense to do - if so what would be the
benefits of doing this and in what way would this not be a good idea

I had once given serious thought to what it would take to write an OS only
in a high-level language. (Standard ML was my language of choice at the
time.) The short answer is, no, it can't be done with Python as it currently
stands.

First, you would need a Python compiler that can compile Python programs to
machine code. Otherwise, what would you write your interpreter in? Python?
What would you write the interpreter's interpreter in? It can't be Python
all the way down. (Turtles are a different story, though. ;)

Second, in order to communicate with hardware, you would need modifications
to the Python language so that you can read and write bytes to specific
memory locations. You would also need to be able to signal interrupts. Plus,
you would also need to be able get the exact memory location of functions,
in order register interrupt handlers.


-- Michael Hobbs
 
G

Grant Edwards

'Recent' meaning since 1974? ;)

Yup. I admit that was sub-optimal phrasing. I should have
said something more like "The first version wasn't but the rest
were."
 
G

Grant Edwards

I had once given serious thought to what it would take to write an OS only
in a high-level language. (Standard ML was my language of choice at the
time.) The short answer is, no, it can't be done with Python as it currently
stands.

First, you would need a Python compiler that can compile Python programs to
machine code. Otherwise, what would you write your interpreter in? Python?

Silicon. ;)
 
C

Carlos Ribeiro

I still don't see why.


Not really -- the interace between the kernel and user-space
isn't particulary C-oriented. IIRC on the x86 platform, you
make a system call by loading values into registers and
executing an interrupt instruction. You can't even make a
Linux system call on Linux using standard C. You have to use
assembly language wrappers that convert between the system call
API and the C API. It would be just as easy to make those
assembly language wrappers convert to a Pascal or Python API.

Now I see your point. It remembers me a little bit about the
discussions between GNU/Stallman and Linux/Linus on what makes a OS.
The kernel is one thing, the OS is another beast. I still think that a
language such as Python can be used to write the OS -- in the sense
where the name Linux is usually called upon (even if technically
incorrect, as the GNU/Stallman camp would quickly point out); it
includes high level APIs, much above the low-level kernel calls . But
the kernel itself is the hard part for a high level language.

As for C being a natural choice as far as the kernel part is
concerned... it may be because it compiles to efficient code, or
because it facilitates integration with assembler, or because it can
efficiently uses registers... Who knows? But the main reason is
probably simple: it's because everyone is just used to it.

--
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: (e-mail address removed)
mail: (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top