Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<unistd.h>
- #include<stdlib.h>
- #include<sys/wait.h>
- using namespace std;
- int main(){
- pid_t pid1,pid2;
- int pipe1[2],pipe2[2];
- pipe(pipe1);
- pipe(pipe2);
- int i,n;
- double voto,v[i];
- pid1=fork();
- if(pid1>0){
- close(pipe1[0]);
- cout<<"Inserire voto: ";
- do{
- cin>>voto;
- if(voto>=0 && voto<=10){
- write(pipe1[1],&voto,sizeof(voto));
- }
- //cout<<"Il voto inserito e': "<<voto<<endl;
- }while(voto!=0);
- sleep(1); //Questo sleep serve ad aspettare il figlio, perchè se finisce il suo compito fa il return e termina il programma
- exit(0);
- }
- if(pid1==0){
- double voti[100],somma=0;
- double m=0.0;
- i=0;
- double n=0.0;
- close(pipe1[1]);
- //cout<<"I voti inseriti sono"<<endl;
- for(i=0;voti[i]!=0;i++){
- read(pipe1[0],&voti[i],sizeof(voti));
- //cout<<voti[i]<<endl;
- if(voti[i]==0){
- break;
- }
- somma=somma+voti[i];
- n++;
- }
- for(i=0;voti[i]!=0;i++){
- cout<<"Il voto inserito e': "<<voti[i]<<endl;
- }
- cout<<"La somma e': "<<somma<<endl;
- cout<<"n e': "<<n-1.0<<endl;
- m=somma/n;
- pid2=fork();
- if(pid2>0){
- close(pipe2[0]);
- write(pipe2[1],&m,sizeof(m));
- }
- if(pid2==0){
- double media=0;
- close(pipe2[1]);
- read(pipe2[0],&media,sizeof(media));
- cout<<"La media e': "<<media;
- exit(0);
- }
- exit(0);
- }
- exit(0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement