Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6.  
  7. int main(int argc, char **argv)
  8. {
  9. //If only one input then user has not provided a file name to calculate.
  10. if(argc == 1)
  11. {
  12. printf("No file provided. Please provide name of file needed to add.\n");
  13. }
  14. else{
  15. FILE *inputFile; //file to be calculated
  16. double inputNumber; //each number in the file
  17. double answer = 0; //final answer
  18. int count = 0; //counter variable to make sure there are numbers in the file
  19. int forkCounter = 1;
  20. int addUpPipe[2]; //float array for pipe
  21. pid_t childPid; //id of child
  22.  
  23. pipe(addUpPipe); //create pipe
  24.  
  25. /**
  26. for(int i = 1; i < argc - 1; i++){
  27. childPid = fork();
  28. if(childPid > 0){
  29. fopen("%s", argv[i]); //Opens provided file name and stores in variable
  30. }
  31. }
  32. **/
  33.  
  34. //Fork process for however many arguments are provided
  35. while((childPid = fork()) > 0 && forkCounter < argc - 1){
  36. fopen("%s", argv[forkCounter]);
  37. ++forkCounter;
  38. printf("Forked %d\n",forkCounter);
  39. }
  40.  
  41. //Parent code
  42. //If parent, wait for children to finish then open pipe, add answers, then send to command line
  43. if(childPid > 0){
  44. int returnStatus;
  45. double value;
  46. double finalAnswer = 0;
  47.  
  48. printf("%s\n", "Entered Parent");
  49.  
  50. while(wait(&returnStatus) > 0); //Wait until children are finished
  51. printf("%s", "Finished waiting for child");
  52. //Reads the added value from pipe
  53. FILE* readPipe = fdopen(addUpPipe[0], "r");
  54. while(fscanf(readPipe, "%lf", &value) != EOF){
  55. finalAnswer += value;
  56. }
  57. printf("%lf", value); //Print completed value to command line
  58. close(addUpPipe[0]); //Close pipe
  59. }
  60. //Child Code
  61. else{
  62. //If file is null it cant be opened or doesnt exist
  63. printf("%s\n", "Entered Child");
  64. if(inputFile == NULL)
  65. {
  66. printf("Cant open file or file does not exist\n");
  67. return 0;
  68. }
  69.  
  70. //Goes through file and adds each number together while increasing the counter variable
  71. while(fscanf(inputFile, "%lf", &inputNumber) != EOF)
  72. {
  73. answer += inputNumber;
  74. count++;
  75. }
  76. //If reached the end of file immediately without count increasing, must be empty
  77. if(count == 0)
  78. {
  79. printf("Error! No numbers in file!\n");
  80. }
  81. //Otherwise, print the answer
  82. else
  83. {
  84. //printf("%d", answer);
  85. write(addUpPipe[1], &answer, sizeof(answer));
  86. write(addUpPipe[1], "\n", 2);
  87. close(addUpPipe[1]);
  88. printf("%s", "Child done");
  89. }
  90. fclose(inputFile); //Close file
  91. }
  92. return 0;
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement