= c = C language == test example == {{{#!highlight c /* cc test.c -o test ./test */ #include #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 #include #include #include #include #include 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 #include #include #include #include #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; } }}} == Redirect stdout == Redirect stdout and stderr to files and keep using printf and fprintf. {{{#!highlight c #include #include #include #include #include int main(int argc, char** argv[]){ int fdstdout = open("/tmp/stdout.txt", O_CREAT|O_RDWR|O_APPEND,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); dup2(fdstdout , STDOUT_FILENO ); int fdstderr = open("/tmp/stderr.txt", O_CREAT|O_RDWR|O_APPEND,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); dup2(fdstderr, STDERR_FILENO ); printf("Test \n"); fprintf(stdout,"Test via stdout \n"); perror("Error test\n"); fprintf(stderr, "via stderr\n"); close(fdstdout); close(fdstderr); } }}}