ObjectiveThe objective of this homework assignment is to develop a C++ program t
ObjectiveThe objective of this homework assignment is to develop a C++ program to:
Gain familiarity with the /proc filesystem
Hone your problem-solving and general C++ coding skills
Project overview: ps functionBackground Here is an example of what a directory listing of /proc looks like:
amf@cse381:~$ ls /proc
1 157 191 227 28 3169 48 71 917 kmsg
10 1585 1915 229 280 3176 49 72 92 kpagecgroup
100 16 194 23 282 32 5 73 93 kpagecount
1000 1608 195 230 29 320 50 74 94 kpageflags
1007 161 196 231 290 321 5096 75 95 loadavg
101 1612 197 232 291 3246 51 76 954 locks
102 1613 198 234 2924 3277 5100 77 959 mdstat
103 1618 1989 235 2928 3283 5110 79 96 meminfo
104 1620 199 236 293 3288 5112 8 97 misc
1048 1621 1993 237 2932 3290 52 80 98 modules
105 1622 1997 239 2937 33 522 81 99 mounts
106 1635 2 240 2945 3376 523 82 acpi mpt
107 1646 20 241 3 3399 524 83 asound mtrr
1077 1651 200 242 30 34 53 84 bootconfig net
108 1655 201 244 3052 3433 55 85 buddyinfo pagetypeinfo
109 1659 202 245 3053 3475 56 86 bus partitions
1092 1668 203 246 3069 35 57 864 cgroups pressure
11 1675 204 247 3070 3508 59 865 cmdline schedstat
111 1697 205 248 3072 3559 6 868 consoles scsi
113 17 206 249 3074 362 600 869 cpuinfo self
114 1704 207 25 3075 38 61 87 crypto slabinfo
1141 1707 208 250 3077 388 62 871 devices softirqs
115 172 209 251 3078 39 63 873 diskstats stat
116 1747 21 252 3080 4 64 874 dma swaps
12 1767 210 253 3083 40 65 88 driver sys
121 1778 211 254 3088 409 655 880 dynamic_debug sysrq-trigger
122 1782 212 255 3095 41 66 881 execdomains sysvipc
123 1829 213 256 31 415 661 884 fb thread-self
13 1831 214 257 3106 43 665 89 filesystems timer_list
130 1841 215 258 3113 44 67 892 fs tty
132 1862 216 259 3114 4428 679 894 interrupts uptime
133 1869 217 26 3116 4446 68 896 iomem version
1337 1873 218 260 3129 4446 686 898 ioports version_signature
1369 1878 219 261 3145 45 69 90 irq vmallocinfo
137 1887 22 262 3157 4583 691 902 kallsyms vmstat
14 1888 224 263 3158 46 693 905 kcore zoneinfo
15 19 225 27 3163 4643 70 907 keys
1527 1903 226 279 3165 47 700 91 key-users
The files with names that are numbers are directories with information about the process with that pid. Here is a sample directory listing of one of these:
amf@cse381:~$ ls /proc/4446
arch_status fd net setgroups
attr fdinfo ns smaps
autogroup gid_map numa_maps smaps_rollup
auxv io oom_adj stack
cgroup ksm_merging_pages oom_score stat
clear_refs ksm_stat oom_score_adj statm
cmdline limits pagemap status
comm loginuid patch_state syscall
coredump_filter map_files personality task
cpu_resctrl_groups maps projid_map timens_offsets
cpuset mem root timers
cwd mountinfo sched timerslack_ns
environ mounts schedstat uid_map
exe mountstats sessionid wchanA few of these files are important to this assignment.
The fd directory shows where the output of standard input (0), standard output (1) and standard error (2) comes from or goes to. bash also has a file descriptor called 255 which it uses for some internal bookkeeping.
amf@cse381:~$ ls -l /proc/4446/fd
total 0
lrwx—— 1 amf amf 64 Nov 16 10:59 0 -> /dev/pts/0
lrwx—— 1 amf amf 64 Nov 16 10:59 1 -> /dev/pts/0
lrwx—— 1 amf amf 64 Nov 16 10:59 2 -> /dev/pts/0
lrwx—— 1 amf amf 64 Nov 16 10:59 255 -> /dev/pts/0
Using the ls -lL command on one of these files shows that it is associated with a terminal:
amf@cse381:~$ ls -lL /proc/4446/fd/1
crw–w—- 1 amf tty 136, 0 Nov 16 11:34 /proc/4446/fd/1
If the process is not associated with a terminal, the output looks like this:
amf@cse381:/proc$ ls -l /proc/1747/fd
total 0
lrwx—— 1 amf amf 64 Nov 16 10:59 0 -> /dev/null
lrwx—— 1 amf amf 64 Nov 16 10:59 1 -> /dev/null
lrwx—— 1 amf amf 64 Nov 16 10:59 2 -> /dev/null
lr-x—— 1 amf amf 64 Nov 16 10:59 3 -> /dev/random
lr-x—— 1 amf amf 64 Nov 16 10:59 4 -> /dev/urandom
l-wx—— 1 amf amf 64 Nov 16 10:59 5 -> /home/amf/.gdfuse/default/gdfuse.log
lrwx—— 1 amf amf 64 Nov 16 10:59 6 -> /dev/fuse
Using the ls -lL command on one of these files shows that it is NOT associated with a terminal:
cse381:/proc$ ls -lL /proc/1747/fd/1
crw-rw-rw- 1 root root 1, 3 Nov 16 10:58 /proc/1747/fd/1
The checkPTS function provided as starter code uses this approach to determine whether a process is associated with a terminal or not.
The loginuid file contains the uid of the user who owns the process.
amf@cse381:/proc$ cat /proc/4446/loginuid
1000The stat file contains more than 50 statistics about the process. Most of these statistics are described in the man page for procLinks to an external site.. This is a sample of what that file looks like:
amf@cse381:/proc$ cat /proc/4446/stat
4446 (bash) S 4428 4446 4446 34816 24809 4194304 4288 197003 3 28 1 8 78 71 20 0 1 0 4659
11534336 1344 18446744073709551615 94444280786944 94444281700109 140731233213088 0 0 0 65536
3686404 1266761467 1 0 0 17 5 0 0 0 0 0 94444281944752 94444281992784 94444303265792
140731233217271 140731233217276 140731233217276 140731233218542 0
The starter code for this assignment includes a definition of a structure with these fields (procStat) and a function that populates the structure by parsing a stat file (parseStatFile).
Homework requirementsYour program must operate in the following manner:
If no command line argument is provided to the program, it should display the pid, CPU time used (user + system), and the command name for all processes owned by the user that are associated with a terminal. (See the Output Samples section below for a template of how this should look.) This is accomplished by first identifying all files in the /proc directory (using the provided getProcFiles function) and reading information from the /proc/[pid]/stat file (using the provided parseStatFile function). To determine if a process belongs to a terminal, a checkPTS function is provided. The way this function is implemented guarantees that the process is owned by the user running the program. Unless you are doing a lot on your VM, you will likely have less than 10 of your processes associated with a terminal.
If the command line argument u is provided, the program should display the pid, CPU time used (user + system), and the command name for all processes owned by the user, whether they are associated with a terminal or not. In this case you do not want to run the checkPTS function on the pid, but rather check if the owner of the process matches the uid of the user running the program. The getuid()Links to an external site. syscall will be helpful for doing this. You should find somewhere around 50 – 100 processes associated with your uid when you run the program with this option.
If the command line argument a is provided, the program should display the pid, CPU time used (user + system), and the command name for all processes running on the computer, regardless of the owner. There will likely be more than 200 processes running on the system.
To get credit for this assignment, you MUST use the files in /proc to get the information needed for this assignment. Do NOT run the ps command via a system or exec call to get the information.You can certainly use ps from the command line to check whether your program is giving reasonable output. See the Output Samples section below for examples of commands to use for this purpose.
I am giving you a lot of the hard or tedious code for this assignment. Your main challenge will be to set up the logic for deciding whether to display information about a process or not.
To meet the coding style guidelines, you will need to write several helper functions. I did it with only three of them (beyond the ones I’ve provided to you), but you may need more depending on how you set up your program logic.
Make sure that your output looks like the samples below. In particular, there should be a header to the output and the time should be formatted as hh:mm:ss. For simplicity, you can assume that no running process has used more than 99:59:59 of CPU.
I am providing you with the following starter code:Please take some time to study the code in this file as there are some interesting functions provided. And this may give you hints on how to implement other helper functions.
A file homework04-starter.cppthat contains an empty main function as well as the include files that you’ll likely need.
A file homework04Helpers.h that contains the implementation of a structure and several helper functions. DO NOT MODIFY THIS FILE! When you submit your program to CODE, it will use this version of homework04Helpers.h.
Output Samples: Note that your exact output will be different. These give examples of how the different command line arguments should affect the outputNo command line arguments:
Display information about all of the user’s processes associated with a terminal.
$ ./Homework04
PID TIME CMD
3509 00:00:02 (bash)
4880 00:00:00 (bash)
5790 00:00:00 (bash)
13439 00:00:00 (bash)
13585 00:00:01 (emacs)
13604 00:00:05 (emacs)
17112 00:00:00 (Homework04)You can check whether the complete set of processes is being displayed by running the following command:
ps a | grep pts | grep -v grep | grep -v psThe pids displayed by your program should match the output of that command, plus there will be one for the Homework04 executable. The output of the ps command will be in a slightly different format than what comes from your program — verifying the set of PIDs is the important thing here.
Command line argument: u
Display information about all of the user’s processes whether they’re associated with a terminal or not.. The output should be in the same format as above, but there should be many more processes displayed (possibly close to 100).
$ ./Homework04 uYou can check whether the complete set of processes is being displayed by running the following command, replacing cse381 with the userid you use to log into your virtual machine:
ps -u cse381 | grep -v ps | grep -v grepThere may be a small discrepancy in the number of processes from the program and the output of this command, but as long as the difference is only a few processes you should be fine.
Command line argument: a
Display information about all processes. The output should be in the same format as above, but there should be many, many more processes displayed (likely more than 200).
$ ./Homework04 aYou can check whether the complete set of processes is being displayed by running the following command:
ps -efThere may be a small discrepancy in the number of processes from the program and the output of this command, but as long as the difference is only a few processes you should be fine.
Grading(5 points) Required for submission: Program must compile with no errors or warning messages and must have no style errors when submitted via the CODE plugin
(10 points) Program works correctly with no command line argument (i.e., it displays the user’s processes associated with a pts)
(5 points) Program works correctly with the command line argument u (i.e., it displays all of the user’s processes)
(5 points) Program works correctly with the command line argument a (i.e., it displays all processes)
(5 points) Documentation and programming style
Notes/TipsDevelop and test this code on your VM!
First write comments to make a plan. Include code snippets in your comments. Then start implementing. Work one feature at a time and submit it. Refactor your program as needed.This assignment is tailored to the incremental development approach I discussed in class as you get additional points when you add new features.
Because the output of this program depends on what’s running on the machine at any particular time, the CODE plugin CANNOT tell you if your output is correct. You will have to compare your output to the samples above and the output of the different ps commands.
Remember that you can use the CODE plugin multiple times to make sure your submission is valid. You can also completely submit code that partially works and do subsequent submissions as you implement more functionality. This guarantees that there’s always a gradable submission in Canvas.
DO NOT use global variables!
DO NOT modify the homework04Helper.h file! The version I am distributing will be used by the CODE plugin to compile your program.