How to learn C programming and the basics of operating systems in a practical way?

Programming icon

There are many ways to learn to program, there are many web pages with tutorials, code examples, etc. You can also find a wide variety of books on programming, both C and many other languages. There are also online or face-to-face programming courses where you can begin to understand these techniques to create your own software from scratch to an advanced level.

Then, on the other hand, there are some physical trainers with certain microprocessors or microcontrollers that you can program to learn this art almost by playing and creating projects. A very good example is the Raspberry Pi itself, the Parallaz boards, or the national ones that you can find in the MKElectronics store, or the well-known board Arduino, Scratch. However, if you want to focus more on software development, I always advise the same: read and modify code.

In this post I will not try to give you programming classes, since it is a topic that I would give for many articles like this explaining from the basics of programming to the most advanced programming. I just want to give you some resources or ways to start to strengthen your knowledge. And I assure you that they are very effective. Also, you know that you can start with any GNU / Linux distro, with the necessary packages for programming such as a text editor, or an IDE, also GCC and GDB, if you want to debug ... Or also use some distribution for programmers. I also think that you already know or should know this type of tools that you have at your disposal.

How to start?

On-screen source code

You can find code snippets in the network to millions, that is, fragments of code that can be used or modified to do very specific tasks. These source codes are the basis for learning, since they are very basic functions that will help you understand how the programs work and you can modify some parameters to get them to do something different. And that's how it starts!

As you move forward, you can jump into read source code, which if well commented will be especially nutritious for your knowledge. Unfortunately not all developers have a good habit of commenting on the lines they write, and in some cases the comments are scarce or confusing even for experienced people. But fortunately we have a large part of the community that comments on their projects quite well.

Platforms where software code such as GitHubFor example, although there are many more, they are very important sources of wisdom. In fact, I recommend that once you have acquired some fluency with snippets or your small programs, go to these types of platforms and access the source code of simple projects. You can use the filter and search engine functions to find specific code in a programming languageas C and then select one that is simpler.

So little by little you can go increasing the complexity of the programs to raise your knowledge to a fairly advanced level. Do not start with projects with a large number of lines, or you will be frustrated by not understanding what this large amount of code is for or what it does. It begins with small programs of a few dozen lines, then for a few of 100 or more and so on until little by little you reach more complex programs.

In addition, I advise you to look for programs that you know quite well in terms of their use. For example, imagine you are using a certain open source text editor or a calculator. Knowing what this program does in practice will make it easier for you to relate the lines of source code that you will see with the functions that the program has. On the other hand, if you choose a code that you have no idea about, only with the help of your basic programming knowledge and the comments that you will find in the code files will not be enough.

For example, take a look at the second example shown in my search for calculation programs in C language of the following image. The description shows that it is a command line program that implements a simple calculator. This is a good code to start with, not only because of the simplicity of the code, but because being text-based you won't have code files related to the GUI, etc.

Github

If we go into this project to see its code, we will first find the files that the developer has uploaded to this platform. Like the details of the LICENSE license, the file with information par excellence README, etc. But notice that there is headers files like helpers.h and struct.h, these two are important, and also the two .c, which are the ones that have the actual C source code for this program:

Headers

If we access one of the headers or .h, we will see that it is a simple program, without too many lines. You will find these types of files many times, unless it is a fairly simple program that does not need them, but it is a include file where some details or additional files are included that the compiler must process to form the final binary of the program.

In a header file you will find direct class declarations, subroutines, defined variables, and others. Sometimes these .h are Libraries that you can use from the main code file (.c) making direct reference to what is declared within these headers without having to repeat the entire structure every time it is needed:

Code header

In the case of the source code itself, like the calc.c file from our example, you will find more lines. About 400 something, and in this specific case they are not commented. Which seriously hinders the understanding of the code. However, being a program as simple as a calculator, you can read and modify it without problems, since it will be intuitive:

Remember, read code and modify it. Those are the keys to learning to program in the best possible way ...

One more step: the kernel

Linux Kernel

Now, when you have already acquired some basic programming skills in C language, you can take a step further and enter the fascinating world of the operating systems and computer architectures. For this you must have somewhat higher knowledge of how computers work, but again the existing open source projects can help us a lot.

Analyze the kernel source code It can make us understand in a better way how this world of operating systems works, and they will also be master classes to obtain our "doctorate" in C programming, as it is something of major words. You might think that the best project to learn this would be the Linux kernel.

Instead, the current dimensions of latest Linux kernel versions they are too complex to be comprehensible to most mortals. Especially for the large amount of code for other subsystems and drivers that you will find added to what is the kernel itself. That may lead you to think that in kernel.org you can find more primitive versions of the kernel, such as the "historic" where there are some "old-versions" such as the Linux 0.01, the first in which the code is much more reduced and simple to analyze.

Kernel.org capture

And although the C code that you will find in the Linux kernel is exquisite, I do not recommend the Linux kernel due to the fact that it is usually scarcely commented on many occasions. I'm not saying that the developers have a bad comment policy, but it can be confusing for someone just starting out. So save the Linux kernel and LKML for later ...

And it is true that they exist some very good books and courses to learn about the kernel, modules and drivers, but maybe not the best place to start. Some examples are (some of them, like Greg's, you can download for free, since they have been released):

  • Linux Kernel Development 3rd Edition by Robert Love.
  • Understanding The Linux Virtual Memory Manager by Mel Goman, although the latter focuses only on kernel memory management.
  • Linux Device Drivers 3rd Edition by Greg Kroah-Hartman, although it is focused on the 2.6 kernel, but it is a great work to understand the kernel drivers and modules.
  • Linux Kernel in a Nutshell is another of the great Greg Kroah-Hartman that gives you a much more global vision.

You can also find some very very interesting websites, beyond your own information and documentation which you can find at kernel.org, like:

Another of the great projects that are still being studied and serving as learning is UNIX-6th, whose code is analyzed in an extraordinary way in books such as Lions' Commentary on UNIX 6th Editionby John Lions. I leave it to you as another alternative idea, although I continue to recommend the following that I am going to comment on ...

But I think that the best resource you have at your disposal is to study and analyze the MINIX source code. It is a much simpler system in which Linux was initially inspired but which has been specially conceived for learning and study. Being intended for future operating system architects to learn, the feedback it has is extraordinary for those just starting out.

More information - MINIX3

There you will find a lot of information and documentation about the project. And you can download the operating system itself, but also have at your disposal a good Wiki where there are also texts for developers. But, as it is about learning C and analyzing how an operating system is constituted, I refer you directly to download the first version of the MINIX 1 source code:

Source code - MINIX1

For example, if you go to main file main.cYou will see that the code here is very different from how you find it in Linux or in the calculator program that I used as an example at the beginning of this post. In this case, you will notice that the number of comments is much more abundant:

MINIX source code

Here, almost every line or every fragment is Commented. Making your reading and understanding. I insist again, it is not just about reading and understanding, it is also about modifying. Make your own modifications and tests, then compile and see the result. That will be the best programming master you can find. I assure you, as I always tell my students, reading and modifying code is the best computer class you are going to have ...

Not only will you be able to analyze the kernel of the operating system, but also other auxiliary elements that make up the complete operating system and also commands which you can run in the shell. It's a great way to learn from "inside" how those commands like ls, cat, cp, grep, echo, etc., that you execute daily, also with fairly well commented code work ...

Command source code - MINIX1

Lastly, I know a guy who has created a kernel made from scratch. His name is Luis and you can find his project at this address GitHub, another good study example that I would recommend. His system is called India, it is quite simple and perfect to acquire the basic knowledge. Who knows? Maybe you will be the next to dare to create a kernel ...

If you want to learn assembler or ASM, an interesting language along with C to program at a low level and, especially, for operating systems ..., there are also very interesting projects such as the case of Hummingbirds, a free system written in assembler. Being in ASM, it is much more complicated, but it also gives a good idea of ​​how machines work on a more intimate level with respect to hardware.

Tutorial: Print source code to study it on paper

PDF with colorful source code

If you are one of those who does not like to spend hours in front of a screen and gaze at it, perhaps you prefer paper. I'm going to show you a small tutorial on how you can print all these source codes that we have talked about or those that you want to analyze. So you can have a PDF with syntax highlighted ready to print on your printer and read on paper, where you can make your annotations and more.

To do this, the first thing will be install the necessary packages in our distro:

 

sudo apt-get install texlive-latex-extra latex-xcolor textlive-latex-recommended

Once we have these files, we will use LATEX To transform the source code into a PDF, and for this we must save this script, give it the execution permissions and execute it in Bash:

#!/usr/bin/env bash

tex_file=$(mktemp) ## Random text file name

cat<<EOF >$tex_file   ## Print the text file header
\documentclass{article}
\usepackage{listings}
\usepackage[usenames,dvipsnames]{color}  %% Allow color names
\lstdefinestyle{customasm}{
  belowcaptionskip=1\baselineskip,
  xleftmargin=\parindent,
  language=C++,   %% Change this to whatever you write in
  breaklines=true, %% Wrap long lines
  basicstyle=\footnotesize\ttfamily,
  commentstyle=\itshape\color{Gray},
  stringstyle=\color{Black},
  keywordstyle=\bfseries\color{OliveGreen},
  identifierstyle=\color{blue},
  xleftmargin=-8em,
}        
\usepackage[colorlinks=true,linkcolor=blue]{hyperref} 
\begin{document}
\tableofcontents

EOF

find . -type f ! -regex ".*/\..*" ! -name ".*" ! -name "*~" ! -name 'src2pdf'|
sed 's/^\..//' |                 ## Change ./foo/bar.src to foo/bar.src

while read  i; do                ## Loop through each file
    name=${i//_/\\_}             ## escape underscores
    echo "\newpage" >> $tex_file   ## start each section on a new page
    echo "\section{$i}" >> $tex_file  ## Create a section for each filename

   ## This command will include the file in the PDF
    echo "\lstinputlisting[style=customasm]{$i}" >>$tex_file
done &&
echo "\end{document}" >> $tex_file &&
pdflatex $tex_file -output-directory . && 
pdflatex $tex_file -output-directory .  ## This needs to be run twice ## for the TOC to be generated  </pre><pre>

After saving it to a file with the name pdf.sh, you can give it permissions and execute it in an easy way:

chmod +x pdf.sh
./pdf.sh

And the result will be a PDF with the text highlighted in color for a better reading of the source code files of the current directory. The name of the PDF document will be all.pdf. Now it will be ready for you to put it in your print queue to have it on paper.

By the way, the only problem is that the source code files The directory where you run this script that contain spaces in their names will not work with this script. So if there are any you can modify their name or alter the script to support them ...

Do not forget to leave your comments, I will be happy to answer any questions you have about it, or to listen to any feedback you have to improve the post. And I hope it has served as a guide to get you started in this world ...


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   slam said

    Very good post… !!!
    I only have a problem with the script, it votes for errors on line 5.

  2.   Javier said

    Hello, good night over here in Mexico, it would be good for you to continue writing through this medium and, if possible, continue filling with knowledge. On the other hand, thank you for dedicating your time and experience on which we are left half. My case that I had is that I left my career due to problems now I work to support myself. Little by little I am picking up things in this sense, really Total Thanks.

  3.   Angel Martinez said

    How do I get all the information regarding programming

  4.   Loveless isma said

    The problem with line 5 possibly (because I have not tried it xd yet) is because it is written like this:

    cat < $ tex_file ## Print the text file header

    when it should be like this:

    cat <$ tex_file ## Print the text file header

    Or so I imagine: U ...

    Greetings.

  5.   Loveless isma said

    Oh hell this time he did it right>: v

  6.   Gregory ros said

    Congratulations on the article, I loved it! Stop programming ago…. xD almost thirty years, countless times I considered taking it up again and due to lack of time and especially LAZINESS I always postpone it. I admit that the documentation that there is now is fantastic, a pity that most of it is in English, I know that now everyone wants to understand it, but my knowledge is limited to little more than the two dozen words used in programming. Is there a tutorial on the kernell in Spanish ?. I do not promise to resume programming, I left it too many times to believe I could do it now, but if there is something that I never lost, it was curiosity. Thanks for the article and best regards.