Advertisement
Guest User

12

a guest
Dec 13th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #include<iostream>
  2. #include<unistd.h>
  3. #include<stdlib.h>
  4. #include<sys/wait.h>
  5.  
  6. using namespace std;
  7.  
  8. int main(){
  9. pid_t pid1,pid2;
  10. int pipe1[2],pipe2[2];
  11. pipe(pipe1);
  12. pipe(pipe2);
  13. int i,n;
  14. double voto,v[i];
  15. pid1=fork();
  16. if(pid1>0){
  17. close(pipe1[0]);
  18. cout<<"Inserire voto: ";
  19. do{
  20. cin>>voto;
  21. if(voto>=0 && voto<=10){
  22. write(pipe1[1],&voto,sizeof(voto));
  23. }
  24. //cout<<"Il voto inserito e': "<<voto<<endl;
  25. }while(voto!=0);
  26. sleep(1); //Questo sleep serve ad aspettare il figlio, perchè se finisce il suo compito fa il return e termina il programma
  27.  
  28. exit(0);
  29.  
  30. }
  31. if(pid1==0){
  32. double voti[100],somma=0;
  33. double m=0.0;
  34. i=0;
  35. double n=0.0;
  36. close(pipe1[1]);
  37.  
  38.  
  39. //cout<<"I voti inseriti sono"<<endl;
  40. for(i=0;voti[i]!=0;i++){
  41. read(pipe1[0],&voti[i],sizeof(voti));
  42. //cout<<voti[i]<<endl;
  43. if(voti[i]==0){
  44. break;
  45. }
  46. somma=somma+voti[i];
  47. n++;
  48.  
  49. }
  50. for(i=0;voti[i]!=0;i++){
  51. cout<<"Il voto inserito e': "<<voti[i]<<endl;
  52. }
  53. cout<<"La somma e': "<<somma<<endl;
  54. cout<<"n e': "<<n-1.0<<endl;
  55. m=somma/n;
  56. pid2=fork();
  57. if(pid2>0){
  58. close(pipe2[0]);
  59. write(pipe2[1],&m,sizeof(m));
  60. }
  61. if(pid2==0){
  62. double media=0;
  63. close(pipe2[1]);
  64. read(pipe2[0],&media,sizeof(media));
  65. cout<<"La media e': "<<media;
  66. exit(0);
  67. }
  68. exit(0);
  69. }
  70. exit(0);
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement