2

I want my child process to execute a bin program when my parent waits for 3 seconds and kills the child. Silly I know, but still gotta do it. Cant seem to use the execv properly here.I've tried to run calendar, gedit etc. just hasn't worked for me. Any suggestions ?

   int main(int argc, char* argv[])
   {
    pid_t pid;

    pid = fork();

    if (pid == 0) { 
      execv("calc",argv);
      return 0;
    }
    else if (pid > 0) { /* parent process */
      sleep(3);
      kill(pid, SIGKILL);
      printf("Child process with the ID: %d has been killed by the parent process with the ID: %d...\n", pid, getpid());
      return 0;
    }
}
  • 1
    Possible duplicate of [fork() kill() and execv()](http://stackoverflow.com/questions/13290594/fork-kill-and-execv) – user3386109 Feb 18 '16 at 18:59

1 Answers1

1

From the man execv:

The execv(), execvp(), and execvpe() functions provide an array of pointers to null-terminated strings that represent the argument list available to the new program. The first argument, by convention, should point to the filename associated with the file being executed.

So - it does not search through the PATH. You can examine the return value of the execv to see it failed.

Use "/usr/bin/gedit" as the first argument and it should work. Also note that on older computers three seconds may be not enough to see program running.

nsilent22
  • 2,763
  • 10
  • 14