Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Terina Burr
- //z
- #include <iostream>
- #include <iomanip>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <cstdlib>
- #include <string>
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <sstream>
- #include <sys/utsname.h>
- #include <fstream>
- using std::cout;
- using std::endl;
- using std::string;
- using std::stringstream;
- using std::ifstream;
- using std::istream;
- using std::cout;
- using std::endl;
- using std::string;
- using std::size_t;
- using std::getline;
- using namespace std;
- /****************************************************************
- FUNCTION: getOStype
- ARGUMENTS: None
- RETURNS: String
- NOTES: Opens a file and stores all the data present as
- a string. Returns the string to the main function
- ****************************************************************/
- string getOStype()
- {
- ifstream inFile;
- string ostype;
- inFile.open("/proc/sys/kernel/ostype");
- if(!inFile) // error testing
- {
- cout << "Error: Cannot open file." << endl;
- exit(1);
- }
- while(!inFile.eof())
- {
- getline(inFile,ostype); // type of operating system
- return ostype;
- }
- inFile.close();
- return ostype;
- }
- /****************************************************************
- FUNCTION: getHostName
- ARGUMENTS: None
- RETURNS: String
- NOTES: Opens a file and stores all the data present as
- a string. Returns the string to the main function
- ****************************************************************/
- string getHostName()
- {
- ifstream inFile;
- string hostname;
- inFile.open("/proc/sys/kernel/hostname");
- if(!inFile) // error testing
- {
- cout << "Error: Cannot open file." << endl;
- exit(1);
- }
- while(!inFile.eof())
- {
- getline(inFile,hostname); // hostname of the system
- return hostname;
- }
- inFile.close();
- return hostname;
- }
- /****************************************************************
- FUNCTION: getOSrelease
- ARGUMENTS: None
- RETURNS: String
- NOTES: Opens a file and stores all the data present as
- a string. Returns the string to the main function
- ****************************************************************/
- string getOSrelease()
- {
- ifstream inFile;
- string osrelease;
- inFile.open("/proc/sys/kernel/osrelease");
- if(!inFile) // error testing
- {
- cout << "Error: Cannot open file." << endl;
- exit(1);
- }
- while(!inFile.eof())
- {
- getline(inFile,osrelease); // release date of operating system
- return osrelease;
- }
- inFile.close();
- return osrelease;
- }
- /****************************************************************
- FUNCTION: getVersion
- ARGUMENTS: None
- RETURNS: String
- NOTES: Opens a file and stores all the data present as
- a string. Returns the string to the main function
- ****************************************************************/
- string getVersion()
- {
- ifstream inFile;
- string version;
- inFile.open("/proc/sys/kernel/version");
- if(!inFile) // error testing
- {
- cout << "Error: Cannot open file." << endl;
- }
- while(!inFile.eof())
- {
- getline(inFile,version); // version number of operating system
- return version;
- }
- inFile.close();
- return version;
- }
- int main()
- {
- /*******************
- Part A Variables
- *******************/
- struct utsname partD;
- uname(&partD);
- string ostype; // operating system type
- string hostname; // host name
- string osrelease; // release date of operating system
- string version; // version of the machine
- ostype = getOStype(); // function to get the Operating System type
- hostname = getHostName(); // function to get the Host Name
- osrelease = getOSrelease();
- version = getVersion(); // function to get the version of the Operating System
- /*******************
- Part A
- ********************/
- cout <<endl<<endl;
- cout<<" Part A "<<endl<<endl;
- cout<<"Field"<<setw(25)<<"File data"<<setw(45)<<"API data"<<endl<<endl;
- cout<<"ostype"<<setw(22)<<ostype<<setw(45)<<partD.sysname<<endl;
- cout<<"hostname"<<setw(21)<<hostname<<setw(45)<<partD.nodename<<endl;
- cout<<"release"<<setw(30)<<osrelease<<setw(45)<<partD.release<<endl;
- cout<<"version"<<setw(59)<<version<<setw(45)<<partD.version<<endl;
- cout<<"machine"<<setw(67)<<partD.machine<<endl;
- cout<<endl<<endl<<endl;
- /*******************
- Part B Variables
- ********************/
- string path; //temp to hold path
- char * pPath; //temp hold path
- string delimiter = ":"; //delimiter shows where to split
- string token; //holds the temp split string
- int pos = 0; //holds current position
- string token1;
- string token2;
- string token3;
- string token4;
- string token5;
- // int length;
- int totalEnt=0;
- int totalBytes=0;
- /*******************
- Part B
- ********************/
- cout<<" Part B "<<endl<<endl;
- pPath = getenv("PATH");
- if(pPath!=NULL)
- //cout << "Your PATH is: " << pPath << '\n';
- path = pPath;
- // length = path.length();
- // while(pos !=length)
- // {
- token = path.substr(0,path.find(delimiter));
- cout << token <<right << setw(13)<< token.length()<<endl;
- totalEnt++;
- totalBytes+=token.length();
- token = path.substr(token.length()+1,path.find(delimiter));
- token1 = token.substr(0,token.find(delimiter));
- cout << token1 <<right << setw(24)<< token1.length()<<endl;
- totalEnt++;
- totalBytes+=token1.length();
- token2 = path.substr(token.length()+token1.length()+2,path.find(delimiter));
- token3 = token2.substr(0,token2.find(delimiter));
- cout << token3 <<right << setw(30)<< token3.length()<<endl;
- totalEnt++;
- totalBytes+=token3.length();
- pos = token.length()+token1.length()+2;
- pos +=token3.length();
- token3 =path.substr(pos+1,path.find(delimiter));
- token4 = token3.substr(0,token3.find(delimiter));
- cout << token4 <<right << setw(34)<< token4.length()<<endl;
- totalEnt++;
- totalBytes+=token4.length();
- pos += token4.length();
- token4 = path.substr(pos+2,path.find(delimiter));
- token5 = token4.substr(0,token4.find(delimiter));
- cout << token5 <<right <<setw(22)<< token5.length()<<endl;
- totalEnt++;
- totalBytes+=token5.length();
- cout<<"Total"<<std::right << std::setw(30)<<totalBytes<<" bytes "<<totalEnt<<" Entries ";
- // pos +=token.length();
- //}
- //cout << path;
- //I KNOW I COULDVE PUT THIS INTO A WHILE LOOP COULDNT FIGURE OUT HOW
- cout<<endl<<endl<<endl<<endl;
- /*******************
- Part C Variables
- *******************/
- int pid1 = 0; // variables to hold the fork values
- int pid2 = 0;
- int pido = 0; // hold pid of original process's parent
- int pid = 0; // hold pid of original process
- int pidp = 0; // hold pid of the parent
- int pidip = 0; // hold pid of the intermediate parent
- int pidc = 0; // hold pid of the child
- stringstream pids; // holds stringstream version of pids
- string ids; // holds string of parent PIDs
- /*******************
- Part C
- ********************/
- pid1 = fork(); // first fork to make 2 processes
- pid2 = fork(); // second fork to make 4 processes
- if(pid1 > 0 && pid2 > 0) // original process
- {
- pid = getpid();
- pido = getppid();
- cout <<endl<< "PARENT: My PID is " << pid <<endl;
- cout<< "my parent is " << pido <<endl;
- cout<<"My Children are " << pid1 << " and " << pid2 << "." <<endl<<endl;
- pids << pido << "," << pid << "," << pid2 << "," << pid1; // convert PID to stringstream
- ids = "/bin/ps -f --ppid " + pids.str();
- system(ids.c_str()); //system call to show all the process involved
- waitpid(pid2,0,0); // wait for child process
- waitpid(pid1,0,0);
- cout << "PARENT: Children process are finished." << endl;
- }
- if(pid1 == 0 && pid2 > 0) // intermediate process
- {
- pidip = getpid();
- pid = getppid();
- cout << "Middle: My PID is " << pidip <<"."<<endl;
- cout<< "My parent is " << pid <<"."<<endl;
- cout<< "My child is " << pid2 << "." << endl<<endl;
- system("sleep 3"); // sleep for 3 seconds
- cout << "Middle: " << pidip << " is awake." << endl<<endl;
- }
- if(pid1 > 0 && pid2 == 0) // child process
- {
- pidp = getpid();
- pid = getppid();
- cout << "Child: My PID is " << pidp <<"."<<endl;
- cout<<"My parent is " << pid << "." << endl<<endl;
- system("sleep 3"); // sleep for 3 seconds
- cout << "CHILD: " << pidp << " is awake." << endl<<endl;
- }
- if(pid1 == 0 && pid2 == 0) // grandchild process
- {
- pidc = getpid();
- pidip = getppid();
- cout << "CHILD: My PID is " << pidc <<"."<<endl;
- cout<<"My parent is " << pidip << "." << endl<<endl;
- system("sleep 3"); // sleep for 3 seconds
- cout << "CHILD: " << pidc << " is awake." << endl<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement