Solved

starting a new process

Posted on 2004-09-15
13
254 Views
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 ??)

thanks
0
Comment
Question by:hapciu
  • 3
  • 2
  • 2
  • +4
13 Comments
 
LVL 23

Expert Comment

by:brettmjohnson
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.
0
 
LVL 11

Expert Comment

by:avizit
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

0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 12071541
0
 
LVL 4

Accepted Solution

by:
pankajtiwary earned 30 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");
        sleep(10);
    }
    else if (pid < 0) {                          // error
        printf("error forking\n");
        exit(1);
    }
    else if ( pid > 0 ) {                         // parent
        printf("I am in parent\n");
        wait(pid);
    }
    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.

HTH
0
 

Expert Comment

by:pankaj_thapa
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*/
         
                execl(test_path,test_name,machine_type,NULL);
                 // this process must exit after failure to exec
                exit(0);
        }

Enjoy
0
 

Author Comment

by:hapciu
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 ...

thanks

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 ?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Expert Comment

by:pankaj_thapa
ID: 12099434

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

 Pankaj.
0
 

Author Comment

by:hapciu
ID: 12104101
what is a child handler ? what do you mean by that ?
0
 
LVL 1

Expert Comment

by:davidcausse
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 :
fork()
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!

David.

0
 
LVL 4

Expert Comment

by:pankajtiwary
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.
0
 
LVL 4

Expert Comment

by:pankajtiwary
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.
0
 
LVL 1

Expert Comment

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

signal(SIGCHLD,cleanupkid);

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
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now