Advertisement
rinab333

CSCI 480 assignment 2

Jul 16th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.14 KB | None | 0 0
  1. //Terina Burr
  2. //z
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #include <unistd.h>
  8. #include <cstdlib>
  9. #include <string>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <sstream>
  14. #include <sys/utsname.h>
  15. #include <fstream>
  16. using std::cout;
  17. using std::endl;
  18. using std::string;
  19. using std::stringstream;
  20.  
  21. using std::ifstream;
  22. using std::istream;
  23. using std::cout;
  24. using std::endl;
  25. using std::string;
  26. using std::size_t;
  27. using std::getline;
  28. using namespace std;
  29. /****************************************************************
  30.  
  31. FUNCTION: getOStype
  32.  
  33. ARGUMENTS: None
  34.  
  35. RETURNS: String
  36.  
  37. NOTES: Opens a file and stores all the data present as
  38. a string. Returns the string to the main function
  39. ****************************************************************/
  40. string getOStype()
  41. {
  42. ifstream inFile;
  43. string ostype;
  44.  
  45. inFile.open("/proc/sys/kernel/ostype");
  46. if(!inFile) // error testing
  47. {
  48. cout << "Error: Cannot open file." << endl;
  49. exit(1);
  50. }
  51. while(!inFile.eof())
  52. {
  53. getline(inFile,ostype); // type of operating system
  54. return ostype;
  55. }
  56. inFile.close();
  57.  
  58. return ostype;
  59. }
  60.  
  61. /****************************************************************
  62.  
  63. FUNCTION: getHostName
  64.  
  65. ARGUMENTS: None
  66.  
  67. RETURNS: String
  68.  
  69. NOTES: Opens a file and stores all the data present as
  70. a string. Returns the string to the main function
  71. ****************************************************************/
  72. string getHostName()
  73. {
  74. ifstream inFile;
  75. string hostname;
  76.  
  77. inFile.open("/proc/sys/kernel/hostname");
  78. if(!inFile) // error testing
  79. {
  80. cout << "Error: Cannot open file." << endl;
  81. exit(1);
  82. }
  83. while(!inFile.eof())
  84. {
  85. getline(inFile,hostname); // hostname of the system
  86. return hostname;
  87. }
  88. inFile.close();
  89.  
  90. return hostname;
  91. }
  92.  
  93. /****************************************************************
  94.  
  95. FUNCTION: getOSrelease
  96.  
  97. ARGUMENTS: None
  98.  
  99. RETURNS: String
  100.  
  101. NOTES: Opens a file and stores all the data present as
  102. a string. Returns the string to the main function
  103. ****************************************************************/
  104. string getOSrelease()
  105. {
  106. ifstream inFile;
  107. string osrelease;
  108.  
  109. inFile.open("/proc/sys/kernel/osrelease");
  110. if(!inFile) // error testing
  111. {
  112. cout << "Error: Cannot open file." << endl;
  113. exit(1);
  114. }
  115. while(!inFile.eof())
  116. {
  117. getline(inFile,osrelease); // release date of operating system
  118. return osrelease;
  119. }
  120. inFile.close();
  121.  
  122. return osrelease;
  123. }
  124.  
  125. /****************************************************************
  126.  
  127. FUNCTION: getVersion
  128.  
  129. ARGUMENTS: None
  130.  
  131. RETURNS: String
  132.  
  133. NOTES: Opens a file and stores all the data present as
  134. a string. Returns the string to the main function
  135. ****************************************************************/
  136. string getVersion()
  137. {
  138. ifstream inFile;
  139. string version;
  140.  
  141. inFile.open("/proc/sys/kernel/version");
  142. if(!inFile) // error testing
  143. {
  144. cout << "Error: Cannot open file." << endl;
  145. }
  146. while(!inFile.eof())
  147. {
  148. getline(inFile,version); // version number of operating system
  149. return version;
  150. }
  151. inFile.close();
  152.  
  153. return version;
  154. }
  155. int main()
  156. {
  157. /*******************
  158. Part A Variables
  159. *******************/
  160. struct utsname partD;
  161. uname(&partD);
  162. string ostype; // operating system type
  163. string hostname; // host name
  164. string osrelease; // release date of operating system
  165. string version; // version of the machine
  166. ostype = getOStype(); // function to get the Operating System type
  167. hostname = getHostName(); // function to get the Host Name
  168. osrelease = getOSrelease();
  169. version = getVersion(); // function to get the version of the Operating System
  170.  
  171. /*******************
  172. Part A
  173. ********************/
  174. cout <<endl<<endl;
  175. cout<<" Part A "<<endl<<endl;
  176.  
  177. cout<<"Field"<<setw(25)<<"File data"<<setw(45)<<"API data"<<endl<<endl;
  178. cout<<"ostype"<<setw(22)<<ostype<<setw(45)<<partD.sysname<<endl;
  179. cout<<"hostname"<<setw(21)<<hostname<<setw(45)<<partD.nodename<<endl;
  180. cout<<"release"<<setw(30)<<osrelease<<setw(45)<<partD.release<<endl;
  181. cout<<"version"<<setw(59)<<version<<setw(45)<<partD.version<<endl;
  182. cout<<"machine"<<setw(67)<<partD.machine<<endl;
  183. cout<<endl<<endl<<endl;
  184.  
  185.  
  186. /*******************
  187. Part B Variables
  188. ********************/
  189. string path; //temp to hold path
  190. char * pPath;   //temp hold path
  191. string delimiter = ":"; //delimiter shows where to split
  192. string token;   //holds the temp split string
  193. int pos = 0;    //holds current position
  194. string token1;
  195. string token2;
  196. string token3;
  197. string token4;
  198. string token5;
  199. //  int length;
  200. int totalEnt=0;
  201. int totalBytes=0;
  202. /*******************
  203. Part B
  204. ********************/
  205. cout<<" Part B  "<<endl<<endl;
  206. pPath = getenv("PATH");
  207. if(pPath!=NULL)
  208. //cout << "Your PATH is: " << pPath << '\n';
  209. path = pPath;
  210. //  length = path.length();
  211. //  while(pos !=length)
  212. //  {
  213. token = path.substr(0,path.find(delimiter));
  214. cout << token <<right << setw(13)<< token.length()<<endl;
  215. totalEnt++;
  216. totalBytes+=token.length();
  217.  
  218. token = path.substr(token.length()+1,path.find(delimiter));
  219. token1 = token.substr(0,token.find(delimiter));
  220. cout << token1 <<right << setw(24)<< token1.length()<<endl;
  221. totalEnt++;
  222. totalBytes+=token1.length();
  223.  
  224. token2 = path.substr(token.length()+token1.length()+2,path.find(delimiter));
  225. token3 = token2.substr(0,token2.find(delimiter));
  226. cout << token3 <<right << setw(30)<< token3.length()<<endl;
  227. totalEnt++;
  228. totalBytes+=token3.length();
  229.  
  230. pos = token.length()+token1.length()+2;
  231. pos +=token3.length();
  232.  
  233. token3 =path.substr(pos+1,path.find(delimiter));
  234. token4 = token3.substr(0,token3.find(delimiter));
  235. cout << token4 <<right << setw(34)<< token4.length()<<endl;
  236. totalEnt++;
  237. totalBytes+=token4.length();
  238.  
  239. pos += token4.length();
  240.  
  241. token4 = path.substr(pos+2,path.find(delimiter));
  242. token5 = token4.substr(0,token4.find(delimiter));
  243. cout << token5 <<right <<setw(22)<< token5.length()<<endl;
  244. totalEnt++;
  245. totalBytes+=token5.length();
  246.  
  247. cout<<"Total"<<std::right << std::setw(30)<<totalBytes<<" bytes "<<totalEnt<<" Entries ";
  248. //  pos +=token.length();
  249. //}
  250. //cout << path;
  251. //I KNOW I COULDVE PUT THIS INTO A WHILE LOOP COULDNT FIGURE OUT HOW
  252.  
  253. cout<<endl<<endl<<endl<<endl;
  254.  
  255. /*******************
  256. Part C Variables
  257. *******************/
  258. int pid1 = 0; // variables to hold the fork values
  259. int pid2 = 0;
  260.  
  261. int pido = 0; // hold pid of original process's parent
  262. int pid = 0; // hold pid of original process
  263. int pidp = 0; // hold pid of the parent
  264. int pidip = 0; // hold pid of the intermediate parent
  265. int pidc = 0; // hold pid of the child
  266. stringstream pids; // holds stringstream version of pids
  267. string ids; // holds string of parent PIDs
  268. /*******************
  269. Part C
  270. ********************/
  271.  
  272. pid1 = fork(); // first fork to make 2 processes
  273. pid2 = fork(); // second fork to make 4 processes
  274.  
  275. if(pid1 > 0 && pid2 > 0) // original process
  276. {
  277. pid = getpid();
  278. pido = getppid();
  279. cout <<endl<< "PARENT: My PID is " << pid <<endl;
  280. cout<< "my parent is " << pido <<endl;
  281. cout<<"My Children are " << pid1 << " and " << pid2 << "." <<endl<<endl;
  282. pids << pido << "," << pid << "," << pid2 << "," << pid1; // convert PID to stringstream
  283. ids = "/bin/ps -f --ppid " + pids.str();
  284. system(ids.c_str()); //system call to show all the process involved
  285. waitpid(pid2,0,0); // wait for child process
  286. waitpid(pid1,0,0);
  287. cout << "PARENT: Children process are finished." << endl;
  288. }
  289. if(pid1 == 0 && pid2 > 0) // intermediate process
  290. {
  291. pidip = getpid();
  292. pid = getppid();
  293. cout << "Middle: My PID is " << pidip <<"."<<endl;
  294. cout<< "My parent is " << pid <<"."<<endl;
  295. cout<< "My child is " << pid2 << "." << endl<<endl;
  296. system("sleep 3"); // sleep for 3 seconds
  297. cout << "Middle: " << pidip << " is awake." << endl<<endl;
  298. }
  299. if(pid1 > 0 && pid2 == 0) // child process
  300. {
  301. pidp = getpid();
  302. pid = getppid();
  303. cout << "Child: My PID is " << pidp <<"."<<endl;
  304. cout<<"My parent is " << pid << "." << endl<<endl;
  305. system("sleep 3"); // sleep for 3 seconds
  306. cout << "CHILD: " << pidp << " is awake." << endl<<endl;
  307. }
  308. if(pid1 == 0 && pid2 == 0) // grandchild process
  309. {
  310. pidc = getpid();
  311. pidip = getppid();
  312. cout << "CHILD: My PID is " << pidc <<"."<<endl;
  313. cout<<"My parent is " << pidip << "." << endl<<endl;
  314. system("sleep 3"); // sleep for 3 seconds
  315. cout << "CHILD: " << pidc << " is awake." << endl<<endl;
  316. }
  317.  
  318. return 0;
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement