cit 500: it fundamentals processes 1. topics 1.processors 1.operation 2.moore’s law 2.processes...
TRANSCRIPT
CIT 500: IT Fundamentals
Processes
1
Topics
1. Processors1. Operation2. Moore’s Law
2. Processes1. Lifecycle2. Virtual address space3. Context switch4. Managing processes5. 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.
VirtualizationProcesses 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 space3GB virtual memory on 32-bit architectures.High 1GB reserved for mapping kernel memory.
32-bit Process Address Space
0x08000000
0xBFFFFFFF
text
data
heap
stack
EIP
ESP
bss
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 processInitializes 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 oneLoads program text from file.Replacing old program text.New program is executed in process.
Process Tree (Solaris)
Process Termination
Voluntaryexit() system call.Compiler automatically adds to binaries.
Involuntarysignalexception
Logging In
login program1. Checks password.2. chdir to homedir.3. chown terminal to user.4. sets group IDs5. initializes env variables6. changes UID to user7. 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 youx = list processes without a ttyu = display user oriented formatw = display wider format (for long command lines)
> ps auxw|headUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1 0.0 0.0 2620 1708 ? 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 00000000 08:01 640163 /sbin/init
0805e000-0805f000 rw-p 00015000 08:01 640163 /sbin/init
0805f000-08168000 rw-p 0805f000 00:00 0 [heap]
b7db8000-b7db9000 rw-p b7db8000 00:00 0
b7db9000-b7eec000 r-xp 00000000 08:01 658668 /lib/libc-2.7.so
b7eec000-b7eed000 r--p 00133000 08:01 658668 /lib/libc-2.7.so
b7eed000-b7eef000 rw-p 00134000 08:01 658668 /lib/libc-2.7.so
b7eef000-b7ef2000 rw-p b7eef000 00:00 0
b7ef7000-b7ef9000 rw-p b7ef7000 00:00 0
b7ef9000-b7f13000 r-xp 00000000 08:01 658665 /lib/ld-2.7.so
b7f13000-b7f15000 rw-p 00019000 08:01 658665 /lib/ld-2.7.so
bfcfa000-bfd10000 rw-p bfcfa000 00:00 0 [stack]
bfffe000-bffff000 r-xp bfffe000 00:00 0
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
SchedulerScheduler
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 BoundSpend most time on computations.Example: computer algebra systems.
I/O BoundSpend most time on I/O.Example: word processor.
MixedAlternate CPU and I/O activity.Example: web browser.
Alternating CPU and I/O Bursts
Scheduling Policy
Scheduler executes policy, determining1. 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: efficiencyBut 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 slicesInteractive 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
References1. 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