Q:- Create a daemon process in linux to write the numbers 1-10 to text file. Use C programming language.
[root@daemon] ls
Code:-
#include<stdio.h> #include<string.h> #include<sys/types.h> #include<unistd.h> #include<sys/stat.h> #include<stdlib.h> int main() { pid_t pid=0; pid_t sid=0; int i=0; FILE *fp =NULL; pid=fork(); if(pid<0) { printf("Fork failed!!"); exit (1); } if(pid>0) { printf("The pid of child process %d",pid); exit (0); } umask(0); sid= setsid(); if (sid<0) exit (1); close (STDIN_FILENO); close (STDOUT_FILENO); close (STDERR_FILENO); fp=fopen("mydaemon.txt","w+"); while(i<10) { sleep(1); fprintf(fp,"%d",i); i++; } fclose(fp); return 0; }
No comments:
Post a Comment