How to create processes in c ++?

1

I have a little domain in C up to the pointers part (I'm a beginner), I have a college professor who passed a question about processes, I tried, but I do not know how to solve:

Question:

  

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

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

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;
}
    
asked by anonymous 03.10.2017 / 04:44

1 answer

0

First remember that the fork() command is a UNIX command, that is, you should run it on a * nix system. Using Cygwin seems to be able to run on windows.

I assume here that you are using a * nix system. Remember that the <unistd.h> library should be included in your code.

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

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;
}
    
05.03.2018 / 13:41