Monday, 30 September 2013

Indent your C code within second

1 comment
GNU
    Indenting huge C program manually is difficult and time consuming. To reduce this effort we can use "GNU indent" tool. We can indent C program automatically with this tool. This tool is only design to indent C programs and not for C++.

Installation

    For  Debian OS(Ubuntu, OpenSuse) we can install using command given below.
sudo apt-get install indent
    For Redhat based OS(like Fedora, CentOS) we can install using command given below.
yum install indent
OR
    You can also download tar.gz and install manually using 
./configure
make
make install   (give command with root/sudoer privileges)

Uses

    As stated previously we can use this tool to automatically indent any C program.
Syntax:
indent prog1.c prog2.c
    This command will indent both the programs, if you want to save the files with different name to access the older files you can use the following syntax or command
indent prog1.c -o prog1_indent.c
    In this you can indent only one file at a time.
Below example shows the unintended C code(uint.c)

         #include"stdio.h"
   #include"stdlib.h"
      int main()
    {
     printf("This is unintended code")  ;
         return 0;
  }

After using intend command you get the intended code as shown below
intend unin.c

#include"stdio.h"
#include"stdlib.h"
int
main ()
{
  printf ("This is unintended code");
  return 0;
}

There are three by default styles of indenting the C program 
  1. gnu-style (it is by default)
  2. k-and-r-style (Kernighan & Ritchie style)
  3. linux-style
    For 2nd & 3rd style you have to specify it explicitly after as shown below
indent --linux-style  prog1.c
indent --k-and-r-style  prog1.c
    There are many options available to customize the above three styles out of which important option are given below you can find detail in the user manual  of indent.
-nsaf  => --no-space-after-for
-nsai  => --no-space-after-if
-nsaw => --no-space-after-while
-sob   => --swallow-optional-blank-lines (delete the extra blank lines)
-tsn    => --tab-sizen (we can set the tab size by changing the n)
(n is the length of the tab)
-nut   => --no-tabs (Use spaces instead of tabs)

    In some programs there are many "carriage return" (\r) characters are present indent tool will replace this "carriage return" to the "new line" (\n) which will cause unnecessary blank lines.
    To remove this unwanted "carriage return" (\r) from program we can use following command
cat prog1.c | tr -d "\r" >prog1.c
    In this command tr is used to translate or delete the characters.
After removing this "carriage return" you can use indent command to get proper output.



 





Read More...

Thursday, 26 September 2013

Pthread_cond_wait & Pthread_cond_signal simple example

3 comments
pthread_cond_t
pthread_cond_t is a condition variable which can be used by thread_cond_wait(), pthread_cond_signal(), pthread_cond_broadcast() and thread_cond_timedwait().

pthread_cond_wait() :
  • This routine stops the execution of the calling thread and wait for appropriate signal to start the thread execution.
  • pthread_cond_wait() must be called after pthread_mutex_lock() and before pthread_mutex_unlock().
  • pthread_cond_wait() release the mutex lock while it is waiting, so that pthread_cond_signal(), which is also called in the mutex should get access and can give signal to waiting thread to awake.
Syntax: pthread_cond_wait(pthread_cond_t *condition, pthread_mutex_t *mutex)

pthread_cond_signal() 
  • This routine is used to wakeup the thread which is waiting for any condition to occur.
  • If in any case pthread_cond_signal() calls first before the execution of pthread_cond_wait() then it cannot awake the thread which is waiting for the condition to occur and your thread calling pthread_cond_wait() will go to the infinite waiting state or can be called as deadlock state.
  • Your pthread_cond_signal() should be called at least once for corresponding pthread_cond_wait() function call.
Syntax: pthread_cond_signal(pthread_cond_t *condition)
This signal will wakeup the pthread_cond_wait() with condition variable condition.
The program given below will explain this routines signal_wait.c


#include "pthread.h"
#include "stdio.h"
#include"unistd.h"
 
volatile int sv=10;
volatile int x,y,temp=10;
pthread_mutex_t mut=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t con=PTHREAD_COND_INITIALIZER;
pthread_t child1,child2,child3;
int condition=1,val=1;
int count=1;
 
void *ChildThread1(void *arg)
{
    sleep(1);
    pthread_mutex_lock (&mut);
        printf("wait called\n");
        while(count<10)                      //if while loop with signal complete first don't wait
        {
            printf("In wait\n");
            pthread_cond_wait(&con, &mut);  //wait for the signal with con as condition variable
        }
        x=sv;
        x++;
        sv=x;
        printf("The child1 sv is %d\n",sv);
    pthread_mutex_unlock (&mut);
}
 
void *ChildThread2(void *arg)
{
 
    while(count<10)
    {
        pthread_mutex_lock (&mut);
            pthread_cond_signal(&con);  //wake up waiting thread with condition variable
                                        //con if it is called before this function
            if(val==1)
            {
                y=sv;
                y--;
                sv=y;
                printf("The child2 sv is %d\n",sv);
                val++;
            }
            count++;
        pthread_mutex_unlock (&mut);
         
    }
    printf("mutex released\n");
    printf("chil2 exit\n");
}
 
int main(void)
{
    int co=1;
    pthread_create(&child1,NULL,ChildThread1,NULL);
    pthread_create(&child2,NULL,ChildThread2,NULL);
    pthread_join(child1,NULL);
    pthread_join(child2,NULL);
 
    pthread_cond_destroy(&con);
    pthread_mutex_destroy(&mut);
    pthread_exit(NULL);
    return 0;
}

Read More...

Monday, 23 September 2013

Windows setup cannot configure Windows to run on this hardware

Leave a Comment
     While installation of Windows some times after restarting Windows on last step "completing installation" this error comes and your installation never completes.
     To install Windows properly in such cases you can follow the steps given below

  1. Reset your BIOS to all default values and restart. Changes in BIOS may be the cause of this error.
  2. After restarting if the problem remains same then follow the steps below.
  3. Once again boot from your bootable CD/DVD or bootable pendrive.
  4. Don't delete/format the windows partition where you have installed your Windows 7.
  5. Select the same partition where you have installed your Windows previously and which is giving the error "Windows setup cannot configure windows while to run on this hardware" and install Windows on this drive once again.
  6. This will give you warning as shown in the figure given below, click on ok button and your installation start.
    Windows.old
  7. Now all your data of previous windows will get store on windows.old folder on the drive where you have installed the windows.
  8. You can delete this partition and make free space on the drive.
Read More...

Friday, 20 September 2013

Minimizing the Finite Automata using c

Leave a Comment
In this given example we have find out the minimization of finite automata with single final state given in the figure below
Finite Automata
We can store this FA in a matrix of 2x7 where row represent the input and column represent the states mapped to array_index+1.
State pair can be shown using lower triangular matrix of (number_of_states - 1) x (number_of_states - 1) array as shown in the figure below which can be used to check the equivalent states.
State Pair
 After Minimization the minimized finite automata looks as shown in the figure given below.
Minimization of FA
Download Source code for c program to minimize the Finite Automata here minimizeFA.c


#include"stdio.h"
#define states 7  //number of states

int
main ()
{
  int s1, s2, temp1, temp2, temp3, temp4, i = 0, j =
    0, state_pair[states - 1][states - 1], f, k, flag = 1;
  int FA[2][states] = { 2, 4, 6, 4, 6, 4, 6, 3, 5, 7, 5, 7, 5, 7 }; //input your state transsion diagram 
  int xtemp[20], it = 0;
  int x[states], l = 0;
  int count = 0, arr1[20], arr2[20], c = 0;
  int temp[states];
  for(i = 0; i < states; i++)
    {
      temp[i] = 0;
    }
  for(i = 0; i < states; i++)
    {
      x[i] = 0;
    }
  //This is Finite automata for {0,1}*{10}
  printf ("\nArray elements are");

  for(i = 0; i < 2; i++)
    {
      printf ("\n");
      for(j = 0; j < states; j++)
 {
   printf ("\t%d", FA[i][j]);
 }
    }

  for(i = 0; i < (states - 1); i++)
    {
      for(j = 0; j < states - 1; j++)
 state_pair[i][j] = 0;
    }

  printf ("\nEnter final state");
  scanf ("%d", &f);
  i = f - 2;
  j = f - 1;

  for(k = 0; k <= f - 2; k++)
    state_pair[i][k] = 1;

  for(k = 0; k < (states - 1); k++)
    {
      if(k <= f - 2)
     {
     }
      else
 state_pair[k][i + 1] = 1;
    }

  while(flag)
    {
      flag = 0;

      for(i = 0; i < (states - 1); i++)
 {
   for(j = 0; j < (states - 1); j++)
     {
       s1 = i + 2;
       s2 = j + 1;

       if(s1 > s2)
  {
    if(state_pair[i][j] != 1)
      {
        temp1 = FA[0][s1 - 1];
        temp2 = FA[0][s2 - 1];
        temp3 = FA[1][s1 - 1];
        temp4 = FA[1][s2 - 1];

        //      printf("S1 %d  S2 %d\n",s1,s2);
        //      printf("d(%d,0)=%d \t d(%d,0) %d\n",s1,temp1,s2,temp2);  
        //      printf("d(%d,1)=%d \t d(%d,1) %d\n\n",s1,temp3,s2,temp4);

        if(temp1 != temp2) //check whether state p!=q for 0 input
   {
     if(state_pair[temp1 - 2][temp2 - 1] == 1
        || state_pair[temp2 - 2][temp1 - 1] == 1)
       {
         state_pair[i][j] = 1;
         flag = 1;
       }
   }

        if(temp3 != temp4) //check whether state p!=q for 1 input
   {
     if(state_pair[temp3 - 2][temp4 - 1] == 1
        || state_pair[temp4 - 2][temp3 - 1] == 1)
       {
         state_pair[i][j] = 1;
         flag = 1;
       }
   }
      }
  }
     }
 }
    }

  printf ("\nSecond array");

  for(i = 0; i < (states - 1); i++)
    {
      printf ("\n %d", i + 2);

      for(j = 0; j < (states - 1); j++)
 {
   if(i >= j)
     {
       printf ("\t%d", state_pair[i][j]);
       if(state_pair[i][j] == 0)
  {
    arr1[count] = i + 2;
    arr2[count] = j + 1;
    count++;
  }
     }
 }
    }

  printf ("\n\n");

  for(i = 0; i < (states - 1); i++)
    printf ("\t%d", i + 1);
  printf ("\n");

  c = 0;
  i = 0;
  printf ("\n\n");
//code to check the same states(from unmark) start here
  while(1)
    {
      if(x[arr1[c] - 1] == 0 && x[arr2[c] - 1] == 0)
 {
   x[arr1[c] - 1] = 1;
   x[arr2[c] - 1] = 1;
   xtemp[it] = arr1[c];
   it = it + 1;
   xtemp[it] = arr2[c];
   it = it + 1;
   for(l = 0; l < count; l++)
     {
       if(x[arr1[l] - 1] == 0 && x[arr2[l] - 1] == 1)
  {
    x[arr1[l] - 1] = 1;
    xtemp[it] = arr1[l];
    it = it + 1;
  }
       if(x[arr1[l] - 1] == 1 && x[arr2[l] - 1] == 0)
  {
    x[arr2[l] - 1] = 1;
    xtemp[it] = arr2[l];
    it = it + 1;
  }
     }
   printf("..............following states are in same group...............\n");
   for(i = 0; i < it; i++)
     printf ("%d\t", xtemp[i]);
   it = 0;
 }
      printf ("\n");
      c++;
      if(c == count)
 break;
    }
  return 0;
}


Read More...

Friday, 13 September 2013

Pass multiple arguments in pthread_create

Leave a Comment
Pthreads

        Posix thread normally known as pthread. It is an API to create threads on Unix like operating system such as GNU/Linux, MAC etc. Windows doesn't provide support for this API. In Windows some third party softwares are available which can be used for writing multithread application using pthread, pthreads-w32 is one of this.
        To create thread pthread uses pthread_create function with syntax given below.

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
        This function creates a new thread and that thread will execute the function specified as third parameter with arguments specified in fourth parameter. There is no provision to transfer more than one argument to the function which is called by pthread_create.
        If you want to transfer multiple arguments then you require to create a structure with arguments you require to pass to the function and then by transferring this structure you can pass as many arguments as you want to transfer. The program pthread.c below is one such example of passing multiple parameters


#include"pthread.h"
#include"stdlib.h"
#include"stdio.h"

struct test
{
 int var1,var2;
 float var3;
};

typedef struct test struct1;

void* fun1(void* a)
{
 struct1 *b;
 b=(struct1*)a;
 b->var3=b->var1+b->var2;
 printf("\nOperation successful the sum is %f\n",b->var3);
}

int main()
{
 pthread_t th1,th2;
 struct1 *a;
 a=(struct1 *)malloc(sizeof(struct1));
 a->var1=10;
 a->var2=20;
 pthread_create(&th1,NULL,fun1,(void*) a);
 pthread_join(th1,NULL);
 return 0;
}


Read More...