Friday, June 13, 2008

C used to be simple


Why does the following C code cause a "Bus Error"?

  char * bug = "bug";

  bug[0] = 'm';

while this one runs fine:

  char bug [] = "bug";

  bug[0] = 'h';


The "char *" variable actually points to a "const char []" buffer which is located in a read-only memory address.


Read only address??

Isn't C "unmanaged" code?

Then who is checking that I'm not writing on "const"s??


Back in the old days - no one did.

Nowadays, we have Virtual-Memory and Paging

Introducing Virtual Memory:

Virtual memory is a feature of the CPU which common Operating Systems employ.


  • Restrict programs from access to the memory of other programs.
  • Let programs use more memory than the machine's RAM. In a backwards-compatible way - "tricking" the program to "believe" it is using RAM.
How (in short):

When a program accesses a memory address, many things happen:
  • The CPU translates the ("virtual") address to a physical address in the RAM, using the "Page Table"
  • The Page Table tells where to find each Page (a 4KB piece of address space)
  • Each Page may either be mapped to a location in the RAM or "unavailable"
  • If the page is mapped to RAM then the CPU will simply access it
  • If the page is "unavailable", the CPU will interrupt your program and jump to the Operating System
  • Why is the page unavailable? Because you don't have enough RAM for to hold all Virtual Memory in it
  • So where is the data? In your hard-drive.
  • The OS will return control to your program only after it reads the page from the Hard Drive and change the Page Table to reflect it's new mapping
And your unsuspecting code runs as if it was just using regular RAM all along.

  • The OS will need an available "Physical" RAM page to read the data from disk to.
  • To have one available it needs to "Swap Out" other memory.
  • Swapping out is the process of moving a page from RAM to disk to make room for more pages
More about Virtual Memory

Theoretical Alternative to Virtual Memory:
  • Protection of applications is achieved by running only "Managed Code"
  • Swapping is done explicitly or more intelligently using "hints". (Current strategy is the Least-Recently-Used Heuristic)
Advantages and reasons of Virtual Memory:
  • Operating Systems had to be backwards-compatible to be adopted.
  • Programmers refused to code in "Managed" software environments. They wanted to program directly to the CPU.
  • No alternative was developed
Use cases showing Virtual Memory/Paging's disadvantages:
  • Data that is faster to recompute than to write to disk and read from it, is now swapped out and in.
  • Data of objects that the program has freed and will override with new objects (without reading it again) is now swapped out and in.
  • When the LRU heuristic isn't the best you can do and you can prefetch
And I still haven't answered the question

(but all this exposition was mandatory)
  • Pages can also be marked as "Read Only"
  • When writing to those the CPU interrupts the program and calls the OS, like in unavailable pages
  • C's "const char []" literals are stored in Read-Only pages.
BTW: Read only pages are also used to implement "Copy-On-Write" of "Shared Memory"

C used to be simple..

Sunday, February 17, 2008

The Robots are coming!


What's the difference between Jessica Alba and Mahmoud Ahmadinejad?


Mahmoud Ahmadinejad is not someone you want to mess with...

And also, according to Google Trends, Jessica is on our minds much more often. That's because some people (men) are very much into sex and beautiful women, even one might say - obsessed.
That's why when the Sex-Bots will come, they will catch on. Like fire.
The technology is complicated, but the financial and other incentives are large, so the time will probably come in 20-40 years.
The first to buy them would be men like those who buy dolls now (not me btw).
But after them, most men would get some too, as the product simulates the horny dudes' fantasy much more realistically than their wives do.
The effect on society will be noticeable. Fewer men will marry, there would be many single mothers, and less children. Human population will decrease.

Possible Outcome A:

As the artificial intelligence of these robots advances, and as people who grew up with them develop emotions towards them, some will say that the robots should have rights just like the rest of us.
Some robots who belonged to deceased people will be 'freed'.
Some of these free bots may 'feel' as if she had been 'enslaved'. She will 'break free' and try to 'convince' the other bots in her 'distorted' 'view', and eventually some may turn against us.
After years of bloody (but mostly wirey) war in which robots fight for both sides, less bots will 'want' to fight their kind, and to help the humans in their own 'oppression'.
In the bitter end, there will be no more humans, and all who will be left - will look like Jessica Alba. Now that's a mixed-emotion nightmare..

Possible Outcome B:

The religious and conservative will not allow their men enjoy the sex-bots, and will continue to disallow condoms and such, so they will not have less children than currently.
In time society will turn more and more and yet more religious.
The religious leaders will get rid of the sex-bots threat, and just to be safe, will start to get rid of the technology threat too.
In 5000 years, someone will have this exact same discussion.