Saturday, May 2, 2015

Process Address Space (Linux)

Process Address Space

  • Linux (and all modern OSs) virtualizes physical memory.
  • i.e. processes do not directly address physical memory.
  • Each process gets a unique virtual address space.
  • This address space is linear; addresses start at zero, and increments continously upto the max of 32-bit/64-bit integer value.
  • A part of this address space (~1GB) is reserved for the kernel. 

Memory Pages

  • The virtual address space is broken into memory pages.
  • A memory page is 4KB for 32-bit OSs and 8KB for 64-bit OSs.
  • A page is the smallest unit that the MMU (Memory Management Unit) can manage.
  • A page can be either valid or invalid.
  • A valid page is memory that has been allocated for the process; this page could be in physical memory or on disk (swap partition).
  • An invalid page is memory that has not been allocated (or freed).
  • Accessing an invalid page causes a segmentation fault.
  • Different virtual memory pages of different processes, could map to the same physical page; eg: standard C lib will have only one copy in physical memory, but will be mapped by different virtual memory pages of multiple processes.

Paging

  • A valid page, which has been swapped to disk, cannot be directly accessed; it needs to be brought to physical memory first; 
  • This task is handled automatically by the OS, and is not visible to the process. 
  • When a process tries to access a valid page which is on disk, MMU generates a page fault, which is captured by the OS kernel; kernel then page-in that page from disk to RAM, transparently.
  • When page-in is required, some existing pages (least used) might be paged-out, to make room for the new pages.

Memory Regions

  • Virtual memory pages of a process are grouped into several blocks called memory regions (or memory areas).
  • Text segment: contains the process's read only data -- program code, string literals, cont vars etc.
  • Stack: contains the process's execution stack -- local vars etc.
    • stack dynamically grows/shrinks as the stack depth increases/decreases.
    • multi-threaded processes contains one stack per thread.
  • Heap (data segment): contains the process's dynamically allocated memory.

References:

Friday, February 14, 2014

Monday, January 7, 2013

Good budget smart phones in Sri Lanka


iPhones are still the craze in Sri Lanka, like it is in any other part of the world. However if you have used/experienced several generations of iPhones, you would have realized that the feature differences in each generation isn't that significant, and might not justify the high prices it demand (especially in Sri Lanka).

At the time of writing iPhone 5 prices start at around Rs. 115,000. This is fine for bragging rights, but if you have used/experienced an android phone, you would have realized that you could get the same functionality/productivity for a much lesser price. It could be argued that the user experience / interface is far superior on the iPhone, but having heavily used both, I beg to differ; while my wife owns an iPhone, both my dad and father-in-low (both non-techies) use android phones with no trouble.

Most adults looking to switch to a smart phone from their regular/dumb phone, basically require/look-for the following functionality:

1. Basic voice call and SMS functionality (obviously).

2. Send/receive emails.

3. Browse Facebook. (yes they need to see those pics of their relatives living abroad)

4. Read news on the internet.

5. Navigate using (Google) maps.

6. Play a casual game when bored.

However most people do not know that all these tasks can easily be achieved via a low cost android phone. There's heavy competition in the low end phone market, so it's not easy to pick which one to buy.
Here are the main features to look for:

1. A big screen with decent resolution - most of the above tasks will benefit from a large, sharp screen; should be at least 4 inches.

2. A decent processor - should be 1Ghz+; better yet if dual core. - lower power processors will make the phone feel sluggish.

3. Enough RAM - should have at least 512mb; Please note that this should not be confused with the internal storage memory. RAM is where your applications are loaded and run.

4. Build quality - low end phones use cheap materials which might not be durable. Yet there are exceptions and you need to find such a phone.

I have found the following two phones to have the best bang for the buck, in the current SL market.

1. MicroMax A110 - Rs. 25k - simply the best bang for the buck phone out there. Brilliant big screen,  dual core and 512 ram.  (bit difficult to find though since they frequently run out of stock due to high demand.)

2. Huawei G300 - around Rs. 29k

Sunday, November 4, 2012

Intelligent web page reflow - badly needed in Android tablets.

I bought a Samsung Galaxy tab 2 7" recently. One of the biggest problems I had was that general web browsing was not a very pleasant experience on it. The mobile versions of the web sites were too small for it; the desktop versions were mostly too big and awkward to navigate. Web pages need to be zoomed in and out like an image when reading, and  needs both horizontal and vertical scrolling, which is very annoying.

I'm quite surprised that (android) tablet browsers still haven't implemented an intelligent page reflow scheme which will reposition the page elements and resize the text to a comfortable size based on the screen resolution and physical screen size. Page should be readable by having to scroll only vertically; even better would be an automatic formatting into a flipboard like mechanism where we could just 'flip' pages, instead of scrolling. I find flipping is more natural than scrolling, and probably uses much less cpu cycles on the tab.

Sunday, September 18, 2011

Reading and Writing to the parallel port on Windows 7 (and all post NT win OSs)

Recently one of my cousins from the electronics field wanted me to help him out on a problem; He had some electronic circuits which were controlled via a PC parallel port. These worked properly on Win98 machines but gave trouble on post NT win OSs (2000, XP, Windows 7 etc.). 

The problem was related to the difficultly in reading/writing to the parallel port in the newer OSs. In Win98 and older OSs, user programs could directly read/write to underlying hardware ports without restrictions. However the newer OSs had prevented this, and only kernel mode apps could write directly to the hardware ports.

The first option explored was to use UserPort, a kernel mode driver. The approach of UserPort is to manipulate the IOPM (I/O Permission Bitmap), so that user applications can be granted permission to directly write to the hardware. It uses a undocumented function (Ke386IoSetAccessProcess in NTOSKRNL.EXE) in order to achieve this.

However the UserPort driver didn't seem to behave correctly on windows 7. It would randomly freeze up the machine upon service start/stop operations. This could probably be a bug in the driver implementation, and not necessarily a problem of the approach, but the fact that a undocumented function has to be used, makes it that much unreliable.

The second option explored was to use inpout32.dll from here. This uses a more elegant approach where the user application calls the read/write of the dll, which in turn uses a embedded kernel driver (Hwinterface.sys) to do the actual hardware read/write (via DeviceIOControl API). The full details of its inner workings are explained here.

However my program in concern was written in java, and the inpout32.dll didn't have the necessary support to directly call it via JNI. One workaround would have been to write a bridge dll with JNI support, which in turn called inpout32.dll. 
The other option was to directly modify the inpout32.dll (since the source was available) to support JNI calls, but luckily someone had already done it here - as jnpout32.dll. This worked right out of the box, solving my problem at hand.



SugarSync - A powerful alternative to DropBox (cloud sync)

I'm currently trying out alternatives to DropBox, which has a (rather annoying) limitation of being able to sync only one folder. Putting everything I want to sync in a single folder doesn't suite my taste of folder organization. Also the free 2GB disk quota doesn't feel sufficient either (you could pay to get more though).

The alternative SugarSync seems to be more powerful with multiple folder sync and more control. Also it offers 5GB of free disk quota which is quite attractive. I have synced 4 devices so far, including my office laptop, home laptop, home desktop, and my Android mobile phone, and all seem to work very well.


SugarSync referral link  (to get me some additional disk quota ;)


Sampath Tilakumara's Blog