Question: C language Implement the Linux ps command and -p option, which displays only the process information for the process with the given process id. It

C language

Implement the Linux "ps" command and -p option, which displays only the process information for the process with the given process id. It is okay if the specified process belongs to the current user. If this option is not provided, display information for all processes belonging to the current user. You only need to support a single -p option.

-s: Display single-character status information about the process. This information can be found in the "status" file in the process directory by looking at the third ("state") field. Note that the information you read from the stat file is a string.

-U: Do not display the amount of user time consumed by this process. This information can be found in the "stat" file in the process directory by looking at the "utime" field. If this option is not provided, display user time information.

-S: Display the amount of system time consumed by this process up to this point. This information can be found in the "stat" file in the process directory by looking at the "stime" field.

-v: Display the current amount of virtual memory used by this program in pages. This information can be found in the "statm" file in the process directory by looking at the first ("size") field.

-C: Do not display the command line used to launch this program. This information can be found in the "cmdline" file in the process directory. Be careful with this, as this file contains a list terminated by a null (zero-byte) string. If this option is not provided, display the command line information.

Regarding module development and some suggestions included in your design:

Option processing: The module will handle command line options and set status variables to record the content specified by the options.

Get process list: If there is no -p option, the module needs to look at /proc to find a list of processes belonging to the user. The module should implement this function.

stat and statm parsers: The module will extract strings from the space-separated list it reads from stat and statm files.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!