LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   why we can't use whichever library... (https://www.linuxquestions.org/questions/programming-9/why-we-cant-use-whichever-library-190467/)

os2 06-06-2004 07:03 PM

why we can't use whichever library...
 
hi

why we can't use whichever library when we code a linux driver?

we can use strcpy but we can't use printf

whey?

thanks

paulsm4 06-06-2004 07:23 PM

Try printk?
 
Because kernel drivers run in a completely different execution context than user code.

One alternative is to use "printk()" instead of "printf()".

I'd definitely consider getting a good book. For example, "Linux Device Drivers, 2nd Ed", A. Rubini & J. Corbet.

Hope that helps .. PSM

os2 06-06-2004 07:34 PM

Re: Try printk?
 
Quote:

Originally posted by paulsm4
Because kernel drivers run in a completely different execution context than user code.

One alternative is to use "printk()" instead of "printf()".

I'd definitely consider getting a good book. For example, "Linux Device Drivers, 2nd Ed", A. Rubini & J. Corbet.

Hope that helps .. PSM

i have read this book

i don't find your answer very clear and complete

paulsm4 06-06-2004 08:55 PM

The standard C library in general, and printf() in particular, require considerable runtime support - runtime support you more or less take for granted in the user environment - and that simply doesn't exist in kernel mode.

If you have the Rubini/Corbet text, please re-read Chap 2, especially the section "Doing It in User Space". Bear in mind that even a simple "malloc()" (which the "printf()" implementation makes liberal use of!) cannot be done in kernel mode (although there is, to be sure, an anologous "kmalloc()").

Please let me know specifically what you're unclear about - I'd be happy to help clarify.

Your .. PSM

Hko 06-07-2004 11:57 AM

Re: why we can't use whichever library...
 
Quote:

why we can't use whichever library when we code a linux driver?
we can use strcpy but we can't use printf[/B]
A quote from Robert Love's book: "Linux Kernel Development" (Sams publishing):
[quote]Unlike a user-space application, the kernel is not linked against the standard C library (or any other library, for that matter). There are multiple reasons for this, including some chicken and egg situations, but the primary reason is speed and size.[quote]

paulsm4 06-07-2004 11:43 PM

Hi, Hko -

Your book is correct that the kernel is not linked against the standard C library ... and, in fact, *cannot* be linked against it.

I'm not sure I completely agree with the "speed and size" arguments (although obviously both are an important consideration in *any* code - especially in systems code).

The "chicken and egg" argument is absolutely correct: the kernel is there to provide exactly that infrastructure that the standard library needs (like the ability to "malloc()" paged memory, for example). It cannot easily provide that infrastructure and at the same time use it.

But, IMHO, the main point is that kernel code (in the executive context) and non-kernel code (in user context) are simply two very different, in many ways incompatible environments.

Another good (albeit totally non-Linux ;-)) book that os2 might find useful is:
"Inside Windows 2000", Solomon/Russinovich, Chap 1, pp 9-13, "Kernel mode vs. user mode".

Hope that helps .. PSM

os2 06-09-2004 03:35 PM

thank


All times are GMT -5. The time now is 03:57 AM.