I am following a quick guide how to create daemon process in C, that was created as an answer to one of Stack Overflow questions.
And here is a problem, to make this code easier for me I have created a file, where I am logging some debug data, but when I make a second fork(), my new child seems not to log anything. I check the list of processes that exists. I have no idea why it did not print anything to my file. Any ideas? 0.o
My adjusted code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
#define LOG_FILE "/local_pv/asiwek/daemon_test/daemon_log.log"
static void skeleton_daemon(FILE* file)
{
pid_t pid;
/* Fork off the parent process */
pid = fork();
/* An error occurred */
if (pid < 0)
exit(EXIT_FAILURE);
/* Success: Let the parent terminate */
if (pid > 0)
exit(EXIT_SUCCESS);
fprintf(file ,"I'm a child! \n");
/* On success: The child process becomes session leader */
if (setsid() < 0)
{
fprintf(file ,"ERROR: setsid()! \n");
exit(EXIT_FAILURE);
}
/* Catch, ignore and handle signals */
//TODO: Implement a working signal handler */
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
/* Fork off for the second time*/
pid = fork();
/* An error occurred */
if (pid < 0)
{
fprintf(file ,"ERROR: fork()! \n");
exit(EXIT_FAILURE);
}
/* Success: Let the parent terminate */
if (pid > 0)
{
fprintf(file, "I'm a parent of the child! pid : %d", pid);
exit(EXIT_SUCCESS);
}
fprintf(file ,"New Child!! \n"); // here program stops logging
/* Set new file permissions */
umask(0);
/* Change the working directory to the root directory */
/* or another appropriated directory */
chdir("/local_pv/asiwek/daemon_test/");
fprintf(file ,"chdir \n");
/* Close all open file descriptors */
int x;
for (x = sysconf(_SC_OPEN_MAX); x>=0; x--)
{
close (x);
}
/* Open the log file */
//openlog ("firstdaemon", LOG_PID, LOG_DAEMON);
fprintf(file ,"skeleton_daemon() end. \n");
}
int main()
{
FILE* f = fopen(LOG_FILE, "w");
skeleton_daemon(f);
fprintf(f ,"Oh boy I am a daemon!!! \n");
while (1)
{
//TODO: Insert daemon code here.
// syslog (LOG_NOTICE, "First daemon started.");
fprintf(f, "Some log.");
sleep (200);
break;
}
//syslog (LOG_NOTICE, "First daemon terminated.");
//closelog();
fclose(f);
return EXIT_SUCCESS;
}
Related
I'm implementing a function which receives a parsed-to-array command lines ("./waiter 20 &" will be parsed, and the function will receive the array
{"./waiter","20","&"} for example).
If the last argument is &, the process should run in the background.
To prevent zombies, I need to use a new thread that will wait for the child process.
The attached code is my working program, and all my efforts to add a new thread that will wait for the child process failed.
Anyone can guide me?
Attaching the code, with some of the leftovers of my unsuccessful tries.
(the function is process_arglist)
update: after a lot of tries using what suggested here, its still failing and im not sure why. updated code attached.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
void func(void* ptr) {
pid_t* mypid = (pid_t*)ptr;
waitpid(*mypid);
pthread_detach(pthread_self());
}
int process_arglist(int count, char** arglist){
int isBackground = 0;
pid_t pid;
int status;
char** parsedList;
if (strcmp(arglist[count-1],"&") == 0) {
printf("###we are in the & situation\n");
parsedList = (char**)malloc((count-1)*sizeof(char*));
if (parsedList == NULL) {
printf( "Error: malloc failed - %s\n", strerror(errno));
}
int i;
for (i=0;i<count-1;i++){
parsedList[i] = arglist[i];
}
/*printf("parsed list:\n");
for (i=0;i<count-1;i++) {
printf(" %d: %s\n", i,parsedList[i]);
}*/
if ((pid = fork()) < 0) { /* fork a child process */
printf( "Error: fork failed");
exit(0);
} else if (pid == 0) { /* for the child process: */
if (execvp(*parsedList,parsedList) < 0) { /* execute the command */
printf( "Error: execvp failed - %s\n", strerror(errno));
exit(0);
}
} else {
pthread_t thread;
pthread_create(&thread, NULL, (void*) &func, (void*) &pid);
}
} else {
if ((pid = fork()) < 0) { /* fork a child process */
printf( "Error: forking child process failed - %s\n", strerror(errno));
exit(0);
}
else if (pid == 0) { /* for the child process: */
if (execvp(*arglist,arglist) < 0) { /* execute the command */
printf( "Error: execvp failed - %s\n", strerror(errno));
exit(0);
}
}
else { /* for the parent: */
while (waitpid(&status) != pid); /* wait for completion */
}
}
}
First, switch from calling wait to calling waitpid. Otherwise, if you have more than one thread waiting, they'll steal each other's notifications.
Second, break the call to waitpid into its own function that takes the PID to wait for as a parameter. Cast it through void *, since that's what's used for thread parameters.
Third, change the call to the function to a call to pthread_create, casting the PID to wait for to a void * to pass to the newly-created thread.
Lastly, have the thread detach itself, since there won't be anything waiting for the thread to terminate.
Question asked again and code modified...
I need to create three programs named as program0 program1 and program2 in linux.
Program0:Creates a parent with two child processes and executes program 1 and program 2 with its childs waits them to finish and close.
Program1:Takes a file name from the user and writes text to the file.It finishes writing when CTNL+D pressed and creates a pipe.After that by using cat command it writes file to stdout and uses dup() to create pipe which has file in it.
Program2:It reads filename from the pipe with the help of dup() and then executes wc command.
So far I managed to create all programs and I have no compling errors.Program 0 executes both programs.Program1 is also working and sends file to the pipe but program2 cannot read it from the pipe is prints weird symbols..
When I try to read from the pipe within the program1 it works(see the deactivated code in program1) but same code is not working if I put it inside program2.
So what how can I make program2 to read from the pipe after that I will try to execute wc command in program2 but first I should be able to see that its taking file input from the stdout so how?
I know its kinda long but please help me guys...
Program 0
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#define MAX 999
int main()
{
pid_t pid1, pid2;
pid1 = fork();
if(pid1<0)
{
fprintf(stderr,"Fork basarisiz");
exit(-1);
}
else if (pid1 ==0)/*child prosesleri*/
{
printf("program1\n");
execlp("./program1","program1",NULL);
execlp("./program2","program2",NULL);
}
else /*parent procsesleri */
{
wait(NULL);
pid2 = fork();
if(pid2<0)
{
fprintf(stderr,"Fork basarisiz");
exit(-1);
}
else if (pid2 ==0)/*child prosesleri*/
{
printf("\n");
printf("Program 2\n");
printf("\n");
execlp("./program2","program2",NULL);
//printf("\n");
}
else
{
}
/////////////////////////////////////////////////////////////////////////
wait(NULL);
printf("\n");
printf("Parent:Two child processes have successfully been created\n");
printf("Parent:Two child processes have successfully been terminated\n");
printf("Parent:This process will now terminate\n");
printf("\n");
exit(0);
}
}
Program 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#define MAX 999
int main()
{
char c[10000];
char file[10000];
int words;
printf("Child1:A text file will be created\n");
printf("Child1:Enter the name of the file\n");
scanf("%123s",file);
strcat(file,".txt");
FILE * pf;
pf = fopen(file, "w" );
if (!pf)
fprintf( stderr, "I couldn't open the file.\n" );
else
{
printf("Child1: Input a number of text lines ended, each ended by a CR (carriage return).\n");
/////////////////////////////
do
{
if (NULL != fgets(c, sizeof(c), stdin))
{
if (0 == strcmp(c, ".\n"))
{
break;
}
fprintf(pf, "%s", c);
}
else
{
if (0 != ferror(stdin))
{
fprintf(stderr, "An error occured while reading from stdin\n");
}
else
{
printf("Child1: Finish the input by CNTL^D\n");
}
break;
}
} while (1);
/////////////////////////////
}
printf("\nChild1:The file %s is succesfully created and saved in the current dictionary\n",file);
//////////////////////////////////////////////
/////////////////////////pipe///////////////
fclose(pf); // close file
char ch;
int outcount = 0;
int fd[2], nbytes;
pid_t childpid;
int i;
char f2[2];
char readbuffer[80];
pipe(fd);
if((childpid = fork()) == -1)
{
perror("fork");
exit(1);
}
if(childpid == 0)
{ printf("\nChild1:The file written to pipe with cat\n");
close(1) ;
dup(fd[1]);
close(fd[0]);
execlp("/bin/cat", "cat", file,NULL);
}
else
{
wait(NULL);
//close(0) ;
//dup(fd[0]) ;
//close(fd[1]);
//nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
//printf("%s\n",readbuffer);
}
return(0);
}
Program 2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd[2],nbytes;
pid_t childpid;
char readbuffer[80];
pipe(fd);
if((childpid = fork()) == -1)
{
perror("fork");
exit(1);
}
if(childpid == 0)
{
}
else
{
close(0) ;
dup(fd[0]) ;
close(fd[1]);
nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
printf("%s\n",readbuffer);
}
return(0);
}
You may want to check the man pages for execve(2) (for starting cat) and dup2(2) (for overriding stdin and stdout as needed) for this one. execve will overwrite the currently executing program by a different one (same PID, same file descriptors), while dup2 will allow you re-define any of the standard file descriptors to point into any file descriptor you provide to it (such as any of the ends of your pipe).
Sorry for the length of this post... I've encountered about a zillion problems in this. Up front I'll say I'm a student and my professor is a worthless resource. So, all I want to to do is have producer fork, then the parent producer will count some stuff in a file and send two ints to consumer, which was launched by the child process. I've tested everything, the fork and the file stuff works and I have printf statements all over the place so I know what is being done and where the code is at.
When I added the
if (pipe(pipefd) == -1) {
perror("pipe");
}
it caused my parent to just terminate. It reaches "parent pipe open" but then it dies. I checked with $ ps to see if it was just hung, but it's not there; it just dies. If I take that snippet out, it runs to the end but I presume if that code isn't there, then it's not actually aware that pipefd is a pipe... right?
I did search on this site and found another example of this and followed what he did as well as the answer and mine just refuses to work. I'm pretty sure it's a trivially easy thing to fix but I've run out of ideas of what to try :(
I don't really want to post all my code because it'll be a huge wall of text but I don't want to accidentally cut something out that turns out to be important either.
producer.c
#include <stdio.h> /* printf, stderr, fprintf */
#include <sys/types.h> /* pid_t */
#include <unistd.h> /* _exit, fork, execl */
#include <stdlib.h> /* exit */
#include <errno.h> /* errno */
#include <string.h> /* strlen */
#include <sys/wait.h> /* wait */
#define SLEEP_TIME 8
int main (int argc, char *argv[]){
//PID
pid_t local_pid;
local_pid = fork();
//Logic to determine if the process running is the parent or the child
if (local_pid == -1) {
/* Error:
* When fork() returns -1, an error happened
* (for example, number of processes reached the limit).
*/
fprintf(stderr, "can't fork, error %d\n", errno);
exit(EXIT_FAILURE);
} else if (local_pid == 0) {
//Child specific code
int child;
char *temp[] = {NULL};
printf("Child PID found\n");
child = execv("./consumer", temp);
_exit(0);
} else {
//Parent specific code
printf("Parent running\n");
//open file
FILE * randStrings;
randStrings = fopen("randStrings.txt", "r");
int file_length;
int num_of_e = 0;
int c; //using this as a char
//until eof
while (feof(randStrings) == 0) {
c = fgetc(randStrings);
//calculate length of file
file_length++;
//count e chars
if (c == 'e') {
num_of_e++;
}
}
//close file
fclose(randStrings);
//send bundle to child
int a[2];
a[0] = num_of_e;
a[1] = file_length;
printf("num of e = %i\n", a[0]);
printf("len = %i\n", a[1]);
//set up parent pipe
int pipefd[2];
if (pipe(pipefd) == -1) {
perror("pipe");
printf("x\n");
}
printf("parent pipe open\n");
close(pipefd[0]); //close the read end
write(pipefd[1], &a[0], sizeof(int));
write(pipefd[1], &a[1], sizeof(int));
close(pipefd[1]);
printf("parent pipe closed\n");
//wait for child to finish running
wait(NULL);
printf("parent out\n");
//terminate
}
}
and consumer.c
#include <stdio.h> /* printf, stderr, fprintf */
#include <sys/types.h> /* pid_t */
#include <unistd.h> /* _exit, fork, execl */
#include <stdlib.h> /* exit */
#include <errno.h> /* errno */
#define SLEEP_TIME 5
int main (int argc, char *argv[]){
sleep(SLEEP_TIME);
printf("Child program launched\n");
//receive bundle
int pipefd[2];
int buf[2];
if (pipe(pipefd) == -1) {
perror("pipe");
printf("child x\n");
}
close(pipefd[1]); //child closes write end
buf[0] = 0;
buf[1] = 0;
/*int i = 0; // i dont like this
while (read(pipefd[0], &buf[i], sizeof(int)) > 0) {
i++;
}*/
printf("child reading pipe\n");
read(pipefd[0], &buf[0], sizeof(int));
read(pipefd[0], &buf[1], sizeof(int));
close(pipefd[0]);
//buf should have the stuff in it
int num_of_e = buf[0];
int file_length = buf[1];
printf("child num of e = %i\n", num_of_e);
printf("child len = %i\n", file_length);
//open file
FILE * resultStrings;
resultStrings = fopen("resultStrings.txt", "w");
for (int i = 0; i < num_of_e; i++) {
//write num_of_e e chars
fputc('e', resultStrings);
}
//or if no e chars, write - chars
if (num_of_e == 0) {
for (int i = 0; i < file_length; i++) {
//write file_length '-' chars
fputc('-', resultStrings);
}
}
//close file
fclose(resultStrings);
printf("child out\n");
}
if you're still here after all that, you deserve a thank you just due to the length of this.
You're doing it wrong. The whole mechanism works because a child process inherits the parent's open file descriptors.
It should go like this:
Open the pipe with pipe(pipefd)
fork()
Parent (producer):
closes the read side (pipefd[0])
writes to the write side (pipefd[1])
Child (consumer):
closes the write side (pipefd[1])
reads from the read side (pipefd[0]) or calls exec
You are opening distinct pipes in both the parent and child process (after you've forked.) It needs to happen before you fork.
Now since you're execing, the new process needs to be aware of read-only pipe. There are a couple ways you could do this:
Pass it the file descriptor number (pipefd[0]) on the command line
dup2(1, fd) it to be the stdin of the newly exec'd process
My server needs to support multiple clients , for the moment let's assume that we're
working with 2 clients .
Here's the server :
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#define FIFONAME "fifo_clientTOserver"
#define SHM_SIZE 1024 /* make it a 1K shared memory segment */
#define ROWS 10
#define COLS 10
void error(char* str)
{
perror(str);
exit(1);
}
int main(int argc, char *argv[])
{
unlink(FIFONAME); // remove any previous fifo pipes
// create a FIFO named pipe - only if it's not already exists
if(mkfifo(FIFONAME , 0666) < 0)
error("mkfifo");
/**
* process 1
*/
// open the fifo for reading
int server_to_client = open(FIFONAME, O_RDONLY);
int reading;
while (1)
{
if (read(server_to_client, &reading ,sizeof(int)) < 0)
perror("read");
else
break;
}
printf("Reading from the fifo : %d\n" , reading);
if (close(server_to_client) < 0)
error("close");
// casting into pid_t
pid_t pid = (pid_t)reading;
// signal to the process that he's the first
kill(pid, SIGUSR2);
/**
* process 2
*/
printf("Now waiting for process 2...\n");
// doing it again - this time for the second process
// remove any previous fifo pipes
unlink(FIFONAME);
// create a FIFO named pipe - only if it's not already exists
if(mkfifo(FIFONAME , 0666) < 0)
error("mkfifo");
printf("Server tester1\n");
server_to_client = open(FIFONAME, O_RDONLY);
// grab the PID of process 2
while (1)
{
if (read(server_to_client, &reading ,sizeof(int)) > 0)
break; // got the data
}
printf("Server tester2\n");
printf("Reading from the fifo : %d\n" , reading);
if (close(server_to_client) < 0)
error("close");
// casting into pid_t
pid = (pid_t)reading;
// signal to the process that he's the first
kill(pid, SIGUSR2);
return 0;
}
The problem is , that both clients needs to pass their PID (this is not a father-son relation !!! those are two separate processes) , and then the server signals with SIGUSR2 to the first process that he is the first one chosen , and if so , then that process works with a character of type X .
On the other hand ,if you're the second process ,you work with a character of type Y .
Here's the client :
int static flagger = 0;
char process_char = 'a';
/**
* handler for SIGUSR2
*/
void my_handler(int signum)
{
printf("foo bar\n");
if (signum == SIGUSR2)
{
printf("Received SIGUSR2!\n");
flagger++;
}
printf("flagger is :%d\n" , flagger);
if (flagger == 1)
{
// then process works with "X"
process_char = 'x';
printf("I'm process 1, working with X char\n");
// exit(1);
}
else if (flagger == 2)
{
process_char = 'Y';
printf("I'm process 2 , working with Y char\n");
// exit(1);
}
}
void error(char* str)
{
perror(str);
exit(1);
}
int main(int argc, char *argv[])
{
pid_t pid;
/* get the process id */
if ((pid = getpid()) < 0)
{
perror("unable to get pid");
}
else
{
printf("The process id is %d\n", pid);
}
int pidInt = (int)pid; // convert the pid to int
// write pid into the fifo
int fd = open("fifo_clientTOserver",O_WRONLY); // open the fifo for writing
if(fd < 0)
{
perror("open");
exit(1);
}
signal(SIGUSR2, my_handler);
printf("Tester1\n");
// writing the PID of the client into the pipe
write(fd, &pidInt ,sizeof(int));
close(fd); // closing the pipe
printf("Tester2\n");
while(1)
{
printf("Waiting for the signal...\n");
sleep(1);
}
// more code
}
I tried to use a static int variable in the client (the flagger) to distinguish between the SIGUSR2 signals (either 1st or 2nd) but it doesn't help since , to each client the static flagger is a new variable that starts with 0 and reaches 1 .
How can I distinguish between the 1st time that a process received SIGUSR2 and the second time that another process received SIGUSR2 ?
If you need to pass data around, then signals are not an appropriate mechanism. Consider using a different IPC method, such as named pipes.
I am trying to create a child that calls sort. The parent sends data to the child through a pipe. My code compiles and runs, but there is no output. What am I doing wrong? Am I not closing the pipes correctly, writing the pipes or outputting the data correctly?
[eddit] On my system I need to call /bin/sort NOT /usr/bin/sort!
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <ctype.h>
int main(void){
int pipes[2];
pid_t pid;
FILE *stream;
if(pipe(pipes) == -1)
printf("could not create pipe\n");
switch(fork()){
case -1:
fprintf(stderr, "error forking\n");
break;
case 0:
dup2(pipes[0], STDIN_FILENO);
pid = getpid();
printf("in child, pid=%d\n");
if(close(pipes[1]) == -1)
fprintf(stderr,"err closing write end pid=%d\n", pid);
execl("/usr/bin/sort", "sort", (char*) NULL);
break;
default:
stream = fdopen(pipes[1], "w");
pid = getpid();
printf("in parent, pid=%d\n", pid);
if (stream == NULL)
fprintf(stderr, "could not create file streami\n");
if(close(pipes[0]) == -1)
printf("err closing read end pid=%d\n");
fputs("bob\n",stream);
fputs("cat\n",stream);
fputs("ace\n",stream);
fputs("dog\n",stream);
if(fclose(stream) == EOF)
fprintf(stderr, "error while closing stream\n");
break;
}
return 0;
}
[edit] Here is my working code. Thank you everyone
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void){
int pipes[2];
pid_t pid;
FILE *stream;
int stat;
if(pipe(pipes) == -1)
printf("could not create pipe\n");
switch(fork()){
case -1:
fprintf(stderr, "error forking\n");
break;
case 0:
dup2(pipes[0], STDIN_FILENO);
pid = getpid();
printf("in child, pid=%d\n", pid);
if(close(pipes[1]) == -1)
fprintf(stderr,"err closing write end pid=%d\n", pid);
if(close(pipes[0]) == -1)
fprintf(stderr,"err closing write end pid=%d\n", pid);
execl("/bin/sort", "sort", (char*) NULL);
exit(EXIT_FAILURE);
break;
default:
stream = fdopen(pipes[1], "w");
pid = getpid();
printf("in parent, pid=%d\n", pid);
if (stream == NULL)
fprintf(stderr, "could not create file streami\n");
if(close(pipes[0]) == -1)
printf("err closing read end pid=%d\n");
fputs("bob\n",stream);
fputs("cat\n",stream);
fputs("ace\n",stream);
fputs("dog\n",stream);
if(fclose(stream) == EOF)
fprintf(stderr, "error while closing stream\n");
break;
}
wait(&stat);
return 0;
}
You most certainly do not have enough close() calls in the code, which will lock the processes up.
Pseudo code:
Create pipe
Fork
In parent:
Close read end of pipe
Write data to be sorted down write end of pipe
Close write end of pipe
Wait for child to die
In child
Close write end of pipe
Duplicate read end of pipe to stdin
Close read end of pipe
Exec the sort program
Exit with an error if the exec returns
Note that the pseudo code ends up closing all four ends of the pipe - the two in the parent and the two in the child. If you don't do that, you will run into deadlock.
The only thing you're really missing is calling wait() or waitpid() at the end of the parent's code, so that it doesn't exit until the child has finished.
No arguments to sort command. Simply running an execl will not work.
A simple program to test would be:
int main(void){
execl("/bin/sort","/bin/sort","filename", (char*) NULL);
}
I will try to create a simple program for you to analyze the situation.
Here you go, try this code:
int main(void){
int pipefd[2];
pid_t pid = 0;
int status;
char data[100]={0};
int fildes[2] ;
int nbytes;
char buf[100]={0};
status = pipe(fildes);
if (status == -1 ) {
// handle eerrror.
}
switch (fork()) {
case -1: /* Handle error */
break;
case 0: /* Child - reads from pipe */
close(fildes[1]); /* Write end is unused */
nbytes = read(fildes[0], buf, 100); /* Get data from pipe */
fprintf(stderr,"Inside child val recieved is %s\n", buf);
/* At this point, a further read would see end of file ... */
execl("/bin/sort", "/bin/sort",buf, (char*) NULL);
close(fildes[0]); /* Finished with pipe */
exit(0);
default: /* Parent - writes to pipe */
close(fildes[0]); /* Read end is unused */
write(fildes[1], "file", strlen("file")); /* Write data on pipe */
close(fildes[1]); /* Child will see EOF */
exit(0);
}
}
Here "file" is a file which need to be sorted.
Hope you can customize it as per your need.
Enjoy..!!!