= c =

C language

== test example ==
{{{#!highlight c
/* 
cc test.c -o test 
./test
*/
#include <stdio.h>
#define SIZE 5
#define FILENAME "test.txt"

void clear(char *buffer)
{
        for (int i = 0; i < SIZE; i++) {
                *(buffer+i) = 0x00;
    }
}

int main(void)
{
    printf("Hello World\n");
    FILE *f = fopen(FILENAME, "w");
    char buffer[SIZE];
    clear(buffer);

    for (int i = 0; i < 3; i++) {
        fwrite("aaaa\n", 1,SIZE, f);
    }

    fclose(f);

    FILE *r = fopen(FILENAME, "r");
    clear(buffer);

    int amount = -1;

    while (amount != 0 ) {
        amount = fread( buffer,1,SIZE,r );
        printf("%d\n",amount);
        if(amount !=0) {
            for(int k=0;k<amount;k++){
                  printf("%c", *(buffer+k) );
            }
            printf("\n");
            clear(buffer);
        }
    }

    fclose(r);
    return 0;
}
}}}

== Posix threads ==
{{{#!highlight c
/* 
cc pthread.c -o pthread -lpthread
./pthread
*/
#include <stdio.h> 
#include <stdlib.h> 
#include <pthread.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>

typedef struct data {
  char msg[128];
  int count;
} threadData;


void signalHandler(int signal){
  if(signal == SIGTERM){
    printf("Caught SIGTERM signal. Going to exit ...\n");
    exit(0);
  }
 
  if(signal == SIGUSR1){
    printf("USR1 caught \n");
  }

  if(signal == SIGUSR2){
    printf("USR2 caught \n");
  } 
}

void *threadCallback( void *ptr ) {
  char *message  = (char *) ((threadData *) ptr)->msg;
  int count =  ((threadData *) ptr)->count;
 
  while(count<10) {
    printf("%s count %d thread id %u \n", message,count , pthread_self() );
    sleep(1);
    count++;
  }
}

int main() {
  struct sigaction signalAction;
  memset (&signalAction, '\0', sizeof(signalAction));
  signalAction.sa_handler = &signalHandler;

  pthread_t thread1Id;
  int thread1Ret;
  threadData thread1Data;
  strcpy(thread1Data.msg,"Message 1234");
  thread1Data.count=5;
   
  pthread_t thread2Id;
  int thread2Ret;
  threadData thread2Data;
  strcpy(thread2Data.msg,"Message 54321");
  thread2Data.count=3;
  
  printf("Main thread id %u \n" , pthread_self() );

  thread1Ret = pthread_create( &thread1Id, NULL, threadCallback, (void*) &thread1Data);
  thread2Ret = pthread_create( &thread2Id, NULL, threadCallback, (void*) &thread2Data);

  if(thread1Ret || thread2Ret) {
    fprintf(stderr,"Error creating threads ");
    exit(EXIT_FAILURE);
  }

  // callback to signalHandler function
  sigaction(SIGTERM  , &signalAction, NULL);
  sigaction(SIGUSR1  , &signalAction, NULL);
  sigaction(SIGUSR2  , &signalAction, NULL);

  pthread_join( thread1Id, NULL);
  pthread_join( thread2Id, NULL);
  printf("Normal exit\n");
  exit(EXIT_SUCCESS);
}

}}}

== cbeat service ==
{{{#!highlight c
// cc cbeat.c -o cbeat

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

#define TRUE 1

void signalHandler(int signal){
  if(signal == SIGTERM){
    syslog(LOG_INFO , "Caught SIGTERM signal. Going to exit ...");
    exit(0);
  }
}

int main(){
  int pid = getpid();
  FILE* handle =  fopen("/tmp/cbeat.pid","wb");
  fprintf(handle, "%d" ,pid);
  fclose(handle);

  // callback to signalHandler function
  signal(SIGTERM , signalHandler  );

  while(TRUE){
    syslog(LOG_INFO,"cbeat test");
    sleep(5);
  }

  return 0;
}
}}}