0

I have the following program, when I run the program, I feel really confused that why my program didn't excute

   int num=i;
       printf("it is No.%d !",num);
       printf("hello , I will excute execvp!");

My program basically create 6 child processes to excute executionbode() function, and then use execvp to overload original program. However, everytime when I run the program, the string "hello, I will execute execvp" never shows up! Also I think those three sentences above also didn't execute in the running program? can someone tell me why? Here is my program

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include "makeargv.h"
#include "redirection.h"
#include <sys/wait.h>



int executionnode(int i);

int main(){
pid_t childpid;
     int i;
     int row=6;
     for(i=0;i<row;i++)
     {   childpid=fork();
         if(childpid==0)
            continue;
         else if (childpid>0)
            executionnode(i);

         else {
           perror("something wrong");
            exit(1);
          }
      }


}


int executionnode(int i){
   sleep(i);
   printf("hello, I am process:%ld\n",(long)getpid());
   wait(NULL);
   char *execArgs[] = { "echo", "Hello, World!", NULL };
   int num=i;
   printf("it is No.%d !",num);
   printf("hello , I will excute execvp!");
   execvp("echo", execArgs);

}

Can someone tell me why? and how to fix it? I feel it is really strange? Is it because of execvp() functions? I just began to learn operating system,so I am really confused about it! Thank you for helping me!

python3
  • 251
  • 4
  • 12
  • this line: `wait(NULL);` will hang forever, because there is not child to this child to wait on. – user3629249 Sep 27 '15 at 16:56
  • 2
    you seem to have some confusion about the returned values from the function: `fork()`. The posted code has the child performing the fork and the parent performing the `execvp()` That is backwards from what you seem to want to do. – user3629249 Sep 27 '15 at 16:58
  • with the posted code, there will be many many children of children of children. etc. In general, only the parent should loop, calling fork() while the children perform the execvp() – user3629249 Sep 27 '15 at 17:02
  • 2
    possible duplicate of [Why the program didn't execute some sentences in this C programming or unix programming(execvp() System calls)?](http://stackoverflow.com/questions/32809899/why-the-program-didnt-execute-some-sentences-in-this-c-programming-or-unix-prog) – Leiaz Sep 27 '15 at 22:21
  • 1
    Exact same-author duplicate already answered there, with a fundamental issue already answered many times previously on this site in general. – Chris Stratton Sep 27 '15 at 22:29

1 Answers1

0

As user3629249 said you have some confusion. You'll get many children of children of children... and that wait(NULL) is useless :).

I used this structure to got your goal in my OS subject excercises.

#include <sys/types.h>
#include <unistd.h> 
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#define N 5

int main(int argc, char const *argv[])
{
  pid_t pid,pids[N];
  int i, num_pids = 0;
  int state = 0;
  int prior[]={1,3,5,2,4};

  pid_t parent_pid = getpid();
  printf("Parent pid is %i\n",father_pid);

  // This for loop is the key
  for (i = 0; i < N && getppid() != parent_pid; i++)
  {
    if ((pid = fork()) < 0)
    {
      printf ("fork error\n");
      exit(-1);
    }
    pids[num_pids++] = pid;
  }

  if (pid == 0) // Child processes
  {  
    printf("I am the child %i\n",getpid());
  }
  else // Parent process
  {
    for (i = 0; i < N; i++)
    { 
      int pid_index = prior[i]-1; // Array starts with 0
      pid = waitpid(pids[pid_index]);
      printf("Children %i ended\n",pids[indice_pid]);
      printf("%i alive children\n",N-1-i);
    }
  }

   return 0;
 } 

This structure works because you save the parent's pid in parent_pid variable and compare the parent of each process pid with getppid(). If this pid is different that parent_pid, this proccess is the parent. In another case the process is a child so it has to stop (these processes don't have to fork). With this way you can get only the forks you need.

The rest of the code is the same: Pid==0 is child process and any other is the parent. You can call executionnode(int i) in child processes block (remember, pid==0 !!! you have a mistake). i variable should have the right value in each call I think.

Good luck!

29antonioac
  • 315
  • 2
  • 12