Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<unistd.h>
  4. #include<fcntl.h>
  5.  
  6. typedef struct{
  7.  
  8. int index;
  9. int num;
  10.  
  11. }proc;
  12.  
  13. /*  
  14.     The process parent receives N files from the user input. For each file it creates
  15.     1 processes child. Each process child reads its file and count the number of
  16.     characters. The first process sends the number to the second process, which reads
  17.     the number, compares it the its own counter and send forward to the third process
  18.     the greater between the two. The last process send the number to the process parent
  19.     who read the pipe and prints the number in the standard output.
  20. */
  21.  
  22. void main(int argc, char **argv){
  23.  
  24.     proc myProc;
  25.  
  26.     int N=argc-1; // represents the number of argument (files)
  27.     int i,j; // indexes
  28.     proc msg; // the message received by parent
  29.     int fd; // file descriptor
  30.     int pid; // the process id returned by fork()
  31.     char ch; // the buffer for file read
  32.     int counter=0; // used to count the number of characters
  33.  
  34.     // declare and populate the N pipes (one for each child)
  35.     int mypipe[N][2];
  36.     for (j=0;j<N;j++)
  37.         pipe(mypipe[j]); // populate with values
  38.  
  39.     // Create the N children
  40.     for(i = 0; i < N; i++){
  41.  
  42.         pid=fork();
  43.  
  44.         if (pid < 0){
  45.             printf("ERROR");
  46.         }
  47.         else if(pid == 0){
  48.  
  49.             // loop on pipes, close the ones not in use
  50.             for (j=0;j<N;j++){
  51.                 if (i!=j) close(mypipe[j][1]);
  52.                 if(j!=i-1)close(mypipe[j][0]);
  53.             }
  54.  
  55.         // Open file
  56.         fd = open(argv[i+1], O_RDONLY);
  57.         // Check if it was succefully opened
  58.         if (fd < 0){
  59.             printf("Error opeing the file %s \n", argv[i+1]);
  60.             exit(1);
  61.         }
  62.  
  63.         // Loop on file and read char by char
  64.         while(read(fd, &ch, 1)>0){
  65.             counter++;
  66.        
  67.         }
  68.  
  69.     //update struct info
  70.         myProc.num = counter;
  71.         myProc.index = i;
  72.  
  73.         // Close file after use
  74.         close(fd);
  75.  
  76.         // If the porcess is not the first one
  77.         if (i>0){
  78.             // reading from previous       
  79.             read(mypipe[i-1][0], &msg, sizeof(proc));
  80.  
  81.            
  82.             if (msg.num > myProc.num) {myProc.num=msg.num;myProc.index=msg.index;}
  83.         }
  84.  
  85.         // Write on pipe the number of characters
  86.         write(mypipe[i][1],&myProc,sizeof(proc));
  87.  
  88.         // Exit
  89.         exit(0);
  90.         }
  91.     }
  92.     // loop on pipes and close the ones not in use
  93.     for (j=0;j<N;j++){
  94.         close(mypipe[j][1]);
  95.         if (j!=N-1) close(mypipe[j][0]);
  96.     }
  97.    
  98.     read(mypipe[N-1][0],&msg,sizeof(proc));
  99.     printf("MSG = %d with count %d \n", msg.index+1,msg.num);
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement