Sunday, May 23, 2010

To-Do: Run a marathon

Just like every other Sunday morning, I started the hike in Mission peak. The trial never gets easy even if you hike them every week. The climate was awesome as usual. While we were climbing down the trail we saw the runners of 23rd Annual Ohlone 50K Wilderness Run

Its a 31 mile run for some good cause. I saw people from all age groups run for the cause. When I saw them, I added a new item to my bucket list[list of things I plan to do before life ends] and that is to run a marathon for a cause. And I really do hope I am able to do it not just once but almost every year. I will make it happen soon!! For now I will just add my small achievement of touching the mission peak here.





View from Mission peak


Looking back at the trail

Wednesday, May 5, 2010

PHP MySql POST GET Cookies Session - Bits and information

I always feel its easier to remember details if we learn it as differences between two objects. So I want to blog the basic differences between cookies and session, POST and GET method. And bits and pieces of information about UNION,UNION ALL and some more. This post has so many random information. Let me know if you find something wrong or different.

Three ways to move information across web pages:
  1. links: a link with href set to destination page.
  2. Forms: set action to point to target, method of how server should process information sent, encoding type optional. Both 1 and 2 requires user action like a click.
  3. Without any user action, user header in PHP to move to another page. header("Location: page.php");

Cookies:
  • Cookies are stored at the client's computer.
  • Once stored at client comp by an application, it can be accessed by any web page of that application.
  • It contains variable = value pairs of information.
  • Cookies are more useful for applications that has no database.
  • Cookies will be available from the next page and not in the current page where it was set.
  • In PHP, setcookie('variable','value'); sets the cookie and if you don't mention time, this cookie will expire when user exits the application.
  • On setting the cookie, it gets stored in one of many built in arrays of PHP called $_COOKIE. For accessing your cookie variable just refer the array with the variable name like $_COOKIE['variable']
  • If we want our cookie to be present even after user exits application then we have to mention the expiration time while we set the cookie. We can use two functions along with setcookie() - time and mktime. time() - returns the current time, we have to add the seconds for which the cookie needs to be alive. eg. setcookie('var','val',time()+3600) - to set cookie alive for 1 hour(60*60 seconds). mktime() - returns date and time and the order of arguments passed is hr,min,sec,mon,day, year. eg. setcookie('var','val',mktime(3,0,0,5,6,2010)); - to make it expire by 3am of May 6th 2010.
  • To remove cookies, use the same setcookie without any values passed. eg. setcookie('var'); setcookie('var','');
  • setcookie limitation: It has to be before any output is sent to the client browser. This limitation is with both session and cookie.
Session:
  • Session details are stored in a file at the server side. In Unix and Linux systems, in \tmp folder and in Windows folder called sessiondata. We can change the location of where the file is stored by chaning session.save_path value in php.ini.
  • Session ID - long nonsense number for every client which cannot be guessed or forged. In PHP, system variable is used for session ID - PHPSESSID.
  • Session ID is passed to every page to access the session details. There are three ways to pass them. 1. If cookies turned on, use the cookies. 2. For links, use the URL. 3. Use hidden variables for form with POST method.
  • Session variable is got and stored in $_SESSION built in array(similar to $_COOKIE array in cookies). They are accessed same way as in cookies.
  • For session to work we need to enable, track_vars while installing PHP.(default its turned on from PHP 4.0).
  • If cookie is turned off at the client side, trans_sid should be enabled to transfer session id. To enable that use session.use_trans_sid = 1 in php.ini.
  • Start a session: session_start() - If sessionID is found, then load $_SESSION with variables and their values. If no ID is found, then it is first time so create a new session and set PHPSESSID.
  • Save a session variable: $_SESSION['var'] = 'val';
  • Close a session: session_destroy() - destroys the session details.
  • session_id() - returns the PHPSESSID value - current session id.
  • unset($_SESSION) - unset the session details in the current page.
  • Limitation: session has to be set before any output is sent to the client browser.
We know that POST and GET are ways of indicating how the server should process the information sent by the form.

POST:
  • Sends information in 2 steps. 1. browser contacts form processing server specified in action. 2. Once contact has been established, send data to server in separate transmission.
  • On server side: 1. read parameters from a standard location. 2. After read, decode parameters before application use form variables.
  • To get post variables from earlier form use built in array $_POST.

GET:
  • In a single transmission, data is sent to the server. Data is appended by the browser to action URL.
  • This is the default method if not specified otherwise.
  • On server side, gets information passed at the end of the URL.
POST or GET what to use?
  • GET: best transmission performance(single transmission) and apt for small forms with short/few fields.
  • POST: Apt for forms with many/long text fields.
  • If inexperienced with server programming use GET to avoid extra steps of processing- read/decode as in POST.
  • For security purpose, use POST to avoid the information you transmit to be available in open for hackers to track. POST has security holes too but atleast it has encoding when transmitting.
  • To invoke server processing outside form tag, eg in a tag, use GET because it lets us use form-like parameters as part of URL.
ENCTYPE field in FORM tag:
  • Two types of enctype options available. 1. Multipart and 2. Text/plain
  • Multipart: forms with file selection fields for upload by user.
  • text/plain: used along with mailto in action attr of form tag. While sending forms to email server rather than a server.
  • The default encoding type: Internet media type Application/x-www-form-urlencode.
UNION and UNION ALL:
  • Union all - combines rows from multiple row sources into one result set. It includes duplicates.
  • Union - does the same thing as union all but excludes duplicates, result would be sorted here without duplicates.
Polymorphism in OO langauges:
  • Ability of 2 or more objects belonging to different classes to respond to exactly same message in different class specific ways.

Thread programming in Java

I spent a day in library refreshing stuffs I learned long back(not so long!) while I was in undergrad. Since my memory is fresh with these topics, I thought I should blog them so next time I can find it all in one place. On a different note, a day spent studying/reading in library is well spent! Okay back to Threads in Java.

Thread - class, Runnable - interface which has the thread implementations. For our application class to use thread we need to either implement or subclass or make the class a member class of our application. Thus three ways to create threads:
  1. Extend Thread class: For standalone applications that don't need to extend another class.
  2. Implement Runnable interface: For classes that need to extend other classes. Due to single inheritance, we can achieve threads by implementing the interface. Eg: Applets that extend JApplet and can't extend Thread too.
  3. Construct a thread passing an inner class that is Runnable: For threads with tiny run methods and little outside interaction.
Thread life cycle functions:
  • Thread() - When subclassing Thread class
  • Thread(name) - when subclassing + for debugging
  • start() - Begins process of assigning CPU time to a thread. Finally it results in calling run() method of the thread object.
  • suspend(), resume(),stop() - deprecated. Because these suspend and resume functions can cause deadlocks.
  • interrupt() - interrupt whatever the thread is doing
  • boolean isAlive() - returns true if thread is neither finished or terminated by call to its stop()
  • join() - to wait for a thread to finish
  • get/setPriority() - higher priority threads gets first chance of CPU
  • wait(), notify(), notifyAll() - semaphore and for thread synchronization.
  • synchronised - keyword - can be used for any object in Java. When used over an object or a method, threads will wait if one thread has already started running.
Thread synchronization:
The wait, notify and notifyAll methods allow any java.lang.Object to be used as a synchronization target.

wait():
This causes the current thread to block in given object until awakened by notify or notifyAll.

notify():
Randomly selected thread waiting on this object is awakened. Then that thread tries to regain the monitor lock. If wrong thread is awakened, will result in deadlock.

notifyAll():
All threads waiting on this object is awakened. Then all try for the monitor lock. Hopefully one of them succeed.

Best practices:
  • To avoid deadlock, use notifyAll to wake up waiting threads.
  • To avoid using deprecated method - stop, use the variable as a flag and use it to find if the thread is done executing.

Wednesday, April 21, 2010

Today's scribbling

I took a tamil book from Sunnyvale Library for my Mom but I ended up drawing the art that was on the cover! And of course I am reading the book too. Here is my reproduction of the art :)


While I was at it, I drew something for my niece Ovia too. Her favorite character of the bunch, Goofy.

There are lot more art on the book's cover, I will post them later!! Ciao.


Monday, April 12, 2010

Who said I can't touch Mission Peak?


Well, I have this habit of proving myself to be what I claim, when somebody challenges me. So thats what happened when my uncle said I can't hike all the way to the Mission peak and I did it. Its this mountain hiking place in Milpitas. My uncle hikes the place with his pals every Saturday. I am an outdoorsy person. But my uncle had hard time believing it and challenged me to touch the peak this sunday(April 11th, 2010). Hence started one of my memorable experience.

I used to go trekking every summer when I visit here while I was in undergrad. But that was just simple not-so-steep mountain climb. This one was longer and steeper than the usual place in Los Altos. We reached the place on time and started climbing. It was steep right from the foot of the mountain. To be honest initially it was tough, but not something I can't handle. We saw a "big" rabbit sleeping on the grass near the trail, light drizzle and so many beautiful scenes as we hiked. The peak of this experience is the peak itself. We couldn't have picked a day with such worse weather. Though it was so mild and fun at the bottom as we reached peak, wind was very strong. It literally pushed us through in some places, I am not exaggerating. The peak was so beautiful and was worth the pain and cold. The fun part was when we started climbing down. It was more difficult than climbing up because it was very difficult to put a break. If you loose a minute of your body control and let your legs loose you will snowball down to the bottom of the mountain, well may be not in good shape because the trail bends in place!!

It took 3 hours for the climb but as I said, it was all worth it. Now, my family agrees I am an outdoorsy person. I am planning to continue this awesome hiking every weekend in this summer!! But I seriously hope the wind becomes weaker and it is not so cold in the coming weeks.

P.S: I touched the Mission Peak proof is the picture above.

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)