Advertisement
Guest User

Untitled

a guest
May 5th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.68 KB | None | 0 0
  1. //need two element int array for pipe
  2. int pd[2];
  3.  
  4. //make pipe before child processes are made so every child writes to this one pipe
  5. if(pipe(pd) == -1)
  6.  44     {
  7.  45       error_exit("pipe");
  8.  46     }
  9.  
  10. //NOTE: each pid = fork() is a new child process, so if in a loop
  11. //you will have n child process that the parent should wait on before
  12. //reading from the pipe, null reads make the program hang up
  13. if((pid = fork()) == 0)
  14.  63       {
  15.  64         fp = fopen(cp, "r");
  16.  65
  17.  66         if(fp == NULL)
  18.  67         {
  19.  68           fprintf(stderr, "%s: unable to read %s\n", pgname, cp);
  20.  69           exit(1);
  21.  70         }
  22.  71
  23.  72         // print current file (argv[i]) just for sanity, remove after testing
  24.  73         printf("%s\n", argv[i]);
  25.  74         printf("pid = %d\n", pid);
  26.  75
  27.  76         count(fp);
  28.  77
  29.  78         //write to pipe
  30.  79         //order is line, word, byte, filename, child pid!
  31.  80
  32.         //note pd[1] is passed to write, this is always what you pass
  33.  81         if(write(pd[1], &words, MSGSIZE) == -1)
  34.  82         {
  35.  83           error_exit("write words failed");
  36.  84         }
  37.  85         if(write(pd[1], &lines, MSGSIZE) == -1)
  38.  86         {
  39.  87           error_exit("write lines  failed");
  40.  88         }
  41.  89         if(write(pd[1], &bytes, MSGSIZE) == -1)
  42.  90         {
  43.  91           error_exit("write bytes failed");
  44.  92         }
  45.  93         return; //return from child statement
  46.  94       }
  47.  
  48. //wait for all child process to end before continuing
  49.  98     while((wpid = wait(NULL)) > 0);
  50.  
  51. //parent can read pipe now
  52. 101
  53. 102     int temp = 0;
  54. 103     char fname[MSGSIZE];
  55.         //numberOfWritesToPipe needs to match number of times a child process wrote to the pipe
  56.     //for example, wrote lines, writes, bytes, and file name from child process, so read pipe 4 times
  57.     int numberOfWritesToPipe = 4;
  58. 104     for(i = 0; i < numberOfWritesToPipe; i++)
  59. 105     {
  60. 106       if(i < 3)
  61. 107       {
  62.         //notice pd[0] passed to write, this is always the case
  63. 108         if((read(pd[0], &temp, sizeof(temp))) == -1)
  64. 109         {
  65. 110           error_exit("read failed (digit)");
  66. 111         }
  67. 112         switch(i)
  68. 113         {
  69. 114           case 0:
  70. 115             bytes += temp;
  71. 116             break;
  72. 117           case 1:
  73. 118             words += temp;
  74. 119             break;
  75. 120           case 2:
  76. 121             lines += temp;
  77. 122             break;
  78. 123         }
  79. 124       }
  80. 125       else
  81. 126       {
  82. 127         if((read(pd[0], buf, MSGSIZE)) == -1)
  83. 128         {
  84. 129           error_exit("read failed (string)");
  85. 130         }
  86. 131         printf("file name?? %s\n", buf);//figure out why this is failing
  87.  132       }
  88. 133     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement