can python do some kernel stuff?
Ivan Illarionov
ivan.illarionov at gmail.com
Wed Jun 4 12:06:42 EDT 2008
On Wed, 04 Jun 2008 09:41:07 -0500, Grant Edwards wrote:
>>> The answer is yes. IPC and py-pf are examples. If you don't think of
>>> packet filtering as kernel coding, I can understand. But clearly the
>>> Python interfaces to fork(), waitpid(), signal(), alarm() and so forth
>>> are forays into the once private garden of C.
>
> The routines listed above aren't in the kernel. They're in libc just
> like routines such as printf, memcpy, etc. The above libc routines do
> make system calls into the kernel to perform the desired functions, but
> those routines are not in the kernel, and calling them is certainly not
> "kernel coding".
>
> Yes, Python provides specific wrappers for many C library functions.
>
> Yes, the ctypes module provides a generic method for calling foreign
> library functions.
>
> No, using those wrappers is not what anybody I know would call "kernel
> coding".
>
>> Being able to call routines in the kernel is *not* the same as kernel
>> coding.
>
> As far as I know, Python doesn't provide the user with the ability to
> make system calls into the kernel. Even if it did, that isn't really
> "kernel coding" either. It's just making system calls.
>
>> Calling C routines is *not* the same as kernel coding. Actually writing
>> the routines that are to be called, and that constitute the kernel
>> itself, *is* kernel coding. And as wonderful as Python is, it is *not*
>> for kernel coding.
>>
>> Having just looked at Py-PF, it is *managing* the firewall, not
>> implementing it. Again, not kernel coding.
>
> Didn't somebody once demonstrate how to put a VM into kernel space so
> that you could write kernel code in Python? Maybe it was just a
> discussion about how it could be done in theory.
>
> There have been a few JVM-in-hardware projects, so I suppose you could
> use Jython to write kernel code for those machines.
I can't understand why somebody might want to do kernel stuff in Python.
It's a *high level language*. It's for high level stuff.
OTOH Python can be easily extended to get as close to the kernel as
anyone may ever need.
I decided to change my "hello world" post to use Linux system call
instead of printf.
#include <Python.h>
PyObject*
hello(PyObject* self)
{
#if defined(__GNUC__) && defined(__i386__) && defined(__linux__)
const char * hello_str = "Hello world (Linux system call)!\n";
int hello_len = 33;
asm __volatile__(
"push %%ebx;"
"movl $4, %%eax;" /* The system call for write (sys_write) */
"movl $1, %%ebx;" /* File descriptor 1 - standard output */
"int $0x80;"
"pop %%ebx;"
: /* no outputs */
: "c" (hello_str), "d" (hello_len) /* input */
);
#else
printf("Hello world!\n");
#endif
Py_RETURN_NONE;
}
static PyMethodDef functions[] = {
{"hello", (PyCFunction)hello, METH_NOARGS},
{NULL, NULL, 0, NULL},
};
DL_EXPORT(void)
init_hello(void)
{
Py_InitModule("_hello", functions);
}
Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from _hello import hello
>>> hello()
Hello world (Linux system call)!
I'm in the mood to write "Hello world" programs today ;)
Ivan
More information about the Python-list
mailing list