C++ or pure C for Linux kernel module, Linux device driver development. What to use?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Again - I think most of what you're saying is absurd. At least the parts that aren't simply false...
IMHO .. PSM
Any particular point that you can refute? I thought not. Responding to you is like talking to a wall. And just as much of a waste of time. Go examine the machine code generated from a C and a C++ compiler for the same program. (You can read assembler, can't you? ;-)
BTW, I attended a talk given by Brian Kernighan in which he compared popular programming languages for clarity of expression. It was his opinion that C++ was the clear winner. (You do know who BK is, don't you? ;-)
I've gone back over your posts and have confirmed that they are totally information-free. Case closed.
Last edited by lmenten; 09-03-2008 at 07:52 PM.
Reason: Clarification
BTW, I attended a talk given by Brian Kernighan in which he compared popular programming languages for clarity of expression. It was his opinion that C++ was the clear winner. (You do know who BK is, don't you? ;-)
C++ code is exactly the same size as the equivalent C code. If you would not otherwise use an object-oriented paradigm, you are free to use straight procedural code with exactly the same footprint but with the advantage of much more compile-time checking. Since the Linux kernel is huge and in a single shared memory space, more compile-time checking is badly needed. Microsoft's Singularity goes even further by requiring managed code throughout to achieve reliability in a monolithic kernel.
Apple's device driver framework (I/O Kit) uses inheritance and wrappers (C++) to achieve more reliable driver code with little or no run-time overhead. The key is an expressive language that enables more complete compile-time checking. In an OS, the most error-prone code are the device drivers. This is why NeXT and OS X have enhanced the reliability of kernel-resident device drivers through a Objective-C framework and C++-based framework, respectively.
C++ is the preeminent language for embedded systems programming and the most stable and secure OS kernels are written in C++. The Linux kernel is, unfortunately, not numbered among the most stable and secure kernels. C++ was designed to enable clearer expression of the programmer's intent while retaining the efficiency of assembler language and C. C++ does not rely upon libraries any more than C does and can leverage any APIs created for C. You must be thinking of Java ; )
I am not a C++ fan, but I agree with you.
Furthermore, C++ can automatically be translated into "C" - see, for example, llvm.org - also supported by Apple.
And, anyway, whenever possible, write a user-space driver.
Another thing on C++ - if classes with data members are used instead of plain variables, access to the said data members is through 'this', i.e. through yet another pointer - some performance degradation.
But that would be true for "C" too whenever a struct is allocated and access is through pointer.
Another thing on C++ - if classes with data members are used instead of plain variables, access to the said data members is through 'this', i.e. through yet another pointer - some performance degradation.
But that would be true for "C" too whenever a struct is allocated and access is through pointer.
I agree. In fact in C++ a struct is a simple kind of class and access to a data member in C++ class results in the same sequence of machine instructions as access to a data member in a C struct (as compiled by a C compiler.) Just as access to a member of a C struct will (often) require access through a pointer, access to a member of a C++ class will(often) require access through a pointer. (If the class is a stack variable, access will be through an offset just as for a struct.)
When programming in any language, one must be aware of the cost/complexity of the expressions, data structures, and algorithms one uses. I haven't met any kernel developer, programming in C or C++, who is unaware of issues of efficiency.
If you are serious about embedded/kernel programming, first learn how to use compiler in a manner it does _not_ link standard runtime.
Your example in post #13 does not satisfy the above requirement because the example is a typical C/C++ main program _with_ standard runtime.
Benchmark a function without standard runtime and see its assembly code - that would be the correct study.
I totally agree. 12 is an unsubstantiated remark, and 13, 22, and 24 are based on a bogus example and have been already well rebutted. As Sergei says, the generated assembler/machine code is a more objective way to compare the languages for efficiency.
I totally agree. 12 is an unsubstantiated remark, and 13, 22, and 24 are based on a bogus example and have been already well rebutted. As Sergei says, the generated assembler/machine code is a more objective way to compare the languages for efficiency.
Only if such code is intended to run a CPU directly, i.e. maintain an idle loop, manage processes, handle interrupts, control hardware, map memory, etc. You can't make a user-space comparison valid for kernel-space no matter how you spin it, even if you write a mock kernel for a virtual machine to be run in user-space.
ta0kira
Only if such code is intended to run a CPU directly, i.e. maintain an idle loop, manage processes, handle interrupts, control hardware, map memory, etc. You can't make a user-space comparison valid for kernel-space no matter how you spin it, even if you write a mock kernel for a virtual machine to be run in user-space.
ta0kira
Once the C++ code is compiled it is essentially indistinguishable from the equivalent code compiled from C. Except that the C++ code will take better advantage of inline optimizations and elimination of redundant pointer validation and so will be faster than the C version.
What I was really commenting on was the intention of the code. Both languages are better in some areas, so comparing code intended for user-space might be a disproportionate comparison. A better comparison would be a basic kernel written in C++ (C++ style) vs. one written in C. If you can't write a functioning kernel that's mostly C-less C++ then you'll just have a C++-enhanced C kernel.
ta0kira
Once the C++ code is compiled it is essentially indistinguishable from the equivalent code compiled from C. Except that the C++ code will take better advantage of inline optimizations and elimination of redundant pointer validation and so will be faster than the C version.
I am not 100% sure, but I think that support of "inline" in a sense of really transforming the code tagged "inline" into inline code, is not mandatory.
What I was really commenting on was the intention of the code. Both languages are better in some areas, so comparing code intended for user-space might be a disproportionate comparison. A better comparison would be a basic kernel written in C++ (C++ style) vs. one written in C. If you can't write a functioning kernel that's mostly C-less C++ then you'll just have a C++-enhanced C kernel.
ta0kira
You've entered a very grey area, since it's difficult to well define "style".
Typical C++ programs create class instances through "new" which is very similar to "malloc".
Neither "new" nor "malloc" memory allocation mechanism is suitable for kernel.
I think the point of using C++ in kernel is its template programming features, for example. I mean, C++ has both CPP and templates while "C" has only CPP.
But, as a Perl guy, I'd say C++ templates are far behind Perl in their features :-).
You've entered a very grey area, since it's difficult to well define "style".
Typical C++ programs create class instances through "new" which is very similar to "malloc".
Neither "new" nor "malloc" memory allocation mechanism is suitable for kernel.
I think the point of using C++ in kernel is its template programming features, for example. I mean, C++ has both CPP and templates while "C" has only CPP.
But, as a Perl guy, I'd say C++ templates are far behind Perl in their features :-).
When programming in the kernel one overrides the new operator to use kmalloc, kfree, or whatever the particular OS provides in the kernel environment. This is all done inline with no additional overhead.
Templates are a nice option but strong type checking, inheritance, polymorphism, references, and other fundamental features of C++ provide great advantages in creating highly reliable software.
There are a number of excellent features coming up supporting such concepts as generic programming. These won't appear in the GNU compilers until they have been adopted as part of the next C++ standard.
Perl has the advantages that the creators can play fast-and loose with the language definition and that the language is interpreted. Great for user-space, but not safe (in most current OS implementations) for kernel space.
My point about inlining is this: In C++ the language expresses modularity well and provides features (private, protected) in the language to express and enforce access restrictions. This is highly desirable in a very large, single address-space monolithic kernel like the Linux kernel. These language features work very well with inlining in supporting information hiding/protection without any loss of efficiency in the code.
For example, the only way to prevent access to private functions and data structures in C is to declare them (or pointers to them) "static" requiring that any functions needing to access these static functions or variables be present in the same C file. This foils modularity and adds the cost of a function call for access.
In contrast, C++ allows the developer to limit visibility/access while still providing total efficiency for access to the protected functions and data structures. Where C requires a function call for access to a protected data structure, C++ does not and is, consequently, very much much faster.
...
Perl has the advantages that the creators can play fast-and loose with the language definition and that the language is interpreted. Great for user-space, but not safe (in most current OS implementations) for kernel space.
...
I think you missed my point regarding Perl.
I meant I use Perl to generate C/C++ code, i.e. I use it as a better than CPP/C++ templates engine.
And, FWIW, Perl is compiled into bytecode (as Java, Python, Ruby, but _not_ as sh, tcl), so it's not an interpreted language.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.