Thursday, April 17, 2008

Process Vs Threads

Recently I had a interview where I was asked to differentiate Processes and Threads. I was aware that Threads are Light Weight Processes but I struggled in giving a definitive & clear answer since I never sat and thought about it deeply - why threads? what is the advantage of having common heap? why create multiple threads if there is a single CPU and all the threads are going to reuse CPU? I did read up a couple of articles after my interview and just sharing it here.

Processes & Problem:
Processes are set of programs/applications that run on top of a operating system. A word processor is a process, firefox browser is a process. Each process is nothing but a set of instructions often called as "text block" that act on a data block (heap) with the help of a stack. Every time you launch a process (application such as firefox) the operating system (kernel) gives the process a memory space to use which is used for both stack and heap. Heap starts from the beginning of the memory block and stack from the other end. Launching a application/process is nothing but to start executing the instructions for that process on the CPU. This is done with the help of the kernel. There will be lot of processes running on a machine but only one CPU. Ever wondered how there can multiple processes can be running (executing instructions) where there is only one CPU? this is where kernel comes in - it schedules CPU's time for each process based on their priority and allocates the time. So even though all the applications are running only - one of them is actively using the CPU. Others are either idle or waiting for I/O.
So what is the problem and why threads if there can be multiple processes running in parallel (virtually)? The problem really lies on how can split a particular application into multiple processes. For example if I have a word application I may want my document to be automatically saved when I am editing it. How do I achieve this? I can do this with single process (one thread of execution) but the problem is the user will not be able to edit when I have to save - because there is only one thread of execution and it is saving the document and not listening to user inputs. I can split this into two processes one for editing and one for saving, but then these processes will have their own heap and accessing the document from the other process and saving it becomes very complicated. Also when these kind of functions that should be done parallely on a common resource (word document) increases the complexity of the application increases exponentially.

Solution - Threads:
Now wouldn't it be nice if there can be multiple processes that can access the same document and execute in parallel? This is what threads are. As I mentioned already each process always has atleast one thread of execution - the primary thread. Threads are nothing but increasing the number of threads of execution within a process. The process that creates the threads is called the primary thread. All threads spawned by the main thread can see the memory for the main process (heap) - in our example all the threads have access to the word document. Lets say one thread is handling the editing part and the other the saving part. Now when the editing thread is waiting for the user input, the CPU is not doing anything. This time can be effectively used by the saving thread and it can save the document. This way the there can multiple functions acting on the same document and several different functionalities.

Some Pointers:
  • Threads have their own stack but a common heap
  • Process have their own stack and heap
  • Threads are within a parent process
  • The parent process is called the primary thread (of execution)

Tuesday, April 1, 2008

Getting Started With Xvid

This is a rough guide for developers who are interested in using xvid as part of their application or contributing to xvid itself. I am just getting started and I just wanted to make my learnings useful to everyone. Below is the steps in order that I followed:

* Download the source code from here. If you want to grab the source code from CVS refer this page for instructions
* There are couple of readme files that comes along with the source code, but the main file to refer for building is doc/INSTALL. Follow the instructions in this file for building it on your platform.
* Once you build and install the libraries try out the examples that comes with the source. It is inside the examples directory
* Run make from the examples directory to compile all the examples
* Run the example programs without any arguments to view the help information
* The source code of these example programs is the starting point to understand the api's provided by xvid
* There is no official documentation as of now for xvid except for the readme files and documentation in code. This is in progress as I heard from xvid-devel list