1) Each process is assigned a time slide, and after that , the process context is going to be switched to another one. Time slice is very short, user normally can't detect the process context switching.
2) There is one scheduler to control and manage the process switch.
3) Each process has one priority assigned to decide the order of process running. Normally time-critical process has higher priority and run before the less-important one.
4) Each process has kernel context, data structure inside the kernel context record the process specific information.
5) load average: during given time period, how many processes are in a runnable or uninterruptable state.
terminal:
1) Run uptime, "10:51:27" is the current time, "up 57 min" indicates that the system is already up for 57 minutes. "2 users" indicates that there are 2 users logged in. "load average:" gives the number of runnable or uninteruptable processes in last 1 minute, last 5 minutes and last 15 minutes.
2) Run ps to get the list of processes currently running.
3) Start a new process with "script_1", which is one non-stopping loop.
4) After 1 minute, run the "uptime" command again. We can see that load average number is increasing since we have one more process running now.
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ uptime
10:51:27 up 57 min, 2 users, load average: 0.36, 0.84, 0.86
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ps
PID TTY TIME CMD
3069 pts/1 00:00:00 bash
3188 pts/1 00:00:00 ps
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 &
[1] 3189
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ uptime
10:52:02 up 57 min, 2 users, load average: 0.70, 0.87, 0.87
2. Listing Process
terminal
PID: process id
TIME: how long since the process is started, it indicated that script_1 is only started for 11 seconds.
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ps
PID TTY TIME CMD
3069 pts/1 00:00:00 bash
3294 pts/1 00:00:11 script_1
3297 pts/1 00:00:00 ps
3. List top intense process in system
terminal:
After running "top" command at terminal shell. We get a description of currently running process in system. The first one is our script_1(non-stopping loop), it consumes 69.9% of cpu resources.
top - 15:32:02 up 6 min, 2 users, load average: 1.68, 1.66, 0.92
Tasks: 153 total, 2 running, 151 sleeping, 0 stopped, 0 zombie
%Cpu(s): 80.4 us, 19.3 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st
KiB Mem: 3585280 total, 1122744 used, 2462536 free, 42132 buffers
KiB Swap: 3667964 total, 0 used, 3667964 free. 423488 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2822 aubinxia 20 0 5292 1072 916 R 69.9 0.0 0:31.98 script_1
2112 aubinxia 20 0 443328 197876 37128 S 15.0 5.5 1:06.06 compiz
1128 root 20 0 288992 68544 11212 S 9.5 1.9 0:37.00 Xorg
2653 aubinxia 20 0 128452 19212 12348 S 1.6 0.5 0:02.38 gnome-terminal
1838 aubinxia 20 0 38544 4452 3468 S 0.7 0.1 0:02.31 ibus-daemon
2738 aubinxia 20 0 602468 208492 43468 S 0.7 5.8 0:28.66 firefox
2825 aubinxia 20 0 5528 1408 1004 R 0.7 0.0 0:00.30 top
4 root 20 0 0 0 0 S 0.3 0.0 0:01.48 kworker/0:0
1137 root 20 0 36988 6428 3612 S 0.3 0.2 0:01.15 accounts-daemon
1881 aubinxia 20 0 125812 16840 11004 S 0.3 0.5 0:01.06 unity-panel-ser
1892 aubinxia 20 0 116480 17556 10132 S 0.3 0.5 0:01.01 ibus-ui-gtk3
2063 aubinxia 20 0 29580 5756 3236 S 0.3 0.2 0:00.46 ibus-engine-sim
4. Self-Made Top Script
owntop:
#! /bin/bash
IFS='
'
HEADFLAGS="-n 8"
PSFLAGS=aux
SLEEPFLAGS=5
HEADER="`ps $PSFLAGS | head -n 1`"
#Run the "ps aux | head -n 1" meaning that we only
#retrieve the first line of its output
#The $HEADER content is:
#USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
while true
do
clear #Clear the screen
uptime #Print out the basic process information
echo "$HEADER" #Print out the HEADER content
ps $PSFLAGS |
sed -e 1d | #Remove the first header line
sort -k3nr -k1,1 -k2n | #sort the remaining lines
head $HEADFLAGS #only retrievethe first 8 lines
#-k3nr means that sort based on 3rd column(CPU Usage), taking it as the number
#using reversed order(descending)
#-k1,1 means that sort based on first column(user name), taking it as string
#-k2n means that sort based on 2nd column(PID), taking it as number, using
#reversed order(descending)
sleep $SLEEPFLAGS
#Sleep 5 seconds and then do the looping again
#meaning it will refresh the process information for
#every 5 seconds
done
terminal:
After running owntop, it will clear up the screen and keep refreshing the process information every 5 seconds.
16:04:35 up 39 min, 2 users, load average: 1.24, 1.24, 1.14
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
aubinxia 2112 29.4 6.0 468940 216972 ? Rl 15:26 11:11 compiz
root 1128 14.6 2.0 294876 72424 tty7 Ss+ 15:25 5:39 /usr/bin/X -core :0 -seat seat0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch
aubinxia 2738 3.6 6.4 607324 233000 ? Sl 15:28 1:18 /usr/lib/firefox/firefox
aubinxia 2838 0.9 0.7 134176 26840 pts/0 Sl 15:39 0:14 emacs owntop
aubinxia 1838 0.5 0.1 38676 4452 ? Ssl 15:26 0:13 /usr/bin/ibus-daemon --daemonize --xim
aubinxia 3848 0.5 0.0 5300 1328 pts/0 S+ 16:04 0:00 /bin/bash ./owntop
aubinxia 2653 0.4 0.5 128460 21240 ? Sl 15:28 0:10 gnome-terminal
root 4 0.3 0.0 0 0 ? S 15:25 0:08 [kworker/0:0]
5. List Processes by User
pusers:
#! /bin/bash
IFS='
'
EGREPFLAGS=
#Read in the egrep flags from script arguments
while test $# -gt 0
do
if test -z "$EGREPFLAGS"
then
EGREPFLAGS="$1"
else
EGREPFLAGS="$EGREPFLAGS|$1"
fi
shift
done
#Setup the default egrep flags
if test -z "$EGREPFLAGS"
then
EGREPFLAGS="."
else
EGREPFLAGS="^ *($EGREPFLAGS)"
fi
case "`uname -s`" in
*BSD | Darwin) PSFLAGS="-a -e -o user, ucomm -x" ;;
*) PSFLAGS="-e -o user,comm" ;;
esac
echo $PSFLAGS
#Because "uname -s" returns "Linux" here
#output: -e -o user,comm
#ps $PSFLAGS
ps -e -o user,comm | #list all processes with 2 columns: user and command
sed -e 1d | head -n 10 | #remove the first line and retrieve next 10 lines
#output at this step:
#root init
#root kthreadd
#root ksoftirqd/0
#root kworker/0:0
#root kworker/0:0H
#root rcu_sched
#root rcu_bh
#root migration/0
#root watchdog/0
#root khelper
sort -b -k1,1 -k2,2 | #sort by first and 2nd column, -b: ignore trailing space
uniq -c |
#output at this step:
# 1 root init
# 1 root khelper
# 1 root ksoftirqd/0
# 1 root kthreadd
# 1 root kworker/0:0
# 1 root kworker/0:0H
# 1 root migration/0
# 1 root rcu_bh
# 1 root rcu_sched
# 1 root watchdog/0
sort -b -k2,2 -k1nr,1 -k3,3 |
awk '{
user = (LAST == $2)? "" : $2
LAST=$2
printf("%-15s\t%2d\t%s\n", user, $1, $3)
}' #Reformat each record, if user name is same as last one, ignore it.
#Final Output:
#root 1 init
# 1 khelper
# 1 ksoftirqd/0
# 1 kthreadd
# 1 kworker/0:0
# 1 kworker/0:0H
# 1 migration/0
# 1 rcu_bh
# 1 rcu_sched
# 1 watchdog/0
No comments:
Post a Comment