Can we start a background process using exec() giving & as an argument?

17,806

Solution 1

In Unix, exec() is only part of the story.

exec() is used to start a new binary within the current process. That means that the binary that is currently running in the current process will no longer be running.

So, before you call exec(), you want to call fork() to create a new process so your current binary can continue running.

Normally, to have the current binary wait for the new process to exit, you call one of the wait*() family. That function will put the current process to sleep until the process you are waiting is done.

So in order to create a "background" process, your current process should just skip the call to wait.

Solution 2

Use the fork() call to create a new process, then exec() to load a program into that process. See the man pages (man 2 fork, man 2 exec) for more information, too.

Solution 3

Fork returns the PID of the child, so the common idiom is:

if(fork() == 0)
    // I'm the child
    exec(...)
Share:
17,806
unwind
Author by

unwind

Handsomely bearded hacker who likes C, Linux, bash, GTK+, OpenGL, game development, text processing, the occasional assembly (though more commonly on random 8-bit machines than x86), Python and stuff. Career I work as a contractor at AFRY Systems Engineering. I'm currently focusing on embedded & connected devices as a tool and accessory software developer at Atlas Copco Industrial Technique. Before that, I helped EA DICE on big-budget, cross-platform games running on Windows, PlayStation 3, and the Xbox 360. Personal projects: I wrote the gentoo filemanager which a few people still seem to enjoy using, even after 10+ years. If you use Geany, check out Gitbrowser and Geanyuniq, two handy plug-ins. main(O){10<putchar(4^--O?77-(15&5128>>4*O):10)&&main(2+O);}

Updated on June 04, 2022

Comments

  • unwind
    unwind almost 2 years

    If not, how can we start a background process in C?

  • avd
    avd over 14 years
    fork and exec has nothing to do with background process
  • unwind
    unwind over 14 years
    @aditya: Would you care to expand on that? fork() is how to create a new process, which is what you need to do if you want to run something in the background from a C program ...
  • Carl Norum
    Carl Norum over 14 years
    @aditya fork has exactly 100% to do with creating a background process. What are you talking about?
  • Sw0ut
    Sw0ut over 5 years
    Be careful, this creates a zombie process.
  • Sw0ut
    Sw0ut over 5 years
    I suggest to create a thread, then in the thread you can fork and wait for the child.
  • R Samuel Klatchko
    R Samuel Klatchko over 5 years
    @Sw0ut - a thread is a very expensive way to prevent a zombie. The standard recommendation is to catch SIGCHLD and then call wait in your signal handler.
  • Sw0ut
    Sw0ut over 5 years
    @Samuel Klatchko I absolutely needed a thread in my program because 1. It must be async (i just want to run the program in background) 2. I run a large amount of times that child program, which leads to a large amount of zombies (about 6000 in 1 hour if unstopped). With the SIGCHLD it didn't work like expected, it seemed to wait for the end of the child program, maybe bause it was run on the main thread.