Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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  has a example of fork() usage , not really useful but shoudl help you get started

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.


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

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

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 fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

715 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