Sunday, September 30, 2012

Interview Questions BB 1

1. How many sockets can you have?you can listen on 65536 different ports. Some of these may be in use or reserved, so you may have fewer actual port availabilities.
what will a server do after getting a request from a client?
2. Multicast vs Broadcast?
When a device send a messege to all possible receiver it is called broadcasting. This message is received by all other devices. Destination address of message contain all bits set to 1. While when a device send a message to a group of devices it is called multicasting. Message is received by only devices which belongs to group mentioned in address field.Broadcast, FM radio and For Multicast, sending group mails.
3.atoi() function in Java and C++ ?
public static void main(String[] args) {
// TODO Auto-generated method stub
String input = "786786";
int length = input.length();
int sum = 0;
int convertedDig = 0; //Converted digit from char
int startIndex = 0; //StartIndex based on sign
int sign = 1; //Default sign is +ve.

//Check for -ve number.
if(input.charAt(0) == '-'){
sign = -1;
startIndex = 1;
}

//Iterate over String starting from 1st digit.(startIndex)
//
for(int i=startIndex;i<length;i++){
switch(input.charAt(i)){
case '0' : convertedDig = 0; break;
case '1' : convertedDig = 1; break;
case '2' : convertedDig = 2; break;
case '3' : convertedDig = 3; break;
case '4' : convertedDig = 4; break;
case '5' : convertedDig = 5; break;
case '6' : convertedDig = 6; break;
case '7' : convertedDig = 7; break;
case '8' : convertedDig = 8; break;
case '9' : convertedDig = 9; break;
}
sum += convertedDig * Math.pow(10, (length-i-1));
}
System.out.println("The atoi conversion is "+sum*sign);
}

4. Differences between pipes, FIFOs, message queues, shared memory, and sockets

Pipe
  • In computer programming, especially in Unix operating systems, a pipe is a technique for passing information from one program process to another.
  •  Unlike other forms of interprocess communication (IPC), a pipe is one-way communication only.
  • A pipe is fixed in size and is usually at least 4,096 bytes.
  •  Basically, a pipe passes a parameter such as the output of one process to another process which accepts it as input. The system temporarily holds the piped information until it is read by the receiving process.
  • Using a UNIX shell (the UNIX interactive command interface), a pipe is specified in a command line as a simple vertical bar (|) between two command sequences. 
  • The output or result of the first command sequence is used as the input to the second command sequence. The pipe system call is used in a similar way within a program.
For two-way communication between processes, two pipes can be set up, one for each direction. A limitation of pipes for interprocess communication is that the processes using pipes must have a common parent process (that is, share a common open or initiation process and exist as the result of a fork system call from a parent process).
Named pipe (FIFO Pipes)

  • In computer programming, a named pipe is a method for passing information from one computer process to other processes using a pipe or message holding place that is given a specific name. 
  • Unlike a regular pipe, a named pipe can be used by processes that do not have to share a common process origin and the message sent to the named pipe can be read by any authorized process that knows the name of the named pipe.
  • A named pipe is sometimes called a "FIFO" (first in, first out) because the first data written to the pipe is the first data that is read from it.
  • While you can use only stdin and stdout with ordinary pipes (i.e redirect input and output streams from one process into another ), using FIFOs you can use filenames rather than just stdin and stdout.

    example:

    mkfifo pipe_test
    grep "test" my_file.txt > pipe_test
    wc -l < pipe_test
Message queuing

  • In programming, message queuing is a method by which process (or program instances) can exchange or pass data using an interface to a system-managed queue of messages
  • Messages can vary in length and be assigned different types or usages. 
  • A message queue can be created by one process and used by multiple processes that read and/or write messages to the queue
  • For example, a server process can read and write messages from and to a message queue created for client processes. 
  • The message type can be used to associate a message with a particular client process even though all messages are on the same queue.
  • The message queue is managed by the operating system (or kernel)
  • Application programs (or their processes) create message queues and send and receive messages using an application program interface (API)
  • In Unix systems, the C programming language msgget function is used with various parameters specifying the action requested, message queue ID, message type, and so forth.
  • The maximum size of a message in a queue is limited by the operating system and is typically 8,192 bytes.
Shared memory
  •  In computer programming, shared memory is a method by which program processes can exchange data more quickly than by reading and writing using the regular operating system services. 
  • For example, a client process may have data to pass to a server process that the server process is to modify and return to the client. Ordinarily, this would require the client writing to an output file (using the buffers of the operating system) and the server then reading that file as input from the buffers to its own work space. Using a designated area of shared memory, the data can be made directly accessible to both processes without having to use the system services. To put the data in shared memory, the client gets access to shared memory after checking a semaphore value, writes the data, and then resets the semaphore to signal to the server (which periodically checks shared memory for possible input) that data is waiting. In turn, the server process writes data back to the shared memory area, using the semaphore to indicate that data is ready to be read.

Other forms of interprocess communication (IPC) include message queueing, semaphores, and sockets.
A good source for programming in C for shell and synchronization : http://www.cs.cf.ac.uk/Dave/C/
Sockets is a method for communication between a client program and a server program in a network. A socket is defined as "the endpoint in a connection." Sockets are created and used with a set of programming requests or "function calls" sometimes called the sockets application programming interface (API). The most common sockets API is the Berkeley Unix C interface for sockets. Sockets can also be used for communication between processes within the same computer.

This is the typical sequence of sockets requests from a server application in the "connectionless" context of the Internet in which a server handles many client requests and does not maintain a connection longer than the serving of the immediate request:

    socket()
    |
    bind()
    |
    recvfrom()
    |
    (wait for a sendto request from some client)
    |
    (process the sendto request)
    |
    sendto (in reply to the request from the client...for example, send an HTML file)

A corresponding client sequence of sockets requests would be:

    socket()
    |
    bind()
    |
    sendto()
    |
    recvfrom()

Sockets can also be used for "connection-oriented" transactions with a somewhat different sequence of C language system calls or functions.


No comments:

Post a Comment