程序代写 CMPUT 379 Lab Seminars (2)

CMPUT 379 Lab Seminars (2)
o Reminder: Use of Lab Machines o A Few Notes on BASH
o Unix Time Values
o Process Resource Limits

Copyright By PowCoder代写 加微信 powcoder

o Getting Process Information
CMPUT 379 (E.S. Elmallah)

Reminder: Use of Lab Machines
❑Make sure you don’t leave stray processes running after you log out, or after closing a remote connection.
o One way of doing this is to use the following command that kills all of your processes on the workstation and logs you out completely from the workstation.
o Be careful, the command does NOT save any of your
% pkill –u $USER
❑Rely on using lab machines (not the “ohaton” server machine) for solving the assignments.
CMPUT 379 (E.S. Elmallah)

A Few Notes on Bash
❑As with many other shells, BASH is both a command language and a programming language; it provides a user with an interface to the OS.
❑In this part, we’d like to learn some features of BASH using simple experiments.
❑Suggested activity:
o Use “% env” (/usr/bin/env) to print variables defined in the shell’s environment list (cf. Sec. 7.5 in [APUE 3/E]).
o Use “% echo $PATH” to print the search path used by the shell to locate executable files.
CMPUT 379 (E.S. Elmallah)

o If the current directory “.” is not in PATH then add it using “PATH=$PATH:.”. Verify the result by printing the new
search path.
o Define a new local variable to the shell using
% NEWVAR=“This is a new variable”
(don’t leave space before or after the “=“ sign).
o Verify the value of NEWVAR using “% echo $NEWVAR”. o Is NEWVAR now part of the shell’s environment list? o If not, try adding it using “% export NEWVAR”.
o In BASH (and other shells), aliases allow a string to be substituted for a word when it is used as the first word of a simple command.
CMPUT 379 (E.S. Elmallah)

o Use the built-in command “% alias” to list all aliases defined in the current session.
o Add another alias to the list:
% alias a1ps=”ps -u $USER -o user,pid,ppid,pgid,state,start,cmd –sort start”
o Verify the above step using “% a1ps”.
o BASH expands the tilde in the string “~/.bashrc” as “$HOME/.bashrc” to point to file “.bashrc” in the user’s home directory.
o Examine the BASH initialization file “~/.bashrc”, and note that you can add new variable definitions and aliases to it.
CMPUT 379 (E.S. Elmallah)

Unix Time Values
❑ See Sec. 1.10, 6.10, and 8.16 in [APUE 3/E] ❑Programming Interface
The following data types are defined:
• Holds a calendar time (the number of seconds
since January 1970)
• The value represents both time and date • Function time() returns such a value
CMPUT 379 (E.S. Elmallah)

o clock_t:
• Holds a wall clock time (measured from some
arbitrary point in the past)
• Counts in units of clock ticks (e.g., 50, 60, or 100 ticks per second)
• To convert clock ticks to seconds use:
– “long clktck= sysconf(_SC_CLK_TCK)” – Divide by clktck
• Function times() returns many wall clock values.
CMPUT 379 (E.S. Elmallah)

o struct tms:
• Declares 4 clock_t values:
– User CPU time
– System CPU time
– User CPU time, terminated children
– System CPU time, terminated children
• Function times() stores its results in a tms struct
CMPUT 379 (E.S. Elmallah)

❑ Core Utilities o /usr/bin/time:
• Run programs and summarize system resource usage
• Example: % pushd /usr/include
% time –p grep _POSIX_SOURCE *.h
o /usr/date:
• Print or set the system date and time
• Example: % date
CMPUT 379 (E.S. Elmallah)

Process Resource Limits
❑ See Sec. 7.11 in [APUE 3/E] ❑Programming Interface
o Every process has a set of resource limit types that can be changed, e.g.,
• RLIMIT_CPU: The maximum amount of CPU time in seconds
• RLIMIT_FSIZE: The maximum size in bytes of a file that can be created
• RLIMIT_NPROC: The maximum number of child processes per real user
CMPUT 379 (E.S. Elmallah)

o Each type has a soft (current) limit, and a hard limit
o A process can exceed a soft limit for a period of time
o A process can change its soft limit to a value ≤ the hard limit using function setrlimit()
o Only a superuser process can raise a hard limit
o A resource limit affects the setrlimit() calling process
and is inherited by any of its children.
o struct rlimit:
CMPUT 379 (E.S. Elmallah)
[APUE 3/E]

❑ BASH Built-in Commands
CMPUT 379 (E.S. Elmallah)

❑Examples:
o Report all soft limits: % ulimit –Sa
o Report all hard limits: % ulimit –Ha
o Set a soft limit of 32 max number of processes:
% ulimit –Su 32
CMPUT 379 (E.S. Elmallah)

Getting Process Information
❑Programming Interface
o /proc is a “process information pseudo-file system”.
o Each numbered directory corresponds to an actual process ID.
o Details of a process can be obtained by looking at the associated files.
❑ Core Utilities and User Commands
o Several programs exist for getting information about the running processes: e.g., /bin/ps, /usr/bin/pstree, /usr/bin/top, etc.
CMPUT 379 (E.S. Elmallah)

Process states
❑ On lab machines, the 𝑝𝑠 command uses the following letters to specify a process state:
CMPUT 379 (E.S. Elmallah)

❑Examples:
Display information about all processes
% ps –u $USER -f
Display information on processes owned by $USER
% pstree -ahp
Draw a process tree for all processes
% pstree –ahp –u $USER
Draw a process tree for processes owned by $USER
Display running processes
% pgrep –a –u $USER “(a1jobs|xclock|xeyes|myclock)”
Report all processes owned by $USER that match the given regular expression
% pkill –u $USER “(xterm|xclock|xeyes|myclock)”
Send SIGTERM to all processes owned by $USER that match the given regular expression
CMPUT 379 (E.S. Elmallah)

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com