hand gesture recognition git hub code location


opencv cmake:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF -D CMAKE_INSTALL_PREFIX=/usr/local ..



C Program for single parent and 26 child process forked by the single parent

C Program for single parent and 26 child process forked by the single parent

neelkanth_surekha#cat test.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    FILE *f;
    char c;
    int a = 'A';

    int i;
    int r, pid;
    int status;
    remove("f.txt");

    /* PARENT FORKS 26 CHILDREN AND WAITS TILL ALL THE CHILDREN TERMINATE */
    for ( i = 1; i <= 26; i++ ){
        pid = fork();
        if ((pid != 0) && (pid > 0))
        {
            /*parent process  pid != 0*/

            printf("I am parent: pid :%d ppid: %d "
                    "waiting for child whose pid is : %d\n", getpid(), getppid(), pid );
            /* wait for child to terminate */
            waitpid(pid, &status, WUNTRACED | WCONTINUED);

            a++;            // increment character
        }
        /* EACH CHILD OF PARENT WRITES A CHARACTER INTO THE FILE */
        else
        {
            printf("############################################################################\n");
            /*child process pid = 0*/
            /*write next character to file*/
            f = fopen("f.txt", "a");
            fprintf(f, "%c", a);

            printf("I am child number %d: pid :%d ppid: %d: Character written into file : %c\n",
                    i, getpid(), getppid(), a);
            fclose(f);
            /* Here every child writes a character into the file and exists here. */
            exit(0);
        }
    }

    /* THE BELOW PIECE OF CODE GETS ONLY EXECUTED FOR PARENT AS CHILD WOULD EXIT ABOVE.
     * SECONDLY, THIS CODE BELOW WOULD ONLY GET EXECUTED AFTER ALL THE CHILDREN HAVE
     * TERMINATED. THIS IS DUE TO THE FACT THAT PARENT IS WAITING FOR EACH OF IT'S CHILD
     * TO TERMINATE.
     * */
    printf("\n");
    printf("\n");
    printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
    printf("Now that all the 26 children have written a charater into f.txt file\n");
    printf(",Parent who was waiting for all the children to terminate opens the resultant file\n");
    printf("and prints it\n");
    printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
    printf("\n");
    printf("I am the parent :  my pid: %d my parent pid %d \n", getpid(), getppid());
   
    f = fopen("f.txt", "r");
    printf("Text read from the file: \n");
    while (1){
        fscanf(f, "%c", &c);
        if ( feof(f))
        {
            break;
        }
        printf("%c", c);
    }
    fclose(f);
    printf("\n");

    return 0;
}

Output:neelkanth_surekha#./a.out 

I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12517
############################################################################
I am child number 1: pid :12517 ppid: 12516: Character written into file : A
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12518
############################################################################
I am child number 2: pid :12518 ppid: 12516: Character written into file : B
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12519
############################################################################
I am child number 3: pid :12519 ppid: 12516: Character written into file : C
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12520
############################################################################
I am child number 4: pid :12520 ppid: 12516: Character written into file : D
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12521
############################################################################
I am child number 5: pid :12521 ppid: 12516: Character written into file : E
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12522
############################################################################
I am child number 6: pid :12522 ppid: 12516: Character written into file : F
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12523
############################################################################
I am child number 7: pid :12523 ppid: 12516: Character written into file : G
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12524
############################################################################
I am child number 8: pid :12524 ppid: 12516: Character written into file : H
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12525
############################################################################
I am child number 9: pid :12525 ppid: 12516: Character written into file : I
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12526
############################################################################
I am child number 10: pid :12526 ppid: 12516: Character written into file : J
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12527
############################################################################
I am child number 11: pid :12527 ppid: 12516: Character written into file : K
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12528
############################################################################
I am child number 12: pid :12528 ppid: 12516: Character written into file : L
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12529
############################################################################
I am child number 13: pid :12529 ppid: 12516: Character written into file : M
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12530
############################################################################
I am child number 14: pid :12530 ppid: 12516: Character written into file : N
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12531
############################################################################
I am child number 15: pid :12531 ppid: 12516: Character written into file : O
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12532
############################################################################
I am child number 16: pid :12532 ppid: 12516: Character written into file : P
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12533
############################################################################
I am child number 17: pid :12533 ppid: 12516: Character written into file : Q
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12534
############################################################################
I am child number 18: pid :12534 ppid: 12516: Character written into file : R
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12535
############################################################################
I am child number 19: pid :12535 ppid: 12516: Character written into file : S
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12536
############################################################################
I am child number 20: pid :12536 ppid: 12516: Character written into file : T
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12537
############################################################################
I am child number 21: pid :12537 ppid: 12516: Character written into file : U
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12538
############################################################################
I am child number 22: pid :12538 ppid: 12516: Character written into file : V
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12539
############################################################################
I am child number 23: pid :12539 ppid: 12516: Character written into file : W
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12540
############################################################################
I am child number 24: pid :12540 ppid: 12516: Character written into file : X
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12541
############################################################################
I am child number 25: pid :12541 ppid: 12516: Character written into file : Y
I am parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12542
############################################################################
I am child number 26: pid :12542 ppid: 12516: Character written into file : Z


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Now that all the 26 children have written a charater into f.txt file
,Parent who was waiting for all the children to terminate opens the resultant file
and prints it
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

I am the parent :  my pid: 12516 my parent pid 14458 
Text read from the file: 
ABCDEFGHIJKLMNOPQRSTUVWXYZ

How to use Function Pointers, compound Literals and typedefs in c

/* ##################################################################### #    IN THIS SECTION, I WOULD COVER 3 CONCEPTS PERTAINING TO FUNCTION #    POINTERS. #    1. How to use Function Pointers. #    2. Typedef for function pointers [i.e. alias names for function  #       pointer]. #    3. 'Compound literals' for a structure containing function  #       pointers.   ##################################################################### */


/******************************************************
 * A function pointer is like any other pointer, but it 
 * points to the address of a function instead of the 
 * address of data (on heap or stack). Like any pointer,
 * it needs to be typed correctly. Functions are defined
 * by their return value and the types of parameters they 
 * accept. So in order to fully describe a function, you 
 * must include its return value and the type of each 
 * parameter is accepts. When you typedef such a 
 * definition, you give it a 'friendly name' which makes 
 * it easier to create and reference pointers using that 
 * definition.
 * So for example assume you have a function:
 *
 *   float doMultiplication (float num1, float num2 ) {
 *       return num1 * num2; 
 *   }
 *******************************************************
 *   then the following is the 'typedef' for a function
 *   pointer.
 ******************************************************   
 *   typedef float(*pt2Func)(float, float);
 *   pt2Func new_alias_name_1;
 *   pt2Func new_alias_name_2;
 *   pt2Func new_alias_name_3;
 *
 *   ----> the above is equivalent to  <----
 *   float (*new_alias_name_1)(float, float);
 *   float (*new_alias_name_2)(float, float);
 *   float (*new_alias_name_3)(float, float);
 *   
 *   Now, we can see how using 'typedef' for 'function
 *   pointers' is a better and a cleaner approach.
 ******************************************************/

#include <stdio.h>

int add(int a, int b);
int mul(int a, int b);
/********************************************************
 * Define 2 function 'add' and 'multiply' with same 
 * Function prototype.  
 ********************************************************/
/*
 * Add two numbers a and b
 */
int add(int a , int b) {
    return (a + b);
}

/*
 * Multiply two numbers a and b
 */
int mul(int a, int b) { 
    return (a * b);
}

/*******************************************************
 * typedef of Function pointers of functions add and 
 * multiply 
 *******************************************************/
typedef int (*operation)(int a , int b);

/* Define a structure with  function pointers as members */
typedef struct do_calculation {
    operation  add_integer;       /* function pointer for 'add' function */
    operation  multiply_integer;  /* function pointer for 'mul' function */
} math;

/***********************************************************
 * Designated Initialization With 'Compound Literals' in C
 *
 * Refer this weblink for more information on compound literals.
 * http://nickdesaulniers.github.io/blog/2013/07/25/designated-initialization-with-pointers-in-c/
 ***********************************************************/
math calc = 
{
    .add_integer = add,
    .multiply_integer = mul,
};

int main(int argc, char **argv)
{
    int  result_add ;       /* to store the result */
    int  result_mul ;       /* to store the result */

    result_add = calc.add_integer(3, 4); 
    result_mul = calc.multiply_integer(3, 4);

    printf ("the result is %d\n", result_add);
    printf ("the result is %d\n", result_mul);

    return 0;
}


Output:

./a.out 
the result is 7
the result is 12

C program on using sigaction() to setup a signal handler with 3 arguments

C program on using sigaction() to setup a signal handler with 3 arguments 


/* Example of using sigaction() to setup a signal handler with 3 arguments
 * including siginfo_t.
 */
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>

static void hdl (int sig, siginfo_t *siginfo, void *context)
{
        printf ("Sending PID: %ld, UID: %ld\n",
                        (long)siginfo->si_pid, (long)siginfo->si_uid);
}

int main (int argc, char *argv[])
{
        struct sigaction act;

        memset (&act, '\0', sizeof(act));

        /* Use the sa_sigaction field because the handles has two additional parameters */
        act.sa_sigaction = &hdl;

        /* The SA_SIGINFO flag tells sigaction() to use the sa_sigaction field, not sa_handler. */
        act.sa_flags = SA_SIGINFO;
//      act.sa_flags = SA_INTERRUPT;

        if (sigaction(SIGSEGV, &act, NULL) < 0) {
                perror ("sigaction");
                return 1;
        }

        while (1)
                sleep (10);

        return 0;
}


C program on fcntl system call usage with flag F_GETFL

C program on fcntl system call usage with flag F_GETFL 


/* F_GETFL in fcntl return positive integer if the First arguemnt
   entered into "fcntl is a valid FD. It returns -1, if the FD is an invalid FD.
 */

/* In the below program File Descriptors 0, 1 and 2 are stdout, stdin and stderr.
   3 is the FD for newly created file "file.txt", which is also a valid FD. So,
   FD's beyond 4 will be invalid and this can be detected using fcntl function call
   with 2nd arguement set to F_GETFL  */


#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

int main()
{
   FILE * fp;
   fp = fopen ("file.txt", "w+");
   if (fp != NULL)
   {
       printf("\n New file descriptor created successfully \n");
   }
   /* write into file */
   fprintf(fp, "%s %s %s %d", "We", "are", "in", 2012);

   int i;

   for(i = 0; i< 5; i++)
   {
       printf("******************************************************\n");
       printf("\n File descriptor entered into fcntl: %d", i);
       int flags;
       if((flags = fcntl(i,F_GETFL,0)) < 0){
          perror("fcntl: ");
       }
       printf("\n return value:%d\n", flags);
       printf("******************************************************\n");
   }

   fclose(fp);
   return 0;
}

Output


 New file descriptor created successfully
******************************************************

 File descriptor entered into fcntl: 0
 return value:32770
******************************************************
******************************************************

 File descriptor entered into fcntl: 1
 return value:32770
******************************************************
******************************************************

 File descriptor entered into fcntl: 2
 return value:32770
******************************************************
******************************************************

 File descriptor entered into fcntl: 3
 return value:32770
******************************************************
******************************************************

fcntl: : Bad file descriptor
 File descriptor entered into fcntl: 4
 return value:-1
******************************************************


C program for Parent process registering for SIGCHLD signal and waits in the signal handler for Child process to die

C program for Parent process registering for SIGCHLD signal and waits in the signal handler for Child process to die

  
#include <stdio.h>

#include <signal.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/wait.h>

#include <stdlib.h>

void handler(int sig)

{

  pid_t pid;

  pid = wait(NULL);

  printf("Pid %d exited. Signal caught\n", pid);

}

int main(void)

{

  signal(SIGCHLD, handler);

  if(!fork())
  {
    printf("\n Child pid is %d\n", getpid());

    printf("\n child exited\n");

    exit(0);
  }

  printf("Parent pid is %d\n", getpid());
  getchar();

  return 0;
}

Output:
Parent pid is 2249
Child pid is 2250
child exited
Pid 2250 exited. Signal caught

C program on Callback function (Function pointer)

 C program on Callback function (Function pointer)


“In computer programming, a callback is a reference to executable code, or a piece of executable code, that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.”


Function Pointers sample code :

#include<stdio.h>

int func(int, int);

int main(void)

{

    int result1,result2;

    /* declaring a pointer to a function which takes

       two int arguments and returns an integer as result */

    int (*ptrFunc)(int,int);


    /* assigning ptrFunc to func's address */                    

    ptrFunc=func;


    /* calling func() through explicit dereference */

    result1 = (*ptrFunc)(10,20);


    /* calling func() through implicit dereference */        

    result2 = ptrFunc(10,20);              

    printf("result1 = %d result2 = %d\n",result1,result2);

    return 0;

}


int func(int x, int y)

{

    return x+y;

}

 Registered Call back Function sample code

"The higher layer function calls a lower layer function as a normal call and the callback mechanism allows the lower layer function to call the higher layer function through a pointer to a callback function."



C program


/* callback.c */
#include<stdio.h>

/* Function Declaration Prototype  */
typedef void (*callback)(void);
void register_callback(callback ptr_reg_callback);

/* registration goes here */
void register_callback(callback ptr_reg_callback)
{
    printf("###############################################\n");
    printf("Inside register_callback\n");
    printf("In this example, the register_callback\n"
           "and callback function are in user space.\n"
           "In case, the callback function is a 'signal handler',\n"
           "then the 'signal handler' callback function is registered\n"
           "in kernel space, which runs in context of user space process.\n"
           "When the user space process receives the signal, the signal\n"
           "handler callback function from userspace is executed from kernel\n"
           "space. Hence, this is how call back is achieved.\n");  
    printf("###############################################\n\n");

    /* calling our callback function func */
    (*ptr_reg_callback)();
}


/* callback function definition goes here */
void func(void)
{
    printf("###############################################\n");
    printf("inside the invoked func, that is callback registered\n");
    printf("###############################################\n\n");
    return;
}

int main(void)
{
    printf("###############################################\n");
    printf("Main Program Starts\n");
    printf("This is a program demonstrating function callback\n");
    printf("###############################################\n\n");

    /* initialize function pointer to func */
    callback add_callback = func;

    /* register our callback function */
    register_callback(add_callback);

    printf("###############################################\n");
    printf("back inside main program\n");
    printf("###############################################\n\n");

    return 0;
}

Output:  ./a.out 

###############################################
Main Program Starts
This is a program demonstrating function callback
###############################################

###############################################
Inside register_callback
In this example, the register_callback
and callback function are in user space.
In case, the callback function is a 'signal handler',
then the 'signal handler' callback function is registered
in kernel space, which runs in context of user space process.
When the user space process receives the signal, the signal
handler callback function from userspace is executed from kernel
space. Hence, this is how call back is achieved.
###############################################

###############################################
inside the invoked func, that is callback registered
###############################################

###############################################
back inside main program
###############################################

< Just remember, callback is nothing but passing the function pointer to the code from where you want your handler/callback function to be invoked.  >  

How to implement signal handler for a process in linux

How to implement signal handler for a process in linux

Signals are types of interrupts that are generated from the kernel, and are very useful for handling asynchronous events. A signal-handling function is registered with the kernel, and can be invoked asynchronously from the rest of the program when the signal is delivered to the user process.



In the below example, signal handler (which is the call back function) is passed to the API "signal" / signal handler is registered with the Kernel, using Function pointer. So, when the kernel receives a particular signal, then it invokes this call back function whose code gets executed then. Basically, the signal handler is passed into signal API from a user space process, but this signal handler code is executed by Kernel, when the Kernel receives that signal.   


 

/ * This code catches the alarm signal generated from the kernel



    Asynchronously */


#include<stdio.h>
#include<signal.h>
#include<unistd.h>

void sig_handler(int signo)
{
    if (signo == SIGUSR1)
        printf("received SIGUSR1\n");
    else if (signo == SIGKILL)
        printf("received SIGKILL\n");
    else if (signo == SIGSTOP)
        printf("received SIGSTOP\n");
}

int main(void)
{    /* register signal handler */
    if (signal(SIGUSR1, sig_handler) == SIG_ERR)
        printf("\ncan't catch SIGUSR1\n");
    if (signal(SIGKILL, sig_handler) == SIG_ERR)
        printf("\ncan't catch SIGKILL\n");
    if (signal(SIGSTOP, sig_handler) == SIG_ERR)
        printf("\ncan't catch SIGSTOP\n");
    // A long long wait so that we can easily issue a signal to this process. So, wait for the signal to be received from the kernel.
    while(1)
        sleep(1);
    return 0;
}
Output :
$ ./sigfunc

can't catch SIGKILL

can't catch SIGSTOP