Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #include <cstdlib>
  6. #include <iostream>
  7. #include <sys/types.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <string>
  12. #include <fstream>
  13.  
  14. using namespace std;
  15.  
  16. int main(int argc, char* argv[])
  17. {
  18.  
  19.     fstream bufferFile;
  20.     ifstream readFile;
  21.     int sem1 = 1;
  22.     int sem2 = 0;
  23.     int aNum = 0;
  24.     int childcount = 0;
  25.     pid_t fork_return;
  26.  
  27.     bufferFile.open("buffer.txt.", ios::out);
  28.     cout << "Writing to buffer file.\n";
  29.     bufferFile << aNum;
  30.     bufferFile.close();
  31.  
  32.    fork_return  = fork( );
  33.     while(childcount < 5){
  34.                if (fork_return == 0) {  /*in the child process*/
  35.                                 if (sem1 == 1) {
  36.                                     sem2 = 0;      
  37.                                     readFile.open("buffer.txt");
  38.                                     //readFile.seekg(ios_base::beg);
  39.                                     readFile >> aNum;
  40.                                     readFile.close();
  41.                                     aNum++;
  42.                                     bufferFile.open("buffer.txt.", ios::out);
  43.                                     bufferFile << aNum;
  44.                                     bufferFile.close();
  45.                                     childcount++;
  46.                                     sem2 = 1;
  47.                                     cout << "A. The number [ " << aNum <<"] modified by child\n";
  48.                                  }
  49.                             sem2 = 1;
  50.                }
  51.  
  52.                else if(fork_return > 0 )  /*in the parent process*/
  53.                      {
  54.                             if(sem2 == 1) {
  55.                                 sem1 = 0;      
  56.                                     readFile.open("buffer.txt");
  57.                                     //readFile.seekg(ios_base::beg);
  58.                                     readFile >> aNum;
  59.                                     readFile.close();
  60.                                     aNum++;
  61.                                     bufferFile.open("buffer.txt.", ios::out);
  62.                                     bufferFile << aNum;
  63.                                     bufferFile.close();
  64.                                     sem1 = 1;
  65.                                     cout << "B. The number [ " << aNum <<"] modified by parent\n";
  66.                              }
  67.                             sem1 = 1;
  68.                     }
  69.     }//big while
  70.  
  71.  
  72.    
  73.    return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement