C Datastructures Networking OS

Saturday 1 February 2014

TYPE COMPATIBILITY:
         C++ defines int,short int ,long int as three different types.Hence they must be cast when their values assigned to one another.
The types of values must be the same for compatibility or else a cats must be applied.

DIFFERENCES IN STORING CHAR CONSTANTS IN C AND C++:
     In C  char constant is considered as interger:
        for example,
                        sizeof('x') returns sizeof(int)
                         i.e size of ASCII value of x is returned
   Whereas in C++,char is not promoted to integer
                       sizeof('x')  returns sizeof(char)

Tuesday 14 May 2013

TRIGGERS AND TYPES....



Triggers are procedures stored in PL/SQL or Java that run (fire) implicitly whenever a table or view is modified or when some user actions or database system actions occur.

CREATE or REPLACE TRIGGER trigger_name
BEFORE|AFTER|INSTEAD OF INSERT|UPDATE|DELETE
   ON table_name
   [FOR EACH ROW ]

DECLARE
   -- Variable declarations

BEGIN
   -- trigger code

EXCEPTION
   WHEN...
   -- Exception handling

END;

You can write triggers that fire whenever one of the following operations occurs:
1.      DML statements (INSERT, UPDATE, DELETE) on a particular table or view, issued by any user
2.      DDL statements (CREATE or ALTER primarily) issued either by a particular schema/user or by any schema/user in the database
3.      Database events, such as logon/logoff, errors, or startup/shutdown, also issued either by a particular schema/user or by any schema/user in the database

Types of Triggers
This section describes the different types of triggers:
  • Row Triggers and Statement Triggers
A row trigger is fired each time the table is affected by the triggering statement. For example, if an UPDATE statement updates multiple rows of a table, a row trigger is fired once for each row affected by the UPDATE statement. If a triggering statement affects no rows, a row trigger is not run.
Row triggers are useful if the code in the trigger action depends on data provided by the triggering statement or rows that are affected

A statement trigger is fired once on behalf of the triggering statement, regardless of the number of rows in the table that the triggering statement affects, even if no rows are affected. For example, if a DELETE statement deletes several rows from a table, a statement-level DELETE trigger is fired only once.
Statement triggers are useful if the code in the trigger action does not depend on the data provided by the triggering statement or the rows affected. For example, use a statement trigger to:
Make a complex security check on the current time or user
Generate a single audit record

  • BEFORE and AFTER Triggers
·         When defining a trigger, you can specify the trigger timingwhether the trigger action is to be run before or after the triggering statement. BEFORE andAFTER apply to both statement and row triggers.
·         BEFORE and AFTER triggers fired by DML statements can be defined only on tables, not on views. However, triggers on the base tables of a view are fired if anINSERT, UPDATE, or DELETE statement is issued against the view. BEFORE and AFTER triggers fired by DDL statements can be defined only on the database or a schema, not on particular tables.

  • INSTEAD OF Triggers
INSTEAD OF triggers provide a transparent way of modifying views that cannot be modified directly through DML statements (INSERT, UPDATE, and DELETE). These triggers are called INSTEAD OF triggers because, unlike other types of triggers, Oracle fires the trigger instead of executing the triggering statement.
You can write normal INSERT, UPDATE, and DELETE statements against the view and the INSTEAD OF trigger is fired to update the underlying tables appropriately. INSTEAD OF triggers are activated for each row of the view that gets modified.





Saturday 20 April 2013

Usage of constant qualifier with pointer

Difference between  const * int ptr  and   int * const ptr...

First of all we need to understand what const qualifier is and how it is used.
            Declaration of variable with const qualifier specifies that its value will not be changed. It depends upon where const variables are stored ,we may change value of const variable by using pointer.The result is implementation-defined if an attempt is made to change a const.
                  #include <stdio.h>
                 #include<conio.h>
                 void main()
                 {
                          const int a=100;
                          a+=100;                                //error
                          printf("value of a=%d",a);
                             
                  }
               
 o/p: assignment of read only variable 'a'
1)pointer to variable:-
          
                     int *ptr;
            In the above declaration the value of both variable and pointer can be changed.Both the pointer and the value pointed by pointer will be stored in read-write area of memory.

                 #include <stdio.h>
                 #include<conio.h>
                 void main()
                 {
                           int a=100,b=200;
                           int *ptr;
                           ptr=&a;                                        //pointer to integer
                           printf("value of ptr=%d",*ptr);
                           ptr=&b;                                       //pointing to another variable
                           printf("value of ptr=%d",*ptr);
                           *ptr=500;                                   //changing the value of pointer
                            printf("value of ptr=%d",*ptr);
                  }
               
           o/p:
                     value of ptr=100;
                     value of ptr=200;
                     value of ptr=500;
2) Pointer to constant :
          const int * ptr;
                  or
          int const * ptr;

We change pointer to point to another variable,but we cannot change the value of the variable which is pointed by the pointer.Pointer is stored in read-write memory and variable is stored in read-only memory.
                       
                 #include<stdio.h>
                 #inlcude<conio.h>
                 void main()
                 {
                         int i=10,j=20;
                         const int *ptr=&i;
                         printf("Value of ptr=%d",*ptr);
                         
                         ptr=&j;                                             //valid
                         printf("value of ptr=%d",*ptr);
               
                        *ptr=100;                                           // error.
                         printf("value of ptr=%d",*ptr); 
                 }
            
o/p:- error.assignment to read only location *ptr
3)Constant Pointer to variable:-

                int  *const ptr;

             We can change the value of variable pointed by the pointer, but we cannot change the pointer to point to another variable.

                       
                 #include<stdio.h>
                 #inlcude<conio.h>
                 void main()
                 {
                         int i=10,j=20;
                         int * const ptr=&i;
                         printf("Value of ptr=%d",*ptr);
                         
                         *ptr=100;                                           // valid.
                         printf("value of ptr=%d",*ptr); 
                         
                         ptr=&j;                                             //error
                         printf("value of ptr=%d",*ptr);
               
                        
                 }     

         o/p: error in assignment to read only location ptr
4) constant pointer to constant variable:
              We cannot change both the value of variable pointed by the pointer and  also pointer to point to another variable.

                 #include<stdio.h>
                 #inlcude<conio.h>
                 void main()
                 {
                         int i=10,j=20;
                         const  int * const ptr=&i;
                         printf("Value of ptr=%d",*ptr);
                         
                         *ptr=100;                                           // error.
                         printf("value of ptr=%d",*ptr); 
                         
                         ptr=&j;                                             //error
                         printf("value of ptr=%d",*ptr);
               
                        
                 } 
 o/p:     error.assignment to read only location *ptr
            error in assignment to read only location ptr

Wednesday 10 April 2013

Socket vs Port...

SOCKET : -

A TCP connection is a 5-tuple organisation.
      It consists of Client Ip,Client port number,Server Ip,Server port,protocol.
     Combination of IP address and Port number is known as endpoint and sometimes called as socket.
    
PORT:-
    The purpose of ports is to differentiate multiple endpoints on a given network address. You could say that     a port is a virtualised endpoint. This virtualisation makes multiple concurrent connections on a single         network interface possible.

TCP VS UDP

TCP (Transmission Control Protocol):-
It is the most commonly used protocol on the Internet. The reason for this is because TCP offers error correction.
 When the TCP protocol is used there is a "guaranteed delivery." This is due largely in part to a method called "flow control."
 Flow control determines when data needs to be re-sent, and stops the flow of data until previous packets are successfully transferred. 
This works because if a packet of data is sent, a collision may occur. 
When this happens, the client re-requests the packet from the server until the whole packet is complete and is identical to its original. 

UDP (User Datagram Protocol): -


This is another commonly used protocol on the Internet. However, UDP is never used to send important data such as webpages, database information, etc;
 UDP is commonly used for streaming audio and video. Streaming media such as Windows Media audio files (.WMA) , Real Player (.RM), and others use UDP because it offers speed! The reason 

UDP is faster than TCP is because there is no form of flow control or error correction. The data sent over the Internet is affected by collisions, and errors will be present. Remember that UDP is only concerned with speed. This is the main reason why streaming media is not high quality.

Wednesday 20 March 2013

DIFFERENCE BETWEEN SEMAPHORE AND MUTEX..

Mutex :
         A mutex object only allows one thread into a controlled section, forcing other threads which attempt to gain access to that section to wait until the first thread has exited from that section. Mutexes are typically used to serialize access to a section of re-entrant code that cannot be executed concurrently by more than one thread.

Semaphore :
       A semaphore restricts the number of simultaneous users of a shared resource up to a maximum number. Threads can request access to the resource (decrementing the semaphore), and can signal that they have finished using the resource (incrementing the semaphore). 

Here comes the question,
                    When to use mutex and when to use semaphore?
                    
The producer-consumer problem:

Consider the standard producer-consumer problem. Assume, we have a buffer of 4096 byte length. A producer thread will collect the data and writes it to the buffer. A consumer thread will process the collected data from the buffer. Objective is, both the threads should not run at the same time.


Using Mutex:

A mutex provides mutual exclusion, either producer or consumer can have the key (mutex) and proceed with their work. As long as the buffer is filled by producer, the consumer needs to wait, and vice versa.
At any point of time, only one thread can work with the entire buffer. The concept can be generalized using semaphore.


Using Semaphore:

A semaphore is a generalized mutex. In lieu of single buffer, we can split the 4 KB buffer into four 1 KB buffers (identical resources). A semaphore can be associated with these four buffers. The consumer and producer can work on different buffers at the same time.


There is misconception that binary semaphore and mutex are same.But they are not.The purpose of mutex and semaphore are different.Just the implementation is similar.

Mutex  is locking mechanism used to synchronize the access to resource.Only one task can acquire the lock.
Whereas Semaphore is a signaling mechanism.


Saturday 9 March 2013

Program to find missing number in a sequence 1 to n...

Algorithm.

step 1: store  the sequence  in the array
step 2: calculate the sum of array;
step: 3: calculate sum of 'n' numbers from 1 to n using the formula n*(n+1)/2;
step 4 : subtract sum of array from sum of n numbers;
        ie  (n*(n+1)/2)-sum of array;
step 5: the difference is the missing number

this approach is better when there is a single missing number...then what about when there are 2 missing numbers...

its very simple
   for the numbers in a sequence the difference will be 1 for every pair
   so find the difference for every pair starting from index 0
   when the difference is greater than 1 then the ( minimum number in pair+1) is the missing             number
   thus we can find more than 1 numbers those are missed from the sequence.