Pages

Subscribe:

Ads 468x60px

Tuesday, February 14, 2012

UCSC Going Down 2012

I am very proud to say that I am a  member of the Batch 06 or the Batch 2007/2008 of UCSC. The things we did for our UCSC was unique and many would not implausible. (I would rather say as the best batch in the history of the UCSC and our seniors would agree with me too of course .. :) ) There were many occasions and functions we did, UCSC Going Down is the tribute to our seniors who have given the guidance to come along this journey. 
UCSC going down was held on 11th February 2012 on the UOC premises. Our guys and girls made a great contribution to make that event a success. So here are some of the photographs of that event and all the credits goes to our photographer Dulini or we all known as "Lenchiii".
Preparing the decorations
Guys working hard
Guess who is working huh.. ;)
from left Samitha, Supun, me and Chathura
Stage decorations
Some souvenirs
Drama event
Keshan in action
My apologies that I can't put all the pictures here and I would like to specially thank all the people who helped us in many ways to make this event a success. 

Friday, February 10, 2012

MySQL C++ Connection Example in Visual Studio

Creating a MySQL database connection using C++ is  really easy. But for a beginner it can be really hard to find the correct procedure cause it will gave really troubles. At the first time when I'm doing it it took me quite some time to make it work. So I'm going to explain how to make a connection to the MySQL database using C++ code. We use the library called boost to make the connection. 

Step 1 (Download the boost library)
First step is to download the boost library. You can download it from here from the official download page Or you can download the one I've compressed and uploaded which is smaller in size from here.

Step 2 (Create the project using Visual Studio 2008)
First go  to File --> New --> Project and you'll get a screen like this. 

mysql c++ connection example

Select Visual C++ --> Win 32 --> Win 32 Console Application  and give a name to the project and press OK. 
Then set Application Type to Console Application and remember to tick the empty project. and click finish.

Then Right Click the project and add new Item a cpp source file called Connect.cpp.

// Standard includes
#include 
#include 
#include 
using namespace std;

// Connector Includes

#include "cppconnection/driver.h"
#include "cppconnection/exception.h"
#include "cppconnection/resultset.h"
#include "cppconnection/statement.h"


// Link to the Connector/C++ library
#pragma comment(lib, "mysqlcppconn.lib")

// Connection details
string server   = "localhost";
string username = "root";
string password = "123"; // 

int main(){
    sql::Driver     *driver; // MySQL Driver Object
    sql::Connection *connection; // MySQL connection Object
    sql::Statement  *statement;   // Statement which holds SQL commands
    sql::ResultSet  *resultSet;    // ResultSet to hold the results
    //Here is the connection
    try{
        driver = get_driver_instance();
        connection = driver->connect(server, username, password);
        statement = connection->createStatement();
        statement->execute("USE performance_schema");
        resultSet = statement->executeQuery("show tables");
        while (resultSet->next()){
            // Iterating the result set
            cout << resultSet->getString(1) << endl;                
        }
    }
    catch (sql::Exception e){
        cout << "Error message: " << e.what() << endl;
        system("pause");
        exit(1);
    }

    //Clear resources
    delete res;
    delete statement;
    delete connection;

    system("pause");
    return 0;
}

Copy the boost folder into the project folder. In this example the folder is Visual Studio 2008\Projects\MySQLConnection\MySQLConnection folder.

Then download this header and dll files and extract them into the same folder Visual Studio 2008\Projects\MySQLConnection\MySQLConnection folder.

Then the final step is right cilck on the Project and go to 
Properties --> Configuration Properties --> C/C++ --> General . 
On the right hand side set the "Additional Include Directories" value to ".\" value. (with out the quotes)

That's it build your project and run the project. And one more final thing when you run the project set the  "Solution Configuration" to Release mode. In the Debug Mode I had some error which I still don't no why.

So that's it now I think you got a clear idea about creating a connection for MySQL using C++ in Visual Studio 2008. See you around with another exciting post... :)

Thursday, February 9, 2012

What are these System Calls?

What are these System calls with respect to Operating Systems?. More simple definition I could give you is that System Calls is the interface between the Operating System and the User Programs. These functions may be vary with respect to the Operating System Type. Systems calls are machine dependent and in Unix Systems System Calls are written in assembly language and there is a library available to initiate those functions within a C program.

When we run our programs mostly in user mode, the programs may need a System service such as writing some buffered data to a file. Then the execution of the User Program put into a TRAP mode and the control is transferred into the Operating System. Then by examining the method called, the kernel will handle the necessary procedures and return the control to the User Program by sending the relevant message.  

Here is a example for calling the write data from a buffer into a file.
             n = write(fd, buffer, nbytes)
Example write System call
Lets examine this example write system call. [1] , [2] and [3] step shows that the parameters of the write method is pushed into the stack. See the order which they are pushed into the stack. The rightmost parameter is pushed first followed by others from right to left. 
*Reason behind this is that in earlier days when printf method is called the format of the string appears on top of the stack.
Then it comes to the call write and it will execute the library procedure for the write [4]. Then step [5] is to put the system call number for write in a register. Step [6] shows the execution of the TRAP instruction to switch modes, from USER MODE to the KERNEL MODE. Then in the KERNEL MODE it examines the system call number and  the relevant system call handler will be pointed via the table indexed all the system calls and the system call handlers [7]. In this moment [8] system call handler runs.
Once the execution completed the control is returned to the USER MODE library procedure [9]. Then the library procedure returns to the user program [10]. To complete the execution it will clear the stack [11].

That is the typical  life cycle of a system call. In POSIX systems there are around hundred system calls. System calls are used in various things Process and File management etc.

Hope you got some knowledge in system calls.. See you next time :) 

Dependency Injection Simplified

Dependency Injection or Dependency Inversion is a design pattern to decouple objects to overcome the dependency relation in objects. Say there are objects tied to each other for example there is a Drawing application.  

Lets say there are two Circle and Triangle Classes which has a draw method in both classes. So when ever I want to use the draw method in my application I have to call the method as below specifically saying the object type.

    Circle circle = new Circle();
    circle.draw();
    Triangle triangle = new Triangle();
    triangle.draw();

Using Polymorphism

Polymorphism Example
Introducing Shape interface or the parent class called Shape, both the draw methods in the Circle and Triangle Classes will override the draw method in the Shape Class.

Removing Dependency (Method Parameter)

To remove the dependency we can introduce a drawMethod() which will take shape as the parameter. Since both Circle and Triangle classes are inherited the dependency have been removed.

   public void  drawMethod(Shape shape){
      shape.draw();
   }
What ever object pass into the drawMethod() it will draw the shape accordingly. But still we haven't fully solve the problem.

 Removing Dependency (Using Drawing Class)

public class Drawing{
         private Shape shape;

         public void setShape(Shape shape){
                  this.shape = shape;
         }  
        public void drawShape(){
                  this.shape.draw();
        }     
}                                                                

Now the advantage of having this Drawing class is that it if fully independent from the type of the Shape. As long as the passed variable is inherited from the Shape Class. The idea is you are separating all the dependency out of a class. If the object you have to draw you don't have to care because the class Drawing has no idea of the type of the object which you are drawing. Drawing class doesn't holding the relationship. Dependency is injected through a different class to the Drawing class.  

So to use the Drawing Class

      Triangle triangle = new Triangle();
      drawing.setShape(triangle);
      drawing.drawShape();

Dependency is not hard coded to the class. It is actually injected by an entity outside the class. The Spring Framework makes it very easy to use this dependency Injection so before hand it's better you understand these concepts properly.



Tuesday, February 7, 2012

UCSC Research Seminars for 4th Year Undergraduates



UCSC Research Seminar is actually a compulsory subject for 4th year Computer Science undergraduates. Every undergraduate has to prepare a presentation on a research paper, and two undergraduates will present  per week. The objective of this Research Seminar is to wider the knowledge of the students and improve their dexterity in presentation skills.

W. G. D. M. Watugala Sir is the coordinator of this lecture series and he is doing a excellent job on organizing the event. Every two candidate will announce the paper that they are going to present two weeks prior their presentation date. So the audience (most probably current 4th year undergraduates and lecturers) can read and study about the paper. At the end of the presentation we are having a nice and descriptive discussion to clarify the important things in the papers.  

I my self also presented a research paper several weeks ago. Which is  "Tamper Detection in Audit Logs". The author of this paper is Richard T. Snodgrass and he is a professor at the University of Arizona. You can download the paper using this LINK. The experience which I gained was remarkable. Sometimes I have to depend some tricky questions thrown by the audience. It was pretty much hard to provide a better answer such shorter time. But I think I managed it really well :) .

We thought that we can involve public crowd for the seminar series. So now you also can participate in this event visiting this facebook page. Join the discussion and perceive what's out there :)

       

Latex for Total Beginners

According to wikipedia  LaTeX  is a document markup language and document preparation system for the TeX typesetting program.
After Searching the internet I was not able to find a proper way to prepare the working environment for work with late, Though there are enough tutorials on how to use latex.  So lets see how to prepare the working environment for latex.

Step 1
  1. Download Miktex Setup From this url http://miktex.org/2.9/setup 
  2. Then install the setup which is pretty much straight forward task 
Step 2

You probably need a better text editor for process tex documents. It's actually like programming, you can go with the harder way by just using notepad or you can go with the east way using a better text editor.

There is a wide range of text editors such as Winefish LEd LyX Kile. They all much do the same and there are some small differences. All the latex editors as far as I know are not WYSIWYG editors. So each time you made a change you have to compile and build the pdf document to see how your document looks like.
So my personal recommendation is Texmaker. It is available for all the platforms Windows, linux and mac. So lets see how we create documets with texmaker editor.
After Downloading the Texmaker setup from the Download Page install it to your computer.

Creating a tex document seems to be little hard at the beginning. Once you have playing a while you'll get to know once you familiar with the tags. Because it's like programming in HTML. 

To check whether your installation was successful let's make a simple tex document. 

/*Taken from wikipedia*/

\documentclass[12pt]{article}
\usepackage{amsmath}
\title{\LaTeX}
\date{}
\begin{document}
  \maketitle
  \LaTeX{} is a document preparation system for the \TeX{}
  typesetting program. It offers programmable desktop publishing
  features and extensive facilities for automating most aspects of
  typesetting and desktop publishing, including numbering and
  cross-referencing, tables and figures, page layout, bibliographies,
  and much more. \LaTeX{} was originally written in 1984 by Leslie
  Lamport and has become the dominant method for using \TeX; few
  people write in plain \TeX{} anymore. The current version  is
  \LaTeXe.
 
  % This is a comment; it will not be shown in the final output.
  % The following shows a little of the typesetting power of LaTeX:
  \begin{align}
    E &= mc^2                              \\
    m &= \frac{m_0}{\sqrt{1-\frac{v^2}{c^2}}}
  \end{align}
\end{document}
 
Now create a new document in Texmaker and copy this code to the editor. Then save it as a .tex document for example example.tex. 
Texmaker Editor
Then press F1 to quick build your document. In the View Log you'll see Process exited normally. which means your tex document build is success. If there is an error you'll probably see the errors in the View Log.
Then press F7 and you'll see your well formatted latex document. 

Now you know how to work with latex. Learn how to format your  documents :)

By the way  this is a fine tutorial for learn latex tags and attributes.

Syntax and Semantics of a Programming Language


Syntax of a Programming Language

The  Syntax  of  a  Programming  Language  is  the  set  of  rules  that  defined  in  that programming language in order to express how the combinations of symbols are formed.
Syntax is differed into two main categories.
  • Abstract Syntax  -->     Provide definition for the constructs
  • Concrete Syntax  -->       Provide definitions of the form of the constructs
E.g. In Java while loop is defined as
While(booleanValue){
//Statements goes  here
}

Semantics of a Programming Language

Semantics reveals  the  meaning  of a  syntacticallcorrect  programming  language  in expressions, statements, program units. Semantics of a programming language also can be divided into two different categories.


Static Semantics

For compiled languages static semantics are usually tested at the compile time.

E.g. checking that every identifier is declared before it is used (in languages that require such declarations)

Dynamic Semantics


As the above classification there are three types of Dynamic Semantics. Dynamic Semantics are usually tested  while at the runtime. Dynamic Semantics of a Programming language defines  how and  when the various  constructs of a  language should  produce a program behavior.

e.g In java
if(a==b){
return;
}

Meaning of this statement is derived by executing this program on a computer. (This is what we called operational semantics also in the same category of Dynamic semantics)

Ambiguity of a Programming Language

If a statement constructed by using a particular grammar has more than one parse tree; it is said to be that grammar is ambiguous.

E.g. Simple example

<E> --> <D> 
  <E> --> <E>
  <E> --> <E> + <E> |<E> - <E> |<E> * <E> |<E> / <E>
  <D> --> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Lets consider statement  1+2*5  for the above grammar




Above grammar is ambiguous since it has two parse trees and the left derivation tree give the answer 11 which is correct and the right derivation tree gives 15 which is incorrect.