Week 3

advertisement
Week 3
COMP3231 Operating Systems
2005/S2
C ONTEXT S WITCHING
➜ Threads and Processes
Slide 1
• What is a process? What is a thread?
Slide 3
• OS services to support processes, threads
➜ User-level operation sigsetjmp saves context and the set of
blocked signals
➜ siglongjmp restores the environment (and jumps to the
instruction the saved pc is pointing to)
• Thread switching
• Kernel level versus user level threads
• Threads in SVR4 Unix, Linux and Windows 2000
➜ Concurrency control
• Mutual exclusion (software, hardware, OS)
T HREADS
P REEMPTION
Do threads need OS support or is it possible to implement
them as user-level library?
Slide 2
AND
S CHEDULING
➜ System call signal allows the user to install alternative signal
handler for some signals (timer)
We need four features to implement multi-threading:
Slide 4
➀ Context switching
➜ alarm sets the timer signal.
➜ Can be used to implement preemption and scheduling: timer
signal activates scheduler, which picks next thread, sets alarm
clock, then activates thread.
➁ Preemption
➂ Scheduling
➃ Handling of blocking system calls
C ONTEXT S WITCHING
1
B LOCKING S YSTEM CALLS
2
Thread
Process
B LOCKING S YSTEM CALLS
Slide 5
➜ wrapper around each potentially blocking system call
Slide 7
➜ introduces fairly high overhead
Kernel
space
Kernel
Kernel
Run-time
system
Thread
table
Process
table
U SER - LEVEL T HREADS
U SER - LEVEL T HREADS
VS
Process
table
Thread
table
K ERNEL L EVEL T HREADS
✔ Scheduling policy tailored to specific application
➜ All thread management is done by the application (library)
✔ Extremely low overhead
➜ Runs in usermode
➜ The kernel is not aware of the existence of threads
Slide 8
• User-level threads are not scheduled by the kernel
• Pure application/library level construct
➜ Used to enhance modularisation
✘ Process blocks if one of its threads blocks
- for system calls, it can be avoided by using wrapper
functions for all possibly blocking operations, introduces
extra system calls
- process blocks on page fault
✘ No inter-process parallelism possible on machines with multiple
CPUs
➜ Also called co-routines
U SER - LEVEL T HREADS
Thread
User
space
➜ page faults can still lead to blocking of whole process
Slide 6
Process
3
C OMBINED A PPROACHES
4
E XECUTION C ONTEXT
O PERATING S YSTEM
Non-process Kernel:
C OMBINED A PPROACHES
➜ Execute kernel outside of any process context
Try to get best of both worlds!
➜ Separate context for execution of OS code
➜ Library offers (user-level) thread interface
Slide 9
OF THE
Slide 11
➜ OS supports kernel-level threads
• OS context may be automatically switched by hardware
➜ Traditional model
➜ Thread library provides API for binding one or more user-level
threads to a kernel-level thread
P1
P2
¥ ¥ ¥
Pn
➜ Most thread management done explicitly at user level
Kernel
(a) Separate kernel
E XAMPLE : S OLARIS T HREAD A RCHITECTURE
Process 1
Process 2
Process 3
Process 4
P1
P2
POS1
POS2
Functions
Process 5
Functions
Pn
¥ ¥ ¥
¥ ¥ ¥
OS
P
n
Functions
Execution Within User
Processes:
Process
Switching
Kernel Functions
➜ OS software executes within context of a user process
Threads
Library
User
L
L
L
L
L
L
L
L
➜ Process in privileged mode while executing OS code
L
(b) OS functions execute within user processes
kernel
• has access(a)
to Separate
additional
(kernel) memory
Slide 10
Slide 12
Kernel
Hardware
User-level thread
P
P
P
P
➜ E.g: UNIX
P1
P1
OS
Functions
P
P2
Pn
OS1
¥ ¥ ¥
¥ ¥ ¥
P2
Pn
Process Switching Functions
OS
Functions
¥ ¥ ¥
OSk
OS
Functions
(c)Process
OS functions
execute
as separate processes
Switching
Functions
Kernel-level thread
L Light-weight Process
P
Processor
(b) OS functions execute within user processes
‘
P1
E XECUTION C ONTEXT OF THE O PERATING S YSTEM
5
P2
¥ ¥ ¥
Pn
OS1
¥ ¥ ¥
E XECUTION C ONTEXT OF THE O PERATING
S YSTEM Functions
Process Switching
(c) OS functions execute as separate processes
OSk
6
P1
P2
¥ ¥ ¥
Pn
Kernel
(a) Separate kernel
Typical Address Space Layout (UNIX):
P1
P2
Pn
Execution Within
Separate
Process(es):
OS
OS
OS
➜ Process-based
(server-based)
OS
FuncFunc¥ ¥ ¥ Functions
tionsfor major OStions
➜ Separate
process
functions
code
(text)
Process
Switching Functions
➜ Clients use
message-passing
IPC to invoke services
bss
DLLs
stack
kernel
0
• Aids distribution
Slide 13
data
Slide 15
➜ OS processes
may execute
unprivileged
(user) mode
(b) OS functions
executeinwithin
user processes
➜ E.g: Windows-2000
max
➜ 0-th page typically not used
➜ text segment is read-only
➜ data segment is initialised data (part R/O)
➜ bss segment is uninitialised data (heap), can grow
P1
P2
¥ ¥ ¥
Pn
OS1
¥ ¥ ¥
OSk
➜ shared libraries (DLLs) allocated in free middle region
➜ stack at top of user space, grows downward
Process Switching Functions
➜ kernel space is in reserved (shared) region
(c) OS functions execute as separate processes
UNIX process states (single-threaded):
fork
UNIX SVR4 P ROCESS M ANAGEMENT
➜ Mostly follows in-process model
Created
➜ In addition has “kernel processes” (daemons)
Preempted
➜ Several parts of kernel data:
Slide 14
return
to user
• user-level context (text, data, stack)
Slide 16
• register context
User
Running
preempt
swap out
return
• system-level context
system call,
interrupt
– process table entry:
∗ global table, always entirely accessible by kernel
∗ process state, IDs, prio, links to other data
– “U area”: process resources
interrupt,
interrupt return
reschedule
process
7
Ready to Run
In Memory
swap in
Ready to Run
Swapped
Kernel
Running
sleep
wakeup
wakeup
exit
Zombie
UNIX SVR4 P ROCESS M ANAGEMENT
not enough memory
(swapping system only)
enough
memory
UNIX SVR4 P ROCESS M ANAGEMENT
Asleep in
Memory
swap out
Sleep,
Swapped
8
Threads in Linux:
➜ Linux has no real threads (with reduced context)
Main process system calls:
Slide 17
➜ Provides clone() system call
fork()
Creates process with copy of parent’s process image
• generalisation of Unix fork()
exec()
Replaces image of calling process with new executable file
• parent and child can share (part of) address space
exit()
Terminate calling process. Return exit status to parent
wait()
Wait for (specific or any) child to terminate. Collect
exit status
kill()
• creates new process
Slide 19
• effectively a shortcut for fork(); mmap()
• child has complete process context
• LinuxThreads lib implemented using clone
➜ PThreads package provides user-level threads
• can be bound to Linux “threads”
Sends signal to process. Default action for many signals is to kill recipient, but may install signal handler
• gives some approximation of lightweight threads
• similar to Solaris
– but Solaris’ “lightweight processes” are faster
Typical use:
Slide 18
L INUX clone SYSTEM
#include <stdio.h>
int pid;
pid = fork();
if (pid < 0) {
perror("fork() failed");
exit(1);
/* We’re the parent! */
} else if (pid > 0)
printf("child PID=%d\n", pid);
} else {
/* We’re the child! */
execve(file);
sprintf(STDERR, "Exec failed!");
exit(1);
}
UNIX SVR4 P ROCESS M ANAGEMENT
CALL
➜ CLONE PARENT: (Linux 2.4 onwards) parent of new task same as
parent of caller
➜ CLONE FS: share file system information
➜ CLONE FILES: share file descriptor table
Slide 20
➜ CLONE SIGHAND: share table of signal handler
➜ CLONE VFORK: parent suspended until child releases vm resources
(exit(), execve())
➜ CLONE VM: parent and child run in the same memory space
➜ CLONE THREAD: (Linux 2.4 onwards) parent and child share the
same thread id
9
T HE T HREAD AND P ROCESS M ODEL IN W INDOWS 2000
10
T HE T HREAD
AND
P ROCESS M ODEL
IN
W INDOWS 2000
Four important concepts in Windows 2000:
➜ Jobs
• collection of processes bundled together
• share quotas: max. number of processes, CPU time, memory
usage, security restrictions
Slide 21
➜ Processes
• unit of resource ownership
➜ Threads
• units visible to scheduler
➜ Fibres
• user-level threads
• created using Win32 API calls, which do not trigger a system
call
Job
Thread
Address
space
Process
User
stack
Slide 22
Process
handle
table
Kernel mode thread stack
P
T T
Access token
T T
T HE T HREAD AND P ROCESS M ODEL IN W INDOWS 2000
P
11
Download