c

C language

test example

   1 /* 
   2 cc test.c -o test 
   3 ./test
   4 */
   5 #include <stdio.h>
   6 #define SIZE 5
   7 #define FILENAME "test.txt"
   8 
   9 void clear(char *buffer) {
  10     for (int i = 0; i < SIZE; i++) {
  11         *(buffer+i) = 0x00;
  12     }
  13 }
  14 
  15 int main(void) {
  16     printf("Hello World\n");
  17     FILE *f = fopen(FILENAME, "w");
  18     char buffer[SIZE];
  19     clear(buffer);
  20 
  21     for (int i = 0; i < 3; i++) {
  22         fwrite("aaaa\n", 1,SIZE, f);
  23     }
  24 
  25     fclose(f);
  26 
  27     FILE *r = fopen(FILENAME, "r");
  28     clear(buffer);
  29 
  30     int amount = -1;
  31 
  32     while (amount != 0 ) {
  33         amount = fread( buffer,1,SIZE,r );
  34         printf("%d\n",amount);
  35 
  36         if(amount !=0) {
  37             for(int k=0;k<amount;k++) {
  38                   printf("%c", *(buffer+k) );
  39             }
  40             printf("\n");
  41             clear(buffer);
  42         }
  43     }
  44 
  45     fclose(r);
  46     return 0;
  47 }

Posix threads

   1 /* 
   2 cc pthread.c -o pthread -lpthread
   3 ./pthread
   4 */
   5 #include <stdio.h> 
   6 #include <stdlib.h> 
   7 #include <pthread.h>
   8 #include <string.h>
   9 #include <signal.h>
  10 #include <unistd.h>
  11 
  12 typedef struct data {
  13   char msg[128];
  14   int count;
  15 } threadData;
  16 
  17 
  18 void signalHandler(int signal) {
  19   if(signal == SIGTERM) {
  20     printf("Caught SIGTERM signal. Going to exit ...\n");
  21     exit(0);
  22   }
  23  
  24   if(signal == SIGUSR1) {
  25     printf("USR1 caught \n");
  26   }
  27 
  28   if(signal == SIGUSR2) {
  29     printf("USR2 caught \n");
  30   } 
  31 }
  32 
  33 void *threadCallback( void *ptr ) {
  34   char *message  = (char *) ((threadData *) ptr)->msg;
  35   int count =  ((threadData *) ptr)->count;
  36  
  37   while(count<10) {
  38     printf("%s count %d thread id %u \n", message,count , pthread_self() );
  39     sleep(1);
  40     count++;
  41   }
  42 }
  43 
  44 int main() {
  45   struct sigaction signalAction;
  46   memset (&signalAction, '\0', sizeof(signalAction));
  47   signalAction.sa_handler = &signalHandler;
  48 
  49   pthread_t thread1Id;
  50   int thread1Ret;
  51   threadData thread1Data;
  52   strcpy(thread1Data.msg,"Message 1234");
  53   thread1Data.count=5;
  54    
  55   pthread_t thread2Id;
  56   int thread2Ret;
  57   threadData thread2Data;
  58   strcpy(thread2Data.msg,"Message 54321");
  59   thread2Data.count=3;
  60   
  61   printf("Main thread id %u \n" , pthread_self() );
  62 
  63   thread1Ret = pthread_create( &thread1Id, NULL, threadCallback, (void*) &thread1Data);
  64   thread2Ret = pthread_create( &thread2Id, NULL, threadCallback, (void*) &thread2Data);
  65 
  66   if(thread1Ret || thread2Ret) {
  67     fprintf(stderr,"Error creating threads ");
  68     exit(EXIT_FAILURE);
  69   }
  70 
  71   // callback to signalHandler function
  72   sigaction(SIGTERM  , &signalAction, NULL);
  73   sigaction(SIGUSR1  , &signalAction, NULL);
  74   sigaction(SIGUSR2  , &signalAction, NULL);
  75 
  76   pthread_join( thread1Id, NULL);
  77   pthread_join( thread2Id, NULL);
  78   printf("Normal exit\n");
  79   exit(EXIT_SUCCESS);
  80 }

cbeat service

   1 // cc cbeat.c -o cbeat
   2 
   3 #include<stdio.h>
   4 #include<syslog.h>
   5 #include<unistd.h>
   6 #include<signal.h>
   7 #include<stdlib.h>
   8 
   9 #define TRUE 1
  10 
  11 void signalHandler(int signal) {
  12   if(signal == SIGTERM) {
  13     syslog(LOG_INFO , "Caught SIGTERM signal. Going to exit ...");
  14     exit(0);
  15   }
  16 }
  17 
  18 int main() {
  19   int pid = getpid();
  20   FILE* handle =  fopen("/tmp/cbeat.pid","wb");
  21   fprintf(handle, "%d" ,pid);
  22   fclose(handle);
  23 
  24   // callback to signalHandler function
  25   signal(SIGTERM , signalHandler  );
  26 
  27   while(TRUE) {
  28     syslog(LOG_INFO,"cbeat test");
  29     sleep(5);
  30   }
  31 
  32   return 0;
  33 }

Redirect stdout

Redirect stdout and stderr to files and keep using printf and fprintf.

   1 #include <stdio.h>
   2 #include <sys/types.h>
   3 #include <sys/stat.h>
   4 #include <fcntl.h>
   5 #include <unistd.h>
   6 
   7 int main(int argc, char** argv[]){
   8   int fdstdout = open("/tmp/stdout.txt", O_CREAT|O_RDWR|O_APPEND,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
   9   dup2(fdstdout , STDOUT_FILENO );
  10   
  11   int fdstderr = open("/tmp/stderr.txt", O_CREAT|O_RDWR|O_APPEND,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
  12   dup2(fdstderr, STDERR_FILENO );
  13 
  14   printf("Test \n");
  15   fprintf(stdout,"Test via stdout \n");
  16   perror("Error test\n");
  17   fprintf(stderr, "via stderr\n");
  18 
  19   close(fdstdout);
  20   close(fdstderr);
  21 }

c (last edited 2019-08-26 16:03:56 by localhost)