C ++ fork () and execv () problems

I'm kind of new to C ++ and working on a simple program in Linux that should call another program in the same directory and get the output of the called program without displaying the output of the called program on the console. This is the piece of code I am working on:

    pid_t pid;
    cout<<"General sentance:"<<endl<<sentence<<endl;
    cout<<"==============================="<<endl;
    //int i=system("./Satzoo");
    if(pid=fork()<0)
        cout<<"Process could not be created..."<<endl;
    else
    {
        cout<<pid<<endl;
        execv("./Satzoo",NULL);
    }
    cout<<"General sentance:"<<endl<<sentence<<endl;
    cout<<"==============================="<<endl;

      

One of the problems I am facing is that I can print the first two lines to the console, but I cannot print the last two lines. I think the program stops working when I call the Satzoo program. Another thing is that this code runs the Satzoo program twice, I don't know why? I see the result on the screen twice. On the other hand, if I use system () instead of execv (), then Satzoo only works once.

I have not figured out how to read the output of Satzoo in my program.

Any help is appreciated.

thank

+2


source to share


5 answers


You don't differentiate between child and parent processes after being called fork()

. This way, both the child and the parent start up execv()

, and hence their respective process images are replaced.

Do you want something more:



pid_t pid;
printf("before fork\n");

if((pid = fork()) < 0)
{
  printf("an error occurred while forking\n");
}
else if(pid == 0)
{
  /* this is the child */
  printf("the child pid is: %d\n", getpid());
  execv("./Satzoo",NULL);
  printf("if this line is printed then execv failed\n");
}
else
{
  /* this is the parent */
  printf("parent continues execution\n");
}

      

+11


source


The function fork()

clones the current process and returns different values ​​in each process. In the "parent" process, the pid of the child is returned. In the child process, it returns zero. Therefore, you usually call it with a model like this:

if (fork() > 0) {
    cout << "in parent" << endl;
} else {
    cout << "in child" << endl;
    exit(0);
}

      

In the above error, I have not followed the error handling.



In your example, both of the above code paths (both parent and child) end up in the clause of else

your call to fork()

, resulting in both of them being execv("./Satzoo")

. This is why your program runs twice, and why you never reach statements beyond that.

Instead of using fork()

and doing everything manually (managing the process correctly is enough work), you might be interested in using the function popen()

:

FILE *in = popen("./Satzoo", "r");
// use "in" like a normal stdio FILE to read the output of Satzoo
pclose(in);

      

+3


source


From the fork()

manpage:

RETURN VALUE
On successful completion, fork () returns 0 to the child and returns the process ID of the child to the parent. Both processes continue to run from the fork () function. Otherwise -1 will be returned to the parent process, no child processes will be created, and errno must be set to indicate an error.

You check to make sure it succeeds, but does not indicate pid

that we are in a child or parent. This way, both the child and parent do the same thing twice, which means your program is executed twice and the final text is never printed. You need to check the return value fork()

more than once.

+2


source


exec - The exec () family of functions replaces the current process image with a new process image.

system - Blocks command execution. The execution of the calling program continues after the system command returns

+1


source


There are three return value tests you want to fork

  • 0: you are a child
  • -1: error
  • other: you are the parent

You ran another program from both the child and parent ...

0


source







All Articles