C Under Linux

Linux under C

C Under Linux : Linux is a clone of the Unix operating system. Its kernel was written from scratch from scratch by Linus Trovalds with assistance from a lossely-kint team of programmers across the world on Internet. It has all the features you would except in a modern OS. Moreover, unlike Windows or unix, Linux is available completely free of cost. The kernel of Linux is available in source code form.

Under Linux one is faced with simply too many choices of Linux distributions, graphical shells and managers, editors, compilers, linkers, debuggers, etc.

Linux Distribution- Red Hat Linux 9.0
Console Shell – BASH
Graphical Shell – KDE 3.1-10
Editor – Kwrite
Compiler – GNU C and C++ compiler

C Programming Under Linux

It is same to the extent of using language elements like data types, control instructions and the overall syntax. The usage of standard library functions is also same even through the implementation of each might be different under different OS. For example, a printf( )
would work under all OS, but the way it is defined is likely to be different for different OS.

The ‘Hello Linux’ Program

As with any platform, we would begin our journey in the Linux world by creating a simple ‘hello world’ program. Here is the source code…..

#include <stdio.h>
int main( )
printf(“Hello Linux\n”);
return 0;

The program is exactly same as compared to a console under DOS/Windows. It begins with main ( ) and uses printf( ) standard library function to produce its output. The difference is in the way programs are typed, compiled and executed.

Parent and Child Process

As we know, our running program is a process. From this process we can create another process. There is a parent-child relationship between the two process. The way to achieve this is by using a library function called fork ( ). This function splits the running process into two processes, the existing one is known as parent and the new process is known as child.

Here is a program that demonstrate this…..

# include <stdio.h>
# include <sys/types.h>
int main( )
printf (“Before Forking\n”);
fork ( );
printf(“After Forking\n”);
return 0;


Before Forking
After Forking
After Forking

One Interesting Fact:

When we use fork ( ) to create a child process the child process does not contain the entire data and code of the parent process. Then does it mean that the child process contains the data and code below the fork ( ) call. Even this is not so. In actuality, the code never gets duplicated. Linux internally manages to intelligently share it. As against this, some data is shared, some is not. Till the time both the processes do not change the value of the variables they keep getting shared. However, if any of the process attempt to change the value of a variable it is no longer shared. Instead a new copy of the variable is made for the process that is attempting to change it. This not only ensures data integrity but also saves precious memory.

Recommended Posts

Spread the love

Leave a Comment

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