LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   background process (https://www.linuxquestions.org/questions/programming-9/background-process-6813/)

gurra 09-23-2001 04:44 PM

background process
 
How do I make my program run as a background process?

I am trying to make a web-server but I don't want it to show in the console.

(I am programming in C)

Thanx

rshaw 09-23-2001 04:55 PM

when you call the program, include an '&' at the end.

Code:

./myprog &

gurra 09-24-2001 10:14 AM

Yes, but the '&' doesn't work on my program since I have made it myself =) .

rshaw 09-24-2001 10:45 AM

the fact that you wrote it isn't the issue, all software was written by someone. if you start this program(s) at the prompt then the '&' will push it to the background.

gurra 09-24-2001 05:35 PM

wow
 
yes, it really runs in the background!!!

I was misled by the output from the program
but now I see that it really runs as a background process.

thank you very much!

acid_kewpie 09-24-2001 06:16 PM

but surely there is a way to do this properly, in code, rather than a fairly nasty command line adaption. Most daemon programs do return to the background once they've set themselves up, such as fetchmail, which runs it's initial check, and then threads into the background.

pinoy 09-24-2001 09:04 PM

A daemon is a background process without a controlling terminal.
See Unix Programming FAQ

isajera 09-27-2001 02:36 AM

kewpie - i use this to deprioritize any process i don't want to hog the cpu - i think this might be what you were referring to. the higher the number the priority is set to, the less cpu it uses.

#include <sys/resources.h>
#include <unistd.h>

void Set_Process_Priority(void){

int prio;

prio = getpriority(PRIO_PROCESS, getpid());
setpriority(PRIO_PROCESS, getpid(), prio + 10);
}

pinoy 09-28-2001 06:55 PM

Here you go.

Code:

int make_daemon()
{
    pid_t pid;
    if ((pid = fork() < 0) {
        perror("fork error\n");
        return 0;
    } else if (pid != 0)
        exit(0);

    setsid();
    chdir("/");
    umask(0);
    return 1;
}

There's a longer version that ensures it never gets a controlling terminal by double forking, but that should do for most unix.

gurra 09-30-2001 02:31 PM

Thanx
 
Thanx, guys!

That Unix Programming FAQ is great!


All times are GMT -5. The time now is 10:44 PM.