Know both Linux or Unix top command and ps command to clear Bloomberg interview question

(Last Updated On: June 15, 2010)

Know both Linux or Unix top command and ps command to clear Bloomberg interview question

This is for questions on how to diagnose for memory leaks or run away processes

top
Process viewer, find the CPU-intensive programs currently running. See ps for explanations of the field descriptors.
Syntax
top options

Options
-b Run in batch mode; don’t accept command-line input.
Useful for sending output to another command or to a file.
-c Show command line in display instead of just command name.
-d delay
Specify delay between refreshes.
-i Suppress display of idle and zombie processes.
-n num
Update display num times, then exit.
-p pid
Monitor only processes with the specified process ID.
-q Refresh without any delay.
If user is privileged, run with highest priority.
-s Secure mode. Disable some (dangerous) interactive commands.
-S Cumulative mode. Print total CPU time of each process,
including dead child processes.

Interactive commands

space Update display immediately.
A Sort by age, with newest first.
c Toggle display of command name or full command line.
f, F Add fields to display or remove fields from the display.
h, ? Display help about commands and the status of secure and cumulative modes.
k Prompt for process ID to kill and signal to send (default is 15) to kill it.
i Toggle suppression of idle and zombie processes.
l Toggle display of load average and uptime information.
^L Redraw screen.
m Toggle display of memory information.
M Sort tasks by resident memory usage.
n, # Prompt for number of processes to show.
If 0 is entered, show as many as will fit on the screen
(default).
N Sort numerically by process ID.
o, O Change order of displayed fields.
P Sort tasks by CPU usage (default).
q Exit.
r Apply renice to a process. Prompt for PID and renice value. Suppressed in secure mode.
s Change delay between refreshes.
Prompt for new delay time, which should be in seconds.
(Suppressed in secure mode.)
S Toggle cumulative mode. (See the -S option.)
t Toggle display of processes and CPU states information.
T Sort tasks by time/cumulative time.
W Write current setup to ~/.toprc. This is the recommended way to write a top configuration file.
Top displays per-process CPU usage (not total server load) it is useful for seeing how much work the machine is doing now compared to some point in the past.
At the top of the display output there are three numbers representing the number of processes waiting for the CPU now, an average for the past five minutes, and an average for the past fifteen minutes. These three numbers are the “load average”
Top should only be used to compare the load average on two different machines if they have an identical configuration (both hardware and software.)
ps
Process status, information about processes running in memory. If you want a repetitive update of this status, use top.
Syntax

ps option(s)
ps [-L]

Options
-L List all the keyword options
This version of ps accepts 3 kinds of option:

-Unix98 options may be grouped and must be preceeded by a dash.
BSD options may be grouped and must not be used with a dash.
–GNU long options are preceeded by two dashes.

Options of different types may be freely mixed. The PS_PERSONALITY environment variable provides more detailed control of ps behavior.

The Options below are listed side-by-side (unless there are differences).
Simple Process Selection:
-A a select all processes (including those of other users)
-a select all with a tty except session leaders
-d select all, but omit session leaders
-e select all processes
g really all, even group leaders (does nothing w/o SunOS settings)
-N negate selection
r restrict output to running processes
T select all processes on this terminal
x select processes without controlling ttys
–deselect negate selection

Process Selection by List:

-C select by command name
-G select by RGID (supports names)
-g select by session leader OR by group name
–Group select by real group name or ID
–group select by effective group name or ID
-p p –pid select by process ID (PID)
-s –sid select by session ID
-t –tty select by terminal (tty)
-u U select by effective user ID (supports names)
-U select by RUID (supports names)
–User select by real user name or ID
–user select by effective user name or ID

-123 implied –sid
123 implied –pid

Output Format Control:

-c Different scheduler info for -l option
-f Full listing
-j j Jobs format
-l l Long format
-O O Add the information associated with the space or comma separated
list of keywords specified, after the process ID, in the default
information display.

-o o Display information associated with the space or comma separated
list of keywords specified.
–format user-defined format
s display signal format
u display user-oriented format
v display virtual memory format
X old Linux i386 register format
-y do not show flags; show rss in place of addr

Output Modifiers:
C use raw CPU time for %CPU instead of decaying average
c true command name
e show environment after the command
f ASCII-art process hierarchy (forest)
-H show process hierarchy (forest)
h do not print header lines (repeat header lines in BSD personality)
-m m show all threads
-n set namelist file
n numeric output for WCHAN and USER
N specify namelist file
O sorting order (overloaded)
S include some dead child process data (as a sum with the parent)
-w w wide output
–cols set screen width
–columns set screen width
–forest ASCII art process tree
–html HTML escaped output
–headers repeat header lines
–no-headers print no header line at all
–lines set screen height
–nul unjustified output with NULs
–null unjustified output with NULs
–rows set screen height
–sort specify sorting order
–width set screen width
–zero unjustified output with NULs

Information:
-V V print version
L list all format specifiers
–help print help message
–info print debugging info
–version print version

Obsolete:
A increase the argument space (DecUnix)
M use alternate core (try -n or N instead)
W get swap info from … not /dev/drum (try -n or N instead)
k use /vmcore as c-dumpfile (try -n or N instead)
NOTES
The “-g” option can select by session leader OR by group name. Selection by session leader is specified by many standards, but selection by group is the logical behavior that several other operating systems use. This ps will select by session leader when the list is completely numeric (as sessions are). Group ID numbers will work only when some group names are also specified.

The “m” option should not be used. Use “-m” or “-o” with a list. (“m” displays memory info, shows threads, or sorts by memory use)

The “h” option varies between BSD personality and Linux usage (not printing the header) Regardless of the current personality, you can use the long options –headers and –no-headers

Terminals (ttys, or screens of text output) can be specified in several forms: /dev/ttyS1, ttyS1, S1. Obsolete “ps t” (your own terminal) and “ps t?” (processes without a terminal) syntax is supported, but modern options (“T”,”-t” with list, “x”, “t” with list) should be used instead.

The BSD “O” option can act like “-O” (user-defined output format with some common fields predefined) or can be used to specify sort order. Heuristics are used to determine the behavior of this option. To ensure that the desired behavior is obtained, specify the other option (sorting or formatting) in some other way.

For sorting, BSD “O” option syntax is O[+|-]k1[,[+|-]k2[,…]] Order the process listing according to the multilevel sort specified by the sequence of short keys from SORT KEYS, k1, k2, … The `+’ is quite optional, merely re-iterating the default direction on a key. `-‘ reverses direction only on the key it precedes.
The O option must be the last option in a single command argument, but specifications in successive arguments are catenated.

GNU sorting syntax is –sortX[+|-]key[,[+|-]key[,…]]
Choose a multi-letter key from the SORT KEYS section. X may be any convenient separator character. To be GNU-ish use `=’. The `+’ is really optional since default direction is increasing numerical or lexicographic order. For example, ps jax –sort=uid,-ppid,+pid

This ps works by reading the virtual files in /proc. This ps does not need to be suid kmem or have any privileges to run. Do not give this ps any special permissions.

This ps needs access to a namelist file for proper WCHAN display. The namelist file must match the current Linux kernel exactly for correct output.

To produce the WCHAN field, ps needs to read the System.map file created when the kernel is compiled. The search path is:
$PS_SYSTEM_MAP
/boot/System.map-`uname -r`
/boot/System.map
/lib/modules/`uname -r`/System.map
/usr/src/linux/System.map
/System.map
The member used_math of task_struct is not shown, since crt0.s checks to see if math is present. This causes the math flag to be set for all processes, and so it is Programs swapped out to disk will be shown without command line arguments, and unless the c option is given, in brackets.

%CPU shows the cputime/realtime percentage. It will not add up to 100% unless you are lucky. It is time used divided by the time the process has been running.

The SIZE and RSS fields don’t count the page tables and the task_struct of a proc; this is at least 12k of memory that is always resident. SIZE is the virtual size of the proc (code+data+stack).

Processes marked are dead processes (so-called”zombies”) that remain because their parent has not destroyed them properly. These processes will be destroyed by init(8) if the parent process exits.
PROCESS FLAGS
ALIGNWARN 001 print alignment warning msgs
STARTING 002 being created
EXITING 004 getting shut down
PTRACED 010 set if ptrace (0) has been called
TRACESYS 020 tracing system calls
FORKNOEXEC 040 forked but didn’t exec
SUPERPRIV 100 used super-user privileges
DUMPCORE 200 dumped core
SIGNALED 400 killed by a signal

PROCESS STATE CODES
D uninterruptible sleep (usually IO)
R runnable (on run queue)
S sleeping
T traced or stopped
Z a defunct (“zombie”) process

For BSD formats and when the “stat” keyword is used,
additional letters may be displayed:
W has no resident pages
< high-priority process N low-priority task L has pages locked into memory (for real-time and custom IO) ENVIRONMENT VARIABLES and PERSONALITY (posix,linux,bsd,sun) Examples: List every process on the system using standard syntax: ps -e List every process on the system using BSD syntax: ps ax List the top 10 CPU users. ps -e -o pcpu -o pid -o user -o args | sort -k 1 | tail -21r List every process except those running as root (real & effective ID) ps -U root -u root -N List every process with a user-defined format: ps -eo pid,tt,user,fname,tmout,f,wchan Odd display with AIX field descriptors: ps -o "%u : %U : %p : %a" Print only the process IDs of syslogd: ps -C syslogd -o pid= When displaying multiple fields, part of the output may be truncated, to avoid this supply a width to the arguments: ps -e -o user:20,args. Since ps cannot run faster than the system and is run as any other scheduled process, the informa

HOW DO YOU START A PROFITABLE TRADING BUSINESS? Read more NOW >>>

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!
This entry was posted in Quant Development and tagged , , , , , , , , on by .

About caustic

Hi i there My name is Bryan Downing. I am part of a company called QuantLabs.Net This is specifically a company with a high profile blog about technology, trading, financial, investment, quant, etc. It posts things on how to do job interviews with large companies like Morgan Stanley, Bloomberg, Citibank, and IBM. It also posts different unique tips and tricks on Java, C++, or C programming. It posts about different techniques in learning about Matlab and building models or strategies. There is a lot here if you are into venturing into the financial world like quant or technical analysis. It also discusses the future generation of trading and programming Specialties: C++, Java, C#, Matlab, quant, models, strategies, technical analysis, linux, windows P.S. I have been known to be the worst typist. Do not be offended by it as I like to bang stuff out and put priorty of what I do over typing. Maybe one day I can get a full time copy editor to help out. Do note I prefer videos as they are much easier to produce so check out my many video at youtube.com/quantlabs