1

I've c code that uses fork and execvp functions, The intent of the code is copy a src_file to target_path once the copying is done, Notify the user, of its status(print msg)

I know that the usage of execvp will replace the current process image. what i wanted to know is, are there any alternatives, such that upon completion of the cmd given to execvp the control returns to my program and run the successive code.

pid = fork();
if (pid == 0) {
  /* child path */
  printf("Hello, This is CHILD(%d).\n", getpid());
  execvp("cp", av);
  printf("Completed Copying\n");
}
else {
  /* Parent's path */
  printf("Hello, This is PARENT(%d).\n", getpid());
  wait(NULL);
}
Sheetal T
  • 90
  • 1
  • 7
  • [Pthreads.](http://en.wikipedia.org/wiki/POSIX_Threads) –  Oct 23 '12 at 06:01
  • 3
    int system(const char *command); – ZhangChn Oct 23 '12 at 06:01
  • You have the wait() call there, which will do what you mentioned. How is it not sufficient for what you need ? – nos Oct 23 '12 at 06:42
  • you could always copy it manually via opening 2 streams – DanZimm Oct 23 '12 at 07:03
  • Pedantically, `execvp` replaces the current program, not the current process. That's how things like the environment block, the file descriptor table (think shell redirection), UID, umask, etc. get inherited. – cdarke Oct 23 '12 at 07:41

1 Answers1

2

execvp will replace the image of current process i.e. the process from which it is called (which is your child created with fork). So why don't you just wait in parent (as you already did) for child to complete and print message?

Following link shows how to test status and return values. file copy

Alternately, Why don't you use fopen() or open() for both source and destination files and copy in a loop using code i.e. using read and write api. This is fastest and you no longer need to call the external binary cp for achieving your goal.

Community
  • 1
  • 1
fkl
  • 5,412
  • 4
  • 28
  • 68