Showing posts with label NP-Unix. Show all posts
Showing posts with label NP-Unix. Show all posts

20 April 2009

Unix Programming - Understanding a simple "C" Program

Post 3
We saw that a program is a passive entity which is sequence of instructions. The sequence of instructions implements a specific algorithm or logic for doing specific operations. Another important aspect of writing a program is segregating the program logic into logical chucks as functions. These functions make program modular so that the program can be easily understood. The program also consists of data and the primary aim of any program is to process the data and produce useful information. There may be different types of data required throughout the process execution. Some data might be required for the entire life of the process and some data may needed only for certain period of time. Each of the process has an address space and the data is stored in process address space. The data is referred by the code (instructions) using variables. Based on the nature of the data whether it required throughout the program or only shorter span of time in a function the variables can be classified into two main types - local and global. Let us discuss about various types of variables with the help of simple example. This understanding is very much essential before discussing process address space.

#include

int COUNT = 0;

void simplefunction(int c, int d)
{
printf("%d, %d", c, d);
}

int main()
{
int a = 1;
int b = 2;
simplefunction(a, b);
return 0;
}

In the above example, we have a variable with name "COUNT". There are two functions "main" and "simplefunction" each two variables. The variables "a", "b", "c" and "d" are called as local variable as their access is restricted the respective functions and the variable "COUNT" is called global variable as the access to global variable is available to all function. The local variables are also called as stack variables. The local variables have shorter life span and global variables exists throughout the life of the entire process. When a process is loaded in to the memory, these variables are stored in a specific region in the process address space, some of them are created when the process starts and some of them are created as and when they are executed and freed as soon as they go out of scope (that is whenever they are not needed).

In the next post, we will discuss about how a process is loaded into the memory, various regions in process address space and how a process is executed.

19 April 2009

Unix Programming - Getting the executable

Post 2

So, you now know what is a process from a layman's view. Process is a live entity which has some mission to be completed and it has some input and output resources and does some computing. These processes are run in computers. If you see the computers right from 1980s, there are many types of computers like super computers, desktop personal computers, laptops and even you have decent amout of computing being done in mobile devices. In all these devices, there are at least handful of processes to tens of thousands of processes running at the same time. If you look at these devices, they are quite different in application and quite different in assembling. For example, your desktop computer may have many hardware components including processor which does the computing. The way the computation is done may vary from processor to processor. So, each processor comes up with a way of getting things done. Each processor will have specific set of instructions, often referred as instruction set, using which the computation can be done.

Assume a scenario like this, you are writing a software that takes a year's time to write but you want that software to be run in most devices. For example, let us assume that you are much concerned about time management and you are writing a simple daily planner. Isn't it reasonable if you want it to be run in your desktop, mobile and even super computer? But if you write a software using instruction set of a specific processor, your software will not work in other devices which has different processor. In order to overcome this, computer scientist came with the concept of high languages like C and C++. These high level languages have specific syntax often expressed using English, special characters and scientific notations. A special software, compiler or interpreter, is used to convert the code written in high level language to machine language. So, now you have moved from writing software for specific device to writing software for whole bunch of devices and using compilers/interpreters to actually generate machine instructions from your high level language.

When you write a program in high level language, it cannot be directly run on your computer. As a first step, you have to compile it, link it and load it. We have seen what is compilation. But what really is linking? In your software, you will do some commonly used operations such as getting input from the user, reading a file, writing to a file and displaying output to the user. What you do with the input and how you process the input to produce output may vary from program to program but all the programs tend to have certain common denominator. Rather than writing code for this common denominator for each software you write, does it sound good to write the common code once and keep it for lifetime. Yes, it is in fact a brillant idea and the common code is called library. When you are writing software you will be quite often referring this common library. For example, in our "Hello, World", printf is a library function that outputs to standard output. Don't bother if you do not get what is standard output. We will uncover that in future. Time being, assume that it is your display. The concept of attaching your code with the common library is called linking. Only after compiling and linking, your code becomes fully functional and ready to be run. We call it as "binary" or "executable" or "exe".

So far, we have prepared a program and not yet executed it which is equall exciting if not more exciting. In the next post, we will again discuss about a simple program little bit deeper. I assure you, before end of next week, I will tell you about process address space :-). I feel that it is very important to know these details so as to get deeper understanding. I just don't want to 100th text book and 100001th webpage. I believe in quality rather than quantity and quantity makes sense when there is quality. Hope you agree with me.

Catch you later

17 April 2009

Unix Programming - What is Process?

Post 1

Today, we have so many programming languages and many software that have been created with those programming languages. It is surprising to see that an operating system design is still scaling from mini devices such as watches, washing machines to super computers and clusters. KISS, stands for Keep It Simple and Stupid, an acronymn given any piece of software that does one thing and does it well. It is not overstatement if I say Unix is a cornerstone in computing. There has been so many software that were produced but yet it is difficult to find one something like Unix. Unix is such a marvelous creation like the ones of Bethoven, Michaelanglo. The concept of Unix is simple - Process have life and files have space. Many operating systems have cloned the concept and design philosophies of Unix. If your Unix/Linux system is doing something, it should be combination of Processes and Files. In this post and few (not sure how many) subsequent posts, we will discussing more on Unix/Linux programming concepts  predominantly from user space and from kernel space as and when it is required. My intention is not cover the entire stuff in a single post, but rather I want to be consistent in posting short post. I feel, it is gives a sense of accomplishment to interested readers after reading few posts. It also gives better retaintivity as you can come back to short posts and quickly go through it which improves your short term memory. Without wasting much time, let us get into action.

What is a process? A process is a live entity, it is a program under execution. But when you generalize with such as definition, we tend to forget what really is a process. This may be an answer that you can give in an interview but not when you are trying to understand the system. It is the processes that move the system. Apart from being a program under execution, what is the process? A process is a black box that takes input, processes it and produces the output. It is an algorithm or group of algorithms that is running on the computer. When you say "taking input", what sort of inputs does it take? The input can be a data from a memory location, a stream of bytes from hard disk or network hosts, a signal from its fellow process or its parent and the input resources can be of any form. So, now you know that you need input. The next thing is how do you intend to operate on the input data. Apart from input resources, you also need a logic (code or machine instruction) that processes the data. Until now we have seen data received from input resources and code that processes the data both requiring some space. After you manipulate the data, you need store the data to a output resource or device. But generally, computing is not that simple. Before storing it to output device, you may need to have so many intermediate stages as a part of your algorithm. When you connect these things, you will get the process. A process is not just a program under execution but much more than that. A process is live entity that has address space, context, input resources (open files, database connection, sockets) and output resources (open files, database connection and sockets), state and so many other things in user space and as well as in kernel space. Not only user space, the process has also something in kernel space like per process area and data structures to access the input/output resources. Everything put togother is a process. It is not just code, it is much more than that.

Assume that you have given a billion dollar to answer this question. If you know "C" programming language or if you do not know "C" you can take any programming language. Can you give me a write up (you can to the comments section) on how the following program is executed in a Unix/Linux system. If you answer this question, I believe the size does not matter. You can work on super computers or high performance clusters. Here you go.
int main()
{
    char *str = "Hello, World";
    printf("%s", str);
    return 0;
}
In the next post, we will be discussing how this program is loaded into the memory and how the address space of this process is going to look like and then slowly move on to how this process is executed, how "Hello, World" is printed.

Catch you later.