LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-21-2005, 08:03 PM   #16
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled

Hi, Igor007 -

Bottom line: if you want to write Linux kernel drivers, you really need to do it in 'C'.

Whether or not you *can* write a kernel driver in C++: I don't know. I doubt it. Whether or not you *should* write a kernel driver in C++: no, you shouldn't.

And if you want a good example of an OS written largely in C++: look at Windows 2000

Need I say more ;-)?
 
Old 08-27-2005, 08:33 PM   #17
jstephens84
Senior Member
 
Registered: Sep 2004
Distribution: (Home)Opensolaris, Ubuntu, CentOS, (Work - AIX, HP-UX, Red Hat)
Posts: 2,056

Rep: Reputation: 94
Matir I think your program proves a big point. While the program may be small take to let say commercial size. I wonder what the results would be. You and your small app has helped me decide that I will study C instead of c++. Thank You.
 
Old 08-27-2005, 08:56 PM   #18
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
No problem. I personally prefer C, but tried to make my pseudo-example as unbiased as possible. I would love for someone to provide a counter-example, even if it is NOT on runtime. Perhaps a counter-example of code readability, or stability. I don't think they'll find one, but I'm all for seeing one if there is one. We can all learn things.
 
Old 08-28-2005, 09:24 AM   #19
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,772

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
All kernel code is written in C -there are no c++ inluces in the kernel tree.
 
Old 08-28-2005, 01:30 PM   #20
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
In answer to the first question:

Q: "Can I write a kernel driver in C++?"),

the answer is :

A: Perhaps you *can* (if you're clever and/or determined enough), but you very definitely *shouldn't*.

In answer to your second (very different, completely independent!) question:

Q: "Can a C++ compiled binary be as efficient as it's C equivalent?"

A: "Quite possibly. But that's completely missing the point."

The question of "C" vs. "C++" (or, for that matter, C vs Perl or Visual Basic or anything else imaginable) is really "what's the best tool for the project"???

There is seldom any "right" answer, and how gosh darn small or fast the resulting binary compiles to is usually the *least* of your concerns. How closely the language maps to the problem domain is usually much more important. How robust and maintainable you can design the implementation is usually *vastly* more important. And, frankly, that's an area where C++ shines.

So no, C++ is not an appropriate choice for a kernel driver.

And yes, C++ might well be an appropriate choice for any library (e.g. programmer's API) or application (e.g. configuration program) that makes *use* of your kernel driver.

Kernel drivers are a "system thing", applications and application libraries are a "user thing", and C++ is more appropriate for the latter than the former.

IMHO.

Last edited by paulsm4; 08-28-2005 at 01:32 PM.
 
Old 09-01-2005, 06:03 AM   #21
Igor007
Member
 
Registered: Aug 2004
Posts: 76

Original Poster
Rep: Reputation: 15
paulsm4

MS Win OSes are not so bad.
Ofcourse the are less secure than Linux
But they are reliable and friendly enough
Espec. Win2003 Server
 
Old 09-01-2005, 03:23 PM   #22
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,143

Rep: Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638
matir's report is significant because the bigger the binary; the more machine-level instructions the processor has to execute.

from my engineering background i can appreciate the reduction in the number of assembly opcodes (and by relation the reduced machine hex codes).

i've taken courses in microprocessors and our major emphasis was reducing instructions to boost performance.

through my previous analysis i conclude through single-statment 'hello-world' programs, 'c' would be about 5 times more efficient than 'c++' in the amount of machine code generated.
 
Old 09-02-2005, 06:03 AM   #23
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Quote:
Originally posted by schneidz
through my previous analysis i conclude through single-statment 'hello-world' programs, 'c' would be about 5 times more efficient than 'c++' in the amount of machine code generated.
That's like looking at an apple and concluding that all apples are red (but in fact they aren't), or using Windows for 30 minutes without crashing it and concluding that it's stable and secure (but it isn't).
Hello World is barely representativ.
 
Old 09-02-2005, 11:57 AM   #24
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,143

Rep: Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638
i think it can give a metric on what to expect on more robust testing (matir's testing).

what hello-world did for me was allow me to speculate on the probability of which is more efficient. much like not all apples are red but most are (probably about 90%). and windoze isn't always stable and secure but it mostly is (insert subjective opinion ----> here).

as a scientist we all test out speculations and then generate theories to be proved or disproved in postulations.

now that we have a basis for a baseline speculation, we should continue the thread by referencing in/formal reports that may bring us closer to a principle.

addy's post is correct because probably not all c++ code will be larger than comparable c code. (although there is more overhead with a fully object/class designed infrastructure).

can someone provide examples where c++ will be faster than c?
 
Old 09-02-2005, 12:20 PM   #25
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Quote:
Originally posted by schneidz

can someone provide examples where c++ will be faster than c?
Yeah, I was the guy with the original study, so my opinion may seem somewhat tainted, but here's my input as a Computer Science major:
C++ was never designed to be faster than C. It was designed to support classes, function and operator overloading, better type promotions, and other features.
 
1 members found this post helpful.
Old 09-02-2005, 12:57 PM   #26
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
1. In general, C++ will be - at BEST - "as fast as" C.

2. One possible exception is using the C++ "inline" construct: the in-lined C++ code might well run faster than the most optimally-coded C equivalent.

3. Which, by the way, is a good example of why "fewer opcodes" is not always directly proportional to "faster execution". Often times *more* code results in *faster execution*. E.g. "loop unrolling".

4. The key point I've been struggling to make is that pure "code efficiency" (be it "code size" or "execution speed") is (I would strongly argue) a relatively *minor* consideration.

Whether C or C++ is the more appropriate tool for the job is *NOT* - repeat *NOT* - a question of whether one produces "tighter code" than the other.

5. And that brings me back to your original question, about using C++ in a kernel driver.

Yes, C++ would be a wonderful tool for the API libraries or utility programs that you might supply *with* your custom driver.

And no, 'C' is the *only* appropropriate tool for writing the kernel driver itself. Period.

6. Final notes:
a) If you're interested in investiging code performance issues, here are some great benchmark programs to play with:

http://www.netlib.org/benchmark/

b) If you're interested in analyzing system performance, this is a great book:

- The Practical Performance Analyst, Neil J. Gunther
(I understand he has a new version that uses Perl tools)

c) And these books have some very good discussions about the
relative UNIMPORTANCE of "optimizing code" (to the exclusion of
other concerns):

- The Practice of Programming, Brian Kernighan

- Code Complete, Steve Connell

I appreciate your interest in the questions "Can C++ be made to run faster than C?" and "How valid are 'Hello world' samples?" Matir did a great job, and I encourage you to look at the benchmarks I mentioned above.

But can we all agree that C++ is *not* a good tool for writing kernel drivers, and move on to another topic????

IMHO .. PSM

Last edited by paulsm4; 09-02-2005 at 12:59 PM.
 
Old 09-02-2005, 12:58 PM   #27
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
Quote:
Originally posted by Matir
Yeah, I was the guy with the original study, so my opinion may seem somewhat tainted, but here's my input as a Computer Science major:
C++ was never designed to be faster than C. It was designed to support classes, function and operator overloading, better type promotions, and other features.
I agree, but I think its definately possible to write C++ code that is just as fast as C code. It all depends on the programmer and their knowledge of the two languages.

I don't think this first C++ program would be any slower or eat any more memory than this second C program, although I don't have time to test it at the moment (I'm in Windows, I'll try to benchmark later). Actually looking at the code I'm not too sure, the C one could be slightly faster. Again I'll try to benchmark later using the same tests used earlier in this thread. I should be home later tonite, in about 12 hours or so.

Code:
#include <iostream>
#include <cstring>
using namespace std;

// C++
class PrintC_String
{
  private:
    char * strptr_;
  public:
    PrintC_String()
    {
        strptr_ = new char[strlen(s) + 1];
        strcpy(strptr_, s);
    }
    void Print() { cout << strptr_ << "\n"; }
};

int main()
{
    PrintC_String s("Hello World");
    s.Print();
}
Code:
#include <stdio.h>
#include <string.h>

/* C */
void Print(const char* s)
{
    printf("%s\n", s);
}

int main()
{
    char* s = "Hello World";
    Print(s);
}
edit: This is not a good example of C++ code that is "just as fast" as C code. If I wanted to do that I would write C code and compile with a C++ compiler. Technically almost all C code is also C++ code. I wanted to use a simple example of C++ that used classes and a simple example of C code that used a function.

Last edited by lowpro2k3; 09-02-2005 at 01:01 PM.
 
Old 04-27-2007, 09:00 AM   #28
lmenten
LQ Newbie
 
Registered: Apr 2007
Posts: 15

Rep: Reputation: 0
C++ is a good language for complex kernel modules

The click router project at MIT was done in C++ for Linux. I have used the netlab.ru.is patches. Sweet. Works like a charm. My module is a stateful firewall and IDS/IPS written in C++. It has been used in VxWorks, pSOS, various flavors of Win, Linux, and under a real-time executive.

It is rather strange that embedded OS provide goods support for C++ but the Linux kernel does not.

Religion.
 
Old 08-20-2008, 04:10 AM   #29
amitsharma91
LQ Newbie
 
Registered: Oct 2005
Posts: 4

Rep: Reputation: 0
Cool C++ usage for ARM

[QUOTE=Matir;1812389]Again, prove your point please. I would leave to see such a demonstration.[/QUOTHi FOlks

Hi Folks

I do not know, can i use C++ in Linux kernel with ARM , can some one help me , where i can get such stuff , and if i want to to do some porting where i should start.
 
Old 08-20-2008, 03:26 PM   #30
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I think the real problems with C++ kernel code are compiler-determined dependencies. Even without using a single #include, you can still incur dependencies when using C++. C++ generally requires some sort of entry and exit code, which g++ automatically links into executables and forces into init and fini in shared libraries. You'd be wasting your time to use static non-POD objects because it requires both initialization and destruction routines.

Most non-trivial kernel code requires dynamic allocation, or the kernel will provide the required memory in certain cases (such as ciphers,) which would require the use of in-place new with C++, which incurs a run-time dependency. As far as I know, the global new is also a run-time routine, though possibly-statically linked in by g++. In both cases you'd have to manually construct your structures, which is unreliable at best even without anything associated with the word "virtual."
ta0kira
 
  


Reply

Tags
arm, c++, compile, fine, kernel, linux, modules


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux kernel development vwal_13 Programming 3 03-09-2005 03:27 AM
Linux Device Driver Development aslv Linux - Certification 0 09-27-2004 09:42 AM
Linux Kernel Development impact on Slackware carboncopy Slackware 5 07-28-2004 04:43 PM
Touch screen driver development for Linux peso Linux - Software 1 02-02-2004 01:15 PM
Lexmark Released A Linux Driver Development Kit FearPasion710 Linux - Software 0 09-09-2003 07:18 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration