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 systemfork ()
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
– Viny_Hidan
Not even the most modern versions of the standard
C++
are able to manipulate processes.fork()
is part of the patternPOSIX
.– Lacobus
has idea of how to solve it in C?
– Viny_Hidan
You’ll have to use
fork()
,wait()
orwaitpid()
along with some method CPI to inform the child process the variablea
manipulated by the parent process. After using thefork()
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.– gfleck
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.– gfleck