Pages

Tuesday, 21 October 2014

Come And See The Core Of Computer

Jab bhe ham kisi JOURNEY ki suruat karte hai, tab ye hamesha acha idea hota hai ki ham jis raste se hoker ja rahe hai uska ek MANTEL MAP hmare pass ho. Ye baat  INTELLECTUAL JOURNEY ke lie bhe TRUE hai, such as learning to write COMPUTER PROGRAMS. Hamare case me ham logo ko ye janna bahut jaruri hai ki computers kaam kaise karte hai. App logo ko ye janna jaruri hai ki computer program akhir hai kya? Kis tareke se kuch ENGLISH WORDS(code) calculates a Complex Math problem and other things.
Jab aap mere is post ko aage Read karenge, don’t worry if you can’t understand everything in detail. (asliyat main, ye namumkin hai that You understand every Detail  brief exposition of this blog post). Bas itna padhne par dimag lagao ki apko ek big IDEA ho COMPUTER WORKING ke bare main.
So Let,s Start…
1) Fetch And Execute Cycle IN Computer
Computer bahut he complex system hai jo bana hai bahut sare components se. but at the HEART — or the brain, jo bhe aap kahna chahe— of the computer is CPU. Aaj kal ke morden computers main CPU is a single chip Like intels i3 i5 i7 you all are familiar with but there are more, like one in your 100-MS calculator, or one in your Android Phone or one in Smart TV and the list goes on. But the main job of a CPU is to execute programs.
Jab bhe CPU starts Executing a program, it do the first work to COPY THE PROGRAM FROM HARD DISK TO RAM(Primary Memory) and why it do so ? I think you all Know the answer. After all it is a point What we all Prepare for our UPTU external exam and my frnds You all are absolutely right Ram is faster than HardDisk so it is ideal to use RAM. But i know some of you might thinking so why not to build a whole computer of 1 TB Ram. Why we even use harddisk ? There are two reason to use harddisk.
  1. Ram is very costly so it is not economical.
  2. It is Volatile, every time turn off the computer it erases all the data in it. So if we want that our cute pics, movies and songs not get erased every time we have to use a hard disk.
Programs ke sath - sath RAM also store the inputs , intermediate outputs and final result of the program. Main memory consists of a sequence of locations. These locations are numbered, and the sequence number of a location is called its address. Ye addresses bahut he important hai inke through hm RAM main rakhi Millions of Informations main se kisi bhe information tak pahunch sakte hain. And most amazing part(Actually, this is why I love RAM) of the RAM is  that its Speed is independent of Information’s memory Location means, chahe info ka address 0 ho ya 100K it can be fetched in a constant time. Is’nt it is amazing and if you want to know how it happens i will suggest u to read your CO and DLD books carefully it has all the answers.
When the CPU needs to access the program instruction or data in a particular location it sends the address of that information as a signal to the memory, memory responds by sending back the data contained in the specified location.
Machine Language ke level par CPU ke Operation Bahut simple hain(Waise in detail it is very Complicated). The CPU executes a program that is stored as a sequence of machine language instructions in main memory. It does this by repeatedly fetching an instruction from memory and executing that instruction. This process goes in this why —
1. Fetch an Instruction -> 2. Execute It
3. Fetch another Instruction -> 4. Execute it  And this Process goes on Forever.
and this process is called FATCH AND EXECUTE CYCLE. And this is all that the CPU ever does.
Details of fatch-and-execute cycle ko janna koi itna jaruri nahi hai, lakin kuch bahut basic cheje hai jo apko janni he chahie. EVERY cpu contains  a few internal registers, you can think of it as a VERY small RAM which also have Addresses and it is even faster than ram, almost equal to the speed of cpu itself. Some of these registers are used for internal purpose like LC(Location Counter Register). LC is very important because it is used to keep track of where it is in the program it is executing. Actually it holds the next Memory location (address) of instruction to be fetched from RAM. During the fatch it is update for next instruction location.
Ek computer machine language programs ko execute krta hai wo bina samghe ki what it actually means and what it do, iska sidha sa reason ye hai ki isko banaya he aise gaya hai. This is not an easy concept. Agar hm computers main aur deep jae to pta chalta hai ki asliyat main computer bna hai millions of TINY SWITCHES se jisko hm transistor kahte hai, every transister have to state on and off(or 0,1), ye transistor bade anokhe hote hain kyunki inko ek dusre se is tarike se joda ja sakta hai ki output of one switch can turn on and off another switch. As a computer computes, these switches turn each other on or off in a pattern which is determined by your program and how actually they wired up.
Now In my entire post i keep repeating the Word Machine Language. But What actually a machine language is ?
Can you tell what this means 1000 1111 ? Some would say it is a sequence of 0’s and 1’s.(Binary) some would convert it Decimal No and some one in Hexa No. But, I will suggest every one to recall the definition of Information. It is nothing, it is simply raw data which has no meaning. Now if i put the same question in this way, Can you tell what this means 1000 1111 BCD ? Now every body reading this post have a common answer 08 15. Similarly Every CPU has an instruction set in binary and whenever cpu encounter these binary number in specific pattern it perform an operation for which it is coded in chip.
Like following code perform addition in 8085 processor  
10001111 00000111 00000011 (1st bunch of sequence is opcode ADD and 2nd 3rd is 7 and 3) 
May be i was wrong because it is 5:00 in the morning and i am little tired and it is possible that i incorrectly computed the opcode of add to binary But the general idea is a machine language instruction is just a sequence of zeros and ones. Each particular sequence encodes some particular instruction. The data that the computer manipulates is also encoded as binary numbers. A computer can work directly with binary numbers because switches can readily represent such numbers. When a machine language instruction is loaded into the CPU, all that happens is that certain switches are turned on or off in the pattern that encodes that particular instruction. The CPU is built to respond to this pattern by executing the instruction it encodes

Monday, 13 October 2014

Programming Pattern #Singleton


Sometime in programming we need to have only one instance of object for example in logger class.
Such type of situation in programming is mostly handled by Singleton Classes.

Singleton is a software design pattern which ensures only one object of a type is created. This is useful when we want to create an object, which manages or coordinates some tasks in the system. A WindowManager, a FileSystem, Mouse Pointer would be good examples for places where Singleton design pattern can be applied.

The singleton pattern is one of the simplest design patterns it involves only one class which is responsible to instantiate itself, to make sure it creates not more than one instance; in the same time it provides a global point of access to that instance. In this case the same instance can be used from everywhere, being impossible to invoke directly the constructor each time.

How To follow Singleton programming Pattern

  • Make sure class can't create more than one variable
  • Provide a global access to that instance 

 Implementation 

  • Singleton class have a private constructor which restricts programmers to create instance of the  class.
  • Singleton class also have a static method and a private variable of the Singleton class type, this private variable used to hold the single instance of the class.
  • Static method is used to expose the private variable.
    class MySingletonClass
     {
	private static MySingletonClass instance;
	private MySingletonClass()
	{
		...
	}

	public static synchronized Singleton getInstance()
	{
		if (instance == null)
			instance = new MySingletonClass();

		return instance;
	}
	...
	public void doSomething()
	{
		...	
	}

        public void doSomethingElse()
	{
		...	
	}
    }

Uses Of Singleton Pattern

  • Logger Class
  • Configuration Class
  • Centralized Coordination of task 

 Profit 

  • Single instance save memory which is very important for low memory devices
  • Simple accessibility of shared Resource 

Note :- 

To keep things simple, above implementation not Support Multithreaded programs. If you are curious and know about multithreaded programming than i am sure you also know that "Locking the Methods" is the way.