[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: How should learning to program in c++ be approached, if learning objectives are sought to be customised?




The header files are very, very complex and they can't be understood.
No reason to read header files.
Both C and C++ have ANSI standards, which developers code against.
Just read standard and do not bother with reading header files.
But start with a good book. 

Studying C by reading header files is like studying linux by reading kernel source code instead of man pages)
 
To me, c appears as a hodge podge of low level and high level
programming languages.
Yes, C is a pretty low level language. Do not use it to build websites or mobile bank clients: it is possible, but not convenient.



I was really excited by the scope provided by Java. It appeared so
easy to me that I could easily build a functional program, to aid
memorising alphabets abcd... in the reverse, "which alphabet came
before which alphabet" using a random function, in a graphical
environment.
Java is a good language (Kotlin also good and has full interop with Java)
Debian provides OpenJDK package and could be used to do Java development.
Intellij Idea Community edition is a free IDE for Java that also works on Debian.

Many enterprise systems and mobile applications (Android is based on Linux) are written on Java and Kotlin.
 

I had read that Java could be used to build an entire Operating System
up by itself. In essence, therefore, device drivers as well.
Well, any turing-complete language could be used to build anything (this is what CS professors tell us)
The question is whether it is convenient or not, and how much time does it take.

If you have unlimited time and do not care about performance, you can write OS on bash:
just create an LLVM backend that compiles bash to the assembler for your CPU.


Once there was a specification of CPU to run JVM on (google for "picoJava")
Not sure if it exists IRL, but if it does -- you can run Java on bare metal:)

 
Unfortunately, that project of building an OS in Java was dropped long
back. Java isn't used for building device drivers for the industry, so
far as I have gathered.

There are several problems with high level languages for driver development.
* Garbage collection complicates direct memory management (and many devices use MMIO) and also makes code less predictable (you can't predict when memory will be freed)
* Most OSes have C API, and Java-C interop is awkward
* To run Java you need to either run JVM in the kernel or compile Java to the native code (JVM compiles Java to the native code while running (JIT) but not before)
Technically, you can run JVM in the kernel (there are several virtual machines in the kernel already: for AML, ebpf etc) but it will pollute the kernel and sounds like an anecdote.
Even java code compiled into the native code needs runtime (for GC) and nobody will add such runtime to the kernel.

Most kernel developers are happy with "C" and suggesting them to use Java for kernel development is like suggesting Linux sysadmin to use Windows: probably a bad idea.

While in theory any language can be used for anything, practically there is no one language to fit it all. 

People use one language to build a website and another language to write device drivers.

 
Is it supposed to mean that theoretically, an entire OS could be built
up from scratch using Python? Including device drivers?

That means you can build a robot and program it in Python.

Honestly, I do not think device drivers are the right things to start programming with.
Find a language that you like (be it Java, Python,  C#, Ruby, whatever) and write software!

  
You said, "... Linux drivers are written in C, but technically you can
mix languages: use C++ and link it against C ...". But I would request
more specificity here:    (a)  if c++ could be used without using any
other programming language to build a device driver. (b)  If it is
practised industrially.

In OS X (Apple) they have "DriverKit": C++ (class-based) API to write drivers.
Other mainstream OSes provide C API for drivers, but again: C++ is backward compatible with C in most cases.

C++ provides "extern "C"" to export symbols to C and can also call any C function, so yes: you can write a driver with C++ even for Linux, 
but probably you would need to disable some C++ features (like Exceptions, probably RTTI etc).
Doing so is very hackish.

Also note that Linus hates C++. If you want to read lots of bad words, ask him about C++ in kernel development (please, dont)


I often wonder aloud why c wasn't re-built to have the modularity and
OOP structure of Java.
If you want to change language heavily, you have choices:
* Break backward compatibility and lose an uncountable number of code lines and years of development. This is not what you want to do.
* Increase language complexity (this is what happened with C++). And complex language means bugs. And bugs in kernel are unacceptable.

OOP (you mean classes, right?) is also controversial. Sometimes it is useful, sometimes not. Stallman hates OOP, for example. 

There is no silver bullet nor golder hammer: what is good for Customer relationship management system is not good for temperature sensor driver on I2C bus.

 
I began learning python, but I am not sure if python could be used in
place of Assembly (or machine) Language, addressing programming needs
from the lowest to the highest level. Perhaps your notes answer my
doubts. Would like to know more.

With micropython you can access registers of underlying hardware.
It doesn't provide access to CPU registers though, but you can write extensions in other languages


So I would like to develop an understanding of programming from the
lowest level to the highest level. The way programming is approached
in textbooks doesn't suit me. I can't have the flexibility to use
programming like a cobweb or a network, jumping from lowest level to
the highest level according to my need.
If you want to solve both low level and high level tasks, you should probably study several languages.

Btw, to do low level development knowing language is not enough: you also need to understand computer architecture, OS architecture etc.
Since most OSes are written on C, every book about OS (Understanding Linux Kernel, Windows Internals etc) assumes you can read C code and sometimes assembler code.

Segmens, pages, code level (user space vs kernel space), stack, buses (PCI-e, USB), cache lines, exception handling, many things must be clear for driver developers, not 
only "C" language.   
 

I read that micropython is written in c. So it is just like
circumlocuting to the same point from where it began.

So confusing!

Yes. CPython is also written on C. And JVM is written on C++ and Java.

The CPU only runs machine code. People created assembler with machine codes to be able to translate assembly language to machine codes.
Then they rewrote the translator to assembler.
Then they wrote a C compiler on assembler.
Then they rewrote the C compiler on C (this is called "bootstrapping") 
Then they wrote Python, Perl and other languages on C.
There is a Python implementation written on Python dialect RPython. Google for "pypy":)

You can also write part of your program on Java and another part on C++ (using JNI or JNA)
You can also use Python that runs on JVM (it is called JPython) and mix Java and Python code. And add some Ruby code (using JRuby). And scala, groovy and kotlin code:)

Facebook created tool that compiles PHP to C++ (HipHop)

World is complex:) 
 
I suggest starting with Python or Java (if you like it) to improve programming skills and study some structures and algorithms.
Then, study C using some good book (like the one from O'reilly) not by reading header files.

C is also crucial for understanding Linux: knowing how C toolchain works (compiling, linking (static vs dynamic), what is position independent code and so on) helped me alot to solve many Linux problems. 


Reply to: