Saturday, February 16, 2013

Process (notes)

 

- a program in execution
- a process contains -> program counter, process stacks & data section.

- process memory layout

Command Line Aruguments

Stack
Stack grows down & Heap grows Up
Heap
Data section (includes BSS)
Text section

    CLA - command line arguments,stored at higher address
    stack - local variables & other info (return info,machine register) stored. In each recursive call, new stack frame is created.
    headp - dynamic memory allocation, shared to all process.
    BSS - Block started by symbol (uninitialized global data)
    data segment - initialized global variables
    text - contains machine instrucitons that cpu executes, shared across various instances of same program. (read only privileges, avoid rewriting)

- process states

process_state_diagram
    new - a process being created
    ready - instructions are executed
    waiting - waiting for some event
    running - waiting to be assigned to processor
    terminate(done) - process has finished

- zombie process – system is referencing the process even if process is  terminated.

- info associated with each process
    Process state
    Program counter
    CPU registers
    CPU scheduling information
    Memory-management information
    Accounting information
    I/O status information

- process queues - job , ready , device
- process scheduler
    - Long term (job) - selects which process to put in ready queue (frequent invoked - very fast)
    - Short term (cpu) - selects which process to execute & allocate CPU (infrequently invoked - very slow)
   
- context switch
    - switching from one process to another, system saves the state of the one process & load the state of new process

- processes can be independent or cooperating
- cooperating process need IPC - shared memory , message passing
    - advantage for co-operating process
        - info sharing
        - computation speedup
        - convenient
        - modularity

    - message passing
        - send /recieve
        - establish connection
        - blocking send / non-blocking
    - shared memory
        - producer consumer problem

Thursday, December 27, 2012

Compilation steps of a C program

There are four steps

1. Preprocessing
2. Compilation
3. Assembling
4. Linker

image

STEPS INPUT OUTPUT GCC
Pre Processing source file

preprocessed output (removing all # defines & replacing macros)

gcc -E a.c
output will be printed in console where we can see all the Macro are replaced.

Compilation

Preprocessed output file

Assembly language code

gcc -S a.c
output will be in a.s file

Assembling

Assembly code

Machine code

gcc -c a.c
output will be in a.o

Linker

Machine code

executable file

gcc a.c
output will be a.out file

Monday, December 24, 2012

Cisco Interview Questions

Had 5 rounds of interview

1. Telephonic technical Round
    * steps of compilation
    * storage classes & memory allocation
    * pass by value & pass by reference
    * questions on pointers
    * stack & heap in C
    * Reverse a linked list
    * Middle element of a linked list
    * difference between insertion & quick sort
    * time complexities of all sorting algorithms
    * difference between TCP & UDP

2. Technical round-1 (F2F)
    * TCP/IP layer
    * how ping works
    * IP header
    * questions about data link layer

3. Technical round-2 (F2F)
    * Find a duplicate in an array of 1 to n elements
    * swap without comparison
    * deletion of a linked list
    * find a loop in a linked list
    * set a bit in a integer
    * stack & heap - malloc & calloc
    * some questions in pointers

4. Technical round-3 (F2F)
    * find the sum of array using recursion
    * pattern matching
    * find the intersection of two linked list and questions on projects done.

5. Manager round (F2F)
    * how trace route works
    * how route works
    * ipconfig works
    * some HR questions like , how you manage pressure, strength etc.,

Monday, November 12, 2012

Implement atoi function


#include<stdio.h>
main()
{
// Declarations
    char str[100];
    int i,n,sign;
// Initializations
    i=0;
    n=0;
    scanf("%s",str);
// First string can be '+','-','tab','space','newline',numbers
    if((str[0]!='+') && (str[0]!='-') && (str[0]!='\t') && (str[0]!='\n') && (str[i]==' '))
    {
    if((str[0]<'0') || (str[0]>'9'))
    {
        return ;
    }       
    }
    else
    {
    // Getting the sign value
    sign=(str[0]=='-')?-1:1;
    if((str[i]=='+') || (str[i]=='-') || (str[i]=='\n') || (str[i]=='\t') || (str[i]==' '))
    i++;
   
    // Converting string to integer
    while(str[i]>='0' && str[i]<='9')
    {
        n=n*10+(str[i]-'0');
        i++;
    }
   
    n=n*sign;
    printf("%d",n);
    }           
}

Monday, October 1, 2012

Merging of two sorted arrays

 

Given two sorted arrays A & B where size of A is m & size of B is m+n .  B contains only n number of elements & m empty positions. Sorting should not take any extra memory.

#include<stdio.h>
main()
{
int A[]={1,3,7,10};
int B[7]={2,4,11};
int size_A;
int size_B;
int i;
size_A=sizeof(A)/sizeof(int);
size_B=sizeof(B)/sizeof(int);
size_A--;
size_B--;
int s=size_B;
int B_Elements= (size_B - size_A);
B_Elements--;

while((size_A >=0) && (B_Elements >=0))
{
if( (A[size_A] ) >= (B[B_Elements])){
        B[size_B]=A[size_A];

        printf("\n%d > = %d",A[size_A ] , B[B_Elements ]);
        size_A--;
        size_B--;
}
else
{
printf("\n%d < = %d",A[size_A ] , B[B_Elements ]);
B[size_B] = B[B_Elements];
size_B--;
B_Elements--;
}
}

if ( size_A >=0 ){
    while ( size_A >= 0 ){
B[size_B] = A[size_A];
size_A--;
size_B--;
    }
}
else if ( B_Elements >=0 ) {
    while (B_Elements >=0){
B[size_B] = B[size_B];
    B_Elements--;
    size_B--;
    }
}
else {
printf("Merging done");
}
printf("\n");
for(i=0;i<=s;i++)
printf("%d  ",B[i]);
}

Permutation of a given string


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void swap(char *x , char *y)
{
    char temp;
    temp=*x;
    *x=*y;
    *y=temp;
}
void do_permute(char *a, int start , int size)
{
    int itr;
    if ( start == size ){
        printf("%s\n",a);
    }
    else{
        for(itr=start;itr<size;itr++)
        {
            swap((a+start),(a+itr));
            do_permute(a,start+1,size);
            swap((a+start),(a+itr));
        }
    }
}
main()
{
    printf("PERMUTATION\n");
    char a[100];
    int size;
    scanf("%s",a);
    size=strlen(a);
    do_permute(a,0,size);
}