Monday, June 20, 2011

C Programming Tips and Tricks – Part1:

Signed/Unsigned types:

• Use unsigned types for bit field manipulation and binary masks. For others, use signed types.
• Use casts in expression to avoid compiler interpretation to choose results.

Malloc(strlen(str)) => Wrong (because, need to add 1 byte for null termination character)
Malloc(strlen(str)+1) => Right

Switch:
In switch statement, each case should be labeled by integer-valued constants.

Static:
• For variables => it retains its value between calls; scope is limited to inside function.
• For functions => visible in only this file.

Extern:
• For variables => defined it elsewhere.
• For functions => Global scope(and is redundant)

Declarations and definitions:
Extern int i => it is the declaration (doesn’t reserve space in memory)
Int i =10 => it is the declaration and definition (Creates type and name of the object and reserve space)

Typedef:
Typedef int integer;
Unsigned integer I; // illegal

Process Address Space:

Unmapped region => For null pointer references.


Object file:
• It contains Data segment, Text segment and details of the size required for BSS segment.

Dangling pointer -> doesn’t reference anything. (Generally occurs, when returning address of a local auto variable from function, as the variable no longer exists after function exited)


Memory Issues:
Memory Corruption:
Freeing or overwriting memory that is still in use.

Memory Leak:
Not freeing memory that is not in use.


Declaration of Structures and Unions:
struct/union optional-tag
{
type1 var1;
type2 var2;
} optional_variable_definitions;

→ For Unions, all the members have an offset zero.



Wednesday, June 8, 2011

Fibre Channel Topologies

Fibre Channel Topologies:




Point-to-point:

     • Simplest Topology
     • Consists of only two fibre channel devices connected directly together.
     • Transmission medium is dedicated (Not shared)
     • Providing guaranteed bandwidth, guaranteed latency and in-order frame delivery.

Arbitrated Loop:
     • Cost effective way of connecting 127 ports in a single network without the need of fabric switch.
     • Media is shared among the devices; Limiting each device’s access.
     • When a port needs to access the loop, it must first arbitrate to access the loop. Once a port wins arbitration, it opens a loop circuit with another port. This creates a dynamic point to point connection between the two ports allowing frame transmission to begin.
     • As long as loop circuit is opened, two ports have sole use of the loop. All other ports are acting as repeaters.
     • When the two ports finish their frame transmission, loop circuit is closed and available for other ports to use.

Switched Fabric:
     • Used to connect many (2 power 24) devices in a cross point switched configuration.
     • Many devices can communicate at the same time; Media is not shared; but it is costlier.

Note: Images from Wikipedia.

Fibre Channel Node Port

Fibre Channel Node Port:

Each node has one or more node ports. Node port is a hardware function used for sending and Receiving information via the Fibre Channel Interface.

Each node port has two unique ID’s.
     1. Port_Name
     2. N_Port_ID

Port_name => Unique 64 bit identifier assigned to the port at the time of manufacturing.
                     Used for Management purposes or when port identification is required.

N_Port_ID => Unique 24 bit identifier assigned at the time of initialization of node port and its attached topology.
                   Since it is 24 bit, the maximum size of Fibre Channel Network is 2^24 ports.
                   Used for Routing the information from sending port to receiving port and also used by receiving port to examine, whether the information delivered to correct port or not.
                   N_Port_ID may change, when the configuration is changed. (Because of this, Port_name should be used for identification of port)

Port is further identified based on the topology it supports. For example, to support arbitrated loop topology, the node port should capable of performing loop specific protocols. This kind of node port is called NL_Port.

Each port has separate Transmit and Receive function.
Port Transmit Function:
Port transmit function contains an encoder that transforms binary data into the format suitable for serial transmission by embedding clock information and frequency distribution.
After encoding, information is serialized and transmitted one bit at a time.

Port Receive Function:
Port receive function receives the serial data, recovers clock information from data stream, deserialize the received data, and decodes deserialized information.


Monday, January 17, 2011

Protocol Definition

We all know what protocol is. That is set of rules or guidelines to communicate between devices to exchange the information. But what are those basic guidelines. This post describes about the protocol basic guidelines.

Rules/guidelines are –

Discovery and Check:
  • We should know, whether node B is available or not
  • Once node B is discovered, we need to check regularly the availability of node B

Address:

  • Need to know the Node B’s address. Similarly from Node B to Node A address.

Language/Format:

  • Node B should understand, what Node A sent
  • Data start and end – Frame start and Frame end
  • Detect errors in data sent – Checksum and CRC

Control speed of Transmission:

  • Node B will have limited buffers. Hence need to control the speed of transmission.
  • Detect loss and etc.

Multiplexing:

  • Segregate the data of multiple application between Node A and Node B

Session:

  • Mechanism to organize the related data

Quality of Service (QoS):

  • Enable information exchange with specified bandwidth, losses, transmission latency, jitter (variance in latency)

But there are certain limitations to achieve QoS. There are

  • Limited bandwidth, but many flows are required – It can be resolved by multiplex flows and prioritize and allocate share.
  • Limited reliability, but perfection required – Resolved by Data Retransmission and Reliable Medium
  • Limited buffers, but need excessive transmission – Resolved by restricting the sender and discarding excess data.

So, now it is easy to understand any protocols using these guidelines :-)

Tuesday, January 11, 2011

Interview Questions - Linux Kernel Process Management

Process:A program (object code) in execution.
Also, includes set of resources such as open files, pending signals, one or more threads of execution, an address space, internal kernel data structures and data section containing global variables.

Process id:Each process has a unique id called pid, used by system for identification.

Process spawning:New process is created and loading the new image in process address space. In Linux, it is done by fork followed by exec system call.

Fork:Creates a new process called child and duplicates the resources (other than pid, ppid and pending signals) from parent process address space to the child process address space. It returns twice from the kernel. Once is the parent process and again in the new born child.

Copy-on-write:Creates a new process and shares the resources from Parent process until anything written to address space. Here child runs first. Best for process spawning.

Vfork:Creates a new process and share the resources (other than page table entries) from parent process. After child runs exec or exit, parent process begins. Here the problem is, if exec failed in child, then parent process will never get executed. Best for process spawning.

Process states – Current status of the process. There are 5 process states
Task Running – List of tasks in run queue
Task Interruptible – Tasks which are sleeping and waiting for the condition to occur. It will respond to signal or interrupts.
Task uninterruptible – Same as Task interruptible. But it won’t respond to signals or interrupts.
Task Zombie – Task are completed. But waiting for the wait call from the parent process
Task Stopped – Tasks explicitly stopped by signals such as SIGKILL, SIGSTOP

Process context:When a program executes a system call or triggers an exception, it enters into kernel space. At this point, Kernel is said to be “Executing on behalf of the process” and is in process context.

Threads:There is no concept of threads in Linux. Here threads are the normal processes which share the resources with other processes.

Kernel Threads:There are certain operations will be done in background. It can be done by Kernel threads. But kernel threads exist only in kernel space and no process address space (mm pointer is NULL).

Process Termination:Processes are terminated by exit system call or return from the main () program or any signal/exception received.
But it releases the process, but not the process descriptor and enters into zombie state.

Removal of Process descriptor:Parent process calls wait system call to child process to get the status and it removes the process descriptor.

Zombie process:When child process is completed, it enters into zombie state till parent process invokes wait system call to child process.

Orphan process:Parent process exits before child process terminated which puts child process in zombie state for ever and wasting system memory by holding process address space. This child process is called orphan process.

Process scheduling:Scheduler -> Kernel subsystem for selecting which process to run next.

Preemption:Process of suspending the running process and invoking the high priority process.

Scheduler policy:
I/O bound processes:
Process spends much of its time for submitting and waiting for an I/O. For example, Text editor.
- Low latency (Quick Response time) => High Priority
- short duration => Less timeslice value

Processor bound processes:Process spends much of its time for executing the program. For examples, video encoder.
- Not interactive process => Less priority
- Long duration => High Timeslice value

But some processes are both I/O bound and processor bound.

Process Timeslice:Amount of time the process to run until it is preempted.
Whenever the new child process is created, Half of the timeslice will be taken from parent process.
Whenever the timeslice of process expired, it will be recalculated.

Nice value – Priority of the process. It ranges from -19 to 20(default value is 0). Larger nice value corresponds to Low priority and Lower nice value for High priority.

Runqueue:Each processor will have runqueue which contains the list of all runnable process.

Waitqueue:Processes that are waiting for an external event to be occurred will be placed in waitqueue. As soon as the condition occurs, the process brings back to run queue for execution.

Init process:Invoked at the end of boot process and its Pid value is 1. Created statically by init_task, where as all other processes are created dynamically.

Storage Area Network (SAN) Basics

SAN:Storage Area Network – It interconnects different types of storage devices (such as disk arrays, Tape drives) with the servers, as if storage devices are locally attached to the servers.
Access - Block Level

NAS:Network Attached Storage – It is a file-level computer data storage connected to a network providing data access to heterogeneous clients.
Access - File Level

DAS:Direct Attached Storage – A storage device directly attached to the server without the storage network.
Access - Block Level

Redundant Array of Independent Disks (RAID):Redundant – An extra “COPY” of data
Array – Number of disks together

Note: Parity is calculated using Exclusive OR (XOR).

Hardware Raid Vs Software Raid:



LUN: Logical Unit Number – Unique identification number of a SCSI device logical unit.

Zoning:Process that partitioning fabric (switch) into smaller groups for adding security.
Each hosts connected to the SAN should have the allowed access to controlled subset of LUNs.
Port Zoning:It uses physical ports to define security zones.

WWN Zoning:It uses name server in the switch to either allow or block based on the WWN.

Lun Masking:Process that makes LUN available to some hosts and unavailable to other hosts.

FC SAN Topologies:
Point to Point:
It is the simplest topology which allows the host and storage connect directly.

Arbitrated Loop:· Devices are connected in a one way loop fashion in a ring topology.
· One node acts as a transmitter and next node acts as a receiver.
· A maximum supported storage device is 127.

Switched Fabric:Storage devices are connected to each other via switches.

WWN:8 byte(64 bit) number. Hardcoded into a fibre channel HBA, used for identifying individual port in fabric.
First 3 bytes number purchased from IEEE OUI(Organizationally Unique Identifier) and the rest supplied by the vendor.

WWNN and WWPN:WWPN -> Assigned to Port
WWNN -> Assigned to Node (endpoint or device). Different WWPN can have the same WWNN on the network.

Multipath:In a SAN environment, each LUNs are accessed through multiple paths, as the storage controller and HBA having multiple ports.