daemonize
This commit is contained in:
12
System2/sendToFile/Makefile
Normal file
12
System2/sendToFile/Makefile
Normal 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
|
||||
@@ -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();
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
coucou, je suis bien en vie!
|
||||
|
||||
Reference in New Issue
Block a user