Saturday 11 February 2012

command line programming in c

There are methods to interact with a program in command line itself,by command line i mean the terminal by which the program can be executed.Is it possible to supply arguments to a program in the command line ?
YES !,


we first learn to compile and run a program using gcc

say you have a program hello.c


to compile we use                       gcc -c hello.c or a simple gcc hello.c would do


The above command creates a default object file by the name a.out so if you wanna execute the program you need to      ./a.out and hit enter

but if you want to give your name to the object file then you can do so by
gcc hello.c -o yourname
the program then can be run by  just  ./yourname





NOW FOR THE COMMAND LINE ARGUMENT PASSING

lets say you made a object file name by using   gcc prog.c -o name

now to pass arguments to this program prog from the command line ,The prog program would have


int main(int argc,char * argv[])
{   .......
    ........
    ........
}

int argc indicate how many arguments are passed and argv will store the arguments

that is if you want to pass a alist of arguments to your prog program with object name then


          ./name prog.c 25 wow! 56.78 this$is$a$string

now observe the syntax i wrote ./object (name of the program ie prog.c) (argument list)

the argument list is seperated by integers and the first is an integer .2nd a string ,3rd a float and the 4th is a string but i have used delimiters such as $ to indicate space..and its your job to replace the $ with space inside the program.
you could have used any other delimiter apart from $

now inside the program the argc is holding value 5

and argv[0] = "prog.c"
argv[1] = 25
argv[2] ="wow!"
and so on

Thats about it ,this provides a simple enough usage of command line argument passing

Tuesday 7 February 2012

anatomy of Operating system

The operating system is used for maintaining and managing the resource of the system,something like an administrator maintaining the resources.
There typically exists two spaces for usage namely the kernel and user space,this allows the OS to differentiate between reliable and non reliable processes.The reliable process being the system processes which are run in kernel mode ,thus these processes have access to hardware and various resources.
The user processes such as those which you use,like a browser for instance are in user space as these are not reliable.They may contain malicious codes or may contain bugs.These user processes are run in user space (There exist separate virtual memory for user and kernel modes processes) Thus eventually for these user processes to run they need to be mapped to kernel processes .There exist various schemes of mapping wherein many user processes can be mapped to a single kernel process or they may be mapped to many kernel processes.
The kernel is just the core constituent of the  OS ,This provides to each of the application which may contain multiple threads (threads are something like a small process or small app) thus each user application can only run when the kernel provides these threads.
However what would happen if the user thread (an application is composed of set of small threads each doing some stuff as was earlier declared) gets blocked (if a thread requires access to resources say a printer but if the resource is being used by another ,then it got to wait or get blocked) ?
The kernel by means of a mechanism termed the "upcall" will indicate to the user application that a thread is being blocked and also allows the kernel thread which was mapped to the user thread ,to be used for some other user thread which can be run!
The reason of kernel and user space is ,to reiterate it safeguards the resources so that the OS can allow all privileges to the recognised and reliable processes which are termed the kernel processes and those which it is not sure of is implemented in the userspace
Generally all the system process such as scheduler,dispatcher etc are the kernel processes and the normal applications such as the time display,GUI apps etc are the user processes.




so in summary for a easier understanding of operating system ,consider the existence of two domains ,the usersmode and the kernel mode..All the important ,reliable processes which are responsible for critical system performance are kept as kernel processes and those which are not are the user processes .
Thus if a user process becomes unstable then it just gets hanged and you may need to do the alt+ctrl+del to kill it.But if any of the kernel process gets corrupted then you the whole operating system come to a halt and the only option is to reboot and install a new OS !!!!
so to avoid all the user process to directly get hold of hardware the OS will map the user process to kernel process and this kernel process actually runs in the system.