Sunday, 5 July 2015

Singly Linked List Operation


                                  SINGLY LINKED LIST OPERATION
#include<stdio.h>
#include<conio.h>


// Syntax for Creating Node
struct node
{
int data;                           // Type of data store in nodes
struct node *link;           // Link pointer for joining nodes
};
struct node *head=NULL;    // head pointer for pointing the first node of linked list
int item;                                // Integer temporary variable for inserting the data in linked list

void main()
{
 int choice;
 clrscr();
 printf("\nEnter 1:ADDEND \n 2:ADDBEG \n 3:ADDAFTER \n 4:DELBEG \n 5:DELEND \n 6:DELAFTER \n 7:DISPLAY \n 8:COUNT \n 9:EXIT");
  while(1)
  {
   printf("\nEnter Choice ");
   scanf("%d",&choice);
    switch(choice)
    {
    case 1:
    AddEnd();
    break;
    case 2:
    AddBeg();
    break;
    case 3:
    AddAfter();
    break;
    case 4:
    DelBeg();
    break;
    case 5:
    DelEnd();
    break;
    case 6:
    DelAfter();
    break;
    case 7:
    Display();
    break;
    case 8:
    Count();
    break;
    case 9:
    exit();
   }
 }
}

// Function for adding node in the last of the Linked List

int AddEnd()
{
 struct node *temp,*q;
  if(head==NULL)
  {
   temp=(struct node *)malloc(sizeof(struct node));
   printf("\nEnter Item ");
   scanf("%d",&item);
   temp->data=item;
   temp->link=NULL;
   head=temp;
  }
  else
  {
   q=head;
    while(q->link!=NULL)
    {
     q=q->link;
    }
   temp=(struct node *)malloc(sizeof(struct node));
   printf("\nEnter Item ");
   scanf("%d",&item);
   temp->data=item;
   temp->link=NULL;
   q->link=temp;
  }
 return 0;
}

// Function for adding node in the beginning of the Linked List

AddBeg()
{
 struct node *temp,*q;
  if(head==NULL)
  {
   temp=(struct node *)malloc(sizeof(struct node));
   printf("\nEnter Item ");
   scanf("%d",&item);
   temp->data=item;
   head=temp;
  }
  else
  {
   temp=(struct node *)malloc(sizeof(struct node));
   printf("\nEnter Item ");
   scanf("%d",&item);
   temp->data=item;
   temp->link=head;
   head=temp;
  }
 return 0;
}

// Function for adding node after some nodes(in the middle) of the Linked List

AddAfter()
{
 int pos,n;
 printf("\nEnter position to Insert new node");
 scanf("%d",&pos);
 n=Count();
  if(pos>n)
   printf("\nNo Available node are less");
  else if(pos==n)
   AddEnd();
  else if(pos==0)
   AddBeg();
  else if(pos>=1 && pos<n)
  {
   int i;
   struct node *q,*temp,*r;
   temp=(struct node *)malloc(sizeof(struct node));
   printf("\nEnter Item ");
   scanf("%d",&item);
   temp->data=item;
   q=head;
    for(i=1;i<pos;i++)
    {
     q=q->link;
      //r=q->link->link;
    }
   temp->link=q->link;
   q->link=temp;
  }
 return 0;
}

// Function for deleting node from beginning of the Linked List

DelBeg()
{
 struct node *q;
  if(head==NULL)
   printf("\nNo Node to Delete");
  else
  {
   printf("\n%d Data has Deleted",head->data);
   q=head;
   q=q->link;
   free(head);
   head=q;
  }
 return 0;
}

// Function for deleting node from last of the Linked List

DelEnd()
{
 struct node *q;
 q=head;
  if(head==NULL)
   printf("\nNo node to Delete");
  else
   {
    if(head->link==NULL)
     {
     printf("\n%d Data has Deleted",head->data);
     free(head);
     head=NULL;
     }
    else
    {
     while(q->link->link!=NULL)
      {
       q=q->link;
      }
     printf("\n%d Data has Deleted",q->link->data);
     free(q->link);
     q->link=NULL;
    }
  }
 return 0;
}

// Function for deleting node after some nodes(in the middle) Linked List

DelAfter()
{
 int pos,n,i;
 struct node *q,*r;
 printf("\nEnter position which is to be Delete ");
 scanf("%d",&pos);
 n=Count();
  if(head==NULL)
   printf("\nNo Available Node to Delete");
  else if(head->link==NULL)
  {
   printf("\n%d Data is Deleted",head->data);
   head=NULL;
   free(head);
  }
 if(pos==0)
 {
  printf("\nDELBEG Function Calling");
  DelBeg();
 }
 else if(pos==n)
 {
  printf("\nDELEND Function Calling");
  DelEnd();
 }
 else if(pos>=1 && pos<n)
 {
  q=head;
  r=head->link;
  for(i=1;i<pos;i++)
   {
    q=q->link;
    r=r->link;
   }
  printf("\n%d Data is Deleted",r->data);
  q->link=r->link;
  free(r);
 }
 return 0;
}

// Function for Counting nodes in Linked List

int Count()
{
 int i,counter=1;
 struct node *q;
 q=head;
  if(head==NULL)
   printf("\nTotal No. of Node is : %d",0);
  else
   {
    while(q->link!=NULL)
     {
      counter++;
      q=q->link;
     }
    printf("\nTotal no. of Node is : %d",counter);
    return(counter);
   }
 return 0;
}

// Function for displaying the Linked List

Display()
{
 struct node *q;
  if(head==NULL)
   printf("\nNo Linked List Available to Show");
  else
   {
    q=head;
     while(q!=NULL)
      {
       printf(" %d",q->data);
       q=q->link;
      }
   }
 return 0;
}

Tuesday, 30 June 2015

Dynamic Memory Allocation

DYNAMIC MEMORY ALLOCATION

Malloc, Sizeof, and Free

The malloc Function is used to allocate a block of memory on run time.

   void *malloc(number_of_bytes)

That is to say it returns a pointer of type void * that is the start in memory of the reserved portion of size number_of_bytes. If memory cannot be allocated a NULL pointer is returned.
Since a void * is returned the C standard states that this pointer can be converted to any type. The size_t argument type is defined in stdlib.h and is an unsigned type.
So:



    char *cp;
   cp = malloc(50);

attempts to get 50 bytes and assigns the start address to cp.
Also it is usual to use the sizeof() function to specify the number of bytes:


    int *ip;
   ip = (int *) malloc(100*sizeof(int));
 
Some C compilers may require to cast the type of conversion. The (int *) means coercion to an integer pointer. Coercion to the correct pointer type is very important to ensure pointer arithmetic is performed correctly. I personally use it as a means of ensuring that I am totally correct in my coding and use cast all the time.
It is good practice to use sizeof() even if you know the actual size you want -- it makes for device independent (portable) code.
sizeof can be used to find the size of any data type, variable or structure. Simply supply one of these as an argument to the function.

SO:



   int i;
   struct Node 
                  {
                    float x,y,z
                  };
   typedef struct Node sn;
 
   sizeof(int), sizeof(i),
   sizeof(struct Node) and
   sizeof(sn) are all ACCEPTABLE

In the above we can use the link between pointers and arrays to treat the reserved memory like an array. i.e we can do things like:

   ip[0] = 100;

or

   for(i=0;i<100;++i) scanf("%d",ip++);


When you have finished using a portion of memory you should always free() it. This allows the memory freed to be available again, possibly for further malloc() calls
The function free() takes a pointer as an argument and frees the memory to which the pointer refers.

Calloc and Realloc

There are two additional memory allocation functions, Calloc() and Realloc(). Their prototypes are given below:
void *calloc(size_t num_elements, size_t element_size};

void *realloc( void *ptr, size_t new_size);
Malloc does not initialise memory (to zero) in any way. If you wish to initialise memory then use calloc. Calloc there is slightly more computationally expensive but, occasionally, more convenient than malloc. Also note the different syntax between calloc and malloc in that calloc takes the number of desired elements, num_elements, and element_size, element_size, as two individual arguments.
Thus to assign 100 integer elements that are all initially zero you would do:


    int *ip;
   ip = (int *) calloc(100, sizeof(int));
Realloc is a function which attempts to change the size of a previous allocated block of memory. The new size can be larger or smaller. 
 If the block is made larger then the old contents remain unchanged and memory is added to the end of the block. If the size is made smaller then the remaining contents are unchanged.
  If the original block size cannot be resized then realloc will attempt to assign a new block of memory and will copy the old block contents.
 Note a new pointer (of different value) will consequently be returned. You must use this new value. If new memory cannot be reallocated then realloc returns NULL.
Thus to change the size of memory allocated to the *ip pointer above to an array block of 50 integers instead of 100, simply do:
   ip = (int *) calloc( ip, 50);

Monday, 8 June 2015

MultiProcessor Overview



Multiprocessors

Definition:
A Multiprocessor is an interconnection of two or more CPUs between memory and IO devices. The term ‘processor’ in multiprocessor can mean either a CPU or an input-output processor(IOP). However, a system with a single CPU and one or more IOPs is usually not included in the definition of a multiprocessor system unless the IOP has computational facilities comparable to a CPU. As it is most commonly defined, a multiprocessor system implies the existence of multiple CPUs, although usually there will be one or more IOPs as well.


         Bus-based multiprocessors

Flynn’s Classification of multiple-processor machines:
{SI, MI} x {SD, MD} = {SISD, SIMD, MISD, MIMD}
SISD = Single Instruction Single Data
            Classical Von Neumann machines.
SIMD = Single Instruction Multiple Data
             Also called Array Processors or
     Data Parallel machines.
MISD Does not exist.
MIMD Multiple Instruction Multiple Data
     Control parallelism.

A Taxonomy of Parallel Computers
                                
NUMA          Non Uniform Memory Access
COMA          Cache Only Memory Access
MPP              Massively Parallel Processor
COW             Cluster Of Workstations
CC-NUMA    Cache Coherent NUMA
NC-NUMA   No Cache NUMA
Characteristics of Multiprocessors
􀂋 Multiprocessors System = MIMD
􀁺 An interconnection of two or more CPUs with memory and I/O equipment
» a single CPU and one or more IOPs is usually not included in a multiprocessor system
􀂄 Unless the IOP has computational facilities comparable to a CPU
􀂋 Computation can proceed in parallel in one of two ways
􀁺 1) Multiple independent jobs can be made to operate in parallel
􀁺 2) A single job can be partitioned into multiple parallel tasks
􀂋 Classified by the memory Organization
􀁺 1) Shared memory or Tightly-coupled system
» Local memory + Shared memory
􀂄 higher degree of interaction between tasks
􀁺 2) Distribute memory or Loosely-coupled system
» Local memory + message passing scheme (packet or message )
􀂄 most efficient when the interaction between tasks is minimal
 

Tuesday, 31 March 2015

Comparison between 8085 and 8086 Microprocessors

 

S.No.8085 Microprocessor8086 Microprocessor
1It is a 8 bit microprocessor.It is a 16 bit microprocessor.
2It has 16-bit address lines.It has 20-bit address lines.
3 It has 8-bit data bus.
It has 16-bit data bus.
4It has 5 flags. It has 9 flags.
5It does not support pipelining.
It supports pipelining.
6It does not support memory segmentation.It supports memory segmentation.
7
The 8085 operates on a 3MHz Clock Speed.
The 8086 operates on 5MHz clock speed.
8The 8085 microprocessor has 6500 transistors.The 8086 microprocessor has 129000 transistors.
9It is accumulator based microprocessor.It is general register purpose register based microprocessor.
10It has no minimum or maximum mode. It has minimum or maximum mode.
11It has 64KB memory.
It has 1MB memory.
12It operates on clock cycle with 50% duty cycle.
It operates on clock cycle with 33% duty cycle.
13In 8085 microprocessor only one processor is being used.In 8086 microprocessor more than one processor is being used(additional external  processor can be used).

Comparison between 8085 and Z80 Microprocessors

S.No. 8085 Microprocessor Z80 Microprocessor
1 Data Lines are MULTIPLEXED It has no MULTIPLEXED lines
2 74 instructions 158 Instructions
3 Operates at 3 to 5MHz Operates at 4 to 20 MHz
4 It has 5 interrupts It has two interrupts
5 No on board dynamic memory It has on board logic to refresh Dynamic memory
6 It contains no Index register It has two Index register
7 It contains SIM & RIM It contains no SIM & RIM

Comparison between 8085 and MC6800 Microprocessors

S.No. 8085 Microprocessor MC6800 Microprocessor
1 It operates on Clock frequency of 3 to 5 MHz. It operates at 1 MHz frequency.
2 8085 has no Index register. It has one index register.
3 8085 has on board clock logic circuit. No clock logic circuit.
4 8085 has one Accumulator Register. MC6800 has two Accumulator Registers.
5 8085 has five interrupts. MC 6800 have two interrupts.
6 It has total 674 Instructions. MC6800 has total 72 instructions.

Comparison between 8086 and 80386 Microprocessors

S.No. 8086 Microprocessor 80386 Microprocessor
1 It is a 16 bit microprocessor and it is first 16 bit microprocessor after 8085(8-bit). It is a 32 bit microprocessor and it is logical extension of the 80236.
2 It has pipelined architecture (not highly) and high speed bus interface on single chip. It is highly pipelined architecture and much faster speed bus than 8086.
3 It is upward compatible with 80386.It means all 8086 instructions are followed by 80386. However, 80386 can support 8086 programming model & can also directly run the programs  written for 8086 in virtual mode if VM=1(in protected mode)
4 It is housed on a 40 pin  DIP package. The chip of 80836 contains 132 pins.
5 It is a built on a HMOS technology.
The 80386 using High-speed CHMOS III technology.
6 No special hardware is equipped for task Switching. It has a  special hardware for task switching.
7
The 8086 operates on a 5MHz. Clock.
The 80386 operate 33MHz clock frequency maximum.
8 The address bus and data bus are multiplexed. It has separate address and data bus for time saving.
9 It has a transistor package density of 29,500 transistors. Transistor density and complexity further increases 2,75,000.
10 It has a total of 117 instructions. It has total 129 instructions
11 It has no mechanism protection, paging.
The 80386  contains protection mechanism paging which has instruction two support them
12 It is operated in one mode only.
It operate in three modes
a)Real
b)Virtual
c)Protected
13 It has only instruction Queue. It has instruction Queue as well as pre fetch queue.
14 In 8086, It is not necessity that all operation are in parallel mode. 80386 all functional units are not parallel
15 8086 has nine flags. It contains all nine flags of 8086 but other flags named IOP,NT,RF,VM.

  Comparison between 8086 and 8088 Microprocessors

S.No. 8086 Microprocessor 8088 Microprocessor
1 The instruction Queue is 6 byte long.
The instruction Queue is 4 byte long.
2 In 8086 memory divides into two banks, up to 1,048,576 bytes.
The memory in 8088 does not divide in to two banks as 8086.
3 The data bus of 8086 is 16-bit wide
The data bus of 8088 is 8-bit wide.
4 It has BHE( bar ) signal on pin no. 34 & there is no SSO(bar) signal.
It does not has BHE( bar ) signal on pin no. 34 & has only SSO(bar) signal. It has no S7 pin.
5 The output signal is used to select memory or I/O at M/IO(bar) but if IO(bar)/M low or logic ‘0’ it selects I/O devices and if IO(bar)/M is high or logic ‘1’it selects memory. The output signal is used to select memory or I/O at M(bar)/IO but if IO/M(bar) is low or at logic ‘0’,it selects Memory devices and if IO/M(bar) is high or at logic ‘1’it selects I/O.
6 It needs one machine cycle to R/W signal if it is at even location otherwise it needs two.
It needs one machine cycle to R/W signal if it is at even location otherwise it needs two.
7 In 8086, all address & data Buses are multiplexed.
In 8088, address bus, AD7- AD0 buses are multiplexed.
8 It needs two IC 74343 for de-multiplexing AD0-AD19.
It needs one IC 74343 for de-multiplexing AD0-AD7.

 

98086 is available in 3 clock speed 5 MHZ,8MHZ and 10MHZ.
8088 is available in 2 clock speed 5 MHZ and 8MHZ.
10The I/O voltage level for 8086 is measured at 2.5 mA.
In 8088,the I/O voltage level is measured at 2 mA.
11 It draws maximum suuply current of 360mA.
It draws maximum suuply current of 340mA.