Go Premium for a chance to win a PS4. Enter to Win


starting a new process

Posted on 2004-09-15
Medium Priority
Last Modified: 2010-04-22
I'm trying to write a program that creates a new process (runs another program) but i couldn't find exactly what I need:

system(command) - very nice, very easy to use, but it WAITS for the created process to return
exec family of functions - they REPLACE the current process with the new one, and I sure don't want that

what i want is to create a new process (like system() does), but also leave the current one alive and running.

i suspect it has smth to do with the fork function but i don't know how to use it (what does it return ??)

Question by:hapciu
  • 3
  • 2
  • 2
  • +4
LVL 23

Expert Comment

ID: 12071032
This is generally done with fork() [or preferably vfork()], followed by exec().
The vfork() clones the current process, the exec() replaces the clone with
the new process.
LVL 11

Expert Comment

ID: 12071513
http://www.cs.cf.ac.uk/Dave/C/node22.html  has a example of fork() usage , not really useful but shoudl help you get started

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.


Accepted Solution

pankajtiwary earned 90 total points
ID: 12072692
fork() creates a new process which is a copy of the original process. What the system command does is fork() and then wait() which blocks the parent as long as the child finishes.

A simple example can be:

#include <unistd.h>
#include <stdio.h>

int main() {
    pid_t pid ;
    if ( (pid = fork()) == 0 ) {                // Child
        printf("I am in child\n");
    else if (pid < 0) {                          // error
        printf("error forking\n");
    else if ( pid > 0 ) {                         // parent
        printf("I am in parent\n");
    return 0 ;

Inside the corresponding if block, you can do whatever you want to do for the parent or the child. But remember, when you are done with everything in the parent and it is going to terminate, do remember to wait() for the child.


Expert Comment

ID: 12082435
Use this code.

        pid = fork();
        if(pid < 0)
          /* Handle Error */              
        else if(pid == 0)
              /* New process : execute ur another executable : test_name is ur executable*/
                 // this process must exit after failure to exec


Author Comment

ID: 12084828
pankajtiwary  :
pankaj_thapa  :

so is it madatory to wait for the child or isn't it ? I would like not to wait because I want this function to launch an application and then have them both run in the same time - the first application (the parent) and the launched app ...


ps: the system function knows how to run a program just like in the console - the full path is not required. i like that :) . execl needs the full path ... can I get around this ?

Expert Comment

ID: 12099434

 U don't need to wait for the child process , (exec process) , to avoid zombie process, u can have a child handler.


Author Comment

ID: 12104101
what is a child handler ? what do you mean by that ?

Expert Comment

ID: 12169580
A Child handler is in fact a PID (Process ID).

If a child process terminate before its parent and its parent doesn't care about it, the child become a zombie (You can see zombie process with "ps"). It is not dangerous but it is not clean programming because resources allocated by the child are not freed.
When you fork you always have to wait for your child or exit.

In fact useful methods for making new process are :
wait() : this method wait for the *first* child terminaison.
wait3() and wait4() : you specify *the* child to wait for.

Tip: signal() should be useful if you don't want to call wait(), you can program a signal handler that call it for you on demand when the child die (see SIGCHLD with "man 7 signal"). Ask me if you want an example.

As always the best way to understand those confusing problems is to test yourself these system calls.
Experts have post many examples this is a good start.

Good luck!



Expert Comment

ID: 12174294
You need to understand a couple of things and wlways keep those in mind, you will never get confused.

1. Once you have created a new process, for the kernel, its 2 different process, so both will run together.

2. Since both will run together and independently, any one can finish any time according to the code.

3. You need to understand what happen when one of them finishes.

4. If parent finishes and child is still running (or sleeping), the child is given to the init process, that means init will now be the parent of the child process and handle everything for the child.

5. If child finishes first and parent is still running (or doing something) the child sends a signal SIGCHLD to the parent so that the parent can do the necessary cleanup. If the parent is not handling this signal and doing sometyhing else (running a loop etc), the child's status still resides with the kernel process table. This actualy contains the exit status of the child. This is because any time the parent can ask for the child about how it exited. This child is called a zombie because its not running and not consuming any resources but it's entry is still with the kernel process table.

6. At any time in its course, the parent can called wait. If the parent calls wait and the child is still running, the parent will block until the child finishes. But if the child has finished and the parent calls wait(), it will return instantly and since the parent knows the child has finished, it can surely make the cleanups and hence there is no need for the child entry to remain in the process table so no zombie.

Hope this helps.

Expert Comment

ID: 12174304
If you see, when you call system(xyz), the command xyz is executed in the child and you can see, as long as the child is running, the parent can not run because it has blocked. Why? Because it calls wait(). Feel free to clear any clarifications.

Expert Comment

ID: 12188532
Add this near the beginning of your program (main() is a good place for it) before you fork any children


add this function to your program

void cleanupkid(int sig)
  int pid;

  do {
    pid = waitpid(-1,0,WNOHANG);
  } while(pid > 0);

you will not have any zombie processes as long as you are not blocking the SIGCHLD signal

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question