Transcript Processes
CIT 500: IT Fundamentals
Processes
1
Topics
1. Processors
1. Operation
2. Moore’s Law
2. Processes
1.
2.
3.
4.
5.
Lifecycle
Virtual address space
Context switch
Managing processes
Scheduling
2
The Processor
Aspects of performance
– Clock speed (GHz.)
– Instructions/clock cycle.
– Number of cores.
Architectures
– IBM Power 5
– Sun UltraSPARC T2
– Intel Xeon, Itanium2
– AMD Opteron
3
Processor Operation
• Fetch instruction from RAM.
• Decode instruction.
– Translate instruction to micro ops.
– Determine what data is operated on.
• Execute instruction.
4
Machine and assembly languages
Machine language
– Each CPU has its own.
– Even different x86 CPUs
have variants.
• MMX, SSE, VME
• cat /proc/cpuinfo
Assembly language
– Human representation
– One-to-one
correspondence of
assembly codes to
machine codes.
5
Inside the Itanium CPU
6
Moore’s Law
7
Limits of Moore’s Law
More transistors ≠ greater speed
– Adding more cores doesn’t make tasks faster if
they cannot be split into multiple processes.
Transistors can’t be smaller than atoms
– Can add transistors by using bigger dies.
– Can add transistors by going 3D.
– Can improve speed without adding transistors.
8
What is a process?
A process is a program in execution.
Program code + dynamic execution context.
Virtualization
Processes provide virtual CPU + virtual memory.
What is in a process?
A process consists of.
Program code.
Address space.
Data.
Resources: open files, signals.
At least one thread of execution.
Threads contain:
Program counter.
Stack.
Register set.
Process Lifecycle
Process Address Space
Process appears to have flat memory space.
Discontinguous areas of physical memory mapped
via page table to produce virtual memory.
Virtual memory space
3GB virtual memory on 32-bit architectures.
High 1GB reserved for mapping kernel memory.
32-bit Process Address Space
0xBFFFFFFF
stack
ESP
heap
bss
data
0x08000000
text
EIP
Translating Virtual to Physical Addrs
Virtual and Physical Addressing
15
Context Switch
When does scheduler switch out process?
Blocked on I/O.
Time slice expires.
How does it switch?
Switch to new page table.
Save hardware context to stack.
Switch stack pointers.
Load hardware context of next process from stack.
Context Switch Example
Where do processes come from?
Kernel creates some processes.
Kernel threads.
init
Processes create all other processes.
Process copies itself with fork().
Original is parent, new process is child.
Child can load its own program with exec().
Process Creation and Termination
fork() and exec()
fork() system call creates a new process
Initializes with copy of parent resources.
Creates a new address space.
Page table is copy-on-write pointer to parent.
Places child process on ready queue.
exec() system call replaces program with new one
Loads program text from file.
Replacing old program text.
New program is executed in process.
Process Tree (Solaris)
Process Termination
Voluntary
exit() system call.
Compiler automatically adds to binaries.
Involuntary
signal
exception
Logging In
login program
1.
2.
3.
4.
5.
6.
7.
Checks password.
chdir to homedir.
chown terminal to user.
sets group IDs
initializes env variables
changes UID to user
execs login shell
Processes and the Shell
Shells forks and execs each command.
– Except for shell built-ins like cd.
– Including each command in a pipeline.
Shell waits for process to terminate.
– Except if you end command line with &.
– The jobs command will show processes started
by the current shell.
Slide #24
Managing Processes: bg and fg
Ctrl-z will suspend the current process.
– Sends a SIGSTOP to the process.
The bg command will background a process.
– Still running, as if started with an & at end of line.
– Use %NUM for job number arguments.
– Use NUM for process ID arguments.
The fg command will foreground a process.
– Shell will wait for process to terminate.
– Same arguments as bg.
Slide #25
Background Execution Techniques
• Run the command with & at the end of line.
– Remember output will still come to shell.
– May want to redirect both STDOUT and STDERR.
• Use ctrl-z to suspend current foreground job,
then use bg to restart in background.
– Remember output will still come to shell.
26
Subshells
A separate instance of your command shell.
Commands in parentheses execute in subshell.
Execute commands in temp environment.
(cd /; pwd; ls)
Combine output of commands to pipeline.
(cat smallFile; echo -e "\n\n-----\n\n"; cat
mediumFile) | less
Run multiple commands in background.
(cp /boot/vm* kernel; bzip2 -9 kernel) &
27
The ps command
ps – show current process
a = list processes from all users, not just you
x = list processes without a tty
u = display user oriented format
w = display wider format (for long command lines)
> ps auxw|head
USER
PID %CPU %MEM VSZ RSS TTY
root
1 0.0
0.0 2620 1708 ?
STAT START TIME COMMAND
S Sep08 0:00 /sbin/init
28
The ps command
Time (TIME)
– CPU time used, not wall clock time.
Resident Set Size (RSS)
– Amount of physical memory currently used.
Virtual Memory Size (VSZ)
– Amount of virtual memory currently used.
– Some of this memory is shared libraries, which
– is space shared by multiple processes.
29
Shared Libraries
Shared libraries contain commonly used code
– Filesystem access
– Data compression
– Network access
Two types of linking
– Static: shared library incorporated in program
code, programs works even if lib not installed.
– Dynamic: shared library loaded at runtime, saves
memory since only one copy loaded on system.
30
Shared Library Use
> ldd /bin/cp
linux-gate.so.1 => (0xbfffe000)
libselinux.so.1 => /lib/libselinux.so.1 (0xb7f76000)
libacl.so.1 => /lib/libacl.so.1 (0xb7f6f000)
libc.so.6 => /lib/libc.so.6 (0xb7e35000)
libdl.so.2 => /lib/libdl.so.2 (0xb7e31000)
/lib/ld-linux.so.2 (0xb7f97000)
libattr.so.1 => /lib/libattr.so.1 (0xb7e2d000)
31
Process Memory Use
> cat /proc/1/maps
08048000-0805e000 r-xp
0805e000-0805f000 rw-p
0805f000-08168000 rw-p
b7db8000-b7db9000 rw-p
b7db9000-b7eec000 r-xp
b7eec000-b7eed000 r--p
b7eed000-b7eef000 rw-p
b7eef000-b7ef2000 rw-p
b7ef7000-b7ef9000 rw-p
b7ef9000-b7f13000 r-xp
b7f13000-b7f15000 rw-p
bfcfa000-bfd10000 rw-p
bfffe000-bffff000 r-xp
00000000
00015000
0805f000
b7db8000
00000000
00133000
00134000
b7eef000
b7ef7000
00000000
00019000
bfcfa000
bfffe000
08:01
08:01
00:00
00:00
08:01
08:01
08:01
00:00
00:00
08:01
08:01
00:00
00:00
640163
640163
0
0
658668
658668
658668
0
0
658665
658665
0
0
/sbin/init
/sbin/init
[heap]
/lib/libc-2.7.so
/lib/libc-2.7.so
/lib/libc-2.7.so
/lib/ld-2.7.so
/lib/ld-2.7.so
[stack]
32
Signals
Software interrupts used for process communication.
–
–
–
–
SIGALRM: alarm() timer has gone off.
SIGHUP: sends signal to re-read config file.
SIGINT: interrupt signal generated by Ctrl-c.
SIGSEGV: segmentation violation sent by kernel when
process accesses invalid address.
– SIGSTOP: suspends process (ctrl-z) until SIGCONT.
– SIGKILL, SIGTERM: terminates a process.
Process can either
– Ignore the signal: SIGKILL/STOP cannot be ignored.
– Catch the signal: Create a signal handler function and
register it with the kernel to be called on signal.
Sending signals with kill
kill [-l] [-signal] PID …
Sends specified signal to specified PIDs.
Send SIGTERM if no signal specified.
Signals can only be sent to your processes.
[-l] List available signals instead of sending.
34
Scheduler
Scheduler
Part of operating system.
Selects a process to run and allocates CPU to it.
Provides semblence of multitasking on single CPU.
Scheduler is invoked when:
Process blocks on an I/O operation.
A hardware interrupt occurs.
Process time slice expires.
Types of Processes
CPU Bound
Spend most time on computations.
Example: computer algebra systems.
I/O Bound
Spend most time on I/O.
Example: word processor.
Mixed
Alternate CPU and I/O activity.
Example: web browser.
Alternating CPU and I/O Bursts
Scheduling Policy
Scheduler executes policy, determining
1. When threads can execute.
2. How long threads can execute.
3. Where threads can execute.
Scheduling Policy Goals
Efficiency
– Maximize amount of work accomplished.
Interactivity
– Respond as quickly as possible to user.
Fairness
– Don’t allow any process to starve.
Which goal is most important?
Depends on the target audience:
Desktop: interactivity
But kernel shouldn’t spend all time context switch.
Server: efficiency
But should offer interactivity in order to serve
multiple users.
Static and Dynamic Priorities
Initial priority value called the nice value.
Set via the nice or renice command.
Static priority is nice value + 120.
Ranges from 100 (highest) to 139 (lowest).
Scheduling based on dynamic priority.
Bonuses and penalties according to interactivity.
Time Slices
Time slice duration critical to performance.
Too short: high overhead from context switches.
Too long: loss of apparent multitasking.
Time slices are 100ms on average.
Interactive processes and time slices
Interactive processes have high priority.
Pre-empt CPU bound tasks on kbd/ptr interrupts.
Long time slices cause slow start of new tasks.
Scheduler Interrupts
• Scheduler interrupt
– Invoked every 1-10ms (depends on cfg) by timer interrupt.
• Decrements task’s time slice.
• If a higher priority task exists,
– Higher priority task is given CPU.
– Current task remains in TASK_RUNNING state.
• If time slice expired,
– Process moved to expired priority array.
Changing nice values
nice –n val command
– Start command and add n to nice value.
– Nice ranges from -20 (highest pri) to 19 (lowest.)
– Only root can use negative values.
renice pri –p PID
– Change nice value for specified process.
– Can only do for your own processes.
– Can only reduce the priority of your processes, i.e.
– can only use positive values unless you are root.
44
References
1. Daniel P. Bovet and Marco Cesati, Understanding
the Linux Kernel, 3rd edition, O’Reilly, 2005.
2. Avi Silberchatz et. al., Operating System Concepts,
7th edition, 2004.
3. W. Richard Stevens and Stephen Rago, Advanced
Programming in the UNIX Environment, 2nd edition,
2005.
4. Nicholas Wells, The Complete Guide to Linux System
Administration, Thomson Course Technology, 2005.
45