Problem Write a function called my2DAlloc which allocates a two dimensional array. Minimize the number of calls to malloc and make sure that the memory is accessible by the notation arr[i][j].
Solution We will use one call to malloc.
Allocate one block of memory to hold the row vector and the array data. The row vector will reside in rows * sizeof(int*) bytes. The integers in the array will take up another rows * cols * sizeof(int) bytes.
[Read More]
Aligned malloc in C
Problem Write an aligned malloc & free function that takes number of bytes and aligned byte (which is always power of 2).
EXAMPLE
align_malloc (1000,128) will return a memory address that is a multiple of 128 and that points to memory of size 1000 bytes.
aligned_free() will free memory allocated by align_malloc.
Solution We will use malloc routine provided by C to implement the functionality.
Allocate memory of size (bytes required + alignment – 1 + sizeof(void*)) using malloc.
[Read More]
Device FIFO queue problem
Problem A device boots with an empty FIFO queue. In the first 400 ns period after startup, and in each subsequent 400 ns period, a maximum of 80 words will be written to the queue. Each write takes 4 ns. A worker thread requires 3 ns to read a word, and 2 ns to process it before reading the next word. What is the shortest depth of the FIFO such that no data is lost?
[Read More]
Prevent reverse engineering of DLLs
Problem What are the best practices to prevent reverse engineering of DLLs?
Solution Why do people want to reverse engineer a DLL?
Hacking/Cracking/Cheating - This is pretty easy actually. You just need to defeat the multiple anti circumvention measures. Without getting caught. The penalties for getting caught are high. DMCA violations, getting CD KEY banned from game, lawsuits… Any good debugger will do. Wikipedia has a good article on Windows debuggers.
[Read More]
How to make sure a process doesn’t access unauthorized part of stack
Problem Discuss how would you make sure that a process doesn’t access an unauthorized part of the stack
Solution As with any ambiguously worded interview question, it may help to probe the interviewer to understand what specifically you’re intended to solve. Are you trying to prevent code that has overflowed a buffer from compromising the execution by overwriting stack values? Are you trying to maintain some form of thread-specific isolation between threads?
[Read More]
What happens after a user presses a key on the keyboard?
Problem Write a step by step execution of things that happen after a user presses a key on the keyboard. Use as much detail as possible.
Solution Following are the steps
You hear a click sound :P ;) The keyboard sends a scan code (electrical signal) of the key to the keyboard controller (Scan code for key pressed and key released is different). The keyboard controller interprets the scan code, i.
[Read More]
Direct memory access(DMA)
Problem Describe direct memory access (DMA). Can a user level buffer/pointer be used by kernel or drivers?
Solution Direct Memory is a feature which provides direct access (read/write) to system memory without interaction from the CPU. So, if DMA is available, then processor can route long read or write requests of disk blocks to the DMA and concentrate on other work.
The “DMA Controller” manages this by requesting the System bus access (DMA request) from CPU.
[Read More]
Branch Target buffer
Problem What is a Branch Target buffer? Explain how it can be used in reducing bubble cycles in cases of branch misprediction.
Solution Branch misprediction occurs when the CPU mispredicts the next instruction to be executed.
The CPU uses pipelining which allows several instructions to be processed simultaneously. But during a conditional jump, the next instruction to be executed depends on the result of the condition. Branch Prediction tries to guess the next instruction.
[Read More]
Virtual memory, page fault and thrashing
Problem Explain the following terms: virtual memory, page fault, thrashing.
Solution In short:
Virtual memory is a technique that uses hard disk pages as main memory. Page fault is the error occurred when transferring pages. Thrashing - When paging occurs very very frequently, causing performance degradation. Now lets go in details:
Virtual Memory
Virtual memory is a computer system technique which gives an application program the impression that it has contiguous working memory (an address space), while in fact it may be physically fragmented and may even overflow on to disk storage.
[Read More]
To determine whether machine is Little-Endian and Big-Endian?
Problem Write a program to find whether a machine is big endian or little endian.
Solution What Little-Endian and Big-Endian? How can I determine whether a machine’s byte order is big-endian or little endian? How can we convert from one to another?
First of all, Do you know what Little-Endian and Big-Endian mean?
Little Endian means that the lower order byte of the number is stored in memory at the lowest address, and the higher order byte is stored at the highest address.
[Read More]