starting a new process

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 ??)

Who is Participating?

Improve company productivity with a Business Account.Sign Up

pankajtiwaryConnect With a Mentor Commented:
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.

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.
avizitCommented:  has a example of fork() usage , not really useful but shoudl help you get started

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

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

hapciuAuthor Commented:
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 ?

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

hapciuAuthor Commented:
what is a child handler ? what do you mean by that ?
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!


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.
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.
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.