1

I've been attempting to create two children to one parent process using fork(), then running two 'different' commands for those two children. I'm attempting to ping two different websites with these child processes. The thing is when the first ping 'command' is executed it doesn't end, I tried solving this by passing -c amount to limit the amount of outputs, but for some reason it's not doing the job. Here's the code:

    pid = fork();
        if(pid!=0) {
          wait(&status);
          printf("----------------------------------------------------\n\n");
          printf ( " I am the parent my PID is %d, myPPID is %d, \n ",getpid(),getppid());
          printf("---------------------------------------------------\n\n");
       }else {
         printf ( " I am the child , my PID is %d , my PPID is %d \n",getpid(),getppid());
         printf("---------------------------------------------------\n\n");
         execl ( "/bin/ping","-c5", "sheffield.ac.uk",(char*)0);
         return 0;
       if(pid!=0){
         printf ( " I am the child , my PID is %d , my PPID is %d \n",getpid(),getppid());
         printf("---------------------------------------------------\n\n");

       }else {
         printf ( " I am the child , my PID is %d , my PPID is %d \n",getpid(),getppid());
         printf("---------------------------------------------------\n\n");
         sleep(2);
         execl ( "/bin/ping","-c5", "shu.ac.uk",(char*)0);
         return 0;
       }

  }
        break;
aaromated
  • 23
  • 7
  • 1
    You might test this with calling a program (that you can write) that prints the command line parameters to see what is passed to the program via `execl`. Are you aware that `argv[0]` normaly holds the name of the command? – Gerhardh Mar 21 '19 at 14:36
  • I was actually unaware of this, but thank you this has answered my question! – aaromated Mar 21 '19 at 14:41
  • `execl()` never returns, as such, unless there is an error. It replaces the running process with a new one. If you want two children, do two `fork()`s. Your code appears to be missing a `}` after the `return 0;`. You can place the second fork there. Alternatively, `system()` does return, in which case you don't need to fork unless you want them to run asynchronously. The second pid test (`if(pid!=0){` seems wrong - why is that the child? – jhnc Mar 21 '19 at 14:42
  • Creating two forks creates 4 processes doesn't it? I'm looking for two children with one parent – aaromated Mar 21 '19 at 14:51
  • fork twice in the parent => parent + 2 children. You'd need to move the `wait` to the end to make them both run together – jhnc Mar 21 '19 at 15:00
  • Note that calling `return 0;` after a failed call to `execl()` is probably not what you want to do. That will cause the child process that was supposed to be replaced by the `execl()` call to do what the parent process would do when it returns from that function. You should probably call `_exit()`. Note the leading underscore - the `_exit()` function doesn't call `atexit()` handlers nor flush output buffers inherited from the parent. – Andrew Henle Mar 21 '19 at 15:22

1 Answers1

2

From execl documentation :

The first argument, by convention, should point to the filename associated with the file being executed.

so you need to replace

execl ( "/bin/ping","-c5", "sheffield.ac.uk",(char*)0);

by for instance

execl ( "/bin/ping", "ping", "-c5", "sheffield.ac.uk",(char*)0);

Because you don't give the filename as the first argument it is replaced by "-c5" and you do ping without that option

Note also from the documentation :

Return Value

The exec() functions only return if an error has occurred. The return value is -1, and errno is set to indicate the error.


Note you can use popen to execute your ping and also to get the output messages it produces

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
bruno
  • 32,421
  • 7
  • 25
  • 37