Sunday, December 6, 2009

GCC - How To Create a Static and Dynamic Libraries/ Characterstics of Static and Dynamic Libraries/ Using Static or Dyanmic Libraries

Topic is called writing libraries.
I will be covering creating and using static and shared libraries. (Yes using too, because that is the main part)
So here is simplest you can do
I will write two files containg a function each, write one header file and use those function in a prog.
Go into some directory
create a dirctory called "incl" this is the place where your manual created header files will be searched.

go to "../incl" create a header file called "myheader.h" and write folloing lines there

#include"stdio.h"
extern void one();
extern void second();

save it... Thats it, your header file is ready.

Now the second part is to create library......Ok there are 1001 documents on net which explains you what a lib does and all that blah blah.. I will just focus here how to create a library.
First I am going to create a static library.

come to base dir and create a dir called "mylib"
Go to mylib dir and create two .c files

first.c
return type one()
{
print("\nThis is from the first libraray\n");
}


second.c

return type second()
{
print("\this is from the second library\n");
}


now just compile these files ( you can not create a executalbe as there is no main funciton here)

gcc -c first.c -o first.o
gcc -c second.c -o second.o


Now you have two objects file with you.
You want to create the static lib.
Ok to be very frank static lib is nothing but puting the all the concerned object files in a single file.
Nothing more. This is done by

1) ar rcs libfirstlib.a first.o second.o

This command is just like ar (archiving the many object files into one )

Now few points to be noted here, your lib must start with "lib" prefix and extension for static is .a.


2) To just make indexing fast in the lib, you CAN (that is it may or may not be needed lib will work even without it)

ranlib libfirstlib.a


Thats it your static lib is ready.



Now comes the part of using this lib and header file into your program.
Come to the base lib again

//use.c
include file "mylib.h"
include file
main function come here()
{
one();
second();
prin("\nBoht of the lib funcitons called .....\n");
}


Write one program here using the lib function and include the header file you have crated.

Ok the thing is simple your compiler and linker don't know anything about where is your .h file which you have included in your program.. nor linker will know anything about the fucntion defination.

So how to do it.

Here is the logic

when compling and linking ( as you know " gcc file.c" is equivalent to compling and linking if you do not want to link you use "gcc -c")..you need to give the header file and lib path where preprocessor and liker will look for.

By default preprocessor will look in some directory (defined differnetlly form sys to sys)
Gen it is /use/include

and lib in /use/lib

Now suppose you do not have premission to write there and location of you header file and lib is different then use this.


gcc -I ./inclu/ -L ./mylib/ -static use.c -lfirstlib


Thic command will do.

-I tells where to look for a header file

and -L about the lib file.


Note you can also copy .h to /usr/include and .a to /usr/lib dir it will work.


So I explained all the synario here about static lib.



Now its turn of shared library. And guess what Shared library I have created is not working. Will post this some time later.

Very Useful links to read are mentioned below

http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html

http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

http://www.gpres.org/tutorials/shared.xhtml

http://gcc.gnu.org/ml/gcc/1999-12/msg00092.html

http://www.iram.fr/~roche/code/c++/c++_AddNumbers.html

http://www.linux.org/docs/ldp/howto/Program-Library-HOWTO/shared-libraries.html

http://randu.org/tutorials/c/libraries.php

http://randu.org/tutorials/c/

Tuesday, December 1, 2009

Semaphores

Semaphores

When They are used
Semaphores are used for two purposes
1) Process Synchronization
2) Critical Section problem

Is there any alternative to Semaphores
Yes there are few solutions know as n process synchronization(software solutions) and Hardware solutions. Hardware solutions becomes complicated when number of process increases and secondly in all these you have busy waiting (it means you are eating CPU cycles when waiting and no other productive work can be done during this). So practically they are never used

What are they
They are synchronization tool. In a very abstract term semaphore is a global variables, which apart form initialization can be modified by only two operations wait() and signal().

Suppose S is semaphore
where wait is as simple as
wait(s)
{
while(s<=0)
; //do no operation
s--;
}

signal(S)
{
S++;
}

Why not Global Variables
Now if this was the only criteria you can simply use global variables and can write above mentioned functions in your C code to create semaphores. But its not that simple in addition to above things we need to ensure
1) Modification of S value must be done exclusively.
2) In addition checking of while(s<=0) in wait and possible modification S--, should also be done without interruption
3) Above implementation is having busy wait too

Example of Usage

1) n process critical section problem
put every process code like this
do {
wait (mutex);
critical section;
signal(mutex);
reminder section;

}while(1);

2) Synchronization
we can also use this for synchronization. e.g two concurrent process P1 and P2 having statements S1 and S2. We want in any case S1 should execute first. this can be achieved easily by
initialize S=0;
in P1 in P2
S1; wait(S);
signal(s); S2;

Implementation

The very basic implementation which I showed above has one problem they also have busy waiting. This is not at very appreciated in multiprogramming environments where wasting CPU is crime equivalent to murdering someone. These type of semaphores are called the spinlock semaphores.

But these semaphore have advantage too. There is no context switch required when process wait on Semaphore. Hence if waiting time is minimal this approach is very helpful.

How to overcome busy waiting.
you can modify wait and signal. when every you have to wait instead of looping you can block the current process. Bock is very common system call present in every Operating system.
in signal you simple call wakeup() for that process.
You also have to make sure that you maintain queue for all blocked process (i think list of PCB will do it)
There is one more important point on wakeup you are simply putting it on ready queue of Short term (or CPU scheduler).. its up to CPU scheduler scheduling policy when that process will be executed.


Critical Section Problem of Semaphore implementation
This is critical aspect of semaphore implementation that they are executed atomically. We must guarantee that no two processes can execute wait and signal operations on the same semaphores at same time. This is called critical section problem and can be solved in 2 ways

1) In uniprocessor environment we can simply inhibit (disable) interrupts during the time that wait and signal operations are executing. This works very well in uniprocessor env because, once interrupts are inhibited, instructions from different processes can not be interleaved.

2)In multiprocessor env inhibiting of interrupts does not work. Instructions from different processes many be interleaved income arbitrary way. We can employ any software solutions (not discussed here) for critical section problem, where critical section consists of wait and signal procedures(remember there it was modification of files, global variable etc). It must be admitted here that we are still using buy wait here but at least that is only on wait and signal and not on full process.