Friday 26 June 2015

C PROGRAM TO FIND ROOTS OF QUADRATIC EQUATION WITH OUTPUT


C PROGRAM TO FIND ROOTS OF QUADRATIC EQUATION WITH OUTPUT
#include<stdio.h>
#include<math.h>
int main()
{
    float a,b,c,x,y,disc,p,q,r;
    printf("\n C PROGRAM TO FIND ROOTS OF QUADRATIC EQUATION ");
    printf("\n Enter the values of a,b,c : ");
    scanf("%f %f %f",&a,&b,&c);
    disc=(b*b)-(4*a*c);
    if(disc == 0)
    {
        x=(-b)/(2*a);
        y=x;
        printf("\n Roots are Equal..");
        printf("\n x= %f , y= %f\n",x,y);
    }
     else if(disc < 0)
    {
        p=(-b)/(2*a);
        q=sqrt(-disc)/(2*a);
        printf("\n Roots are Complex and Imaginary..");
        printf("\n x= %f+%fi , y= %f-%fi\n",p,q,p,q);
    } 
     else if(disc > 0)
    {
        r=sqrt(disc);
        x=((-b)+r)/(2*a);
        y=((-b)-r)/(2*a);
        printf("\n Roots are Real Numbers..");
        printf("\n x= %f , y= %f\n",x,y);
    }
    else if(a==0)
    {
      x=(-c)/b;
      printf("\n Root is x = %f\n ",x);
    }
    getchar();
    return 0;
}
--------------------------------------------------------------------------
sda@JKSFHG:~/Desktop$ gcc code.c -lm
sda@JKSFHG:~/Desktop$ ./a.out 

 C PROGRAM TO FIND ROOTS OF QUADRATIC EQUATION 
 Enter the values of a,b,c : 12
13
14

 Roots are Complex and Imaginary..
 x= -0.541667+0.934486i , y= -0.541667-0.934486i
sda@JKSFHG:~/Desktop$ 
---------------------------------------------------------

Wednesday 24 June 2015

C PROGRAM TO CHECK WHETHER GIVEN YEAR IS LEAP YEAR OR NOT WITH RESULT

//  C PROGRAM TO CHECK WHETHER GIVEN YEAR IS LEAP YEAR OR NOT
#include<stdio.h>
void LeapYear(int);
void main()
{
    int year;

    printf("\n  C PROGRAM TO CHECK WHETHER GIVEN YEAR LEAP YEAR OR NOT  \n");
    printf("\n Enter the year : ");
    scanf("%d",&year);
    LeapYear(year);
    //getch();
   // return 0;
}
void LeapYear(int yr)
{
    int rem1,rem2;
    rem1 = yr%4 ; //rem1 = 0 for leap year
    rem2 = yr%100; //rem2! = 0 for leap year
    if((rem1 == 0) && (rem2!=0) || yr%400 == 0)
    {
        printf("\n The given year %d is Leap Year..\n",yr);
    }
    else
    {
        printf("\n The given year %d is Not Leap Year..\n",yr);
    }
    
}
------------------------------------------------------------------------
output:
root@KDFNKF:/home/rald/Desktop# ./a.out 

  C PROGRAM TO CHECK WHETHER GIVEN YEAR LEAP YEAR OR NOT  

 Enter the year : 2012

 The given year 2012 is Leap Year..
----------------------------------------------------------------------------------------------------------

c program to print Prime Numbers Between 1 and N


//  c program to print Prime Numbers Between 1 and N 

#include<stdio.h>
void main()
{
 int num,i=1,j,count;
         //clrscr(); 
 printf("Enter Num value To Print Prime Numbers between 1 and Num: ");
 scanf("%d",&num);
 printf("Prime Numbers upto %d :\n \n",num);

 while(i<=num)
 {
  count=0;
  for(j=1;j<=i;j++)
  {
   if(i%j==0)   //checking whether num is dvisible by j
   count++;
  }
  if(count==2)   //if num is divisible by 2 numbers,then it is prime
   printf("%d ",i);
   i++;
 }
 printf("\n\n");
             //getch(); 
}
----------------------------------------------------------------------
output :
-----------------------------------------------------------------------
root@AHDFEWPU0038:/home/rjas/Desktop# gcc code.c 
root@AHDFEWPU0038:/home/rjas/Desktop# ./a.out 
Enter Num value To Print Prime Numbers between 1 and Num: 25
Prime Numbers upto 25 :
 
2 3 5 7 11 13 17 19 23 

Tuesday 16 June 2015

Inter-Integrated Circuit (I2C) protocol basics (with I²C master example)

Inter-Integrated Circuit (I2C)



  • As the name suggests, Inter-IC (or the Inter-Integrated Circuit), often shortened as I2C (pronounced eye-two-see), I2C (pronounced eye-squared-see), or IIC, was developed as a communication protocol to interact between different ICs on a motherboard, a simple internal bus system. 
  • It is a revolutionary technology developed by Philips Semiconductor (now NXP Semiconductors) in 1982, and is used to connect low speed peripherals (like keyboard, mouse, memory, IO/serial/parallel ports, etc.) to the motherboard (containing the CPU) operating at much higher speed.
  • These days you can find a lot of devices which are I2C compatible manufactured by a variety of companies (like Intel, TI, Freescale, STMicroelectronics, etc). 
  • The Inter-integrated Circuit (I2C) Protocol is a protocol intended to allow multiple “slave” digital integrated circuits (“chips”) to communicate with one or more “master” chips. 
  • Like the Serial Peripheral Interface (SPI), it is only intended for short distance communications within a single device. 
  • Like Asynchronous Serial Interfaces (such as RS-232 or UARTs), it only requires two signal wires to exchange information.
  • I2C bus is used by many integrated circuits and is simple to implement. 
  • Any microcontroller can communicate with I2C devices even if it has no special I2C interface.

I2C terminology

  • Transmitter This is the device that transmits data to the bus
  • Receiver This is the device that receives data from the bus
  • Master This is the device that generates clock, starts communication, sends I2C commands and stops communication
  • Slave This is the device that listens to the bus and is addressed by the master
  • Multi-master I2C can have more than one master and each can send commands
  • Arbitration A process to determine which of the masters on the bus can use it when more masters need to use the bus
  • Synchronization A process to synchronize clocks of two or more devices
This resulted in few upgrades to the standard-mode I2C specifications:
  • Fast Mode – supports transfer rates up to 400 kbit/s
  • High-speed mode (Hs-mode) – supports transfer rates up to 3.4 Mbit/s
  • 10-bit addressing – supports up to 1024 I2C addresses
The before mentioned reference design is a bus with a clock (SCL) and data (SDA) lines with 7-bit addressing. The bus has two roles for nodes: master and slave:

Master node — node that generates the clock and initiates communication with slaves
Slave node — node that receives the clock and responds when addressed by the master

The bus is a multi-master bus which means any number of master nodes can be present. Additionally, master and slave roles may be changed between messages (after a STOP is sent).

There may be four potential modes of operation for a given bus device, although most devices only use a single role and its two modes:

master transmit — master node is sending data to a slave
master receive — master node is receiving data from a slave
slave transmit — slave node is sending data to the master
slave receive — slave node is receiving data from the master

example of the I²C protocol as an I²C master: 

// Hardware-specific support functions that MUST be customized:
#define I2CSPEED 100
void I2C_delay(void) { volatile int v; int i; for (i=0; i < I2CSPEED/2; i++) v; }
bool read_SCL(void); // Set SCL as input and return current level of line, 0 or 1
bool read_SDA(void); // Set SDA as input and return current level of line, 0 or 1
void clear_SCL(void); // Actively drive SCL signal low
void clear_SDA(void); // Actively drive SDA signal low
void arbitration_lost(void);
 
bool started = false; // global data
void i2c_start_cond(void) {
  if (started) { // if started, do a restart cond
    // set SDA to 1
    read_SDA();
    I2C_delay();
    while (read_SCL() == 0) {  // Clock stretching
      // You should add timeout to this loop
    }
    // Repeated start setup time, minimum 4.7us
    I2C_delay();
  }
  if (read_SDA() == 0) {
    arbitration_lost();
  }
  // SCL is high, set SDA from 1 to 0.
  clear_SDA();
  I2C_delay();
  clear_SCL();
  started = true;
}
 
void i2c_stop_cond(void){
  // set SDA to 0
  clear_SDA();
  I2C_delay();
  // Clock stretching
  while (read_SCL() == 0) {
    // add timeout to this loop.
  }
  // Stop bit setup time, minimum 4us
  I2C_delay();
  // SCL is high, set SDA from 0 to 1
  if (read_SDA() == 0) {
    arbitration_lost();
  }
  I2C_delay();
  started = false;
}
 
// Write a bit to I2C bus
void i2c_write_bit(bool bit) {
  if (bit) {
    read_SDA();
  } else {
    clear_SDA();
  }
  I2C_delay();
  while (read_SCL() == 0) { // Clock stretching
    // You should add timeout to this loop
  }
  // SCL is high, now data is valid
  // If SDA is high, check that nobody else is driving SDA
  if (bit && read_SDA() == 0) {
    arbitration_lost();
  }
  I2C_delay();
  clear_SCL();
}
 
// Read a bit from I2C bus
bool i2c_read_bit(void) {
  bool bit;
  // Let the slave drive data
  read_SDA();
  I2C_delay();
  while (read_SCL() == 0) { // Clock stretching
    // You should add timeout to this loop
  }
  // SCL is high, now data is valid
  bit = read_SDA();
  I2C_delay();
  clear_SCL();
  return bit;
}
 
// Write a byte to I2C bus. Return 0 if ack by the slave.
bool i2c_write_byte(bool send_start,
                    bool send_stop,
                    unsigned char byte) {
  unsigned bit;
  bool nack;
  if (send_start) {
    i2c_start_cond();
  }
  for (bit = 0; bit < 8; bit++) {
    i2c_write_bit((byte & 0x80) != 0);
    byte <<= 1;
  }
  nack = i2c_read_bit();
  if (send_stop) {
    i2c_stop_cond();
  }
  return nack;
}
 
// Read a byte from I2C bus
unsigned char i2c_read_byte(bool nack, bool send_stop) {
  unsigned char byte = 0;
  unsigned bit;
  for (bit = 0; bit < 8; bit++) {
    byte = (byte << 1) | i2c_read_bit();
  }
  i2c_write_bit(nack);
  if (send_stop) {
    i2c_stop_cond();
  }
  return byte;
}

Monday 15 June 2015

Recover deleted files used by any process in Linux from RAM memory.

Recover deleted files used by any process in Linux from RAM memory.

if a process is using the file, or if the file is open , the inode is not released for overwriting util the process is done with the file. Such files will remain in the server memory (RAM).

example.

Create a test file.
# touch testfile.txt

Echo some random data on it.
# cat /dev/random > testfile.txt

Open the file using some command like below.
# less  testfile.txt

# ps -ef | grep -i less
less 4607 root  4r  REG 254,4   21  
           8880214 /root/testing.txt (deleted)

All the open files remain in the memory and hence in the /proc filesystem. The important columns in the above output are the second one, which gives you the PID of the process that has the file open (4607), and the fourth one, which gives you the file descriptor (4). Now, we go look in /proc, where there will still be a reference to the inode, from which you can copy the file back.

# ls -l /proc/4607/fd/4
lr-x------ 1 root root 64 Apr  7 03:19 
             /proc/4607/fd/4 -> /root/testing.txt (deleted)
 
To recover the deleted file in memory, just copy as below.
 #cp /proc/4607/fd/4 testing.txt.bk

you got your file back. Just make sure not to use "-a" switch while copying the file as this will copy the broken softlink.

How to Execute commands on a remote Linux machine?

Execute commands on a remote Linux machine

  • The syntax for running a command or script on a remote server is:

ssh [USER]@[IP] [command or script]


  • Suppose you want to display the directory contents of /root of a remote host, you can run the following command in linux terminal:


[premal@ubunu]$ ssh root@198.168.2.156 ls -l /root
root@198.168.2.156 's password:
total 15

drwxr-xr-x. 2 root root 4096 Mar 12 00:05 device_drivers

drwxr-xr-x. 2 root root 4096 Mar 12 01:31 pthreads_ls

Wednesday 8 April 2015

sysfs structures for Linux USB

Q: What are the sysfs structures for Linux USB?
A: For example the directory will have something like:
 
# ls  /sys/bus/usb/devices/
1-0:1.0      1-1.3        1-1.3.1:1.0  1-1:1.0
1-1          1-1.3.1      1-1.3:1.0    usb1

The names that begin with "usb" refer to USB controllers. More accurately, they refer to the "root hub" associated with each controller. The number is the USB bus number. In the example there is only one controller, so its bus is number 1. Hence the name "usb1".

"1-0:1.0" is a special case. It refers to the root hub's interface. This acts just like the interface in an actual hub an almost every respect; see below.
All the other entries refer to genuine USB devices and their interfaces. The devices are named by a scheme like this:

 bus-port.port.port ...
In other words, the name starts with the bus number followed by a '-'. Then comes the sequence of port numbers for each of the intermediate hubs along the path to the device.
For example, "1-1" is a device plugged into bus 1, port 1. It happens to be a hub, and "1-1.3" is the device plugged into port 3 of that hub. That device is another hub, and "1-1.3.1" is the device plugged into its port 1.

The interfaces are indicated by suffixes having this form:
 :config.interface

That is, a ':' followed by the configuration number followed by '.' followed by the interface number. In the above example, each of the devices is using configuration 1 and this configuration has only a
single interface, number 0. So the interfaces show up as;

 1-1:1.0  1-1.3:1.0  1-1.3.1:1.0

A hub will never have more than a single interface; that's part of the USB spec. But other devices can and do have multiple interfaces (and sometimes multiple configurations). Each interface gets its own entry in sysfs and can have its own driver.

------------------------------------------------------------------------------------------------------------------------
overall summery of the sysfs structure path :

1-1.3:1.0
|_usb root hub - bus number -1
    |_ port number - 1 of root hub
       |_port number - 3 of intermediate hub
          |_current configuration number - 1
            |_ current interface number - 0

---------------------------------------------------------------------------------------------------------------------

Salient features of sysfs includes:

  1.  A hierarchical view of the complete device tree where the various components of the driver model are organized as directories, attributes as files, and interconnections as links.
  2. A type-safe kernel interface.
  3. A facility by which any layer can export its attributes.
  4. A true representation of the various relationships that exist between devices, buses, and drivers in the system.