>Cut short your web-addresses: TinyURL

>An utility called “tinyurl” makes your life easier with shorter URL without creating an all new website. Many times it become cumbersome(i.e. for a long string) and unsafe(if you miss to copy contents of the link) to send a link to your friend.

TinyURl helps you with providing an alias for a long link. Concept is very simple but effective.
e.g. a link is: “http://http://www.blogger.com/post-create.g?blogID=4233584295994918105”

You may get an easier name for this link as: http://tinyurl.com/abc

Here is more information this:

>Exceptional exception handling in C++


using namespace std;

int a = 20;

void foo()
try {
throw a;
catch(int &x)
// You can’t modify value of “a”
x = 10;

int main()

A=20 !!!!
When you write “throw a”, an invisible, phantom exception object gets created. You pass reference to that object and hence any change made to original object have no effect.

Mutex v/s Semaphore v/s Spinlock


– All of these are used for synchronization


Mutex provides one person to access a single resource at a time, others must wait in a queue. Once this person is done, the guy next in the queue acquires the resource.

So access is serial, one guy after another. There is a context switch of the requester thread if the mutex is unavailable.

Semaphore is useful if multiple instances (N) of a resource are shared among a set of users. As soon as all N resources are acquired, any new requester has to wait. Since there is no single lock to hold, there is as such no ownership of a semaphore.

Spinlock is an aggressive mutex. In mutex, if you find that the resource is locked by someone else, you (the thread/process) switch the context and start to wait (non-blocking).

Whereas spinlocks do not switch context and keep spinning. As soon as the resource is free, they go and grab it. In this process of spinning, they consume many CPU cycles. Also, on a uni-processor machine, they are useless and perform very badly.

C Structures: Calculating offset of an element

Let’s say we have the following structure:

int a;
char c;
} example;

To calculate offset of ‘c’, we have following ways:

1. Use the macro offsetof() defined in header file stddef.h.
unsigned int x = offsetof(struct example, c);
This macro takes care of padding of elements done by the compiler.

2. Other way to get the offset f c is:// C snippetexample var, example *ptr;ptr = &var;offset = &(ptr->c)- &(ptr->a)//

------|base_addr = 0x0000
int a |
------|addr = (0x0000 + 0x0004)
int c |
------|addr = (0x0004 + 0x0004)

We are getting the distance of ‘c’ from base of the structure. So, address &(ptr->c) should be calcuated by compile as: base_address of var + distance of ‘c’ from base
If we somehow set base_addr as zero, we will get the offset i.e. 0x0004.

A hack to accomplish this result is as follows:
#define POINTER = ((struct example*)(0))unsigned int x = (unsigned int) & (POINTER->c);

We are typecasting an address ‘0’ pointing to a type of data ‘struct example’.
Now when you do (x)->c, it is equivalent to: BASE_ADDESS(x) + size_of_data elements preceding ‘c’. We are tricking the compiler by giving BASE_ADDRESS as zero.

This hack may be incorrect if element padding is done by the compiler.

>Signals in Linux

>- List of signals: $kill -l
– You can’t handle SIGSTOP and SIGKILL. You can’t priorities which signal to handle.
– Signals are generated by setting the appropriate bit in the task_struct’s signal field. If the process has not blocked the signal and is waiting but interruptible (in state Interruptible) then it is woken up by changing its state to Running and making sure that it is in the run queue. (linuxhq.com)
– Signals that are sent to a process if an illegal flow of execution happens, are synchronous. They are also called trap e.g. illegal memory access.
– Asynchronous signals are also called interrupts and are sent from a process to another process or thread-to-thread.

>Some useful questions

>- Access privileges for private functions in C++?
– Controlling exclusive access to a variable with two processor and two threads?
– Pattern substitution with sed?
– What is md5?
– When do you allocate memory for static variables?
– Phases of compiler, AST?
– When do we allocate storage for static?
– Data hiding and encapsulation?
– Is Vtable per class/object?
– Difference between COFF and ELF?
– Compiler phase of function in-lining?
– Difference between macro and function in-lining?
– Types of parsers?
– YACC is which parser?
– How lex and yacc work?
– How does linker gets info about static variables?
– How protected specifier works in C++?
– What is memory leak?
– Accessing static outside its scope?
– How assembler works?
– When do we do semantic analysis?
– You have a file with write-only access, and you have N threads. How would you ensure sharing of this file among these threads considering performance a prime concern?
– Singleton class? A real world example.
– How would you classify a file based on contents of the class? How many search keyword in the file are necessary?

>Bouquet of questions-3

>o How can you add attributes in gcc, e.g., changing function call way from cdecl to stdcall?

GCC allows you to attribute your functions with __attribute__ macro.
This macro allows you to write more readable, clean code. I liked,

– __attribute__((destructor))
– __attribute__((constructor))
– __attribute__((warning(“Function definition is not found)))

There are plenty of them, check them out at: http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

o What does following code do?

int flag = 2;
int (*fp)(void) __attribute__((cdecl));
void fun() __attribute__((warning(“No definition”)));
int main(void)
fp = main;
while(flag–) {
int x = (*fp)();

>Bouquet of questions-2

>o What is a bus error?

Bus refers to address bus and an error means passing an illegal address to the address bus. There are two signals that can be sent by kernel for an illegal address:

A SIGBUS is issued for an obvious wrong virtual address, there is no address translation and CPU can outright say that address is bogus.

SIGSEGV is issued when after translating the address, CPU realizes that address is bogus.

SIGBUS is always better since it avoids address translation. But which signal to issue, depends on CPU.

o What will be the output?

int a[10];
printf(“%d”, sizeof(a);

>Bouquet of questions

>o What is a cache line?

– Smallest unit of data transfer between cache and main memory.
– Finest level of granularity

o How can I get IPC information over processes?

– Use $ipcs

o Tell the value of enum’s elements.

enum e_tag{ a, b, c, d=20, e, f, g=20, h

– a=0, b=1, c=3, d=20, e=21, f=22, g=20, h=21

o Why use volatile?

– To avoid compiler optimization on variables involved in two cases:
+ Shared library
+ Value updated implicitly by hardware

Avoid optimization of Load/Store instructions by the compiler.
e.g., If you have a variable(X) that reads from a shared lib variable(Y), while Y is being updated by other process. Compiler would have no idea of this dependency of external value updates and it may remove these LOAD/STORE instructions during compilation.