Monday, August 07, 2006

Interview FAQ I

1) Priority inversion ?

When a thread running at lower level holds the synchronization lock and is prempted by another thread running at elevated level then dead lock situation arises. In that case the priority of the thread holding the lock is temporarily increased so that it releases the lock soon. This is called priority inversion

2) Which synchronization primitive should I use ?

Spinlocks: Multiprocessor safe, busy wait, keeps on spinning on the processor until the lock is released.
Semaphore: Multiprocessor safe, sleep wait, process is moved to sleep queue if some one else is holding the lock. More than one resource can be protected using semaphores.
Mutex: Multiprocessor safe, sleep wait, process is moved to sleep queue if some one else is holding the lock. Its mutually exclusive lock, only one resource can be shared.

3) How to debug memory corruption without tools ?

You can write a wrapper function with boundaries defined and maintain accounting for malloc/calloc and free. You just need to compile this as a library and prload the library using LD_PRELOAD.

4) Compute the parity of byte with order 1:

Precompute the parity and do a lookup from the array by just indexing into the array.

5) Convert the string from "This is the string" to "string the is This".

Algorithm:

Do an inplace string reversal first.

Change "This is the string" to "gnirts eht si sihT"

Reverse characters of each word inplace.

"string eht si sihT"
"string the si sihT"
...

6) Little Endian/ Big Endian

union endian{
struct {
char a;
char b;
char c;
char d;
} en;
int p;
}k k1;

k1.p = 1;

if (k1.a == 1 )
{
printf" Little Endian";
}
else {
printf" Big Endian";
}

7) Determining the offset with in a struct p

((char *) 0 - (char *) (&((struct *p) 0)->offset))

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home