How to create c++ processes?

Asked

Viewed 1,170 times

1

I have a bit of mastery in C until the pointers part(I’m beginner), I have a college teacher who passed a question about processes,tried , but I don’t know how to solve:

Question:

Knowing that the processes generated by the system call fork( ) sane encapsulated and protected, create a C program using the call system fork () to bear a child, the father being a 0 a 10 assigned to an "A" variable, and the child a variable settings "A" of the father plus "B = 10" of the son, and print the final result of sum of A + B through child. Use operating system API for memory sharing for communication between these lawsuits.

I tried to do it in c++(I have more affinity), but it did not turn:

int main(){
    pid_t filho;
    int i,status;
    pid_t pid;
    filho=fork();

    if(pid==0)
    {
        cout<<"Sou o processo filho"<<"\n"; 

        for(i=0; i<10; i++){
            int a=i;
            cout<<i;                                                                                                    
        }                                                                                                                           

        exit(0);
    }
    else
    {                                                                         
        cout <<"Eu sou o pai , agora posso executar o meu código" << endl;
    }

    return 0;
}
  • I edited and put what I asked in the question

  • Not even the most modern versions of the standard C++ are able to manipulate processes. fork() is part of the pattern POSIX.

  • has idea of how to solve it in C?

  • You’ll have to use fork(), wait() or waitpid() along with some method CPI to inform the child process the variable a manipulated by the parent process. After using the fork() a child process is created totally separate from the parent process, the child’s variables do not "see" the father’s variables and vice versa, although they have the same names they occupy different spaces of memory.

  • Ah, one more thing, this has nothing to do with C++ but with the OS. The fork() is a call provided by the kernel, it is not a call from the standard C++ library (libstdc++), it is important to understand these concepts.

1 answer

1

First remember that the command fork() is a UNIX command, i.e., you must run it on a *Nix system. Using the Cygwin it seems possible to run it in windows.

I assume here that you are using a *Nix system. Remember that the library <unistd.h> shall be included in its code.

In the if (pid==0) you should have put if (filho==0) because the previously executed command was filho=fork(); then the result was in the variable filho and not in pid.

Here is a modified version of your code:

#include <unistd.h> // Esta biblioteca que define o fork()
#include <iostream>

using namespace std;

int main(){
  pid_t filho;
  int i;         // A variável status não era utilizada
  //pid_t pid;      Nem esta
  filho=fork();  // Aqui ocorrerá a divisão dos processos

  if(filho==0) // Aqui estavo o erro, você não guardava o resultado
               // do fork no pid, mas sim no filho
  {
    cout<<"Sou o processo filho"<<"\n"; 

    for(i=0; i<1000; i++){ // Aumentei o loop para ficar fácil ver
                           // os dois processos ao mesmo tempo
      cout<<i << ' ';                                                                                                    
    }                                                                                                                           
    cout << endl;

    exit(0);
  }
  else
  {                                                                         
    cout <<"Eu sou o pai , agora posso executar o meu código" << endl;

    for (int k=1000; k<2000; ++k) // Coloquei um loop aqui
    {
      cout << k << ' ';
    }
    cout << endl;
  }

  return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.