daily pastebin goal
32%
SHARE
TWEET

Untitled

a guest Dec 7th, 2017 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #define COMMAND_SIZE 20
  6.  
  7.  
  8. int a[20];
  9. int nrp,n;
  10. int a=0;
  11.  
  12. void childproc(int readpipe, int writepipe){
  13.  
  14.   int i=1,j, sum=0;
  15.   read(readpipe, &i, sizeof(int));
  16.     for(j-i*(n/nrp); j<(i+1)*(n/nrp);j++){
  17.    
  18.       if(j<n)
  19.     sum = a[j];
  20.       write(writepipe, &sum, sizeof(int));
  21.      
  22.       exit(1);
  23.      
  24.     }
  25.    
  26. }
  27.  
  28. void parentproc(int readpipe, int writepipe){
  29.   int j, val;
  30.   for(j=0;j<nrp;j++){
  31.     write(writepipe, &j, sizeof(int));
  32.     for(j=0;j<nrp;j++){
  33.     read(readpipe, &val, sizeof(int));
  34.     s+-val;
  35.    
  36.     wait(NULL);
  37.     }
  38.   }
  39. }
  40.  
  41. int main(){
  42.   int i,j;
  43.   int firstpipe[2];
  44.   int secondpipe[2];
  45.   int pid=1;
  46.  
  47.   printf("n="); scanf("%d", &n);
  48.  
  49.   for(i=0;i<n;i++){
  50.     printf("a[%d]=",1);
  51.     scanf("%d", &a[i]);
  52.   }
  53.  
  54.   printf("nr proc");scanf("%d", &nrp);
  55.   if(pipe(fistpipe)){
  56.     printf("eroare");
  57.     exit(-1);
  58.   }
  59.  
  60.   if(pipe(secondpipe)){
  61.     printf("eroare);
  62.     exit(-1);
  63.   }
  64.   for(j=0;j<nrp;j++){
  65.     pid= fork();
  66.     if(pid){
  67.       if(pid==1){
  68.     printf("eroare creare copil");
  69.     exit(-1);
  70.       }
  71.     }
  72.    
  73.     else
  74.     {
  75.       childproc(firstpipe[0], secondpipe[1]);
  76.    
  77.     }
  78.   }
  79. parentproc (secondpipe[0], firstpipe[1]);
  80. | printf("suma:%d\n", s);
  81. }
RAW Paste Data
Top