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).
process hierarchy that was done as part of an earlier lab exercise. Refer to the Essentially this program automates the manual process of tracing the Linuxearlier lab exercise 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 or
/lib/systemd/system 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. To read the full command
(which has spaces in it) use std::getline method. Keep I/O simple and straightforward.
- 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 [ | Must pass to earn any points | ]: |
$ ./raodm_hw3 proc_info1.txt 1Process tree for PID: 1PID PPID CMD1 0 /sbin/init |
Test case #2 [Must pass to earn full points]:
$ ./raodm_hw3 proc_info1.txt 27426
Process tree for PID: 27426
PID PPID CMD
1 0 /sbin/init
949 1 /usr/sbin/sshd -D
27282 949 sshd: salvucwa [priv]
- 27282 sshd: [email protected]/31
- 27323 -bash
- 27324 script
- 27425 bash -i
Test case #3 [Must pass to earn full points]:
$ ./raodm_hw3 proc_info1.txt 27535
Process tree for PID: 27535
PID PPID CMD
1 0 /sbin/init
949 1 /usr/sbin/sshd -D
25640 949 sshd: raodm [priv]
25681 25640 sshd: [email protected]/22
25697 25681 -bash
27535 25697 ps -fe
Submit to Canvas
This homework assignment must be turned-in electronically via Canvas via the CODE Plugin. Ensure your program compiles without any warnings or style violations and operates correctly, at least for the base case. Once you have tested your implementation, upload just one C++ source file via the CODE plugin.
Reviews
There are no reviews yet.