Link to home
Start Free TrialLog in
Avatar of hapciu
hapciu

asked on

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

thanks
Avatar of brettmjohnson
brettmjohnson
Flag of United States of America image

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.
Avatar of avizit
avizit

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

ASKER CERTIFIED SOLUTION
Avatar of pankajtiwary
pankajtiwary

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of hapciu

ASKER

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 ?

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

 Pankaj.
Avatar of hapciu

ASKER

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

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

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