117
Linux Development Lecture 8 11.02.2016

Linux Development Lecture 8 11.02.2016

Embed Size (px)

DESCRIPTION

Schedule Multiprocessing Threads

Citation preview

Page 1: Linux Development Lecture 8 11.02.2016

Linux DevelopmentLecture 8

11.02.2016

Page 2: Linux Development Lecture 8 11.02.2016

Schedule• Multiprocessing• Threads

Page 3: Linux Development Lecture 8 11.02.2016

Multiprocessing

Page 4: Linux Development Lecture 8 11.02.2016

Uniprogramming

Page 5: Linux Development Lecture 8 11.02.2016

Multiprogramming

Page 6: Linux Development Lecture 8 11.02.2016

Multiprogramming

Page 7: Linux Development Lecture 8 11.02.2016

Batch multiprogramming vs time sharing

Page 8: Linux Development Lecture 8 11.02.2016

Process• A program in execution• An instance of a program running on a computer• The entity that can be assigned to and executed on a

processor

Page 9: Linux Development Lecture 8 11.02.2016

Process• A unit of activity characterized by• A single sequential thread of execution• A current state• An associated set of system resources

Page 10: Linux Development Lecture 8 11.02.2016

Problems when designing software• Improper synchronization• Failed mutual exclusion• Nondeterminate program operation• Deadlocks

Page 11: Linux Development Lecture 8 11.02.2016

Process• Consists of three components• An executable program• Associated data needed by the program• Execution context of the program

• All information the operating system needs to manage the process

Page 12: Linux Development Lecture 8 11.02.2016

Process

Page 13: Linux Development Lecture 8 11.02.2016

Memory management• Process isolation• Automatic allocation and management• Support of modular programming• Protection and access control• Long-term storage

Page 14: Linux Development Lecture 8 11.02.2016

Virtual memory• Implements long-term store• Information stored in named objects called files• Allows programmers to address memory from a logical

point of view

Page 15: Linux Development Lecture 8 11.02.2016

Paging• Allows process to be comprised of a number of fixed-

size blocks, called pages• Virtual address is a page number and an offset within

the page• Each page may be located anywhere in main memory• Real address or physical address is the main memory

address

Page 16: Linux Development Lecture 8 11.02.2016

Virtual memory

Page 17: Linux Development Lecture 8 11.02.2016

Virtual memory addressing

Page 18: Linux Development Lecture 8 11.02.2016

Scheduling and resource management• Fairness• Give equal and fair access to resources

• Differential responsiveness• Discriminate among different classes of jobs

• Efficiency• Maximize throughput, minimize response time, and

accommodate as many uses as possible

Page 19: Linux Development Lecture 8 11.02.2016

Key elements of OS

Page 20: Linux Development Lecture 8 11.02.2016

System structure• View the system as a series of levels• Each level performs a related subset of functions• Each level relies on the next lower level to perform

more primitive functions• This decomposes a problem into a number of more

manageable subproblems

Page 21: Linux Development Lecture 8 11.02.2016

Levels• Level 1• Electronic circuits• Objects are registers, memory cells, and logic gates• Operations are clearing a register or reading a memory

location• Level 2• Processor’s instruction set• Operations such as add, subtract, load, and store

Page 22: Linux Development Lecture 8 11.02.2016

Levels• Level 3• Adds the concept of a procedure or subroutine, plus call/return

operations• Level 4• Interrupts

Page 23: Linux Development Lecture 8 11.02.2016

Levels – Concepts related to multiprogramming• Level 5• Process as a program in execution• Suspend and resume processes

• Level 6• Secondary storage devices• Transfer of blocks of data

Page 24: Linux Development Lecture 8 11.02.2016

Levels – Concepts related to multiprogramming• Level 7• Creates logical address space for processes• Organizes virtual address space into blocks

Page 25: Linux Development Lecture 8 11.02.2016

Deal with external objects• Level 8• Communication of information and messages between

processes• Level 9• Supports long-term storage of named files

• Level 10• Provides access to external devices using standardized

interfaces

Page 26: Linux Development Lecture 8 11.02.2016

Deal with external objects• Level 11• Responsible for maintaining the association between the

external and internal identifiers• Level 12• Provides full-featured facility for the support of processes

• Level 13• Provides an interface to the OS for the user

Page 27: Linux Development Lecture 8 11.02.2016

Modern OS• Microkernel architecture• Assigns only a few essential functions to the kernel

• Address spaces• Interprocess communication (IPC)• Basic scheduling

Page 28: Linux Development Lecture 8 11.02.2016

Modern OS• Multithreading• Process is divided into threads that can run concurrently

• Thread• Dispatchable unit of work• executes sequentially and is interruptable

• Process is a collection of one or more threads

Page 29: Linux Development Lecture 8 11.02.2016

Modern OS• Symmetric multiprocessing (SMP)• There are multiple processors• These processors share same main memory and I/O facilities• All processors can perform the same functions

Page 30: Linux Development Lecture 8 11.02.2016

Multiprogramming and multiprocessing

Page 31: Linux Development Lecture 8 11.02.2016

Modern OS• Distributed operating systems• Provides the illusion of a single main memory space and single

secondary memory space• Object-oriented design• Used for adding modular extensions to a small kernel• Enables programmers to customize an operating system

without disrupting system integrity

Page 32: Linux Development Lecture 8 11.02.2016

Linux Kernel Process handling

Page 33: Linux Development Lecture 8 11.02.2016

Process description and control

Page 34: Linux Development Lecture 8 11.02.2016

OS Requirements• Fundamental Task: Process Management• The Operating System must• Interleave the execution of multiple processes• Allocate resources to processes, and protect the resources of

each process from other processes, • Enable processes to share and exchange information, • Enable synchronization among processes.

Page 35: Linux Development Lecture 8 11.02.2016

Concepts• Computer platforms consists of a collection of hardware

resources• Computer applications are developed to perform some

task• It is inefficient for applications to be written directly for

a given hardware platform• OS provides an interface for applications to use

• OS provides a representation of resources that can be requested and accessed by application

Page 36: Linux Development Lecture 8 11.02.2016

The OS manages execution of apps• Resources are made available to multiple applications• The processor is switched among multiple application• The processor and I/O devices can be used efficiently

Page 37: Linux Development Lecture 8 11.02.2016

Process• A program in execution• An instance of a program running on a computer• The entity that can be assigned to and executed on a

processor• A unit of activity characterized by the execution of a

sequence of instructions, a current state, and an associated set of system instructions

Page 38: Linux Development Lecture 8 11.02.2016

Process elements• A process is comprised of:• Program code (possibly shared)• A set of data• A number of attributes describing the state of the process

Page 39: Linux Development Lecture 8 11.02.2016

Process elements• While the process is running it has a number of

elements including• Identifier• State• Priority• Program counter• Memory pointers• Context data• I/O status information• Accounting information

Page 40: Linux Development Lecture 8 11.02.2016

Process control block• Contains the process elements• Created and manage by the operating system• Allows support for multiple processes

Page 41: Linux Development Lecture 8 11.02.2016

Trace of the process• The behavior of an individual process is shown by listing

the sequence of instructions that are executed• This list is called a Trace• Dispatcher is a small program which switches the

processor from one process to another

Page 42: Linux Development Lecture 8 11.02.2016

Process execution• Consider three processes being executed• All are in memory (plus the dispatcher)• Lets ignore virtual memory for this.

Page 43: Linux Development Lecture 8 11.02.2016

Trace from processor point of view

Timeout

Page 44: Linux Development Lecture 8 11.02.2016

Two state process model• Process may be in one of two states• Running• Not-running

Page 45: Linux Development Lecture 8 11.02.2016

Queuing Diagram

Page 46: Linux Development Lecture 8 11.02.2016

Process creation• The OS builds a data structure to manage the process• Traditionally, the OS created all processes• But it can be useful to let a running process create another

• This action is called process spawning• Parent Process is the original, creating, process• Child Process is the new process

Page 47: Linux Development Lecture 8 11.02.2016

Process termination• There must be some way that a process can indicate

completion.• This indication may be:• A HALT instruction generating an interrupt alert to the OS.• A user action (e.g. log off, quitting an application)• A fault or error• Parent process terminating

Page 48: Linux Development Lecture 8 11.02.2016

Five-state process model

Page 49: Linux Development Lecture 8 11.02.2016

Using two queues

Page 50: Linux Development Lecture 8 11.02.2016

Multiple blocked queues

Page 51: Linux Development Lecture 8 11.02.2016

Suspended processes• Processor is faster than I/O so all processes could be

waiting for I/O• Swap these processes to disk to free up more memory and

use processor on more processes• Blocked state becomes suspend state when swapped

to disk• Two new states• Blocked/Suspend• Ready/Suspend

Page 52: Linux Development Lecture 8 11.02.2016

One suspend state

Page 53: Linux Development Lecture 8 11.02.2016

Two suspend States

Page 54: Linux Development Lecture 8 11.02.2016

Reason for suspendingReason Comment

Swapping The OS needs to release sufficient main memory to bring in a process that is ready to execute.

Other OS Reason OS suspects process of causing a problem.

Interactive User Request e.g. debugging or in connection with the use of a resource.

Timing A process may be executed periodically (e.g., an accounting or system monitoring process) and may be suspended while waiting for the next time.

Parent Process Request A parent process may wish to suspend execution of a descendent to examine or modify the suspended process, or to coordinate the activity of various descendants.

Page 55: Linux Development Lecture 8 11.02.2016

Processes and resources

Page 56: Linux Development Lecture 8 11.02.2016

OS control structures• For the OS is to manage processes and resources, it

must have information about the current status of each process and resource. • Tables are constructed for each entity the operating

system manages

Page 57: Linux Development Lecture 8 11.02.2016

OS Control tables

Page 58: Linux Development Lecture 8 11.02.2016

Memory tables• Memory tables are used to keep track of both main and

secondary memory. • Must include this information:• Allocation of main memory to processes• Allocation of secondary memory to processes• Protection attributes for access to shared memory regions• Information needed to manage virtual memory

Page 59: Linux Development Lecture 8 11.02.2016

IO Tables• Used by the OS to manage the I/O devices and channels

of the computer.• The OS needs to know• Whether the I/O device is available or assigned• The status of I/O operation• The location in main memory being used as the source or

destination of the I/O transfer

Page 60: Linux Development Lecture 8 11.02.2016

File tables• These tables provide information about:• Existence of files• Location on secondary memory• Current Status• other attributes.

• Sometimes this information is maintained by a file management system

Page 61: Linux Development Lecture 8 11.02.2016

Process tables• To manage processes the OS needs to know details of

the processes • Current state• Process ID• Location in memory• etc

• Process control block• Process image is the collection of program. Data, stack, and

attributes

Page 62: Linux Development Lecture 8 11.02.2016

Process attributes• We can group the process control block information into

three general categories:• Process identification• Processor state information• Process control information

Page 63: Linux Development Lecture 8 11.02.2016

Process ID• Each process is assigned a unique numeric identifier.• Many of the other tables controlled by the OS may use

process identifiers to cross-reference process tables

Page 64: Linux Development Lecture 8 11.02.2016

Processor State Information• This consists of the contents of processor registers. • User-visible registers• Control and status registers• Stack pointers

• Program status word (PSW)• contains status information• Example: the EFLAGS register on Pentium processors

Page 65: Linux Development Lecture 8 11.02.2016

Process control information• This is the additional information needed by the OS to

control and coordinate the various active processes.

Page 66: Linux Development Lecture 8 11.02.2016

Structure of process images in Virtual Memory

Page 67: Linux Development Lecture 8 11.02.2016

Role of the Process Control Block• The most important data structure in an OS• It defines the state of the OS

• Process Control Block requires protection• A faulty routine could cause damage to the block destroying

the OS’s ability to manage the process• Any design change to the block could affect many modules of

the OS

Page 68: Linux Development Lecture 8 11.02.2016

Modes of execution• Most processors support at least two modes of

execution• User mode• Less-privileged mode• User programs typically execute in this mode

• System mode• More-privileged mode• Kernel of the operating system

Page 69: Linux Development Lecture 8 11.02.2016

Process creation• Once the OS decides to create a new process it:• Assigns a unique process identifier• Allocates space for the process• Initializes process control block• Sets up appropriate linkages• Creates or expand other data structures

Page 70: Linux Development Lecture 8 11.02.2016

Switching processes• Several design issues are raised regarding process

switching• What events trigger a process switch? • We must distinguish between mode switching and process

switching.• What must the OS do to the various data structures under its

control to achieve a process switch?

Page 71: Linux Development Lecture 8 11.02.2016

When to switch processMechanism Cause Use

Interrupt External to the execution of the current instruction

Reaction to an asynchronousexternal event

Trap Associated with the execution of the current instruction

Handling of an error or anexception condition

Supervisor call Explicit request Call to an operating systemfunction

Page 72: Linux Development Lecture 8 11.02.2016

Change of process state• The steps in a process switch are:

1. Save context of processor including program counter and other registers

2. Update the process control block of the process that is currently in the Running state

3. Move process control block to appropriate queue – ready; blocked; ready/suspend

Page 73: Linux Development Lecture 8 11.02.2016

Change of process state4. Select another process for execution5. Update the process control block of the process selected6. Update memory-management data structures7. Restore context of the selected process

Page 74: Linux Development Lecture 8 11.02.2016

Is the OS a process ?• If the OS is just a collection of programs and if it is

executed by the processor just like any other program, is the OS a process?• If so, how is it controlled?• Who (what) controls it?

Page 75: Linux Development Lecture 8 11.02.2016

Execution of OS

Page 76: Linux Development Lecture 8 11.02.2016

Non-process Kernel• Execute kernel outside of any process• The concept of process is considered to apply only to

user programs• Operating system code is executed as a separate entity that

operates in privileged mode

Page 77: Linux Development Lecture 8 11.02.2016

Execution within user processes• Execution Within User Processes• Operating system software within context of a user process• No need for Process Switch to run OS routine

Page 78: Linux Development Lecture 8 11.02.2016

Process based OS• Process-based operating system• Implement the OS as a collection of system process

Page 79: Linux Development Lecture 8 11.02.2016

Threads

Page 80: Linux Development Lecture 8 11.02.2016

Processes and threads• Processes have two characteristics:• Resource ownership - process includes a virtual address

space to hold the process image• Scheduling/execution - follows an execution path that may

be interleaved with other processes• These two characteristics are treated independently by

the operating system

Page 81: Linux Development Lecture 8 11.02.2016

Processes and threads• The unit of dispatching is referred to as a thread or

lightweight process• The unit of resource ownership is referred to as a

process or task

Page 82: Linux Development Lecture 8 11.02.2016

Multithreading• The ability of an OS to

support multiple, concurrent paths of execution within a single process.

Page 83: Linux Development Lecture 8 11.02.2016

Single thread approaches• MS-DOS supports a single user process and a single

thread. • Some UNIX, support multiple user processes but only

support one thread per process

Page 84: Linux Development Lecture 8 11.02.2016

Multithreading• Java run-time environment is a single process with

multiple threads• Multiple processes and threads are found in Windows,

Solaris, and many modern versions of UNIX

Page 85: Linux Development Lecture 8 11.02.2016

One or More threads in a process• Each thread has• An execution state (running, ready, etc.)• Saved thread context when not running• An execution stack• Some per-thread static storage for local variables• Access to the memory and resources of its process (all threads

of a process share this)

Page 86: Linux Development Lecture 8 11.02.2016

Threads vs processes

Page 87: Linux Development Lecture 8 11.02.2016

Benefits of threads• Takes less time to create a new thread than a process• Less time to terminate a thread than a process• Switching between two threads takes less time that

switching processes• Threads can communicate with each other • without invoking the kernel

Page 88: Linux Development Lecture 8 11.02.2016

Thread use in a single-user system• Foreground and background work• Asynchronous processing• Speed of execution• Modular program structure

Page 89: Linux Development Lecture 8 11.02.2016

Threads• Several actions that affect all of the threads in a process • The OS must manage these at the process level.

• Examples:• Suspending a process involves suspending all threads of the

process • Termination of a process, terminates all threads within the

process

Page 90: Linux Development Lecture 8 11.02.2016

Activities similar to processes• Threads have execution states and may synchronize

with one another.• Similar to processes

• We look at these two aspects of thread functionality in turn.• States • Synchronisation

Page 91: Linux Development Lecture 8 11.02.2016

Thread execution states• States associated with a change in thread state• Spawn (another thread)• Block

• Issue: will blocking a thread block other, or all, threads• Unblock• Finish (thread)

• Deallocate register context and stacks

Page 92: Linux Development Lecture 8 11.02.2016

Remote procedure call• Consider:• A program that performs two remote procedure calls (RPCs) • to two different hosts • to obtain a combined result.

Page 93: Linux Development Lecture 8 11.02.2016

RPC single thread

Page 94: Linux Development Lecture 8 11.02.2016

RPC multithread

Page 95: Linux Development Lecture 8 11.02.2016

Categories of thread implementation• User level threads (ULT)• Kernel level threads (KLT), aka:• Kernel-supported threads• Lightweight processes

Page 96: Linux Development Lecture 8 11.02.2016

ULT• All thread management is

done by the application• The kernel is not aware of the

existence of threads

Page 97: Linux Development Lecture 8 11.02.2016

ULT and process states

Page 98: Linux Development Lecture 8 11.02.2016

Kernel – level threads• Kernel maintains context

information for the process and the threads • No thread management done

by application• Scheduling is done on a

thread basis• Windows is an example of

this approach

Page 99: Linux Development Lecture 8 11.02.2016

Combined approaches• Thread creation done in the

user space• Bulk of scheduling and

synchronization of threads by the application

• Example is Solaris

Page 100: Linux Development Lecture 8 11.02.2016

Relationship between thread and processes

Page 101: Linux Development Lecture 8 11.02.2016

Symmetric multiprocessing

Page 102: Linux Development Lecture 8 11.02.2016

Traditional view• Traditionally, the computer has been viewed as a

sequential machine.• A processor executes instructions one at a time in sequence• Each instruction is a sequence of operations

• Two popular approaches to providing parallelism• Symmetric MultiProcessors (SMPs)• Clusters (ch 16)

Page 103: Linux Development Lecture 8 11.02.2016

Categories of Computer systems• Single Instruction Single Data (SISD) stream• Single processor executes a single instruction stream to

operate on data stored in a single memory• Single Instruction Multiple Data (SIMD) stream• Each instruction is executed on a different set of data by the

different processors

Page 104: Linux Development Lecture 8 11.02.2016

Categories of computer systems• Multiple Instruction Single Data (MISD) stream (Never

implemented)• A sequence of data is transmitted to a set of processors, each

of execute a different instruction sequence • Multiple Instruction Multiple Data (MIMD)• A set of processors simultaneously execute different

instruction sequences on different data sets

Page 105: Linux Development Lecture 8 11.02.2016

Parallel processor architectures

Page 106: Linux Development Lecture 8 11.02.2016

Symmetric multiprocessing• Kernel can execute on any processor• Allowing portions of the kernel to execute in parallel

• Typically each processor does self-scheduling from the pool of available process or threads

Page 107: Linux Development Lecture 8 11.02.2016

Typical SMP organization

Page 108: Linux Development Lecture 8 11.02.2016

Multiprocessor OS design considerations• The key design issues include• Simultaneous concurrent processes or threads• Scheduling• Synchronization• Memory Management• Reliability and Fault Tolerance

Page 109: Linux Development Lecture 8 11.02.2016

Linux Process/Thread model

Page 110: Linux Development Lecture 8 11.02.2016

POSIX threads• Named after 1995 UNIX Threads standard• Used in Linux• Still in development

Page 111: Linux Development Lecture 8 11.02.2016

Some more threads diagrams

Page 112: Linux Development Lecture 8 11.02.2016

Some more diagrams

Page 113: Linux Development Lecture 8 11.02.2016
Page 114: Linux Development Lecture 8 11.02.2016

Pthreads joining and detaching

Page 115: Linux Development Lecture 8 11.02.2016

Create joinable threads

Page 116: Linux Development Lecture 8 11.02.2016

Thread join

Page 117: Linux Development Lecture 8 11.02.2016