How I learned Advanced Concepts in C while doing Projects

[image-source: quotefancy]

This is one of my favorite quotes of Dennis Ritchie which I totally agree. The first programming language which I learned was C back in my under-graduation. Yes, Professors teach C as the first programming language in schools. The learning environment is so ridiculous that we don’t know the purpose of #include<stdio.h>. All we know is those are the lines which are included at the beginning of the program. Some professors would like to teach efficiently by giving more real time examples but they are limited to their schedules and time constraints.

My first program in C was “Hello World “program. This is a simple program which every student starts learning to code. That is how I started coding too. Later, I learned the data types, variables, arrays, functions, pointers, structures, unions and so on. Theoretically, all those concepts were fine but I never understood the purpose of them. “What is main? , what is void? , What are the return values?” These are the questions which run in my mind. As a beginner, it is quite difficult to understand the concepts and their applications. At one point, I felt like “why are they teaching me the sum of two integers, Fibonacci series, Prime numbers, when I don’t use them in any real-time applications”. Those are the fundamental programs which help in understanding the concepts of loops, conditions etc. These programs help in developing our logical skills and problem-solving skills which build the base for solving some advanced solutions.

Later, When I moved into the advanced concepts of C like multithreading I found it difficult to digest them but still, I managed to clear my exam with distinction. It was during my masters I realized the importance of multithreading concepts without which no application is efficient.

It was because of these 2 projects where I learned the advanced concepts of C. My first assignment was to develop our own shell where we can execute simple Linux commands and to read a file and some words from the command line and to check if the words are present in the given input file. We must implement this using pipes. Ahhh pipes, an easy way to communicate between parent and child processes.

This is my own shell interpreter which runs until “exit” command is used.

At first, it took some time to understand to concepts of IPC, pipes, fork (), concurrent programming and synchronizing them. Later, when I started working on this project I got to know how a process communicates with another process using pipes, what happens when fork () is called, how to redirect from stdin/stdout to pipes. A child process is spawned when fork () is called. I used child process to execute the commands using execvp () system call. Parent process waits for the child process to complete using wait () system call.

Though my project was not successful, I am satisfied with the level of understanding.

My second project was to implement a Routing Information Protocol where 21 routers communicate with each other and generate their own routing tables. Basically, we started with 7 routers to make sure our program is working fine. Our first challenge was how can we define a table with 3 columns (dest_id, link, cost) in C, we started learning all the available data types. We came across Structures in C which is a collection of different data items. As we have to store 3 different data items with a single block of memory we thought structures will do the thing for us. We then initialized all the tables with local values and their neighboring nodes. We used pthreads to make it a multithreaded application. In order to achieve synchronization, we learned the concepts of mutex locks.

Finally, we converted the algorithm into a code. There are some bugs in it but we enjoyed while we programmed.

What I learned from my 2 failure C projects:

  • How parent and child processes communicate with each other using IPC techniques.
  • Some system calls like fork (), execvp ().
  • The working of concurrent program and thread synchronization.
  • Multi-threaded application with use of pthreads and mutex to lock and unlock.

I suggest my fellow students to do more and more stuff to understand the concepts of any programming language.

This is my first blog post. Thanks for reading and feedback is appreciated.

Happy coding