Objective |
The objective of this part of the homework is to develop 1 C++ program to: Print process hierarchy for a given PID (process ID) Gain familiarity with developing a C++ class Continue to gain familiarity with development and testing of C++ programs Continue to bolster concepts of stream/file processing. Review basics of string processing & problem solving Continue to gain familiarity with the use of std::unordered_map |
Develop a C++ program to print full process hierarchy
Objective
The objective of this program (developed as a C++ class) is to print the full hierarchy of processes (starting with /sbin/init) for a given PID (process ID, specified as a command-line argument). The data about processes is read from a given text file (as command-line argument).
hierarchy that was done as part of an earlier lab exercise. Refer to the earlier lab Essentially this program automates the manual process of tracing the Linux processexercise of using the output of ps -fe to trace processes to recollect the manual process of tracing the process hierarchy. |
Background
In Linux, processes are organized as a tree, rooted at /sbin/init, which is the first process that the Linux kernel starts running. Each process is identified by a unique ID called PID (short for process ID). Furthermore, each process has a PPID (short for parent process ID). The process hierarchy can be determined manually, by recursively tracing the PPID in the output of ps -fe command. However, this program is designed to automate this task of tracing the process hierarchy.
Data file formats
Prior to solving any problem is important to study the supplied data. So ensure you view the data files (yes, of course you can do this in NetBeans). The supplied data files are exactly the output of ps -fe command, also reviewed in lab exercise(s).
Program requirements & Tips:
- This program should be developed as a C++ class with the following 2 files:
- h: This header file should contain the class declaration. You will need 2 public methods u a method that loads process data from a given file into 2 unordered maps (discussed below) v a method that prints the process tree for a given PID. You may add any private helper methods as you see fit. It is up to you to decide meaningful names for methods and their arguments.
- cpp: This source file should contain the implementation for the methods you have defined in the header file (ensure you #include MUID_hw3.h in your source file). This file will also contain the implementation for the main method. Your main method should create an object and calls methods on the object with suitable parameters.
- To ease printing the process hierarchy, in your class use 2 unordered_maps as instance variables in your class to store the following:
- pidppid information to ease look-up of parent process ID.
- pidcmd information to ease look-up the command associated with a PID.
- Use std::istringstream to process each line. Even if you dont use a specific column of data, it is still easier to read it and simply not use it.
- Note the order in which processes are listed in the sample output. It is top-down (and not bottom-up)
- For printing the process hierarchy in a top-down manner, you may use an iterative or a recursive solution as you see fit. However, the recursive solution will most likely be shorter than an iterative solution.
Sample outputs
One you have completed your program, you can test its operation using the command shows below and compare your output to the output shown below. Note that for the 3 column output, each column is separated by 1 tab (i.e., PIDtPPIDtCMD) character
Base case #1:
$ ./raodm_hw3 proc_info1.txt 1
Process tree for PID: 1
PID PPID CMD
1 0 /sbin/init
Test case #2 [Must pass to earn full points]:
$ ./raodm_hw3 proc_info1.txt 27426Process tree for PID: 27426PID PPID CMD1 0 /sbin/init949 1 /usr/sbin/sshd -D27282 949 sshd: salvucwa [priv]27323 27282 sshd: [email protected]/3127324 27323 -bash27425 27324 script27426 27425 bash -i |
Test case #3 [Must pass to earn full points]:
$ ./raodm_hw3 proc_info1.txt 27535Process tree for PID: 27535PID PPID CMD1 0 /sbin/init949 1 /usr/sbin/sshd -D25640 949 sshd: raodm [priv]25681 25640 sshd: [email protected]/2225697 25681 -bash27535 25697 ps -fe |
Reviews
There are no reviews yet.