25 September 2008CIS 340 # 1 Last Covered Sophistication of OS control into process control.
-
Upload
millicent-ferguson -
Category
Documents
-
view
216 -
download
0
Transcript of 25 September 2008CIS 340 # 1 Last Covered Sophistication of OS control into process control.
25 September 2008 CIS 340# 1
Last Covered• Sophistication of OS control into
process control
25 September 2008 CIS 340# 2
ConsiderThe underlying system structure of an operating
system is envisioned as a series of levels. Which of the following is NOT TRUE about these levels?
a) Each level performs a set of related functions
b) Each level is dependent upon the next lower level to perform a more primitive set of functions
c) The lowest level of interactions deals directly with the hardware
d) The lowest level of interactions deals with handling multitasking/multiprogramming functions
e) None of the above
25 September 2008 CIS 340# 3
Topics• To review your Project 1
(continuing)
• To understand the operating system• Basic responsibilities• Standard enhancements
– Process management» tasks, roles, versions
– Thread distribution– Interrupt-handling
• Distributed processing support– Communication aspects
25 September 2008 CIS 340# 4
OS Key Elements
25 September 2008 CIS 340# 5
Process
A program in executionThree components
1. An executable program
2. Associated data needed by the program
3. Execution contextcontext of the program
– All information needed to manage the process» Program counter» Stack» Data section
25 September 2008 CIS 340# 6
State Diagram of a Process: 5 States
What is the structure of a process?
25 September 2008 CIS 340# 7
Process Control Block (PCB): Data Structure
Holds information associated with each process
One for each process
1. Process state
2. Program counter
3. CPU registers
4. CPU scheduling information
5. Memory-management information
6. Accounting information
7. I/O status information
25 September 2008 CIS 340# 8
Process Control Information:Scheduling & State Information via PCB
Information needed by the operating system to perform its scheduling function
•Process state: ready, waiting ….
•Priority: Attribute describing scheduling priority
default, current, highest-allowable
•Scheduling-related information: Relative to scheduling algorithm used
duration delayed, duration last executed
•Event: Identity of event process needs to resume execution
25 September 2008 CIS 340# 9
EX: CPU Switch From Process to Process
How are processes handled?
25 September 2008 CIS 340# 10
Process Scheduling == Queue Handling
Achieved by process migration among queues
• Job queue
Set of all processes in the system
• Ready queue
Set of all processes residing in main memory
Ready and waiting to execute
• Device queues
Set of processes waiting for an I/O device
25 September 2008 CIS 340# 11
Consider
A process is ____
a) Equivalent to a program
b) A queue to be controlled
c) A program in execution
d) Decomposable into segments
e) None of the above
25 September 2008 CIS 340# 12
EX: Process Scheduling Pathways
25 September 2008 CIS 340# 13
EX: Ready Queue And Various I/O Device Queues
25 September 2008 CIS 340# 14
SchedulersLong-term scheduler
Aká job scheduler
Selects processes to enter the ready queue
Controls the degree of multiprogramming
Invoked very infrequently (seconds, minutes) may be slower
Short-term scheduler
Aká CPU scheduler
Selects process to be executed next
Allocates CPU to the processInvoked very frequently (milliseconds)
must be fast
25 September 2008 CIS 340# 15
FYI: Addition of Medium Term Scheduling
25 September 2008 CIS 340# 16
Context Switch• When CPU switches to another process• State of the “old process” saved • Load the saved state of the “new process”• “out with the old, in with the new”
• Time dedicated to switching is overhead • No “useful work” while switching
• No program execution
• Hardware speedsHardware speeds influence time to provide the context switch effort
Def:Def:
Is process manipulation
costly?
25 September 2008 CIS 340# 17
Process Creation• Parent process creates children processes
In turn create other processes Tree of processes
• Resource sharing POSSIBILITIES– Parent and children share all resources
– Children share subset of parent’s resources– Parent and child share no resources
• Execution POSSIBILITIES– Parent and children execute concurrently
– Parent waits until children terminate
• Address space POSSIBILITIES– Child duplicates parent
– Child has a distinct program
25 September 2008 CIS 340# 18
EX: Process Creation: UNIX
fork – System call creating new process
exec – System call used after a fork– Replaces the process’s memory
space with a new program
25 September 2008 CIS 340# 19
Process Termination• Process executes last statement
– Requests operating system to delete
exit system call– Output / “returned data” from child to parent
wait system call– Process’ resources are deallocated by operating system
• Parent may terminate execution of children processes
abort system call– Child has exceeded allocated resources– Task assigned to child is no longer required– Parent is exiting
• If a parent terminates, children must terminate• Cascading termination
25 September 2008 CIS 340# 20
Concurrent Processes: Two Types1. Independent
– Cannot affect or be affected by other processes executing in the system
– If does not share data2. Cooperating
– Can affect or be affected by the execution of another process
Advantages (process cooperation)– Information sharing
e.g. Access to same file
– Computation speed-upe.g. Some form of parallelism
– Modularitye.g. subunits of processes or threads
– ConvenienceAllows user to multitask
Def:Def:
25 September 2008 CIS 340# 21
EX: Producer-Consumer Problem
Paradigm for cooperating processes
• producer process produces information
• consumer process consumes it
• unbounded-buffer places no practical limit on
buffer size
• bounded-buffer assumes a fixed buffer size
25 September 2008 CIS 340# 22
Bounded-Buffer – Shared-Memory Solution
• Shared data#define BUFFER_SIZE 10
Typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
• When in == out :: buffer empty• in next free position• out first full position• Solution is correct, but can only use
BUFFER_SIZE-1 elements
in
out
buffer
2
3
4
5
6
7
8
9
1
0
25 September 2008 CIS 340# 23
in
outH
buffer
2
3
4
5
6
7
8
9
1
0
nextProduced H
25 September 2008 CIS 340# 24
in
outH
buffer
2
3
4
5
6
7
8
9
1
0
nextProduced H
25 September 2008 CIS 340# 25
in
out
A
D
F
G
B
C
K
J
H
buffer
2
3
4
5
6
7
8
9
1
0
25 September 2008 CIS 340# 26
Bounded-Buffer – Producer Process
when
out in in+1 (in+1) % BUFFER_SIZE
0 0 1 1%10 1
0 2 3 3%10 3
0 9 10 10%10 0
item nextProduced; /* local */
while (1) {
while (((in + 1) % BUFFER_SIZE) = = out)
; /* do nothing; cannot store */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE; /* moves ptr */
}
Will
anything be in
buffer[9] – assume nothing leaves ?
25 September 2008 CIS 340# 27
Bounded-Buffer – Consumer Process
item nextConsumed; /* local */while (1) {
while (in == out); /* do nothing; nothing in the queue
*/nextConsumed = buffer[out];out = (out + 1) % BUFFER_SIZE; /* moves ptr
*/}
when
out in out+1 (out+1) % BUFFER_SIZE
0 0 1 1%10 1
0 2 3 3%10 3
0 9 10 10%10 0
25 September 2008 CIS 340# 28
in
out
A
D
F
G
B
C
K
J
H
buffer
2
3
4
5
6
7
8
9
1
0
nextConsumed
25 September 2008 CIS 340# 29
in
out
A
D
F
G
B
C
K
J
buffer
2
3
4
5
6
7
8
9
1
0
nextConsumed
25 September 2008 CIS 340# 30
in
out
A
D
F
G
B
C
K
J
buffer
2
3
4
5
6
7
8
9
1
0
nextConsumed H
25 September 2008 CIS 340# 31
in
out
A
D
F
G
B
C
K
J
buffer
2
3
4
5
6
7
8
9
1
0
nextConsumed H
25 September 2008 CIS 340# 32
What’s Next? Roles for ProcessesRoles for Processes
?Reflections
25 September 2008 CIS 340# 33
REFLECT:Problems Arising with System Software
1. Improper synchronization
– Ensure a process waiting for an I/O device receives the signal
2. Failed mutual exclusion
– Memory overwrites
3. Nondeterminate program operation– Program A should only depend on
input to it
– Activities of other programs -- Program B, C... Z -- should not influence Program A’s outcome
4. Deadlocks
25 September 2008 CIS 340# 34
Modern Operating Systems:Multithreading
Process divided into threads – Thread
– Dispatchable unit of work– Each executes sequentially– Each is interruptable– Multiple ones may work
concurrently– Process
– A collection of one or more threads
Def:Def:
Threads vs. Processes• Both threads and processes are methods of parallelizing an application.
• Processes
– are independent execution units
• contain their own state information
• use their own address spaces,
– only interact with each other via interprocess communication mechanisms (generally managed by the operating system).
– applications are typically divided into processes during the design phase,
• a master process explicitly spawns sub-processes when it makes sense to logically separate significant application functionality.
– processes are an architectural construct.
• Thread
– is a coding construct that doesn't affect the architecture of an application.
– all threads within a process share the same state and same memory space,
• can communicate with each other directly,
• because they share the same variables.
25 September 2008 CIS 340# 35
Threads vs. Processes• A single process might contains multiple threads;
• Threads
– typically are spawned for a short-term benefit
– is usually visualized as a serial task
– doesn't have to be performed in a linear manner (such as performing a complex mathematical computation using parallelism, or initializing a large matrix), and then are absorbed when no longer required.
• The scope of a thread is within a specific code module
• http://www.cafeaulait.org/course/
25 September 2008 CIS 340# 36
25 September 2008 CIS 340# 37
Process • Have a virtual address space • Holds the process image/statusHandled? Protected access to processors,
other processes, files, and I/O resources
• Has an execution state e.g. running, ready
• Saved thread context when not running• Has an execution stack• Some per-thread static storage for local
variables• Access to the memory and resources of its Access to the memory and resources of its
processprocess– All threads of a process share process
Thread
25 September 2008 CIS 340# 38
Single and Multithreaded Processes
25 September 2008 CIS 340# 39
Thread DynamicsSuspending a process • All threads of the process suspended
WHY? All threads share the same address space
Termination of a process• All threads within the process terminated
States – Spawn thread Spawn another
– Block– Unblock
– Finish Deallocate register context and stacks
25 September 2008 CIS 340# 40
OS Varieties
MS-DOS • Supports a single thread
UNIX • Supports multiple user processes• Supports only one thread per process
Windows 2000, Solaris, Linux, Mach, OS/2
• Support multiple threads
25 September 2008 CIS 340# 41
Why Add the Complexity?Greater responsiveness
• Takes less time to create a new thread than a process
• Less time to terminate a thread than a process• Less time to switch between two threads within the
same process
Greater resource sharing• Threads within the same process share memory
and files,• Can communicate with each other without invoking
the kernel• Hence passage to kernel avoided
Economy
Utilization of multiprocessor architectures
25 September 2008 CIS 340# 42
Multithreading Models: How Does User Process Work with Kernel Process
Many-to-One• Many user-level threads mapped to single kernel thread
• Option if system does not support kernel threads
One-to-One• Each user-level thread maps to kernel thread
EX: Windows 95/98/NT/2000- OS/2
Many-to-Many• Enables many user level threads to be mapped to many
kernel threads• Allows the operating system to create a sufficient
number of kernel threadsEX: Solaris 2
Windows NT/2000 with ThreadFiber package
25 September 2008 CIS 340# 43
Consider
The information needed by the operating system
to record the status of a process in order to continue with the execution of a process at
a later time is maintained in the ___.
a) Process control block
b) Process memory queue
c) Process buffer socket
d) Process switch block
e) Process cache
25 September 2008 CIS 340# 44
Complexity of Threading: Issues
Each OS designed to handle the points• Semantics of fork() and exec()
system calls• Thread cancellation• Signal handling• Thread pools• Thread specific data
25 September 2008 CIS 340# 45
User-Level Threads
• W2000, Linux, and OS/2
• Scheduling is done on a thread basis
NOTE!!! Kernel maintains context information
for the process and the threads
Kernel-Level Threads
Combined Strategy
• All thread management is done by layer “above” / “outside” kernel
NOTE!!! Kernel is not aware of the existence of
threads
• Solaris• User space handles:
• Thread creation • Bulk of scheduling
• Synchronization of threads
25 September 2008 CIS 340# 46
EX: Windows 2000 Threads
• One-to-one mapping
• Each thread contains thread id register set separate user and kernel stacks private data storage area
25 September 2008 CIS 340# 47
EX: Linux Threads
• Referred as taskstasks • Thread creation
system call clone()
clone() • child task to shares the address
space of the parent task (process)
25 September 2008 CIS 340# 48
EX: Solaris 2 Threads
light-weight process: A single-threaded sub-process which, unlike a thread, has its own process identifier and may also differ in its inheritance and controlling features.
Def:Def:
Solaris 2 Process
25 September 2008 CIS 340# 49
Different Thread Realizations
25 September 2008 CIS 340# 50
Can you answer? Threads / Processes
• How are they the same?
• How are they different?
25 September 2008 CIS 340# 51
What’s Next? InterruptsInterrupts
?Reflections
25 September 2008 CIS 340# 53
Migration of OS Concepts and Features through Platforms
Batch
TIME SHARE
25 September 2008 CIS 340# 54
Many-to-One Model
25 September 2008 CIS 340# 55
One-to-one Model
25 September 2008 CIS 340# 56
Many-to-Many Model