Installing NS 2.35 in Ubuntu 12.04

Ubuntu 12.04 Precise Pandolin has been launched and as usual comes with good number of recent software and nice updates. Installation of ns2 over this Ubuntu 12.04 is as usual similar to Ubuntu 11.10, so the same steps hold good for 12.04 also.

The users can use Ubuntu 12.04 for quite some more time as it a LTS (Long Term Support) OS.  Here are the simple steps to install NS2.35 in Ubuntu 12.04

Step 1: Download NS-2.35 from http://sourceforge.net/projects/nsnam/files/allinone/ns-allinone-2.35/ns-allinone-2.35.tar.gz/download

Step 2: Copy the ns-allinone-2.35.tar.gz to the home folder (Example in my case /home/pradeep)

Step 3: There are some softwares need to be downloaded for running NS2, download those softwares and then untar or extract it and give the following commands one by one

sudo apt-get update        (This is to update the Ubuntu repository)

sudo apt-get install build-essential autoconf automake libxmu-dev     (This is to download the GCC compilers, TCL, Tk ,etc)

tar zxvf ns-allinone-2.35.tar.gz    (This is to extract the tar.gz file into a folder)

cd ns-allinone-2.35

./install    (This is to install)

Step 4: Once the installation is succeeded, you will shown to set the path information. set the PATH information in the following files /home/pradeep/.bashrc (You cannot see this file through GUI because it is a hidden file, so open the file using gedit or vi)

gedit /home/pradeep/.bashrc

and then edit the PATH and LD_LIBRARY_PATH

That’s it!!!!

Inter process communication using Shared Memory

Explanation:

One of the simplest interprocess communication methods is using shared memory. Shared memory allows two or more processes to access the same memory as if they all called malloc and were returned pointers to the same actual memory. When one process changes the memory, all the other processes see the modification.

 Shared memory is the fastest form of interprocess communication because all processes share the same piece of memory. Access to this shared memory is as fast as accessing a process’s nonshared memory, and it does not require a system call or entry to the kernel. It also avoids copying data unnecessarily. Because the kernel does not synchronize accesses to shared memory, you must provide your own synchronization. For example, a process should not read from the memory until after data is written there, and two processes must not write to the same memory location at the same time.A common strategy to avoid these race conditions is to use semaphores, which are discussed in the next section. Our illustrative programs, though, show just a single process accessing the memory, to focus on the shared memory mechanism and to avoid cluttering the sample code with synchronization logic.

 There are three flag values include while dealing with shared memory, these flags are supplied as the third parameter of the function

 int segment_id = shmget (shm_key, getpagesize (), IPC_CREAT | S_IRUSR | S_IWUSER);

IPC_CREAT—This flag indicates that a new segment should be created.This permits creating a new segment while specifying a key value.

IPC_EXCL—This flag, which is always used with IPC_CREAT, causes shmget to fail if a segment key is specified that already exists.Therefore, it arranges for the calling process to have an “exclusive” segment. If this flag is not given and the key of an existing segment is used, shmget returns the existing segment instead of creating a new one.

Mode flags—This value is made of 9 bits indicating permissions granted to owner, group, and world to control access to the segment. Execution bits are ignored. An easy way to specify permissions is to use the constants defined in <sys/stat.h> and documented in the section 2 stat man page.1 For example, S_IRUSR and S_IWUSR specify read and write permissions for the owner of the shared memory segment, and S_IROTH and S_IWOTH specify read and write permissions for others.

Program:

 

#include <stdio.h>

#include <sys/shm.h>

#include <sys/stat.h>

int main ()

{

int segment_id;

char* shared_memory;

struct shmid_ds shmbuffer;

int segment_size;

const int shared_segment_size = 0x6400;

/* Allocate a shared memory segment. */

segment_id = shmget (IPC_PRIVATE, shared_segment_size, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);

/* Attach the shared memory segment. */


shared_memory = (char*) shmat (segment_id, 0, 0);

printf ("shared memory attached at address %p\n", shared_memory);


/* Determine the segment’s size. */

shmctl (segment_id, IPC_STAT, &shmbuffer);

segment_size = shmbuffer.shm_segsz;

printf ("segment size: %d\n", segment_size);


/* Write a string to the shared memory segment. */

sprintf (shared_memory, "Hello, world.");


/* Detach the shared memory segment. */

shmdt (shared_memory);


/* Reattach the shared memory segment, at a different address. */

shared_memory = (char*) shmat (segment_id, (void*) 0x5000000, 0);

printf ("shared memory reattached at address %p\n", shared_memory);


/* Print out the string from shared memory. */

printf ("%s\n", shared_memory);


/* Detach the shared memory segment. */

shmdt (shared_memory);


/* Deallocate the shared memory segment. */

shmctl (segment_id, IPC_RMID, 0);

return 0;

}

 

Observation

The prorgam can be compiled by

$ gcc -o sharedmem sharedmem.c

Semaphore implementation using POSIX Threads

Aim: To illustrate the semaphores in Linux using C for handling two threads.

Requirements:GCC Compiler with Linux OS

Explanation 

 This program has two threads, one for reading the input and another for converting the text to upper case letter. However, the thread for converting the text will wait for the semaphore to be released before it starts the operation.

Program:

 //illustration of semaphores.

#include <stdio.h>


#include <unistd.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

#include <pthread.h>

#include <semaphore.h>


#define BUFFER 1024

 


sem_t sem;

 

char buffer[BUFFER];


void *read_thread(void *arg)

 

{

while(strncmp("stop", buffer,4)!=0)

{

printf("Enter text");

fgets(buffer,BUFFER, stdin);

sem_post(&sem);

}

pthread_exit("read Thread exit successful");

}


void *convert_thread()

 

{

int i;

sem_wait(&sem);

while(strncmp("stop",buffer,4)!=0)

{

printf("Converted text");

for(i=0;i<strlen(buffer);i++)

printf("%c", toupper(buffer[i]));

sem_wait(&sem);

}

pthread_exit("convert_thread exit successfully");

}


int main()

 

{

int result;

pthread_t rthread, cthread;

void *thread_result;

result=sem_init(&sem,0,0);

if(result != 0)

{

printf("Semaphore initialisation failed");

exit(1);

}

printf("enter text, the program will convert it into upper caswe, to stop enter 'stop'\n");

result=pthread_create(&cthread, NULL, convert_thread, NULL);

if(result != 0)

{

printf("convert thread creation failed");

exit(1);

}


result=pthread_create(&rthread, NULL, read_thread, NULL);

 

if(result != 0)

{

printf("read Thread creation failed");

exit(1);

}


result=pthread_join(rthread, &thread_result);

 

if(result!=0)

{

printf("read Thread join failed");

exit(1);

}

printf("read threaed joined, %s \n", thread_result);


result=pthread_join(cthread, &thread_result);

 

if(result != 0)

{

printf("Convert thread join failed");

exit(1);

}


printf("Conmvert thread joined, %s \n", thread_result);

 

sem_destroy(&sem);

exit(0);


}

Observation:

In this program, one thread reads the input and another thread converts the input string into upper case letters. This is repeated till 'stop' typed. The prorgam can be compiled by


$ gcc -o multithread multithread.c -lpthread

 

Multithreading Example using POSIX Threads

Requirements : GCC Compiler and any Linux OS with pthread Library

Explanation:

There are two threads in the following program which are implemented using function pointers, in which one thread is helpful for getting a input from the keyboard and other thread is helpful to convert it to upper case letters. But here both the threads does not depends on each other.

 That is, the second thread automatically converts the string to upper case letters itself, does not bothering about the first thread(the string entered through the keyboard). Two seconds delay is given to enter the string via the key board.

Program:

 

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

#include <pthread.h>

#define BUFFER_SIZE 1024

char buffer[BUFFER_SIZE];

void *read_thread(void *arg)

{

while(strncmp("stop", buffer,4) !=0)

{

printf("enter text:");

fgets(buffer,BUFFER_SIZE, stdin);

sleep(2);

}

pthread_exit("Read thread exit successful");

}

void *convert_thread()

{

int i;

while(strncmp("stop",buffer,4)!=0)

{

sleep(3);

printf("Converted text:");

for(i=0;i<strlen(buffer);i++)

printf("%c",toupper(buffer[i]));

}

pthread_exit("Convert_thread exit successful");

}

int main()

{

int result;

pthread_t rthread, cthread;

void *thread_result;

printf("enter text, the program will convert it into upper case, to stop enter 'stop'\n");

result=pthread_create(&rthread, NULL, read_thread, NULL);

if(result != 0)

{

printf("Read_thread creation failed");

exit(1);

}

result=pthread_create(&cthread, NULL, convert_thread, NULL);

if(result != 0)

{

printf("Cpnvert_thread creation failed");

exit(1);

}

result=pthread_join(rthread, &thread_result);

if(result != 0)

{

printf("read_thread join failed");

exit(1);

}

printf("read_thread joined, %s\n", thread_result);

result=pthread_join(cthread, &thread_result);

if(result !=0)

{

printf("convert_thread join failed");

exit(1);

}

printf("convert_thread joined, %s\n", thread_result);

exit(0);

return 0;

}

Conclusion:

In this program, one thread reads the input and another thread converts the input string into upper case letters. This is repeated till 'stop' typed.

The prorgam can be compiled by

$ gcc -o multithread multithread.c -lpthread

After observing the output the lack of coordination between the two threads is evident. Even if the input is not given from the keyboard, the thread does the conversion to upper case letter. So to avoid that, it is necessary to use semaphores.

Send email attachments to Dropbox

Have you ever wonder to send email that get stored in Dropbox. Here is a way to do it. http://sendtodropbox.com/. Create a dropbox account and connect the account with this website http://sendtodropbox.com/

Once you authorize dropbox to this website, you will be getting a email id something@sendtodropbox.com and just you can send email to this Mail ID. Automatically, the contents are stored in the "Attachments" folder inside dropbox. 

This will be a very nice application, if in case if your office does not have internet and you want to send information to someone else, you can send email and that gets stored in Dropbox.

Also if you are a teacher or professor, you can accept assignments, project reports to be sent to a email address and that gets stored in dropbox folder. Also there is a provision for a teacher to group the contents based on the students Registration number, categorize based on date of email or users email address.

Also if a small company want to hire employees, they can filter resumes based on the subject of the email. A Small example is given below

Example:

In the above image you can automatically unzip the contents of the email and as well you can categorize the emails like 

attachments / Date / Filename (so this is what the folder structure in Dropbox).

Try it!!!!

Store Gmail attachments in Dropbox or Box.net

Many of us will definitely have a gmail account one way or other. If you have a gmail account and if you feel you cant manage the attachments of your gmail account, here is the easier way to do it. 

Just visit http://www.attachments.me and create a account by giving access to your Gmail account, Please cofirm the authorisation and here you go, automatically all the attachments in your email will be started appearing in attachments.me. 

You can share, download, archive any attachment. Once done if you want to store that in folder there are two other ways, either you can download all the attachments in Dropbox or Box.net. Both are supported by www.attachments.me.

Click "Accounts" in the top right corner of your attachments.me page and in the right side menu, you can authorize BOX and Dropbox. Select as per your choice and give the credentials of dropbox and now you can see the attachments of your gmail is started appearing in Dropbox.

Once if it is in dropbox, you can get those files anywhere like iPhone, Android Phones, Desktops, Mac OS, etc.