daemonize

This commit is contained in:
2023-03-29 17:13:13 +02:00
parent 962ee7180e
commit 0f0c9a48dc
7 changed files with 126 additions and 14 deletions

View File

@@ -0,0 +1,12 @@
CC=gcc
all: main clean
main: main.o
$(CC) -o main main.o
main.o: main.c
$(CC) -c main.c
clean:
rm -f *.o

View File

@@ -1,23 +1,34 @@
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
void handler(int signum)
{
FILE *fp;
fp = fopen("test.txt", "w");
fprintf(fp, "coucou, je suis bien en vie!\n");
fclose(fp);
}
int main()
{
signal(SIGUSR1, handler);
for (size_t i = 0; i < 30; i++)
if (signum == SIGUSR1)
{
printf("i = %d, In main\n", i);
sleep(1);
FILE *fp;
fp = fopen("test.txt", "w");
fprintf(fp, "coucou, je suis bien en vie!\n");
fclose(fp);
}
else if (signum == SIGUSR2)
{
exit(EXIT_SUCCESS);
}
else
{
printf("received signal %d\n", signum);
}
return 0;
}
int main(void)
{
if (signal(SIGUSR1, handler) == SIG_ERR)
printf("can't catch SIGUSR1\n");
if (signal(SIGUSR2, handler) == SIG_ERR)
printf("can't catch SIGUSR2\n");
for (;;)
pause();
}

View File

@@ -0,0 +1 @@
coucou, je suis bien en vie!