Size: 3609
Comment:
|
Size: 3605
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 163: | Line 163: |
FILE* handle = fopen("/var/run/cbeat.pid","wb"); | FILE* handle = fopen("/tmp/cbeat.pid","wb"); |
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 {
11 for (int i = 0; i < SIZE; i++) {
12 *(buffer+i) = 0x00;
13 }
14 }
15
16 int main(void)
17 {
18 printf("Hello World\n");
19 FILE *f = fopen(FILENAME, "w");
20 char buffer[SIZE];
21 clear(buffer);
22
23 for (int i = 0; i < 3; i++) {
24 fwrite("aaaa\n", 1,SIZE, f);
25 }
26
27 fclose(f);
28
29 FILE *r = fopen(FILENAME, "r");
30 clear(buffer);
31
32 int amount = -1;
33
34 while (amount != 0 ) {
35 amount = fread( buffer,1,SIZE,r );
36 printf("%d\n",amount);
37 if(amount !=0) {
38 for(int k=0;k<amount;k++){
39 printf("%c", *(buffer+k) );
40 }
41 printf("\n");
42 clear(buffer);
43 }
44 }
45
46 fclose(r);
47 return 0;
48 }
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 }