LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-21-2013, 07:07 PM   #16
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197

Quote:
Originally Posted by btmiller View Post
You're not going to be able to use "new" and "delete" (as is statesdabove),
Not to beat this to death, but I most certainly would use new and delete if writing a kernel in C++.

I might (or might not) overload those operators to get more efficient access to something like a slab allocator. But with or without overloading new and delete for some performance critical objects, I would have new and delete default to passing their allocation work onto malloc. As I said above, one would write a replacement for malloc when writing a kernel. But most of the C++ value of new/delete is still in place after you replace malloc.

Quote:
Don't be so dismissive of "experts", just because you don't happen to like the conclusions they've drawn.
There are simple right and wrong aspects to this, not matters of opinion. If you don't have that knowledge yourself, I understand you have no reason to trust me rather than some professor. But don't tell me I don't happen to like their conclusions. From my perspective, these are questions of simple fact, neither complicated nor based on opinion (how object references work, how STL works, etc.)

Last edited by johnsfine; 12-21-2013 at 07:09 PM.
 
Old 12-21-2013, 08:52 PM   #17
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
The real question is whether or not it's a good idea to write a kernel in C++. This is a metter of opinion. The facts of how C++ works are not in dispute here; what is in dispute is whether the language feature of C++ make it a good choice for OS development. My contention is that, for a novice OS developer, C++ adds substanital complexity and is probably not the best language choice to learn about OS development.

Interesting discussion, though.
 
Old 12-22-2013, 09:04 AM   #18
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by johnsfine View Post
Not to beat this to death, but I most certainly would use new and delete if writing a kernel in C++.

I might (or might not) overload those operators to get more efficient access to something like a slab allocator. But with or without overloading new and delete for some performance critical objects, I would have new and delete default to passing their allocation work onto malloc. As I said above, one would write a replacement for malloc when writing a kernel. But most of the C++ value of new/delete is still in place after you replace malloc.
The problem is that memory allocation in the kernel does not work like malloc... Using it that way causes a LOT of wasted memory due to various allocation limitations- like page size limits. Minimum allocation is a page - so how big is a page? Depending on hardware, that minimum might be 1MB, or 4K and it might even differ depending on WHERE in the kernel things are being done. Unless using layered allocations... (which gets more complex- they are not necessarily contiguous sections). What works in one place will not necessarily work in another. Hence there are multiple DIFFERENT memory allocation methods used.
Quote:
There are simple right and wrong aspects to this, not matters of opinion. If you don't have that knowledge yourself, I understand you have no reason to trust me rather than some professor. But don't tell me I don't happen to like their conclusions. From my perspective, these are questions of simple fact, neither complicated nor based on opinion (how object references work, how STL works, etc.)
Most kernels are dynamic... they add modules/remove modules on the fly. C++ doesn't like it when things disappear - unless you go to using pointer tables... In which case you aren't using C++ anymore.

Now for a static kernel, without on-the-fly reconfiguration, C++ might work just fine. The results might be a bit bloated, but it will work - just don't expect portability without also having to recompile for each platform, even if using the same CPU. Once you start dynamically loading modules you get out of the C++ environment and back into the low level coding that is the foundation of C.

C was DESIGNED for such low level coding as this. C++ was designed for applications. That it COULD be used for low level coding is a bonus... but when doing such low level coding the advanced language features just get in the way.

Last edited by jpollard; 12-22-2013 at 09:10 AM.
 
Old 12-22-2013, 10:21 AM   #19
coldbeer
Member
 
Registered: May 2006
Location: Orion–Cygnus Arm, MWG
Distribution: Slackware, Ubuntu
Posts: 249

Rep: Reputation: 130Reputation: 130
Quote:
Originally Posted by maples View Post
I read something somewhere (sorry, I can't remember) about being able to write C++ code and turn it into an operating system. Am I right, or is the page wrong / I misinterpreted it?

I know a little C++ and, assuming that I am right, would like to give it a try.

Having worked as an embedded software engineer for a long time: Here's what will happen in the real world.

1) If you write an OS in C++, and its a competitive product, your competitor will have many opportunities to write the same thing in C and make their product faster and smaller. Thus putting your product at risk.

2) If you write an OS in C++ as an employee for a company: At crunch time (memory+performance), another engineer will start rewriting your C++ code as C and the C++ code will be resigned to a old node in a CM system.

This can happen with C compared to ASM, but C can be written to be competitive with ASM in many cases without too much fuss.

So yes, you may be able to do it but the rewards/risk doesn't side with C++ - as proven by commercial OSs on the market today.

In addition, if the OS is for an embedded application, the choice of the micro controller is a cost factor of the product and is also a variable from product conception to proto. Many times the micro is switched to a different size/brand during development and the code has to be rewritten - sometimes to ASM if the micro is bleeding edge. There's a lag time from new micro until a compiler is available for it and the first compiler will always be a C compiler, not C++. Going from C to ASM and from ASM to C is a straight forward process. If you have written your code in C++ you're screwed - You'll have to re-design your code instead of just converting from one language to another.

My $.02

Last edited by coldbeer; 12-22-2013 at 10:39 AM.
 
1 members found this post helpful.
Old 12-22-2013, 02:15 PM   #20
maples
Member
 
Registered: Oct 2013
Location: IN, USA
Distribution: Arch, Debian Jessie
Posts: 814

Original Poster
Rep: Reputation: 265Reputation: 265Reputation: 265
Thanks for all the input!

So it sounds like an OS in C++ not going to be something I would be able to do. I'm just a kid in high school and thought it would be interesting to try to write a "Hello, World!" OS over Christmas break, with maybe a few inputs and outputs.

Quote:
Originally Posted by Myk267 View Post
I'll look into this, thanks.

Again, thank you everyone for the response!

Last edited by maples; 12-22-2013 at 02:17 PM.
 
Old 12-22-2013, 02:31 PM   #21
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by johnsfine View Post
...

an apples to fruit salad comparison that is unfair in any context in which it might mean anything, but especially unfair regarding kernel development.

Try implimenting a general purpose priority queue algorithm (using a heap) in C and (also in C) applying that algorithm to several different queues of different kinds of polymorphic (within some of the queues) data structures. Now do the same in C++ and compare execution speed. It isn't just easier to code in C++, the result will also execute faster. For someone at my level of programming, implimenting the priority queue templates in C++ is easy enough that fitting the project requirements even a tiny bit better than the STL priority queue is worth rewriting it. The STL one beats what you might do in C, but I can (and in several projects have) beat that. Another programmer writing a kernel in C++ might just use the STL priority queue. It is better than you would be likely to do yourself in C. An OS kernel needs several priority queues and it is a place performance can really matter.
I don't doubt you but do you have an example ?
 
Old 12-22-2013, 06:31 PM   #22
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by schneidz View Post
I don't doubt you but do you have an example ?
Different priority queues? no problem.

Task scheduling is one.
IO scheduling is another(buffer ordering).
And then there is process scheduling.
Real time scheduling.
interrupt scheduling.

None of these share the same data structure, yet all are scheduling features.

I don't have an example of a generic C++ priority scheduling algorithm that could be used for all four. They work totally differently.

Last edited by jpollard; 12-22-2013 at 06:33 PM.
 
Old 12-22-2013, 09:53 PM   #23
allanf
Member
 
Registered: Sep 2008
Location: MN
Distribution: Gentoo, Fedora, Suse, Slackware, Debian, CentOS
Posts: 100
Blog Entries: 1

Rep: Reputation: 20
Quote:
Originally Posted by maples View Post
I read something somewhere (sorry, I can't remember) about being able to write C++ code and turn it into an operating system. Am I right, or is the page wrong / I misinterpreted it?

I know a little C++ and, assuming that I am right, would like to give it a try.
Yes, you could write an OS in C++ (although parts might have to be in assembler).
 
Old 12-23-2013, 09:41 PM   #24
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Quote:
Originally Posted by maples View Post
So it sounds like an OS in C++ not going to be something I would be able to do. I'm just a kid in high school and thought it would be interesting to try to write a "Hello, World!" OS over Christmas break, with maybe a few inputs and outputs.
If all you want to do is make the character string "Hello, world" appear on the screen, it's not that hard and can be done with a little bit of basic C with a little inline assembler to write directly into video memory. Look at some of the basic bootloader examples on the OSDEV wiki, and you'll quickly see how it's done.

If you want to do more than this, you'll need to get into some fairly in-depth C (or C++) and assembler work, as well as learning more than you ever really wanted to know about how hacky the x86 architecture really is (you'll get to become good friends with the A20 line, for instance).
 
Old 12-29-2013, 01:38 PM   #25
maples
Member
 
Registered: Oct 2013
Location: IN, USA
Distribution: Arch, Debian Jessie
Posts: 814

Original Poster
Rep: Reputation: 265Reputation: 265Reputation: 265
I followed the link that MYK267 posted, and realized that most of this is way over my head. Maybe I'll come back to it when I graduate from high school.

Thanks for your time!
 
Old 12-29-2013, 02:21 PM   #26
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
The fun part was the C++ entry... "To write a kernel in C++, simply use the code above (which also happens to be legal C++) and save it in a kernel.cp (or what your favorite C++ filename extension is)."

And I had forgotten about the C++ name mangling causing problems.
 
  


Reply



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
[SOLVED] diff - how do i read it as true or false? Markus72 Linux - Software 3 03-27-2011 04:13 AM
if statement ignoring true/false and proceeding anyway. Goblin_C_Noob Programming 4 03-30-2008 09:56 AM
comparison is always true/false jubaitca Programming 20 11-05-2006 06:55 PM
true or false? alaios Programming 7 07-16-2005 10:54 AM
Return true or false if I have ping reply Menestrel Programming 4 11-29-2004 12:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration