Saturday 28 April 2012

Linux Kernel Mode


There are 2 modes in Linux: Kernel mode and User mode. Kernel mode is at a higher privilege level than user mode. All user programs and the applications running on the machine run in user mode. Only the kernel executes in kernel mode. A program executing in user mode can’t access kernel code, so can’t directly call system calls. This is done for security purposes because if a user program can access the kernel code, he can change it or access the memory which he should not access. So, the kernel assigns itself the highest privilege level: kernel mode. User processes are at the least privilege level: user mode. The kernel is protected by hardware, because programs executed in user mode can’t access the memory that belongs to programs executed in kernel mode.

This protection by hardware has a problem. User processes can’t access any feature provided by the kernel. In other words, user processes can’t make use of the system calls provided by the kernel. Again to cope up with this problem we go take help from the hardware. The only possible way to use system calls is to escalate the privilege level of the user process. So, we make use of interrupts, in this case software interrupt. A software interrupt can be seen as a special jump instruction whose target address is restricted by the kernel. The kernel sets the target address of the software instruction to the special routine that handles the system calls. Now suppose the user wants to use a system call. First of all what is done is that the code corresponding to the system call is put in a register for the kernel to understand which system call the user wants to call. The task of setting the system call code is done by the library procedure and is hidden from the user. After setting the code for the specific system call, a trap to the kernel takes place. Then the dispatcher in the kernel calls the specific system call using the code set in the register. Then a context switch takes place in which it saves the content of the registers of the user program. Then finally the control passes to the kernel procedure which performs its task.

But there is another side of the story. The system call by this hardware approach can become very slow because the software interrupt and the context switch, which takes place in the kernel require heavy and complex operations. This can be gauged by seeing that a system call is on an average about 30 times slower than a simple function call.

So the next question which comes to mind is how to accelerate system call? The obvious way to do this is to execute the user process in kernel mode. Then no software interrupt and context switch is needed. System calls would be just like normal function calls because the user process can access the kernel directly. But this is not as easy as it sounds. There are a lot of issues with this approach, foremost of which is security.

Sunday 8 April 2012

SOAP protocol

SOAP is a protocol for accessing a Web Service. SOAP is a simple XML-based protocol to let applications exchange information over HTTP. SOAP is designed to be a new protocol for the decentralized, distributed environment, which utilises the power of the Internet and XML to pass typed information between nodes. Originally, SOAP stood for Simple Object Access Protocol, but was later changed simply to SOAP with version 1.2, because it did not directly use Objects. SOAP is a light weight protocol that is platform independent, transport independent and operating system independent.



There are two types of SOAP requests. The first is the Remote Procedure Call (RPC) style request similar to other distributed architectures. This is usually synchronous; the client sends a message and waits to get a response or fault message back from the server. The second type of SOAP request is the document request. In this case, a full XML document is passed to/from the client and server, in side a SOAP message.

RPC

SOAP-RPC is an implementation of a Remote Procedure Call (RPC). In this case, a function on a remote machine is called as if it were a local function. All of the marshalling and unmarshalling of data is handled by SOAP and is performed in XML. RPC-style web services are tightly coupled and interface-driven. The clients invoke the web service by sending parameters and receiving return values. RPC-style web services follow call/response semantics; therefore, they are usually synchronous, which means that the client sends the request and waits for the response until the request is processed completely.


Document

With a document style message the client and/or server passes an XML document as the body of the SOAP message instead of parameters. An example of this is an invoice. The document is marked-up with both XML and a schema, which is common to both the sender and receiver. The sender is the consumer, which makes a function call to the web service with some parameters, and in return the vendor sends back, not data results, but an XML document that contains all of the information that a normal invoice would have, the difference being that it is marked-up with XML, and is machine-readable. Document style messaging has other advantages over a remote procedure call, which is meant to be relatively static, and any changes to the RPC interface would break the contract between the service and the application. Changing the RPC description would cause all of the applications that rely on a specific method signature of SOAP-RPC to break. Good design dictates that the method signature of an RPC message service should never change. With document messaging, the rules are less rigid and many enhancements and changes can be made to the XML schema without breaking the calling application, because what is sent is an XML document rather than a structured return value. Web service applications should be able to use a guaranteed delivery mechanism to improve its reliability, scalability, and performance. Since a document message is usually a self contained XML file, it is better suited for asynchronous processing and can be placed directly into the queue. The reliability of the application is improved because the message queue guarantees the delivery of the message even if the target application is not currently active, performance is improved because the web application simply delivers the document to a queue and is then free to perform other tasks, and scalability is improved because the document is offloaded to one or more instances of an application that handles its processing.

Applications of SOAP

  • Business to Business integration : SOAP allows businesses to develop their applications, and then make those applications available to other companies.
  • Distributed applications : Programs like databases could be stored on one server and accessed and managed by clients across the Internet.
One thing when we think of going for SOAP on our business server is that there are many other ways to do the same thing that SOAP does. But the most important advantage you would get by going for SOAP is that its not constrained by application language(Java, C#, C++, Perl or any other language) or the platform (Windows, Unix, Mac), and this makes it much more versatile than other solutions.


Lets go little deep into SOAP. As we know SOAP does not use objects. To pass anything besides the basic data types in Java to the web service, you must write your own Serialize and Deserialize functions. These are functions written that convert a class or object into XML and back. So it is true that SOAP does not pass objects, but instead passes representations of the data in the objects, so that it can be used to create new objects. Apache SOAP comes with a built-in serialization class called BeanSerializer. This class can take a Java Bean and automatically serialize and deserialize it.


Saturday 7 April 2012

Artificial Neural Networks

Artificial neural networks are Artificial Intelligence models that try to mimic the brain in the way it stores knowledge and processing information. Artificial Neural Networks which are commonly known as Neural Networks have been motivated from its inception by the recognition that the human brain computes in an entirely different way from the conventional digital computers. The first wave of interest in neural networks emerged after the introduction of simplified neurons by McCulloch and Pith in 1943. These neurons were presented as models of biological neurons that could perform computational tasks.

A Neural Network consists of a pool of simple processing units which communicate by sending signals to each other over a large number of weighted connections. The idea is to create a mathematical function to mimic a neural communication. This can be achieved by the following points:
  • Represent a neuron as a boolean function.
  • Each neuron can have an output capacity of either 1 or 0.
  • Each neuron also has a set of inputs (either 0 or 1), with as associated weight.
  • The neuron can compute a weighted sum over all the input and compare it to some threshold t.
  • If the sum is greater than t, then output 1 else output 0.
This is how we solve a problem by neural networks. In some cases we may require more than 1 neurons to come up with a boolean function, which is mostly the case. To come up with a good function is a tedious task. This single neuron which I am talking about is called a "perceptron" in Neural Network terminology.

Perceptron: 
A perceptron is a simulated neuron that takes the agent's percepts (eg. feature vectors) as input and maps them to the appropriate output value.

percept.jpg 

This can be represented in an equation form as follows:
w1 *  x1 + w2 * x2 + ........  + wn * xn >= t

Perceptron Learning:
Now the most important question which should be haunting you by now. How does the perceptron learn ? Well, a perceptron learns by adjusting its weights in order to minimize the error on the training set. To start off, consider updating the value for a single weight on a single example x with the perceptron learning rule:
wi = wi + error
error = a * ( true value - output) * xi
where a is the learning rate having value from 0 to 1.

Now that we can update a single value on one example, we want to update all weights so that we minimize error on the whole training set. Now this is really an optimization search in weight space, which is the hypothesis space for perceptrons.

Now the question is how do we begin ? Its simple, just initialize all the weights to small random values. Now we train our neural network by supplying input values to it and also giving it the expected output values to adjust its weights. The perceptron training rule allows us to move to the point in weight space that minimizes the error.

This a very brief introduction to neural networks for those who want to get an insight into Neural Networks. Buts there is a lot more to explore and a lot more complexity involved.