# FreeBSD as a system programming learning environment?



## toxemicsquire (Jul 27, 2014)

I'm learning C as a my first programming language, and I want to become a system programmer. I'm still covering the basics, but I think I got all of them down. I use Gentoo Linux on my desktop, but it would be unsuitable for operating system learning because of how large and clumpy the kernel is. I think I know the basic concepts of what an OS is. I just need a system I could experiment on. I think FreeBSD would be a suitable platform for that because of the clean code, documentation, and stability. Would it? What books would be able to guide me through the OS code?


----------



## fonz (Jul 27, 2014)

_The Design and Implementation of the FreeBSD Operating System_
Marshall Kirk McCusick et al. (a new version should be out real soon)
_The Unix Programming Environment_
Brian W. Kernighan and Rob Pike
_Designing BSD Rootkits: An Introduction to Kernel Hacking_
Joseph Kong
_FreeBSD Device Drivers: A Guide for the Intrepid_
Joseph Kong
Searching this forum...
Appendix B of the FreeBSD Handbook
Edited to add: books such as _Operating System Concepts_ by Silbershatz et al. or _Operating Systems_ by Stallings discuss the components and algorithms used internally by operating systems.


----------



## kpa (Jul 27, 2014)

My opinion is likely not a popular one here but in my opinion learning C as the first programming language doesn't make much sense. The learning curve is unnecessarily steep and you end up "reinventing the wheel" many times ove because the language itself and the C standard library are seriously incomplete in terms of standard tools that would be very helpful for a beginner to learn the basics of programming and not get tangled in the nitty gritty details that C often does to you. My advice here is to learn another easier programming language on the side, preferably an interpreted one. Python and Perl are good choices.


----------



## worldi (Jul 27, 2014)

Well, no-one becomes a marksman by playing with waterpistols. C is not only about software but also about hardware (stack vs heap, memory alignment, registers, caches, different CPU instruction sets, etc.). Knowing this kind of stuff is crucial when your goal is to become a system programmer and most other languages hide this very interesting stuff from you.


----------



## drhowarddrfine (Jul 27, 2014)

He wants to be a system programmer so learning C is a prerequisite. My first programming language was machine code followed by assembly. I learned those _be_cause it was required of me as a hardware designer.  You don't learn something else because it is easier.


----------



## Oko (Jul 27, 2014)

kpa said:
			
		

> My opinion is likely not a popular one here but in my opinion learning C as the first programming language doesn't make much sense.


The foundations of computational theory are laid in three ground-breaking papers, two of which written by Alan Turing and one written by Alonzo Church. In two of them Alan and Alonzo respectively introduce the Turing machine and Lambda calculus and in the third one Alan proves the equivalence of those two constructions. Naturally the first programing language  should be Scheme, which is a minimalistic implementation of Lambda calculus. Naturally the first and the only book taught should be Structure and Interpretation of Computer Programs. Now in the real world most programmers will want to skip this step and go right away to C  :beer 



			
				kpa said:
			
		

> The learning curve is unnecessarily steep and you end up "reinventing the wheel" many times ove because the language itself and the C standard library are seriously incomplete in terms of standard tools that would be very helpful for a beginner to learn the basics of programming and not get tangled in the nitty gritty details that C often does to you. My advice here is to learn another easier programming language on the side, preferably an interpreted one. Python and Perl are good choices.


I was holding my breath while reading the rest of your post fearing you are going to recommend Java as a great system programming language :x


----------



## wblock@ (Jul 27, 2014)

C is really an overgrown macro assembler, so assembler is a good lead-in to it.  The problem with using more advanced languages is that they make the old languages so frustrating.  Like using C for dealing with strings after using Perluabython.



			
				Oko said:
			
		

> fearing you are going to recommend Java as a great system programming language



Years ago, there was a thread in Usenet discussing the classic Perl method of removing leading and trailing spaces from a line, two regex statements.  Yes, they are regexes, but they are short and there are no loops or branching.  (Yes, this can be done in one statement, but two is faster and less complicated.):

```
s/^\s+//; # remove spaces at the start of a line
s/\s+$//; # remove spaces at the end of a line
```
Someone complained about that being hard to understand, and said that a Java program would be much better.  Following that was posted a 15- or 20-line Java version using loops to look for spaces.  At first glance, it appeared to have at least one and maybe several off-by-one errors, and it did not handle tabs.

To this day, I'm not sure if that was serious or a joke.


----------



## toxemicsquire (Jul 28, 2014)

But would FreeBSD be simpler to learn about its internals, than perhaps than most other operating systems? And isn't it very unix-like, and you would be able to implement your own simple unix-like operating system after reading one of those books listed on top?


----------



## Oko (Jul 28, 2014)

toxemicsquire said:
			
		

> But would FreeBSD be simpler to learn about its internals, than perhaps than most other operating systems? And isn't it very unix-like, and you would be able to implement your own simple unix-like operating system after reading one of those books listed on top?


The first book to read for anybody trying to learn about internals of UNIX should be either Lion's commentary or  The Design of the UNIX Operating System by Maurice J. Bach. From the moment you start learning C from  Kernighan's and Ritchie's book to the time you write your first kernel patch it should not take you more than 5-7 years of ten hours a day of learning. Of course it would take you much longer if you are an averagely intelligent person. I anticipate you could implement your own baby UNIX in the following five years.


----------



## max21 (Aug 1, 2014)

It doesn’t take long to understand basic C programming. Most of that hard crazy looking code (/) is already written and there are tons of example programs out there to rip apart so to experiment with. The most important thing to learn is what the code does and how it works. This will reduce the time to learn, where years will turn into months and months to weeks.


----------



## fonz (Aug 1, 2014)

max21 said:
			
		

> where years will turn into months and months to weeks.


Yikes. No matter how smart you are, you don't become a *good* C programmer in mere weeks. That's simply impossible. You'll only become the dangerous kind of programmer who *thinks* he knows what he's doing.


----------



## max21 (Aug 1, 2014)

C is not easy but I would not discourage a young person to take the dive other than to say take time out to have a social life. If no one did it back in the old days we might not have anything today. Somebody has to learn more. The clock doesn't stop just because it's hard.

Things have changed from bottom to top. I don't even know what I want to do myself after reading this..

http://www.javaworld.com/article/209374 ... mming.html


----------



## fonz (Aug 1, 2014)

max21 said:
			
		

> C is not easy but I would not discourage a young person to take the dive other than to say take time out to have a social life.


Oh, I agree with that. C is somewhat deceptive because it's quite a small language, so the basics are easily learned. But it takes a lot of practice and experience to get really good at it. And I sure wouldn't want to discourage anyone from trying (I think @Oko's comment is somewhat pessimistic in that sense). On the contrary, I'd say go for it. Just remember that getting good at C takes time and practice. And you're pretty much certain to screw up a few times along the way.


----------



## Maelstorm (Aug 24, 2014)

With my experience, I learned C out of a book by doing the examples and exercises.  When I fist started programming, I was using GW-BASIC.  Then I moved to Pascal and IA32 Assembler.  I learned C later on while using FreeBSD.  The CIS 001 (Introduction to Computers) class taught at the local College starts people off with programming in Visual Basic (gah!).  Then they move to C# and IBM SYS370 assembler in later classes.  There are so many languages out there that no one person can learn them all.  You have the popular ones like C, C++, Java, VB, VC++, VC#, PHP, Python, and Ruby.  Everything basically comes down to using the right tool for the job.  

However, UNIX, and subsequently FreeBSD was written in C, which necessitates one to learn C if doing system admin or system programming work.  There's a big difference between system programming and application programming.  For system programming, anything that can cause a fault in the software must be checked for.  A divide by zero error can kill your system (exception in kernel mode causes a system panic).  All input must be verified.  While working on the other side of the system call tree, you do not have alot of the "nice" functions available to use as you would when working in user space.  This has to do with the mission of the kernel being radically different than that of an application.

The expectation of the kernel is resource management.   Resources is defined as being hardware devices, files, memory, network, processes, CPU time, and most importantly, security.  As a result of this, the kernel library is limited to functions that enable it to achieve that goal.  For applications, we all know that their expectations very far and wide.  They can be anything from a simple game of pong or chess to the sophisticated control program for a telephone exchange, from something simple like your digital wrist watch to something that people's lives depends on such as space craft navigation, the point defense system on a naval warship, to a flight control computer on an aircraft.

In my time, I've seen code that was supposedly written by professional software engineers that was so atrocious I wouldn't turn it in to my dog.

As for the subject on hand, FreeBSD is an excellent platform to learn system programming on.  Once you learn it, you can then claim on your resume that you can do system level programming in a UNIX environment.  Do this on your own computer at home though.  I doubt that an employer would appreciate you causing a panic in a mission critical system.  Furthermore, start with writing kernel modules.  You don't want to start modifying kernel source right off the bat because you can end up with a non-bootable system if you screw up (Yes, speaking from experience here.).  If you do modify kernel source, make sure you make a backup copy of your existing kernel in case something goes wrong...and something will go wrong...trust me.

Other people have given some very good resources for you to look at so I won't list them here.  But, a prerequisite is to learn C, which you are doing.  In the kernel, there is some machine specific code that is written in assembler for the target platform, but that's for CPU specific stuff such as memory protection, virtual memory, context switching, locks, and the like.


----------

