Posted: March 19th, 2013 | Author: Aneesh Dogra | Filed under: C, Computer Science, Science | Tags: C, fork | No Comments »
First let’s consider a program which loops 2 times:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#define N 2
int main(){
int i;
for (i = 1; i <= N; i++) fork();
printf("Hey! from %d\n", getpid());
}
This program prints “Hey”, 4 times to the console, hence it creates 3 child processes. Let’s figure out how!
Note: The child processes have their local copies of variables, hence any operations on the variables won’t affect the parent’s copy or the other children’s copy.
1) The parent process (P0) runs through the loop 2 times, hence creating 2 child processes (P1 and P2). These child processes get their local copies of ‘i’ which is 1 for the first child and 2 for the second child. Thus, P2 runs through the loop 1 time while the P1 runs through the loop 2 times.
Note: The child always start executing from the instruction immediately after fork, which is i++ in our case.
2) P2 increments i from 2 to 3, followed by the comparison “i <= N” which obviously evaluates to false. Hence, P2 doesn’t create any processes.
3) P1 increments i from 1 to 2, it then executes fork() which creates another child with i as 2, lets call this child “P1_1″. After that it increments i from 2 to 3 and exits out of the loop.
4) P1_1 increments i from 2 to 3, and exits out of the loop.
So, at the end of the loop we have 3 children (P1, P2, P1_1).
Let’s now figure out the number of children for a program which loops ‘N’ times.
Let Pn be the nth child of the parent process, Pn_n be the nth child of the nth child of the parent process and so on.
Children with ‘i’ as 1: 1 (P1)
Children with ‘i’ as 2: 2 (P1_1, P2)
Children with ‘i’ as 3: 4 (P1_2, P2_1, P3, P1_1_1)
Do you notice a pattern in the above written numbers?
Number of children with ‘i’ as 1: 1
Number of children with ‘i’ as 2: 
Number of children with ‘i’ as 3: 
..
Number of children with ‘i’ as N: 
Now we know that sum of G.P is 
Hence, total number of child precesses created is:
= 
Posted: March 18th, 2013 | Author: Aneesh Dogra | Filed under: C, Computer Science, Science | Tags: C, fork | No Comments »
If your answer to the above asked question is
you are wrong. Consider the following C program.
#include <stdlib.h>
#include <stdio.h>
int main(){
pid_t c1 = fork(); // parent
pid_t c2 = fork(); // parent, c1
pid_t c3 = fork(); // parent, c1, child of parent & c2, child of c1 & c2
printf("Hello!\n");
}
For 1st fork call we have 1 caller i.e the parent, this creates 1 child that then starts executing the rest of the program.
For 2nd fork call we have 2 callers i.e the parent and c1 this creates 2 children these then start executing the rest of the program.
For 3rd fork call we have 4 callers i.e the parent, c1, child of parent & c2 and child of c1 & c2.
At the end of 3 fork calls we have 1 (1st fork) + 2 (2nd fork) + 4 (3rd fork) = 7 children and 1 parent. Thus, The printf gets executed 8 (
) times. We can easily deduce that the pattern in the number of children is a G.P with 2 as its common ratio.
We know that sum of G.P is
where n is the number of terms, a is the first term and r is the common ratio.
For our case: a = 1, r = 2. Hence the sum =
= 
So, for a program containing n fork calls followed by another instruction ‘a’. The instruction will be executed
times (
).
Posted: March 16th, 2013 | Author: Aneesh Dogra | Filed under: Computer Science, Science | Tags: Python, PyUtilb | No Comments »
Pipy_downloads, a part of PyUtilb, is a utility to calculate the total number of downloads that a particular PyPI package has received across all versions tracked by PyPI. I wanted to check the number of downloads for all pypybox2d packages, so I ran the following command:
pypi_downloads pypybox2d
which broke pypi_downloads and popped out the following error message:
Traceback (most recent call last):
File "/usr/local/bin/pypi_downloads", line 8, in
load_entry_point('pyutilib.misc==5.3.1', 'console_scripts', 'pypi_downloads')()
File "/Library/Python/2.7/site-packages/pyutilib.misc-5.3.1-py2.7.egg/pyutilib/misc/pypi_downloads.py", line 166, in main
_main(args, options.exact)
File "/Library/Python/2.7/site-packages/pyutilib.misc-5.3.1-py2.7.egg/pyutilib/misc/pypi_downloads.py", line 147, in _main
PyPIDownloadAggregator(pkg, exact=exact).stats()
File "/Library/Python/2.7/site-packages/pyutilib.misc-5.3.1-py2.7.egg/pyutilib/misc/pypi_downloads.py", line 116, in stats
keys.sort(key=lambda x: [int(y) for y in x.split('.')])
File "/Library/Python/2.7/site-packages/pyutilib.misc-5.3.1-py2.7.egg/pyutilib/misc/pypi_downloads.py", line 116, in
keys.sort(key=lambda x: [int(y) for y in x.split('.')])
ValueError: invalid literal for int() with base 10: '1-r331'
Looking into the code, it seems that Pypy-downloads will break for any package with version format other than x.x.x where x represents a number. It can be fixed with a simple hack:
keys.sort(key=lambda x: [int(y) for y in x.strip('-r331').split('.') if y.isdigit()])
Now, it strips off ‘-r331′ from the version number. And also checkes if each x in x.x.x is a digit, if it isn’t a digit it ignores that part of the version number.
Posted: February 14th, 2013 | Author: Aneesh Dogra | Filed under: Physics, Science | Tags: Light, Physics, Special Relativity | No Comments »
Photons have 0 rest mass. So how come they possess energy? I mean if you take photons as regular matter you can deduce that they have no momentum as they have 0 mass and hence according to Energy-Momentum relation it doesn’t have energy. Right? Wrong!
The linear momentum’s definition is only valid for regular matter. Photons are not regular matter. Let’s take the example of fields, say electromagnetic field. An electromagnetic field can give momentum to a charge. Hence, according to the conservation of momentum EM fields have momentum. But do they have mass? Of course it doesn’t its a field after all. Hence we deduce that mass is not necessary for momentum to exist at least not for all kinds of matter.
If photon has momentum it sure can have energy. That answers our question!
Posted: February 7th, 2013 | Author: Aneesh Dogra | Filed under: Computer Science, Internet Security | Tags: Django, Google App Engine, Internet Security, XSS | No Comments »
Just to clear up the title: There are no “security holes” in Django or GAE per se but the way people use these and assume some things cause the XSS vulnerability.
Autoescaping was added in [6671] on Nov. 14 2007 in Django i.e Django 0.96 doesn’t have this functionality. Google App Engine uses Django 0.96 as a default (unless you specify the version explicitly using use_version). Now most of the people new to Google App Engine [especially the ones migrating from other platforms] doesn’t know this thing, hence causing the vulnerability.
How to check if you code is vulnerable:
1) Do you use Django version 0.96 or older. If no, you are safe. If yes, read further.
2) Do you escape each user editable variable in your Django templates using the ‘escape’ filter or are you properly escaping these variables. If yes, you are safe. If no, you are vulnerable.
How to fix:
The most elegant way to fix is to simply use a later version of Django.
For example to use Django 1.2 add the following lines at the top of your handlers.
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from google.appengine.dist import use_library
use_library('django', '1.2')
But if for some reason you can’t do that. You’ll have to use the escape filter to escape each user-editable variable in your templates.
Posted: January 8th, 2013 | Author: Aneesh Dogra | Filed under: Mac | Tags: Fedora, Mac | 1 Comment »
Fedora 18′s latest Mac EFI images fail to work on my Macbook Pro late 2011 edition, the bootloader just hangs in there.
Its possible to install Fedora 17 on MBP without major issues, and there is a documented method to update it to Fedora 18 using YUM, but please don’t even bother trying. I am writing this blog post after trying to update and messing up my bootloader, so now I can’t even run fedora 17. I don’t blame Fedora for this, they very clearly asserted that the yum method is not recommended and may break things, indeed it did.
This is a blog post to warn you about it, and save you the reinstallation and the data wipeout. Stay frosty!
Posted: December 21st, 2012 | Author: Aneesh Dogra | Filed under: Physics, Science | Tags: Gravity, Physics, Planets | No Comments »
A comet is an icy small Solar System body (SSSB). Basically, Its a body traveling in a large elliptical orbit around the sun due to the force of gravity. Comets have a changing velocity, from around 1 km sec at aphelion to 100 km/sec or more when it is closest to the Sun. In this article we’ll discuss why is that, why does it travel so fast when closer to the sun and earth.
Kepler’s Second law of planetary motion
We can explain this phenomena using the Kepler’s Second law of planetary motion.

Illustration of Kepler’s Second Law of planetary motion. Source: Wikipedia
The second law states that, “A line joining a planet and the Sun sweeps out equal areas during equal intervals of time”.
Comet’s orbit:

Haley’s Orbit
We can notice how long and eccentric the elliptical orbit of Halley is. We can easily notice the area difference.
The comet has to travel a large area in a short period of time when it is near the sun. This can only be achieved it it travels faster (much faster). The area difference is so huge that great amount of speed is required to cover equal areas, and thus is our result.
Posted: December 19th, 2012 | Author: Aneesh Dogra | Filed under: Mac | Tags: Fedora, Fedora Mac Installation Setup, Installation, Mac, Setup | 7 Comments »
Fedora 17 provides EFI boot support which makes it possible for us to boot it on Mac. This is a detailed guide on how to install Fedora on your 64 bit Mac Book Pro.
Check if your system is supported
ioreg -l -p IODeviceTree | grep firmware-abi
If this returns “Firmware-abi” = < “EFI64″> then your system is supported.
Dowloading
You need the 64 bit version of fedora 17 (any spin).
Partitioning
Before you install another system you need to partition your Mac HDD to resize your Mac partition and add some extra free space.
1) Open Utilities

2) Open Disk Utility

3) Select the “Apple HDD” (The top one in the hierarchy)

4) Click on partition.

5) Decrease the size of the Macintosh HD to make some free space.

6) Click Apply
Building your USB
Unetbootin doesn’t work (atleast for me), you can try but for me the bootloader just hangs in there forever. A simpler method, which actually works, is to make your USB using ‘dd’ command.
1) Get your USB’s Identifier.
Run :
diskutil list
Output:
➜ ~ diskutil list
/dev/disk0
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme *500.1 GB disk0
1: EFI 209.7 MB disk0s1
2: Apple_HFS Macintosh HD 399.2 GB disk0s2
3: Apple_Boot Recovery HD 650.0 MB disk0s3
4: Apple_HFS untitled 209.7 MB disk0s4
5: Microsoft Basic Data 524.3 MB disk0s5
6: Linux LVM 99.3 GB disk0s6
/dev/disk1
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme *4.0 GB disk1
1: EFI 209.7 MB disk1s1
2: Microsoft Basic Data MY USB 3.7 GB disk1s2
Note: I have multiple OS’s installed on my Mac already, so your output might differ mine.
My disk’s name is “My USB” and we can infer from the output that its identifier is disk1s2 , which is in the disk /dev/disk1 [This might be different for your system]. Make sure you get this step right!
2) Unmount your USB
diskutil unmountDisk /dev/disk1
3) Copy the image onto your USB
su -c "dd if=SOURCE of=/dev/rdisk1 bs=8M"
where SOURCE is the .iso file, and /dev/rdisk1 is your drive.
Notice the r in /dev/rdisk1
Note: This command takes a while and shows nothing on the screen.
Booting
Now, that you have your USB ready, insert it into a slot and boot your mac. As you see the white screen press and hold the “option key”. You should see a screen something like :-
Select “Fedora”/”Fedora Media” from the list.

Now, you should be able to install fedora like you do it in any other system.
Some Issues with Fedora on mac
1) You can’t increase the sensitivity/speed of your trackpad. There is a setting entry for that purpose but that doesn’t seem to work.
2) Your brightness settings and keyboard backlight settings do not get saved. Every time you reboot you need to readjust them.
3) F* key shortcuts (F1, F2 … F12) doesn’t seem to work. That is because they are already booked to do other functions like increasing volume, brightness, backlight etc, and using Fn + F* combos do not seem to work.
4) Your wireless drivers won’t get installed automatically. I had to download mine and install them manually.
5) The bluetooth mouse support is crappy. I have to restart the bluetooth drivers 5-6 times before I get the external mouse connected. By the way I use Apple magic mouse, which obviously works perfectly with my Macbook pro running OS X Lion.
Posted: November 26th, 2012 | Author: Aneesh Dogra | Filed under: Physics, Science | Tags: Light, Physics, Special Relativity | No Comments »
Universe kind of imposes a speed limit on anything which has a finite mass. Nothing can go faster than the speed of light. In this post we’ll be discussing why is that?
In the special theory of relativity, there is a concept of mass-energy equivalence which states that mass of a body is the measure of its energy content. In this concept, mass is a property of all energy, and energy is a property of all mass, and the two properties are connected by a constant. The equivalence is defined by the famous equation.

In special relativity “mass” is defined in two different ways in special relativity: one way defines mass (“rest mass” or “invariant mass”) as an invariant quantity which is the same for all observers in all reference frames; in the other definition, the measure of mass (“relativistic mass”) is dependent on the velocity of the observer.
Relativistic mass and Rest mass are connected by a factor, named Lorentz Factor.

Where m0 is the rest mass and m is the mass of an object in motion and γ is the Lorentz factor.
The Lorentz factor is defined as:

where:
- v is the relative velocity between inertial reference frames,
- β is the ratio of v to the speed of light c.
- τ is the proper time for an observer (measuring time intervals in the observer’s own frame),
- c is the speed of light.
Getting back to our question, when we travel at the speed of light. γ (lorentz factor) becomes 1/0 i.e infinite.
And according to Energy-Mass equivalence.
mc^2 = E
Thus, we conclude it would take infinite amount of energy to make an object travel at the speed of light. That answers our question.
Posted: November 25th, 2012 | Author: Aneesh Dogra | Filed under: C, Computer Science | Tags: C, dynamic memory allocation, memory, Programming | 1 Comment »
C provides a suite of functions to provide robust and concrete dynamic memory allocation namely malloc, realloc, calloc and free.
1) Malloc
void * malloc ( size_t size );
size: The number of bytes you wish to allocate.
Malloc allocates a block of memory and returns a void pointer to the start of that memory location. If the function failed to allocate the requested block of memory, a null pointer is returned.
When you malloc a block, it actually allocates a bit more memory than you asked for. This extra memory is used to store information such as the size of the allocated block, and a link to the next free/used block in a chain of blocks, and sometimes some “guard data” that helps the system to detect if you write past the end of your allocated block. This data serves as an easy and fast reference to the system when you use other calls like realloc or free.
2) Calloc
void * calloc ( size_t num, size_t size );
num: Number of elements
size: Size of each element
Calloc allocates a block of memory for an array of “num” elements each of size “size”.
A successful calloc call results in the allocation of a block num * size bytes long, each initialized to 0.
The initialization part differs it from malloc.
3) Realloc
void * realloc ( void * ptr, size_t size );
ptr: Pointer to a memory block previously allocated with malloc, calloc or realloc, or a null pointer (to allocate a new block)
size: New size for the memory block, in bytes
Realloc increases the size of the memory block pointed by ptr. If the current address of the memory block doesn’t has “size” free bytes after it. Realloc allocates a completely new memory block at a different location. So, realloc may move the memory block to a different address, so you might get a different ptr in return.
When the ptr is NULL, realloc exactly behaves like malloc. It allocated a memory block of size bytes long.
4) Free
void free ( void * ptr );
ptr: Pointer to a memory block previously allocated with malloc, calloc or realloc.
Free deallocates the previously allocated memory using a call to malloc, calloc or realloc. Making it available again for further allocations.
Note: The ptr should exactly point to the memory location returned by malloc, calloc or realloc (why? check the malloc section.). If Even if its off by 1 byte the result is undefined (mostly result in crash).
Usage Examples
1) Basic malloc, free usage :-
#include <stdio.h>
#include <stdlib.h> // for access to the memory allocation functions
#define NO_OF_INTS 5
#define NO_OF_CHARS 20
#define NO_OF_LONGS 5
int main()
{
void *upper_bound; // To avoid invalid reads.
int *int_ptr;
char *char_ptr;
// Many people would suggest to initialize your pointers to NULL
// but I don't insist upon it as it just wastes a mov instruction
// but be sure to never dereference a pointer you didn't initialized
// as that would result in a read from a random address which will result
// in a segmentation fault, so initialize them if you are gonna use them
// when required
// Tip 1: Always cast the returned pointer from malloc, Most of the compilers are kind enough to give you warnings for that.
int_ptr = (int *)malloc(sizeof(int) * NO_OF_INTS); // same as "int array[NO_OF_INTS]"
// Tip 2: Always check the output of malloc
if (int_ptr == NULL) {
printf("Failed to allocate memory for ints.");
return -1;
}
char_ptr = (char *)malloc(sizeof(char) * (NO_OF_CHARS + 1)); // we need an extra byte for the NULL, same as "char array[NO_OF_CHARS]"
if (char_ptr == NULL) {
fprintf(stderr, "Failed to allocate memory for chars.");
}
// Now, we have 2 pointers each pointing to the start (array[0]) of their respective allocated memory blocks.
// Let's populate the memory blocks
printf("Notice the intervals in the addresses.\n");
// int_ptr
upper_bound = int_ptr + NO_OF_INTS;
for (; int_ptr < (int *)upper_bound; int_ptr++) {
printf("Setting %p to : 1\n", int_ptr);
*int_ptr = 1;
}
// char_ptr
upper_bound = char_ptr + NO_OF_CHARS;
for (; char_ptr < (char *)upper_bound; char_ptr++) {
*char_ptr = 'a';
}
*char_ptr = '\0'; // terminate the string
printf("This should display a string of %d A's : %s\n", NO_OF_CHARS, char_ptr - NO_OF_CHARS);
// Tip 3: Always free your pointers
free(int_ptr - NO_OF_INTS);
free(char_ptr - NO_OF_CHARS);
// Note: Free takes in the address of the start of the memory block you allocated. (the one returned by malloc)
// if you input any other memory location. It will result in crash.
// For proof, try the following :-
//free(int_ptr);
//free(int_ptr - NO_OF_INTS);
//free(char_ptr - NO_OF_CHARS);
// even one byte off the address would result in a crash.
return 0;
}
2. Basic realloc usage :-
#include <stdio.h>
#include <stdlib.h> // for access to the memory allocation functions
#define NUMBER_NEW_INTS 10
// Try to fiddle with this value, and you'll notice that
// for small values, you'll get the same memory location back
// from realloc with an extended size.
int main()
{
int *int_ptr;
int *temp_ptr; // for realloc
int_ptr = (int *)malloc(sizeof(int));
if (int_ptr == NULL) {
printf("Failed to allocate memory for");
return -1;
}
printf("Allocated a memory block for 1 int @ %p.\n", int_ptr);
// Tip: Always use a temp pointer with realloc.
// Reason :-
// If realloc fails to get a new block it will return NULL, which will
// overwrite your ptr and result in a memory leak because the previous memory
// location which we allocated with malloc (or calloc) would still be unfreed.
temp_ptr = realloc((void *)int_ptr, sizeof(int) * NUMBER_NEW_INTS); // please give me some more memory
if (temp_ptr == NULL) {
// Can't get any memory
free(int_ptr);
return -1;
}
// Now you are safe to overwrite int_ptr
int_ptr = temp_ptr;
printf("Allocated a memory block for %d int(s) @ %p.\n", NUMBER_NEW_INTS + 1, int_ptr);
free(int_ptr);
return 0;
}