Showing posts with label UNIX / LINUX. Show all posts
Showing posts with label UNIX / LINUX. Show all posts

Saturday, 9 August 2014

Client Server Program in C to Simulate TCP Multicast Server


  This program simulates a multicast server in C. The following line of code sets the number of clients accepted by the server program. 

#define NC 4

Here the number of clients is set as 4. First run the server process, then run the client processes.

Click here to download the Client program.

Clcik here to download the Server Program.

Sunday, 27 July 2014

Client Server Program in C to Simulate Go-Back-N-ARQ


  This program was also written some 5 years before. This again is a simulation program trying to imitate Go-Back-N ARQ. It is a client server program. Click here to view the Wikipedia article on Go-Back-N_ARQ. Here also the server side acts as the receiver and hence you should run this program first. Afterwards you should also run the client program which acts as the sender side. 

Figure Below Shows the Receiver

Figure Below Shows the Sender


The line given below sets the window size for the sender and the receiver.

#define W 5 
If you want you can change the window size to some other value. Currently the window size is 5.

The following lines in the receiver side decides the probability with which a frame fails to reach the destination.

#define P1 50
#define P2 10

You can increase the probability of a frame being corrupted by increasing the value of P2 and decrease the probability by decreasing the value of P2. Currently there is a 20% probability for frame corruption.

Click here to download the Receiver Side program.

Click here to download the Sender Side program.


Click here to view the article on Selective Repeat ARQ.

Client Server Program in C to Simulate Selective Repeat ARQ


  I wrote these programs 5 years ago. It is a simulation program trying to imitate the working of Selective Repeat ARQ. Click here to view the Wikipedia article on Selective Repeat ARQ. A client server C program is used to simulate the working of Selective Repeat ARQ. The server acts as the receiver. The client acts as the sender. Because of this reason first run the receiver program and afterwards run the sender program. 

Figure Below Shows theReceiver



Figure Below Shows the Sender
  

The line given below sets the window size for the sender and the receiver.

#define W 5 
If you want you can change the window size to some other value. Currently the window size is 5.

The following lines in the receiver side decides the probability with which a frame fails to reach the destination.

#define P1 50
#define P2 10

You can increase the probability of a frame being corrupted by increasing the value of P2 and decrease the probability by decreasing the value of P2. Currently there is a 20% probability for frame corruption.




Tuesday, 21 January 2014

Creating C Dynamic Libraries in Linux


  Dynamic library is a relocatable object file with metadata. Dynamic libraries are linked at run time. They are built from one or more object files ( .o files). In Linux Dynamic libraries are called shared objects and the extension is .so. The name of a Dynamic library should start with lib. For example libtest.so is a valid Dynamic library name. 

Creating the Dynamic Library File

Let the name of the C Program file be p1.c,

  • Creating object files: Use gcc to create object files as Position independent Code as follows.

          gcc -fPIC -c p1.c 

  • Creating the Shared Object: 
          gcc -shared p1.o -o libtest.so

Storing & Linking the Static Library File

You need to change the LD_LIBRARY_PATH as follows,

 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/newpath

  • Store the dynamic library in the standard directory /usr/lib

          Compile & link the dynamic library file with target.c as follows,
          gcc target.c -ltest

  • Store the static library in a directory you have created. 
          For example in /usr/student/mylib.
          Compile & link the static library file with target.c as follows,
          gcc target.c -L/usr/student/mylib -ltest


  • You can also change the LD_LIBRARY_PATH as follows so that the compiler will search even the new path, 

          export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/student/mylib

          Now compile & link the dynamic library file with target.c as follows,
          gcc target.c -ltest


Thursday, 9 January 2014

Creating C Static Libraries in Linux


  A static library is a set of functions and variables which can be added to the target application at compile time. In Linux static libraries has an extension .a. Archive files are made up of object files ( Extension is .o ). The name of the static library should start with lib. For example libtest.a is a valid static library name

Creating the Static Library File

The command ar is used to create static libraries.

The procedure is described below.

Let target.c be the source file to which static libraries are to be linked.

Let p1.c and p2.c contain the functions to be added to the static library libtest.a.

gcc p1.c creates p1.o and gcc p2.c creates p2.o. 

The command to create the static library libtest.a is shown below,

ar -rcs libtest.a p1.o p2.o

Storing & Linking the Static Library File

  • Store the static library in a standard directory like /usr/local/lib.
            Compile & link the static library file with target.c as follows,
         
                     gcc target.c -ltest
  • Store the static library in a directory you have created. For example in /usr/student/mylib.
            Compile & link the static library file with target.c as follows,         
                     gcc target.c -L/usr/student/mylib -ltest

      The command nm can be used to find out the object files stored inside a static library file.

 

Tuesday, 24 December 2013

Sharing User Defined C Header Files in Linux


  In C/C++ header files are used to store common code so that they can be reused at a later stage. Header files are having an extension .h in C/C++. In this post I am discussing the different ways to share a user defined C header file in Linux.

1.  The first technique is to store the header file in the current directory along with the C program accessing the header file. In this case the header file name is included by using Double Quotes instead of angle brackets.

For example the name of the header file be myheader.h. This header file can be included in a C program as shown below,

#include "myheader.h"

2. Another technique is to store the header file in a separate directory and add the whole path while including the header file. Here also the include statement uses Double Quotes instead of angle brackets. 

For example the header file myheader.h is stored in the directory /home/myheaders. Then this header file can be included in a C program as shown below,

#include "/home/myheaders/myheader.h"

3. Finally if you are satisfied with the quality of your header file you can add it to the standard header directories in Linux. The two standard header directories in Linux are,
  • /user/local/include - This directory is used to store header files for third-party libraries.
  • /usr/include - This directory is used to store Operating System Header files.  
For example if the header file myheader.h is copied into one of the two standard header directories mentioned above, then the header file can be included in a C program as shown below,

#include <myheader.h>      

Here Angle Brackets are used in the include statement instead of double quotes. Hence the compiler searches for the header file only in the standard header libraries. 

Tuesday, 6 August 2013

Installing NAM on Fedora 17


  After installing NS-2.35 on Fedora 17 by calling the script ./install on the shell the following message is printed on the screen many often. "Please compile your nam separately". The message tells us that the NAM utility has not been installed properly. In order to install the NAM utility change the file xwd.c

The location of the file xwd.c is ns-allinone-2.35/nam-1.15/xwd.c. In the file xwd.c comment the following line of code as shown below.

// #include <X11/Xmu/WinUtil.h>

Save the file xwd.c.

Run the ./install script on the Shell again. NAM utility will be installed properly.

       

Installing NS-2.35 on Fedora 17


  Installing NS-2 on Fedora 17 only requires executing the ./install command on the Shell. But many often the Make process will fail. If the Make process fails then change the file ls.h.

The location of ls.h is the location of ls.h is  ~/ns-allinone-2.35/ns-2.35/linkstate/ls.h.

Open the file ls.h and change the code in line number 137.

The code in line line number 137 is  void eraseAll( ) { erase(baseMap::begin( ), baseMap::end( )); }  

Change it into the code given below,
void eraseAll( ) { this->erase(baseMap::begin( ), baseMap::end( )); } 

After making the change save the file ls.h.

Now again run the script ./install in the Shell. The installation will be complete.

Thursday, 28 February 2013

Step By Step Compilation of C Programs in Linux


  In Linux a C program is compiled using GCC (GNU Compiler Collection) Compiler System. If the name of the program is a.c, the program is compiled using the following command.

gcc a.c

The above command will generate an executable file named a.out.

gcc a.c -o pgm

The above command will generate an executable file named pgm.

But before generating the executable file the following actions are performed  by the gcc utitility.
  • From the C program file a.c a file a.i is obtained after preprocessing.
  • From the preprocessor output file a.i the assembler generates a.s which contains the assembler code.
  • From the assembly file a.s the object file a.o is obtained after compilation.
  • From the object file a.o the executable file pgm is obtained after linking.

If required gcc allows you to do these steps separately. 


Consider the program named a.c shown below.

#include<stdio.h>

#define MAX 10

int main(int argc, char **argv)
{
    int i;
    i=MAX;
    printf("\ni = %d\n",i);
    return 0;
}

  • The following command will generate the preprocessed file a.i from the c program file a.c.
gcc -E a.c -o a.i

Figure Below Shows the Preprocessed File a.i


  • The following command will generate the assembly file a.s from the preprocessed file a.i.

gcc -S a.i

Figure Below Shows the Assembly File a.s


  • The following command will generate the object file a.o from the assembly file a.s.
gcc -c a.s
  • The following command will finally generate the executable file pgm from the object file a.o.
gcc a.o -o pgm

  • The executable file pgm can be run by using the following command.
./pgm

Figure Below Shows All the Files Generated in the Process