C – create Process Daemon

#include <stdio.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, char *argv[])
{
/*
* Funzione che mi crea un demone
*/

int pid;

// create - fork 1
if(fork()) return 0;

// it separates the son from the father
chdir("/");
setsid();
umask(0);

// create - fork 2
pid = fork();

if(pid)
{
printf("Daemon: %d
", pid);
return 0;
}

/****** Codice da eseguire ********/
FILE *f;

f=fopen("/tmp/coa.log", "w");

while(1)
{
fprintf(f, "ciao
");
fflush(f);
sleep(2);
}
/**********************************/
}

source

Leave a Reply