Search This Blog

Thursday, October 27, 2011

How scheduler gets executed in vxworks

Scheduler can be invoked either synchronously or asynchronously or both. Scheduler is not a task, it is mere a function call invoked in the context of the interrupt or from the invoking task context
Asynchronous method
-------------------------
When a BSP is configured, we also configure the hardware timer interrupt's which invokes the scheduler on its expiry. Together with this every time an interrupt occurs i.e. Ethernet interrupt, clock interrupt etc the scheduler would be called.
Synchronous method
------------------------
When ever the current executing task gets blocked the scheduler would be called. 
Note: Scheduler would be called each time a system call is called. 
         

Sunday, April 10, 2011

bit magic and mathematics

Multiply by 2 
 Method 1: x * 2 
 Method 2: x << 2 
Dividing by 2
 Method 1: x / 2 
 Method 2: x >> 2 
*Same way multiplication and division operation can be carried out for numbers by shifting. This  
  operation is applicable if we need a even multiple
Find whether the number is multiple of 4 
A number is multiple of 4, if the last 2 digits of the number is divisible by 4. 
To be noted: The bit pattern representation of 4 is 100 i.e. the last 2 bits are 0. 
                   It can be another way to find the divisibility
Given a number x: if (x % 4 == 0) => divisible by 4
                     x: if (x ^ 3  == 3) => divisible by 4
Round off a number to a nearest number divisible by 4
Possible values on dividing a number by 4 is 0,1,2,3. 
Method 1:
Given a number x:
           y = x % 4;
           switch (y)
           {
              case 0: 
                return x;
              case 1:
              case 2:
               return x - y;
              case 3:
               return x + 1;
           }
Method 2:
Given a number x:
         switch ( x & 3)
         {
            case 0:
              return x;
            case 3:
              return x++;
            case 1:
            case 2:
             return x -= (x & 2) + ( x & 1);
         }
Method 3:
Given a number x:
       x =  (( x & 3 ) && (x + 1) ) || (x & ~3);
Find whether a given number is a multiple of 2 / a number an even number
Method 1:
Given a number x:
    if (x % 2 == 0)  => true
Method 2:
Given a number x:
   if ( x & 1) => false 
Method 3:
Given a number x:
   if ( x & (x-1)) => true
logic here is that in case of number 1 less than the even number, all the lower order bits would get set
Find the lowest bit set of a given number:  
Method 1:
Given a number x:
    x & -x gives the lowest bit set
Finding a complement of a number/reversing the bits in a given number:
Method 1:
Given a number x:
  ~x gives the complement of x i.e. set bits becomes unset and vice-versa
Method 2:
Given a number x:
  -x == ~x + 1
  so ~x == -x -1 
  so another way of expressing would be -(x + 1)
Method 3:
Given a number x:
  XOR operation can be used to reverse the set bits. 
  x ^ ~0 where ~0 sets all the bit to 1
Adding 2 numbers
Method 1:
Given 2 numbers x and y:
 x + y <==> (x & y) + (x | y)
Finding the maximum between 2 numbers:
Method 1:
Given 2 numbers x and y:
max = y;
if ( x > y)
  max = x;
Method 2:
Given 2 numbers x and y:
  max(x, y) == x + ((y - x) & -(x < y))


importance of x ^ y ^ z 
if ( z == x ) then x ^ y ^ x returns y
Swapping 2 numbers:
Method 1:
Given 2 numbers x and y:
  swap (int x , int y)
  {
     int temp = x;
     x = y;
     y = x;
  }
Method 2:
Given 2 numbers x and y:
  swap (int x, int y)
  {
    x ^= y;
    y ^= x;
    x ^= y;
  }
Bit interleaving:
Method 1:
Given 2 numbers x and y, interleave the bits in z such that the bits in x form the odd and that of y in even position
for ( i = 0; i < 8 * sizeof (i); i++)
{
  z |= ( ( x & (1U << i) ) <<  i ) | ( ( y & (1U << i) ) << (i + 1) )
}
Determine the sign of a number:
      -1  if x < 0
      +1 if x > 0
       0  if x = 0
Method 1:
Given a number x:
  s8 sign_num (s32 x)
  {
     return (x > 0 + x < 0);
  }
Method 2:
Given a number x:
   (x >> 32) results in -1 or 0 depending on whether x is signed or unsigned number respectively.
   (x >> 32) || ( (u32)(-x) >> 32 )
Returning the absolute value of a integer number:
Method 1:
Giver a number x:
int abs_val(int x)
{
  return x > 0:x ? -x;
}
Method 2:
Given a number x:
int abs_val (int x)
{
   int int_type = x >> 32;  // -1 for signed and 0 for unsigned types
   return (x ^ int_type) - int_type;
}
setting a bit at a particular position:
Method 1:
Given a number x and bit to be set @ position pos
int set_pos (int pos, int &x)
{
  return x | (1 << pos);
}
unsetting a bit at a particular position:
Method 1:
Given a number x and bit to be unset @ position pos
int set_pos (int pos, int &x)
{
   return x & ~(1<< pos);
}
Flipping bit at a particular position:
Method 1:
Given a number x and bit to be flipped is @ position pos
int flip_pos (int pos, int &x)
{
  return x ^ (1 << pos);
}
Check whether the bit at a particular position is set:
Method 1:
Given a number x and bit state to be determined @ position pos
int state_pos(int pos, int &x)
{
  return x & ( 1 << pos);
}
Modify a particular bit at a particular position:
Method 1:
Given a number x and bit state to be changed @ position pos
int modify_bit(int pos, int &x, bool val_to_set)
{
  return ( x & ~(1 << pos) ) | ( (-1) & (1 << pos) );
}
Unset the rightmost set bit 
Method 1:
Given a number x 
  (x & x -1) turns the 1st rightmost bit off.
Determine whether a given number is of the form 2 pow n -1
Method 1:
Given a number x:
   (x & (x + 1)) returns 0 if true
Extract the rightmost 1st set bit of a given number x
Method 1:
Given a number x:
  x & -x returns either 0 or rightmost set bit

Extract the rightmost 1st 0 set bit of a given number x
Method 1:
Given a number x:
  -x & (x + 1) returns either 0 or rightmost set bit

Monday, March 21, 2011

UML simplified

  • UML stands for the Unified Modeling Language (a standard modeling language)
  • it acts a bridge between visionary and developer. It captures the vision for a system and enables as to communicate the vision using set of symbols and diagrams. Each such diagram has certain role to play in the development process
  • UML diagrams acts as a blueprint for the developers in building the solution for the client
In the software development life cycle there involves the role of many players.Analyst documents the requirement of the client. This document is then put forward to the developer to code and provide the final solution. It might happen that the requirement might not be clear to developer or mis-interpreted by analyst etc lot of permutation and combination. So there is a need of an organized design process to which all the players can agree upon. UML provides this organized information.
  • UML notation acts as a notion or blue print for sold design
  • Multiple views of a system can be represented using various models. These models tells what the system does not how it is to be implemented. A set of diagrams that enable us to examine, assess and modify in order to understand and develop a system
  • different UML models --

              1. class diagram -- useful in the requirement gathering from the client
              2. Object diagram -- 
              3. Use case diagram -- representation of the system behavior from a user's standpoint.              So the tool is useful for gathering system information from an end user. 
              4. State diagram -- depicts the state of an object. 
              5. Sequence diagram -- 
              6. Activity diagram -- 
              7. Communication diagram -- 
              8. Component diagram -- 
              9. Deployment diagram -- shows the physical layout of a computer based system
              10.Composite structure diagram -- represents the internal of a class
             11.Interaction diagram
             12. Timing diagram
             13. package diagram -- holds the list of class that are present in the subsystem


Interface is a class that has behaviors but no attributes in it



Saturday, March 19, 2011

ALL ABOUT SOCKETS

Note folks, the below contents are arbitrary collated information about sockets. 
  • sockets are said to be connection end points. They are compared to the telephone systems installed present at the end users 
  • unix treats sockets identical to the files, socket descriptor returned for a socket call is next available file descriptor 
  • by default sockets are full-duplex communication mode
  • seek operation, valid on files cannot be performed on socket
  • On closing a socket, remote process gets an EOF indication 
  • shutdown contrast to close is used to perform partial closure of socket. 
        syntax: int shutdown ( int socket_des, int how);
                
HOW
Significance
0 (SHUT_RD)
No further read allowed
1 (SHUT_WR)
No further write allowed
2 (SHUT_RDWR)
No further read and write allowed (equiv close)
  • performing a shutdown of write, makes the kernel to flush all the pending data to be sent, send immediately. Causes an EOF indication, to be sent to the remote socket as an indication that no more data would be sent on this socket
  • if the socket descriptor is duplicated then only after last close is that the actual socket closure is performed
  • shutdown on a duplicated socket, results on an immediate affect
  • sockets created prior to the fork invocation, are duplicated in the child process also
  • shutdown of the read on socket, causes any pending read data to be silently ignored. 
  • int socket (int domain, int connection_type, int protocol); 
        domain => selects the address family (AF_INET)
        connection_type => selects the type of the connection i.e. connected, connectionless   
                                    SOCK_DGRAM, SOCK_STREAM, SOCK_RAW
        protocol => 0 if selected by default then OS takes care of selecting the protocol type

    Generic socket address sizeof 16 bytes
       struct sockaddr 
       {
          sa_family_t sa_family;
          char          sa_data[14];
       };


   internet socket IPV4
       socket sockaddr_in
       {
         sa_family        sin_family; 
         u16               sin_port;
         struct in_addr sin_addr;  // holds the IP address in the network byte order
         u8                 sin_zero[8];
       };
       struct in_addr
       {
          u32  s_addr;
        }

  • network byte order is big endian format. It is fixed to a particular format to attend harmony between various systems using either little or big endian format
  • different functions to convert host to network order and vice versa
         unsigned long htonl(unsigned long hostlong); 
         unsigned short htons(unsigned short hostshort);
         unsigned long ntohl(unsigned long netlong); 
         unsigned short ntohs(unsigned short netshort);
  • assigning sin_port with zero, implies a request to kernel to assign an local port number for the socket connection
  • sin_addr is a byte by byte representation of ip address in the hexal formal. Implies 127.0.0.1 would yield 7F000001
IP Addressing
  • IP address follow dotted decimal notation and is of format a.b.c.d where a,b,c,d are unsigned value of a byte
  • IP consist of 2 components 
               1. Network byte => identifies the network where the host can contacted
               2. Host number  => identifies the host out of the several on a particular network
  • netmask is a value which when bitwise ored with the IP address give the network address of a corresponding network. 
        Below table shows the netmask of the known classes in the network
  • certain network addresses are reserved for private addressing i.e. strictly to be used within an organization
SOCKET contd ....
  • inet_addr function converts the ip address from the string format to long
                      unsigned long inet_addr (const char* ip_addr); 
        // returns INADDR_NONE if the input ip address is not in the dotted decimal notation

  • ipnet_aton is another function that can be used to convert the ip address from string notation to the corresponding long format
                    int inet_aton(const char *string, struct in_addr *addr);

  • inet_ntoa is a function used to convert the hexal representation to corresponding ip address notation in string
                  char *inet_ntoa(struct in_addr addr);
  • inet_network converts the ip address to host order hex val
                   unsigned long inet_network(const char *addr); 
  • inet_netof returns the network address in hex for an address
                  unsigned long inet_netof(struct in_addr addr);
  • inet_lnaof returns the host address in hex for an address
                  unsigned long inet_netof(struct in_addr addr);
  • inet_makeaddr returns the ip address in hex, given network addr and host addr in hex
                  struct in_addr inet_makeaddr(int net,int host);







    Tuesday, March 8, 2011

    what is fragmentation ?

    Fragmentation refers to the inefficient storage of the memory space.

    • It leads to low storage capacity and performance degradation to say the least.
    • There are 3 different types of fragmentation that can be seen in GPOS i.e External, internal and Data fragmentation
    Internal Fragmentation: storage allocated by the OS to increase the efficiency. 
    • These storage is present inside the allocated region but never being used. Hence gets the term "internal"
    • instances -- structure padding, the padding is done to increase the CPU read and write cycle but is unusable and is wasted 
    • File starts from the beginning of a cluster, so the space between the EOF and the beginning of the next cluster called File slack is wasted
    External Fragmentation:This problem arises due to the inefficient storage allocation algorithms
    • Every time an application asks for data allocation/deallocation then the continues memory is left fragmented, not contiguous
    • Since this happens outside the allocated area it is called external fragmentation
    • Ideally we would expect OS as a memory manager, would serialize all the available memory but performing this is not as simple as this text :-)
    • This can lead to problem like application asking for 1024 bytes of memory but we are left with 1000 bytes, 300 bytes, 230 bytes of chunk of memory. This would result in the memory allocation failure even though we have enough memory left in our RAM
    Data fragmentation: This problem arises when related memory chunks are non-continues i.e. broken up into many segments and each not close to each other. Here related memory implies that the memory used for storing some file, image etc that which ideally would be expected to be stored in continues memory location
    • This lead to increase in seek time i.e. data fetch time
    • A file or an image etc with data fragmented are stored in different memory location are linked together using the linked list. So to open a file, it might take to traverse a linked list of segments of memory which holds that file information. 
    Note: Data fragmentation is a possible scenario to highlight the application of linked list in interviews