This program was created to log the counter every 5 seconds. The while loop needs to be running while the daemon process is running in the background and logging the counter.
The error I’m receiving is: Segmentation fault (core dumped). the terminal ask to input a number, when I input the number I receive the error.
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> int main(int argc, char* argv[]) { int clos =0; int count=0;
I think the while loop below may be in the wrong place.
while(clos !=1){ printf("Type: "); scanf("%d",clos); if (clos !=1){ count = clos; } } FILE *fp= NULL; pid_t process_id = 0; pid_t sid = 0; // Create child process process_id = fork(); // Indication of fork() failure if (process_id < 0) { printf("fork failed!n"); // Return failure in exit status exit(1); } // PARENT PROCESS. Need to kill it. if (process_id > 0) { printf("process_id of child process %d n", process_id); // return success in exit status exit(0); } //unmask the file mode umask(0); //set new session sid = setsid(); if(sid < 0) { // Return failure exit(1); } // Change the current working directory to root. chdir("."); // Close stdin. stdout and stderr // Open a log file in write mode. fp = fopen ("Log.txt", "w+"); while (1) { //Dont block context switches, let the process sleep for some time sleep(1); fprintf(fp, "Logging info...n"); fprintf(fp, counter); fflush(fp); // Implement and call some function that does core work for this daemon. } fclose(fp); return (0); }
Advertisement
Answer
Like kjohri said, you need to pass a pointer to clos in the scanf function
Add an & symbol
scanf("%d", &clos); ^
Also it looks like you named the variable count, but in the function you wrote counter. Instead write:
fprintf(fp, "%d", count);