Sunday, April 11, 2010

POSIX Thread programming in C

I have to work on a project for Network programming subject this semester. The project objective is to develop an application that acts as a TCP server on top of UDP socket. Hence our project should take care of everything a TCP server basically and internally does for us, right from three way handshake till 4 way handshake. While there is that, the server have to be concurrent and it should handle few attacks like xmas tree, syn flooding.

So for the concurrency in server handling, our team decided to go with thread instead of forking new process to handle incoming connections since thread is lighter than forking. But the real reason being is I have worked on process creation in Unix for some projects in Undergrad. But I never experimented with thread creation. So what is learning when there is no experimentation involved.

While I am at it, I thought I should also blog the things I learn from thread programming in C. These things usually tend to slip away with time. Hence the post. Now this post may contain very basic instructions but for me these are baby steps. So if you are looking for complex details, this is not the post you should be reading. Thanks.

Misc:

  • Good sources: source1, source2, source3
  • All thread programs need "#include " and while compiling use "gcc -pthread filename.c" to include pthread.h. Hence use IDE to write code but terminal to run the files.

Wednesday, April 7, 2010

Virtual OS

I never really appreciated the use of virtual OS softwares until I had to carry two laptops around all day just because the simulator I need for the network lab assignment can run only on windows. So here I am installing VMware in Ubuntu 9.10 followed by Windows XP and over it the famous OPNET. I am charting the steps for my future reference and to help if someone happen to stumble upon my blog. I am also chart out other miscellaneous things that I had to learn when I do this.

Miscellaneous:

1. How to find RAM on ubuntu ?

gnome-system-monitor &

This system monitor provides better visualization of the system resources and its usage ( Source)


2. How to run .rpm file in Ubuntu?

  • sudo apt-get install alien
  • alien -k name_of_rpm_file.rpm
  • dpkg -i file.deb
The "alien" converts the rpm file into its .deb equivalent. Then run the .deb file to install the package which we downloaded as .rpm file. (source)

3. How to remove non empty directory in linux?

rm -rf

This forces and does remove non empty directory recursively. Caution while removing root directories. (source)

4. How to run .deb file in linux?

dpkg -i name.deb

It installs the software from the package. (source)

5. What is difference between .rpm and .deb installations? which one is preferred?

RPM and DEB are both package types. While using RPM, we have to take care of all dependency between files in the installation process. In DEB it is taken care and we have to just use the above command and install the package. RPM can be very frustrating and time consuming. (source)


Main VMware installation: (source1) (source2)

1. Download .tar.gz file from vmware site and also register to get the license details.
2. Uncompress the file using "tar -zvxf VMware-server-2.0.2-203138.i386.tar.gz"
3. go to the directory "vmware-server-distrib" and execute "./vmware-install.pl"
4. It looks like we need to download the patches to install vmware separately. We can get that using "wget http://www.ubuntugeek.com/images/vmware-server.2.0.1_x64-modules-2.6.30.4-fix.tgz"
5. Uncompress the file using "tar xvzf vmware-server.2.0.1_x64-modules-2.6.30.4-fix.tgz" and execute the shell as super user using "sh vmware-server.2.0.1_x64-modules-2.6.30.4-fix.sh"
6. To avoid directory confusions, we need to remove a directory as super user using "rm -rf /usr/lib/vmware/modules/binary"
7. After installing the patch required, we should be able to run "./vmware-config.pl" command successfully.
8.Then follow the instructions in the installation.
9. Continue with the default values specified for the network connections, NAT creations.
10. After a lot of probing and poking, it will ask for the activation license serial number. You would have it in a link in the registration mail. Then follow the on screen instructions.
11. If everything goes successful, you will get the following screen


Starting VMware services:
Virtual machine monitor done
Virtual machine communication interface done
Virtual ethernet done
Bridged networking on /dev/vmnet0 done
Host-only networking on /dev/vmnet1 (background) done
DHCP server on /dev/vmnet1 done
Host-only networking on /dev/vmnet8 (background) done
DHCP server on /dev/vmnet8 done
NAT service on /dev/vmnet8 done
VMware Server Authentication Daemon (background) done
Shared Memory Available done
Starting VMware management services:
VMware Server Host Agent (background) done
VMware Virtual Infrastructure Web Access
Starting VMware autostart virtual machines:
Virtual machines done

The configuration of VMware Server 2.0.2 build-203138 for Linux for this
running kernel completed successfully.


12. Now we have to manage the server using firefox browser. In address space type https://:8333 for HTTPS and http://:8222 for HTTP.

13. After adding the certificate security, we need to login as root. If we don't have a password set for root, we can do it using "sudo passwd root". For this to happen, user should have administrator privileges.

14. Login using the newly set root password.

15. Use the interface to install new OS and continue working on OS over OS..

16. Create a virtual machine with options as Windows XP, NAT, 256MB RAM, 8GB memory, with CD and USB linked to host operating system.

17. Insert Windows(in my case) installation cd in the disc drive and when you power on the virtual machine, it would detect automatically and start the installation process. I noticed that this process was far more easier than installing windows on a real machine.

18. Install VMware tools to get good responsiveness.

19. Enjoy working on two OS seamlessly.

Happy Coding!!


LATER ADDED:

God it feels so good to have both the OS in one laptop and to swtich between them easily. Anyways I found one other excellent guide to do the same as I stated above. It just uses apt-get to install vmware server. But I prefer, when you want to learn, do it by downloading package and installing it. You will learn more than you signed up for. But in future when you are just reinstalling, you can just follow the simple steps as here. (source)





Monday, March 29, 2010

Art - Love of my life

Its been a long time, I posted something non technical and about my interests. Well, the title is pretty clear about what I am gonna talk about in this post. I always had a knack for painting and all the artsy stuffs. Given that nobody in my family's been interested in art, this is something that I can proudly say I immensely enjoy learning with an open mind. As one of my art teacher told me, art is all about having an open mind and different perspective on a scene. All this started when I very badly wanted to do something for school annual day competitions. I had a friend who was very good at drawing things as such, I guess I can say I may be inspired by him. When I won the drawing competition, I felt so happy and started doing more paintings. Till now I have done quite a number of drawings and few paintings. I love

  1. drawing cartoons:
  2. Glass painting:
  3. Oil painting:
  4. Coffee painting (Monochromatic painting):
  5. And ofcourse trying out pencil shading and other stuffs.
I did a lot of glass paintings in college which I gifted my friends and professors. Even last week when I talked after one year to my research professor for whom I gave a picture as our farewell gift, he said everybody loved it. These little things make me happy and content with my ability.
Recently since I am busy between chaffering Ovia to school and amma to shopping, working on MS projects and course work, I rarely find time to paint. But when I do, it becomes my world!!! Hope I would soon post many more of my work :)

Friday, February 12, 2010

How to send an email using mutt in ssh session in a Linux machine?

The process is simple.

1. install a ssh client in your machine. In a command line,
"sudo apt-get install openssh-client openssh-server"

Note: server is optional.

2. You will need a shell account. Its available for free from sdf.org. Just follow the instructions printed on the applet. After creating an account, logout from that account.

3. In the command line, type

"ssh @sdf.lonestar.org"

4. The connection will prompt for the password that you configured with the sdf account. Type it in and press enter.

5. If the password is right, you will have to configure your backspace key and then a crescent will appear and you can start typing your unix commands.

6. For sending mail, you need to open a mail client. I am using mutt. In the command line, type

"mutt"

It will open the inbox. The set of controls that are available will be listed on the top. You will have to setup the /Mail folder, just enter yes and the OS will take care of it.

7. To send a new mail, press "m" or to reply to a mail press "r". It will prompt for To: address and then Subject: After the subject, the pico text editor(may be a different editor) opens and you can type in anything you want. Then press "ctrl+o" to write it out and press "ctrl+x" to exit the editor. The screen will return to the compose mail page. Press "y" to send the message out.

You can explore about other options like adding cc, attaching a file, replying to a group, deleting the mail as you go.

Note: These steps are based on Ubuntu 9.10 OS. If there is a better option to anything I listed here, please share it in the comments field. Thanks in advance.

Tuesday, January 12, 2010

String reverse in Java

In SPOJ, there is a problem to take two integers and reverse them and find their sum and reverse the sum.

The catch here is when two large integers are given. In java there is a object to help you with reverse, for deleting a char at a index in a string and lot more. It is StringBuffer class.

But the solutions to the problems in C takes very less time and space than Java does. I am not sure how thats possible.

Factorial and their trailing zeroes

In SPOJ, there is a problem to find the number of trailing zeroes in the factorial of a number. Though the question seems intimidating, the solution is rather simple.

zeroes come from multiplying the number by 10. To find the number of times the factorial has been multiplied by 10, we can find the number of times 10 is a factor in the factorial expansion of the number. 5 x 2 = 10 is also possible. So instead of finding the factors of 10, we can find it for 5. All the powers of 5 like, 25,125,etc should also be considered to add number of 5 factors to the expansion.

Thus, to find number of trailing zeroes, keep adding the floor of (num/power of 5) until the division is less than 1. The reason why we ignore the reminders in the factors are we need only whole factors not partial ones.

For more detailed explanation see the Source

Monday, January 11, 2010

BitInteger Java solution for finding factorial

In SPOJ, there is a problem to find factorial for numbers between 1 and 100. Though the algorithm for the problem is very easy and simple, finding the right datatype to handle the factorial of larger numbers was not so easy. But the solution is however made simple by java.math.Integer

Amazing thing about BigInteger, the number of digits that it can hold is limited by the memory of the system. It would be unfair to the rest of people who are trying to find the solution if I post the solution here but I can sure help others and save the time I spent researching on this problem.

Follow this sample program: http://leepoint.net/notes-java/data/numbers/60factorial.html