Friday, May 20, 2016

IPC part 2

Difference between pipe and message queues(POSIX)
  • Pipes aren't limited in size, message queues are.
  • Pipes can be integrated in systems using file descriptors, message queues have their own set of functions, though linux supports select(), poll(), epoll() and friends on the mqd_t.
  • Pipes, once closed, require some amount of cooperation on both sides to reestablish them, message queues can be closed and reopened on either side without the coorporation of the other side.
  • Pipes are flat, much like a stream, to impose a message structure you would have to implement a protocol on both sides, message queues are message oriented already, no care has to be taken to get, say, the fifth message in the queue
Difference between shared memory and message queues
Shared memory is exactly what you'd think: it's an area of storage that can be read and written by more than one process. It provides no inherent synchronization; in other words, it's up to the programmer to ensure that one process doesn't clobber another's data. But it's efficient in terms of throughput: reading and writing are relatively fast operations.
A message queue is a one-way pipe: one process writes to the queue, and another reads the data in the order it was written until an end-of-data condition occurs. When the queue is created, the message size (bytes per message, usually fairly small) and queue length (maximum number of pending messages) are set. Access is slower than shared memory because each read/write operation is typically a single message. But the queue guarantees that each operation will either processes an entire message successfully or fail without altering the queue. So the writer can never fail after writing only a partial message, and the reader will either retrieve a complete message or nothing at all.
Also the following information is useful for difference between shared memory and message queues
When using shared memory with consideration for possible race-conditions where one process writes to it and another reads from it, something to bear in mind. There is an associated risk of using the former, suppose two processes are using it, one to write to it, the other to read from it, the one that is writing dies due to abnormal condition, the process reading it could hang or crash.
Shared memory can be deemed as faster (low overhead, high volume of data passing) then queues. But queues on the other hand, requires high overhead (the set up for making a queue to be permanent etc) with low volume of data.
The onus with shared memory is that you have to implement synchronization in order to be thread safe. Have a look at the excellent article by Beej on IPC.
When using Queues, they are thread-safe, and not alone that, messages are held in the queue regardless of the outcome, suppose two processes are using the queue, when one process writes to it (in a form of a message) and the other process that is about to read from it gets to die or killed off due to a crash or abnormal condition under a such circumstance, that message is still in place, the other process if restarted can read from the queue, i.e. no data is lost.

Thursday, May 19, 2016

Some good questions

1) What is the output of the following program
#include <stdio.h>
int main()
{
      char *p = NULL ;
      printf("%d%s\n", p, *p) ;
}
Results:
          1) Warnings
int.c:5:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]
  printf("%d%s\n", p, *p) ;
  ^
int.c:5:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
        2) IF compiled, it will crash.

2) what are different types of communication(IPC)
Pipes, message queues, shared memory, signals.
Does pipes can be used between  childs of same parent.
IF single shell, having multiple processes. Can pipe will be used? No

3) how routing table lookup takes place
It uses IP address and subnet mask to fetch the best matching route.

4) How to decide the router to send the packet to upper layer or lower layer
router needs to determine based on the destination ip address present in the packet. If its own ip, then forward to L2 layer else forward to L4(tcp) layer.

5) which data structure will be used in routing table.
Tries.

6) Why hash tables are used.
They are good in insertion, deletion and search. All operations takes place with o(1) complexity.

7) If chaining is required, then why need to use Hash table.
At very first hand, we are not aware whether is there any duplicates will be present or not. Eventhough chaining will take O(n) time, still prefer hash.

8) Is message queues are synchronous or asynchronous?
Asynchronous

9) What are the different signals cannot be catched?
SIGKILL and SIGSTOP

10) Why threads are used over processes.
Two reasons
a) Communication between thereads are faster. Context swiching between threads is faster.

b) Easy to share the data between threads.
Other points
a) faster
b) need synchronization data between processes.
c) etc


Few more questions
1) How server will handle multiple clients?
2) what are vtables for pure virtual functions?
3) how malloc and free works
4) which ipc mechanism used for
5) what happen for child process whose parent is died
6) what is mutex and binary semaphore. Differences


1) which pattern will be used for memory management.
2) How the memory layout for dynamic libraries used for 2 processes.
3) why map.erase() will not work? what are the other operations should we use for deletion of entries in map.
4) What happens if destructor is called in private section of class.
5) Have you used templates?
6) why "this" is a pointer?
7) How gdb works internally?
8) what happens when you set a break point in GDB.
9) Does all the symbols will be loaded in GDB.