0

i ve searched quite a lot already and am aware that maybe i already came across some helpful answers but were unable to understand them...not really much of a programmer i am: )

the case is i would like to implement a daemon - background process totally independent of what is happening in the meantime - to parse data which are being received from the server (am writing IRC client) but to do it in non-blocking way using select().

Here s fragment of my code. But i cant get to printf under the comment //rest of the program

i hope it s understandable enough.

pID=fork();
        if(pID < 0){
            //failed to execute fork()
            perror("Error while forking");
            getchar();getchar();
            exit(1);
        }
        if(pID > 0){
            exit(0);
            }
        //child down here
            umask(0);
            //setting new session
            sID = setpgid(0,0);
            if(sID < 0){
                perror("Error while setting new session");
                getchar();getchar();
                exit(1);
            }
            while(1){
                sleep(1);
                if((readReady = readReadiness(sockfd))==0)
                    continue;
                else{
                    //here am going to recv() data from server
                    printf("I am child and parsing\n"); //testing if its in background -i would like it not to print to stdout (run in background)

                }
            }


        //rest of the program down here
        printf("Why isnt it printing to stdout ??\n");

should i use grandchild ? (double forking ?) or... ?

Thanks in advance

azrahel
  • 1,143
  • 2
  • 13
  • 31
  • 2
    Your parent process has already exited, and the child is looping in `while(1)` so what do you expect to reach the printf ? – William Morris May 06 '12 at 15:08
  • Ok, i get it, but U did not answer to any part of my question William :) what should i do than ? use double fork so hierarchy will look like: parent->child->grandchild than kill child so grandchild is orphaned and has no terminal control over it, and than parent can work further ? and u suggest i should not use infinite loop ? so how to daemonize it ? thx – azrahel May 06 '12 at 17:59
  • my code is what i get from this link: http://stackoverflow.com/questions/8319484/regarding-background-processes-using-fork-and-child-processes-in-my-dummy-shel that s why it looks that way. answer still needed. greetings – azrahel May 06 '12 at 20:14
  • 1
    I don't know what the question is. Your problem was that the code didn't reach the printf at the end and I think you now know why that is. – William Morris May 06 '12 at 20:25

1 Answers1

2

My comment above was neither answer nor suggestion; it was a question.

I don't see why you need to fork again - you have a process without a controlling terminal (thanks to setpgid) as you desire. You can process received data in the while(1) loop. Looks like you are set to go - the 'rest of the program' goes in the while loop.

EDIT

Calling fork creates a child copy of the running process. Calling setsid in the child will disconnect the child from the terminal but will not affect the parent (which exits anyway). I suggest you start off with something like:

static void
process_stuff(int sockfd)
{
    while (1) {
        /* receive data on socket and process */
    }
}

void
run_daemon(int sockfd, int foreground)
{
    if (!foreground) {
        if (fork()) {
            exit(EXIT_SUCCESS); /* parent */
        }
        /* child */
        setsid();
    }
    process_stuff(sockfd);
}

The reason for having the if (foreground) clause is that it allows you to debug the server in the foreground under gdb when it doesn't work.

William Morris
  • 3,554
  • 2
  • 23
  • 24