views:

366

answers:

4

I'm trying to create a child process in another process. I am writing both the programs in C language. First I write a dummy process which will be the child process. What it is doing is only to write a string on the screen. It works well on its own. Then I write another program which will be the parent process. However, I can't make it happen. I'm trying to use fork and execl functions together, but I fail. I also want the child process does not terminate until the parent process terminates.

How should I write the parent process?

Thanks.

Here is the code for the child process:

#include <stdio.h>

int main(void) {
  while(1) {
    printf("*");
    sleep(1);
  }
}

And here is the parent process:

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

int main(void) {
  if (fork()) {
    while(1) {
      printf("-\n");
      sleep(5);
    }
  } else {
    execl("./", "dummy", (char *)0);
  }
}
+1  A: 

Basic use of fork in C

int PID = fork();

if( PID < 0 ) {
    //fail
    return PID;
}
else if( !PID ) {
    //child process
    return exec( prog, args );
} 
else {
    //parent process
    return 0;
}
Mimisbrunnr
Yes, the parent process WILL exit, even if some of its children / descendants are still alive. If the parent wants to wait for them, it has to do so deliberately.
MarkR
A: 

There is no way to force the child process to "not terminate" when it's done (you'll still be able in the parent to wait for it to get info on how it terminated, but that's about it). Apart from that, any of the many examples of fork/exec on the web, such as this one, should work -- why don't you try it and see if it performs as you wish (in which case you'll just need to change whatever you were doing differently in your own attempt). If it doesn't work as desired (except for the impossibility per the first sentence in this A;-), please edit your to add copious detail about how the code behaves differently than you expect it to.

Alex Martelli
You can put a loop in the child process after it has finished its work but before it exits, that waits for the parent to die (eg. by waiting for `EOF` on a pipe connected to the parent).
caf
If you control the child process's source code, never have any error in it, _and_ can somehow guarantee it won't receive unwelcome signals, yes, but those can be pretty hard things to guarantee.
Alex Martelli
+6  A: 

The fork() system call may return three different statuses: failure (<0), parent process (>0) or child process (==0). You must test the return value properly.

int pid = fork();

if (pid < 0) {
  /* handle error */
  perror("fork");
  exit(1);
} else if (pid > 0) {
  /* parent code */
} else {
  /* child code */
}

Your execl() system call is wrong. The first argument is the path to the program you want to execute, "./" is not valid, it should be something like "./dummy" at least. The next argument is by convention the command name (argv[0] in the executed program), which may be a repetition of the first argument. So:

execl("./dummy", "dummy", NULL);

Also, note that the printf("*") statement in the child program will probably buffer and you won't see anything on the terminal. You must either add a "\n" to the end or call fflush(stdout) to flush the standard output.

Juliano
+1 Yup, almost certainly permission error on the execl.
Duck
Thanks for your reply, it helped me a lot.
Hakan Svensson
A: 

I have a question My first job calls the sql. immeditely calls the second job which generates the report and send a mail. In this case, second job takes more time, i would like to stop the execution of the first job till my second jobs complete its execution. Any quick answers would be appreciated Thanks in advance

A.Gopal

gopal