Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string.h>
  4. #include <string>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <sys/types.h>
  9. #include <sys/wait.h>
  10. #include <semaphore.h>
  11. #include <sys/shm.h>
  12. #include <sys/sem.h>
  13. #include <sys/ipc.h>
  14. #include <vector>
  15. #include <sstream>
  16. #define SHMSIZE 1024
  17.  
  18. using namespace std;
  19.  
  20. namespace patch
  21. {
  22. template < typename T > std::string to_string( const T& n )
  23. {
  24. std::ostringstream stm ;
  25. stm << n ;
  26. return stm.str() ;
  27. }
  28. }
  29.  
  30. struct process
  31. {
  32. int r;
  33. string name;
  34. vector<string> lines;
  35. };
  36.  
  37. int main(int argc, char * argv[])
  38. {
  39. int firstRun = 1; //Skipping First Line of Assign-1.ip.
  40.  
  41. int quantum = 0; //For taking input of quantum.
  42. int count = 0; //For number of processes.
  43. int pchtoint;
  44. string c;
  45. char * pch; //For tokenization.
  46.  
  47. string reading_file; //Reading a line from file.
  48.  
  49. char * readarr; //Converting "reading_file" to readarr for tokenization.
  50.  
  51. process * p;
  52.  
  53. //=== Quantum Input ===//
  54. cout<<"Enter Quantum size [1-1000]: ";
  55. cin>>quantum;
  56.  
  57. while(quantum < 1 || quantum > 1000)
  58. {
  59. cout<<"Wrong input!!! Enter Again [1-1000]: ";
  60. cin>>quantum;
  61. }
  62. //=====================//
  63.  
  64. //===Filing===//
  65. ifstream read("Assign-2.ip");
  66.  
  67. if(read.is_open())
  68. {
  69. while(!read.eof())
  70. {
  71. getline(read, reading_file);
  72.  
  73. readarr = new char[reading_file.size() + 1];
  74. for(int i = 0; i < reading_file.length(); i++)
  75. {
  76. readarr[i] = reading_file[i];
  77. }
  78.  
  79. if(firstRun > 1)
  80. {
  81. int countingline = 0; //counting the number of lines in a process.
  82. pch = strtok (readarr," ,");
  83.  
  84. while (pch != NULL)
  85. {
  86. c = pch[1];
  87. pchtoint = atoi(c.c_str());
  88. p[pchtoint-1].r++;
  89. p[pchtoint-1].lines.push_back(pch);
  90. for(int i = 0; i < p[pchtoint-1].lines.size(); i++)
  91. cout<<p[pchtoint-1].name<<"=="<<p[pchtoint-1].lines.at(i)<<endl;
  92.  
  93.  
  94. pch = strtok (NULL, " ,");
  95. }
  96. }
  97. else
  98. {
  99. pch = strtok (readarr,",.-");
  100. while (pch != NULL)
  101. {
  102. count++;
  103. pch = strtok (NULL, ",.-");
  104. }
  105. p = new process[count];
  106. string s = "p";
  107. for(int i = 0; i < count; i++)
  108. {
  109. s = s + patch::to_string(i+1);
  110. p[i].name = s;
  111. s = s[0];
  112. }
  113. firstRun++;
  114. }
  115. }
  116. }
  117. else
  118. {
  119. cout<<"Cannot open file!!!"<<endl;
  120. }
  121. read.close();
  122. return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement