-1

I'm writing a program that contains two processes. One process gets two numbers from input, and add them together. The second process shows the added result.

The problem is it can't get number from input.

#include <sys/types.h>
#include <unistd.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int a,b,c=1;
bool res = false;
void Add(){
if(res==true){
c=a+b;
cout<<"Process 1 was done !\n";
cout<<c<<endl;}
}
void Print(){
if(c!=1){
cout<<c<<endl;
cout<<"Process 2 was done !\n";}
}


int main(){
cout<<"Enter a,b :\n";
cin>>a>>b;
res= true;
pid_t pid;
pid  = fork();
if(pid==0)
Print();
else
Add();

return 0;}

Running on Ubuntu.

p.campbell
  • 98,673
  • 67
  • 256
  • 322
1Programmer
  • 73
  • 3
  • 9
  • Whats the problem, it seems to work for me? If I enter "2 3" I get "Process 1 was done ! 5" Please explain what it is that you expect the answer to be. – Tom Apr 13 '11 at 11:26
  • the problem is when i enter 2 and press enter , a message comes that say : command not found ! – 1Programmer Apr 13 '11 at 11:30
  • other problem is why we can't call Add Method before Print,actually we can,but we get wrong result . i want to get this result that : – 1Programmer Apr 13 '11 at 11:41
  • You probably need to prepend ./ infront of the command. For example if you compiled g++ my_code.cpp -o my_prog; then you run the command ./my_prog – Tom Apr 13 '11 at 11:42
  • Actually i want to do my main code ! please look at it . i edited my post ! – 1Programmer Apr 13 '11 at 11:47
  • You need to work harder with your question. Your edit just repeated the code block you had already posted - a code block that compiles and runs without error. If you are having problems, please show exactly how to reproduce them (stating the error, the output that demonstrates the error). If the program runs but does not give you the output you expect, please write what output you get and what output you expect. I cannot help you with your question because your question in the comments "but we get wrong resuult, I want to get the result that:" does not make sense to me. – Tom Apr 14 '11 at 08:46

1 Answers1

1

If you compile your program

g++ processes.cpp -o process

then you run the program

./process

Your program also has the problem that when you fork the code, the global variables are copied.
In you child process the value of c is never updated and so

int c=1;
void Print(){
if(c!=1){
  //this code will never be called 
}

Finally your instruction

cout<<"Enter a,b :\n";

is misleading, since you do not process the comer,

entering

2,3 

will give you the wrong result to add, You should instead enter

2 3
Tom
  • 5,219
  • 2
  • 29
  • 45
  • thanks,so what should i do to solve theese problems,i am very beginner in processes ! – 1Programmer Apr 13 '11 at 11:58
  • sorry i have question,you said that : "the global variables are copied. In you child process the value of c is never updated and so" . in processes zero return is not child ?! so child will be Add method and Parent will be Print, Won't be ?! – 1Programmer Apr 13 '11 at 12:04
  • i said i am beginner in process,it is clear from my code . so i will be Thankful from you if you explain process structures in a simple way . thanks ...! – 1Programmer Apr 13 '11 at 12:06
  • isn't there anyone help me ?! – 1Programmer Apr 13 '11 at 18:05
  • @1Programmer: you need to open a terminal/console window or otherwise "login" to the Linux host and get a shell prompt. If you need help doing that, explain what kind of OS you're using and how you're accessing the Linux box and editing the files. If you can get a shell prompt, then just type in the `g++...` and `./process` commands listed in this answer above, which will compile then run your program. More generally, if you want more help, you might try upvoting an answer that seems helpful to show some appreciation before asking for more time and effort. – Tony Delroy Apr 14 '11 at 05:14
  • @1Programmer: check out http://www.cs.ucr.edu/~mfast/linux_tutorial.html for a basic introduction to C++, Linux and the shell prompt. – Tony Delroy Apr 14 '11 at 05:20
  • thanks my friends,but in first case i want to solve my program errors,can anybody help me to do this ? – 1Programmer Apr 14 '11 at 06:11
  • here main problem is how to exchange data between processes ? – 1Programmer Apr 14 '11 at 11:24
  • @1Programmer http://stackoverflow.com/questions/1963642/shared-memory-for-fork If you want more specific help please edit your question to say EXACTLY what the problem is - EXACTLY what input you have tried, and EXACTLY what the output is, and why that is not what you want. You question is far too vague. – Tom Apr 14 '11 at 11:42