Uploaded by Nick Luong

comp4736-merged

advertisement
COMP 4736
Introduction to Operating Systems
Chapter 1 (a)
Mehrdad Oveisi
Fall 2022
Components of a Modern
Computer (1)
– One or more processors
– Main memory
– Disks
Managing all these components
– Printers
requires a layer of software – the
– Keyboard
operating system
– Mouse
– Display
– Network interfaces
– I/O devices
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Hardware and Software
• A computer is a machine designed to perform
operations specified with a set of instructions called
a program.
• Hardware refers to the computer equipment.
– keyboard, mouse, terminal, hard disk, printer, CPU
• Software refers to the programs that describe the
steps we want the computer to perform.
• The software that manages the hardware and shares
the hardware between different application
programs is called the operating system.
Components of a Modern
Computer (2)
Figure 1-1. Where the operating system fits in.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
The Operating System as an
Extended Machine
Figure 1-2. Operating systems turn ugly hardware into
beautiful abstractions.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
The Operating System as a
Resource Manager
• Top down view
– Provide abstractions to application programs
• Bottom up view
– Manage pieces of complex system
• Alternative view
– Provide orderly, controlled allocation of resources
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
History of Operating Systems
• The first generation (1945–55) vacuum tubes
• The second generation (1955–65) transistors
and batch systems
• The third generation (1965–1980) ICs and
multiprogramming
• The fourth generation (1980–present)
personal computers
• The fifth generation (1990–present) mobile
computers
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Transistors and Batch Systems
Figure 1-3. An early batch system.
(a) Programmers bring cards to (IBM) 1401.
(b)1401 reads batch of jobs onto tape.
(c) Operator carries input tape to (IBM) 7094.
(d) 7094 does computing.
(e) Operator carries output tape to 1401.
(f) 1401 prints output.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Transistors and Batch Systems (3)
Figure 1-4. Structure of a typical FMS job.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Single Job to Batch
• Earliest machines had very rudimentary OS.
– To run a job needed to load the compiler as well
as the code for the job.
– Required a great deal of operator intervention
– CPU not efficiently used
– Batch processing evolved to reduce the amount of
time wasted setting up single jobs
Early Batch Processing
Card reader
Line printer
CPU
Early Batch Processing
• Collect a group of jobs
– Each job was submitted as a stack of punched cards.
•
•
•
•
•
Job card language definition card (JCL)
One card per line of code in program
Cards for load and run instructions (JCL)
Cards containing data for program
End card indicating end of job (JCL)
• Groups of jobs were collected and submitted as a
batch to the card reader
• Each job was
– read in, executed, produced its output, terminated,
– then the next job took over the machine
Monitor
• Monitor was a piece of software (early OS) that controlled the
sequence of events during batch processing. It had
– a resident portion and
– a nonresident portion that included optional utilities etc.
• The monitor reads the job (one job at a time)
– places it in the user area of memory.
• When the job is completely loaded,
– the monitor transfers control to the newly loaded program (using a
branch instruction)
• When the job is completed
– it passes control back to the monitor,
– which begins processing the next job.
Operating System
• Commands to
–
–
–
–
Read a single card to memory
Compile to machine language
Place machine language code in memory
Start execution
• (load address of first instruction in program counter
• then begin execution)
– Write output to the line printer (or other output device)
– Trap condition switches control from program to OS
• END card being executed
• Illegal opcode, divide by zero, …
Problems with early batch processing
• Input and output, particularly from peripheral I/0
devices (card reader, line printer), are very slow
– when compared to the execution of other instructions
• When input and output is happening, the CPU is
mostly idle. An expensive resource is being wasted
• A program trapped in an infinite loop would never
terminate
Improving batch processing
• Offload the slow I/0 tasks to less costly and
powerful computers
• Use faster I/O media (like tapes) for input to
fast powerful machine
• Add timers, if your time runs outs an interrupt
is generated which terminates your program
(deals with infinite loops)
• Adds complexity, improves efficiency
Improving batch processing
input
output
output
Card reader
Less powerful
CPU
input
Fast
CPU
Line printer
Line printer
Less powerful
CPU
Transistors and Batch Systems
Figure 1-3. An early batch system.
(a) Programmers bring cards to (IBM) 1401.
(b)1401 reads batch of jobs onto tape.
(c) Operator carries input tape to (IBM) 7094.
(d) 7094 does computing.
(e) Operator carries output tape to 1401.
(f) 1401 prints output.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Interrupts and traps
• Interrupts are a mechanism by which
– other modules (memory, I/0, timers …) may
– interrupt the normal sequencing of instructions
by the processor
• Traps are caused by the user,
– e.g. the $END card supplied by the user causes a
trap that ends the program.
The next generation (1965-1980)
Integrated Circuits (IC) and Multiprogramming
• More complicated OS
– 1) Deal efficiently both with
• I/0 intensive and
• CPU intensive jobs
– 2) Multi programming
• with partitioned memory
ICs and Multiprogramming
Figure 1-5. A multiprogramming system with
three jobs in memory.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
The next generation (1965-1980)
ICs and Multiprogramming
• More complicated OS
– 3) Switches between tasks
• Read cards to job queue on disk
– whenever card reader is ready
• Print results to printer from printer queue on disk
– only when printer is available and job is complete
• Load and run jobs
– 4) Changes enabled by going from tape to disk
• Tape is purely sequential
• Disk is more flexible (not purely sequential)
The next generation (1965-1980)
ICs and Multiprogramming
Card reader
Program queue
DISK
Line printer
Printer queue
Fast
CPU
Simultaneous Peripheral Operation Online
(Spooling)
• Load jobs from card reader to spooling queue
on disk
• Load output from jobs into printer spooling
queue on disk
• When card reader or printer are available can
add to/print from queue
• When CPU finishes a job can immediately load
the next job from the queue
Multiprogramming
• Partition memory into pieces (often of
different sizes)
• Load one job into each partition (choose
partition according to needs of job)
• Have multiple jobs executing simultaneously,
one in each partition
• When one job is I/O bound another can be
using the CPU
Processors
Figure 1-6. Some of the components of a
simple personal computer.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Machine language
• Each type of processor (like Pentium 4, Athalon, Z80,
…) has its own instruction set
• Each instruction in an instruction set does a single
thing like access a piece of data, add two pieces of
data, compare two pieces of data …
• Each instruction is represented by a unique number
This # may be different for different instruction sets,
but no two instructions in the same instruction set
will have the same #
Machine Language programs
• In machine language a program is a list of
instructions
– Each instruction is represented by a number
– Inside the memory of the computer each number
is represented in binary (as a constant length
string of 1’s and 0’s)
– The long string of 0’s and 1’s is easy for the
computer to understand and very difficult for a
Human to read or write
Assembler
• Assembler languages make it easier for the
programmer.
– Assembler is easier for humans to read/write
– The numbers that identify each of the instructions
in the instruction set are replaced with
mnemonics like ADD, CMP, …
– The code, written using these mnemonics is
written into a text file.
Assembler Programs
• The code for an Assembler program is written into a
text file.
• The computer read 1’s and 0’s not text
• How do we translate to machine readable form?
– A computer program called a compiler is used to translate
the text file (called a source file) containing the assembler
code into machine readable code
– The compiler writes a binary file containing the machine
readable code (called an object file)
Computer Software: Languages
• Some Computer Languages
– Machine language (machine instruction set)
– assembly language
– high level languages (Compilers/Interpreters)
• C, C++, Ada, Fortran, Basic, Java
• Do YOU know of any others?
• mathematical computation tools (MATLAB, Mathematica, ...)
• Application software is written using computer languages.
• Operating systems are also written using computer
languages (often C, or assembler)
Computer Software: Applications
• Application Software (Software Tools)
–
–
–
–
–
–
Word processors (Microsoft Word, WordPerfect, ...)
Spreadsheet programs (Excel, Lotus1-2-3, ...)
Computer games
Communication software (email, chat, web browser…)
Telecommunication software (VOIP, …)
Integrated programming environments
User mode / kernel mode
• Most application software runs in user mode. Applications
have access to a subset of the instruction set that does not
include most direct hardware access
• Operating systems run in kernel mode (supervisor mode) and
have access to the complete instruction set, including the
instructions used to directly manage the hardware
• Application software running in user mode can used system
calls to access hardware managed by the Operating System
• User mode programs may perform duties for the OS
Modes
Applications
KERNEL (OS)
SERVICE
PROCESSES
USER MODE
Application interface (system libraries)
Operating System
KERNEL MODE
Hardware
For some operating systems, the boundaries between kernel
mode and user mode may not be as clearly defined (embedded
systems, interpreted systems)
COMP 4736
Introduction to Operating Systems
Chapter 1 (b)
Mehrdad Oveisi
Fall 2022
The Operating System Zoo
•
•
•
•
•
•
•
•
•
Mainframe Operating Systems
Server Operating Systems
Multiprocessor Operating Systems
Personal Computer Operating Systems
Handheld Computer Operating Systems
Embedded Operating Systems
Sensor Node Operating Systems
Real-Time Operating Systems
Smart Card Operating Systems
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Basic computer configuration
CPU
I/O devices
Registers
Arithmetic Unit
Controller
Cache
BUS
Memory
DISK
I/O buffers
Memory Hierarchy
• Different types of memory have
– different access speeds and costs
• Faster access speed implies
– higher cost
• Greater capacity often implies
– lower access speed
• From fastest access to slowest access
–
–
–
–
–
Registers
Cache
Memory
Disk
Tapes
Registers in CPU
• Data registers
• Accumulator
– intermediate arithmetic and logic results
• Address registers
• Control/Status registers
–
–
–
–
Program counter (more later)
Stack pointer
Instruction register
Status registers
• Index register (segment pointer, stack pointer)
Controller
• “Fetch, Decode, Execute” cycle (each instruction)
– Fetch next instruction: Instruction contains op-code
(operation-code) and possibly data
– Decode op-code
– Execute op-code (using data if necessary)
Controller
• Instructions
– access data, moving it from memory (or disk or cache)
to/from registers, and between registers
– Complete arithmetic and logical manipulations of data in
registers
Executing an instruction (1)
• Examine program counter
• Fetch instruction indicated by program counter
opcode
Address(es) of data
instruction
• Increment program counter to point at next
instruction to be executed
• Place fetched instruction in instruction register
Executing an instruction (2)
• Decode the instruction
opcode
Address(es) of data
instruction
• Determine what is to be done
• If needed load address into an address
register
Executing an instruction (3)
• Executing the instruction in the instruction register,
may result in one or more of the following
– Fetch any data from memory (locations given in instruction)
and place into the appropriate data registers
– Place results from a data register or the accumulator into a
memory location indicated in the instruction
– Operate (arithmetic or logical operation) on data in data
registers and save the result in the indicated register
– Control the flow of the program (for example change the
value in the program counter register)
Adding 2 numbers (Z=X+Y)
•
•
•
•
•
•
•
•
Program counter points to instruction to load value at location X
Instruction is fetched into the instruction register, decoded and executed
to load the first number (X) into data register A
Program counter is incremented and now points to an instruction to load
value at location Y
Instruction is fetched into the instruction register, decoded and executed
to load the second number into data register B
Program counter is incremented and now points to an instruction to add
the values in data register A and B
Instruction is fetched into the instruction register, decoded and executed
to add the two numbers and place the result in the accumulator register.
Program counter is incremented and now points to an instruction to place
the value in the accumulator register into memory location Z
Instruction is fetched, decoded and executed to place result in Z
Memory (2)
Figure 1-9. A typical memory hierarchy. The numbers are
very rough approximations.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Memory Hierarchy
•
As you go down the pyramid
a) Decreasing cost per bit, Increasing capacity
b) Increasing access time, Decreasing frequency of access
–
–
Note that the fastest memory, sometimes referred to as
primary memory, is usually volatile (register, cache, most
or all of main memory)
Nonvolatile (continues to store information when the
power is off) memory is usually slower. Usually not part of
the CPU. Referred to as secondary or auxiliary memory.
Examples flash memory (flash that holds the BIOS, or
removable flash), internal and external hard drives, CD,
tape, …
Registers and cache
• Both are parts of the CPU
• Register access speed comparable to CPU clock
speed
• In the most recent generations of CPUs,
cache memory may be as fast or as much as several
times slower than registers
• Registers
– 64x64 for 64 bit, 32x32 for 32 bit (usually maximum)
– Usually < 1 or a few Kbyte
• Cache
– As little as none, or as much as 8Mbytes or more
Concept of Cache
• Provide memory on the CPU that is slower
than the registers, cheaper and larger than
registers, but can be accessed must faster
than main memory
• The next few instructions, and data that will
be needed will be loaded into cache in
anticipation of faster access by the processor.
Cache and main memory
CPU
Registers
Main memory
Cache
Memory and Cache
Caching system issues:
1. When to put a new item into the cache.
2. Which cache line to put the new item in.
3. Which item to remove from the cache when
a slot is needed.
4. Where to put a newly evicted item in the
larger memory.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Using Cache
• Instructions (and data) are generally moved from main memory
to cache in blocks.
– In main memory one of these blocks (N bytes of memory) is called a
cache line
• Cache has a series of slots, (N byes long, often 64 bytes)
– Each slot can hold a copy of one cache line in main memory.
• There are many more cache lines of memory in main memory
than there are slots in the cache memory.
• Each time a copy of a new cache line is loaded into a cache slot,
the contents of that slot is overwritten
Cache design
• Cache size and Cache line size
– Determined to optimize access time
• Mapping function
– Which cache lines may be loaded into which cache slots
• Replacement algorithm
– When is a cache line in a cache slot replaced by another
cache line
Hit ratio
• A CPU access that finds its information in the faster
cache memory is called a hit.
• If it is necessary to also access the slower main
memory (or lower-level cache) to find the
information, then the access is not a hit it is a miss.
• The proportion of accesses that are hits is called the
hit ratio
Hit ratio example (1)
• Assume that any access to the cache takes .01μs (10 nsec),
any access to main memory takes 0.1μs (100 nsec)
• Any instruction or piece of data not in cache will have to be
accessed in main memory and moved to cache before being
accessed (0.1+0.01μs)
• For a hit ratio of x% what is the average memory access time?
0.12
0.10
0.08
0.06
0.04
0.02
0.00
0.25
0.50
Hit ratio
0.75
0.00
1.00
Average access time
(microsec)
Hit ratio example (2)
Cache Line size
• As block size (cache line size) increases from a single byte the
hit rate will, at first increase.
– It is very likely that bytes near a needed byte will be accessed at about
the same time
– But as cache line size increases the number of lines decrease
• As cache line size increases past it’s optimal value then the hit
rate will begin to decrease
– This happens when it becomes more probable that the next access will
involve the cache line that was removed from the cache slot used for a
recent addition
– This also depends on the mapping function and replacement
algorithm which determine which slot will be used for the next cache
line moving into cache
Effect of Line size (example)
NOTE: MB/s = 1/nsec * 1000
Cache specifications on common systems
• Most modern systems have an onboard cache (like
we have been discussing) called an L1 cache
• Many modern systems also have a 2nd and / or 3rd
level of cache between the L1 cache and the main
memory called the L2 (L3) cache.
– L2 cache was external to the CPU, on a separate chip on
the motherboard, in systems as recent as Pentium II
– In more recent systems L2 cache is in the CPU package
(onboard) or part of the CPU itself (on die)
Multiple levels of cache: L2
CPU
Main memory
registers
L1 cache
(data)
L2 Cache
L1 cache
(instructions)
Memory and Cache
Figure 1-8. (a) A quad-core chip with a shared L2 cache.
(b) A quad-core chip with separate L2 caches.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Multiple levels of cache: L3
L1 cache
(instructions)
registers
L1 cache
(data)
L
Main memory
core1
L2 Cache
registers
L1 cache
(data)
L2 Cache
L1 cache
(instructions)
core2
L3
cache
Modern Cache Architectures
Shared cache requires more
complicated cache controller
Core 2 Duo Core2 Quad
Individual caches are more
difficult to keep synchronized
Nehalem (i5, i7)
AMD K10 (Phenom 9)
Main memory
• RAM (random access memory)
– Mostly volatile: contents lost when power cycles
– May include a small amount of nonvolatile RAM (or
ROM) that is often used to hold basic information
• Bootstrap loader
• BIOS (Basic input output system)
Recap: Basic computer configuration
CPU
I/O devices
Registers
Arithmetic Unit
Controller
Cache
BUS
Memory
DISK
I/O buffers
Disk
• Hard disk
• CD DVD Blu-Ray
Disk storage is much cheaper that memory
• Access time for disk is at least one order of
magnitude slower than for memory
Disks
Figure 1-10. Structure of a disk drive.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Input / Output
• Reading or writing data from a storage device is not
simple
– Each device has its own controller (hardware)
– Each device is managed by a device driver (software to use
the controller)
• Device drivers are specific to hardware and to the operating
system using the device
– Input and output is SLOW in comparison to CPU
operations.
Busy Waiting
• Reading or writing data to a storage device is SLOW in comparison to
the time it takes to complete one CPU operation
• The CPU must send one or more instructions to the controller (device
driver) to make the I/O device begin to read or write.
– These instructions tell the controller where to find the data and/or where to
store the data
• The CPU can then wait until the I/O operation is finishes.
– While it waits the CPU will be in a loop
– Each time through the loop the CPU will check a register in the controller
to see if the I/O operation is complete
– This is called busy waiting.
Alternatives to Busy waiting
• CPU is a valuable resource, busy waiting does not
efficiently use CPU resources
• Want to use the CPU to execute other instructions
while the I/O operation is completed by the I/O
device (instead of executing the busy waiting loop)
• To use the CPU for other calculations while to I/O
device controller completes the I/0 operation we
need to use interrupts:
– a mechanism to tell the CPU when the controller completes
the I/O
Interrupts
• Interrupts are a mechanism by which other modules (memory,
I/0, timers …) may interrupt the normal sequence of instructions
being executed by the processor
• Interrupts are a critical component of the operation of spooling
and multiprogramming (as saw already).
• Interrupts allow the transfer of control between different
programs (remember the OS is also a program)
• Interrupts can be generated by hardware (asynchronous) or by
particular instructions in software (synchronous)
I/O Devices
Figure 1-11. (a) The steps in starting an I/O device
and getting an interrupt.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
I/O Devices
Figure 1-11. (b) Interrupt processing involves taking the interrupt,
running the interrupt handler, and returning to the user program.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Some types of interrupts
• I/0
– Signaling normal completion of an operation (read or write)
– Signaling error during operation
• Timer expiry
– Begin regularly scheduled task
– End task that has exceeded allocated time
• Program generated
– Instruction causes hardware error condition (divide by 0, overflow,
illegal instruction or address, interrupt instruction …)
• Hardware failure
– Parity error …
Increase in efficiency
With interrupts
No interrupts
B1
B1
Write Instruction
CPU (processor)
wait
Write Instruction
B2
Interrupt
Complete Write
B2
B3
B3
Time
Files (1)
Figure 1-14. A file system for a university department.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Files (2)
Figure 1-15. (a) Before mounting, the files on the CD-ROM are not
accessible. (b) After mounting, they are part of the file hierarchy.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Files (3)
(We will see next what a process is)
Figure 1-16. Two processes connected by a pipe.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Processes (1)
•
•
•
•
•
Key concept in all operating systems
Definition: a program in execution
Process is associated with an address space
Also associated with set of resources
Process can be thought of as a container
– Holds all information needed to run program
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Processes (2)
Figure 1-13. A process tree. Process A created two child processes,
B and C. Process B created three child processes, D, E, and F.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
What is a process?
• A program in execution
• An instance of a program running on a
computer
• An entity that can be assigned to and
executed on a computer
What is a process?
• A process (active entity) is
a program (static entity) in execution,
and includes
– An address space, an area in memory (memory
location x to y) containing
• List of instructions
• Any required data
• The stack for the program
– Registers
– Other information (includes resources used)
Building an executable file: C
• Write code for your application (1 or more files)
• Use a preprocessor to add critical components to your code
– Prototypes of functions in libraries
– Control for conditional compilation
• Use a compiler to translate your preprocessed code to
machine language creating an object file for each code file
you compiled
• Use the linker to combine your object files and the object files
for libraries used by your code and create an executable file
Large Programming Projects
Figure 1-30. The process of compiling C and header
files to make an executable.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Make an executable
The simplest situation: one .c file
input and one executable output.
E.g. using the gcc compiler:
$ gcc myprog.c
This will output an executable file
a.out which can then be executed by
the user:
$ ./a.out
Figure 1-30. The process of compiling C and
header files to make an executable.
The executable file
• Your executable file will contain
– A list of instructions in machine language
– Reserved space for your local variables
• Your executable file will be stored on disk
Executing the executable file
• Through clicking on the executable or
command line, instruct the OS to …
• add the program (the job) to its long-term
queue
– waiting to be placed in memory and run
• and loads the job into memory
• and let it begin to run.
• When your job (your program) is loaded and
starts to run, it becomes a process
Operating system
• The operating system is not one process
• It creates and manages and terminates
processes for users
• It may create processes to control and share
resources within the computer (printers …),
manage those processes and even terminate
those processes
• It handles system calls from user processes
• Remember only one process can execute in a (single
core) CPU at any one time
– That process may be a user program or one of the OS
controlled processes
Process restrictions
• Processes are restricted to their own areas of
assigned memory
• Processes may run in “privileged” or “kernel” mode.
– full direct access to all resources and instructions in the
system.
– Usually reserved for the OS and its owned processes
• Processes may run in “user” or “protected” mode.
– access to a limited set of instructions.
• limited direct access to hardware of the system critical management
functions.
– Usually the only mode available to user processes
Process context
• The context of a process must be preserved
• It includes
– The values of the registers
– Any other information necessary to restore the process
after it has been saved.
• Saving or restoring the context of a process is called context
switching
– One context switch is required to save the currently running program.
– The scheduler runs to determine the next process to run.
– Then another context switch occurs to restore the context of the next
program
Process management
• The OS keeps track of all processes.
• Usually a process descriptor block is used to record
all properties of a particular process.
• A process table is used to manage the process
descriptor blocks.
• A process can run in “user” or “kernel” mode.
• What happens if a “user” mode process wants to do
output?
– (i.e. access hardware for output requiring “kernel” mode)
– Answer: “system calls”
Recap: The operating system
• Topics that are important to understand
advances in operating systems include
– Memory management
– File system management
– Scheduling, resource management
– Providing user interfaces to access “kernel”
functions (such as input and output)
– Advances in hardware (structure of system)
– Security and protection of information
COMP 4736
Introduction to Operating Systems
Chapter 1 (c)
Mehrdad Oveisi
Fall 2022
SYSTEM CALLS
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
System Calls
• Encapsulates commonly used functions of OS
– easily available to “user” processes,
– written in various languages
• System calls are accessed
– Using system function calls (read(), … in C)
– Using command line instructions (mkdir, …)
– GUI tools (Windows Explorer, Mac Finder, …)
System Calls
Figure 1-17. The 11 steps in making the system call
read(fd, buffer, nbytes).
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
How system calls work (UNIX)
• System calls run partially in system mode
• A system call is made by a “user” process,
– and when the system subroutine starts executing,
– the process is still in “user” mode.
• Within the system call
– a context switch from “user” mode controlled by the
“user” process to “kernel” mode controlled by the OS
– The OS executes the required “kernel” mode operations
– another context switch back to the “user” mode before
the system call returns to the “user” process
System Calls: An example
• To read or print, a user program will make a system call
• The system call will
– Switch context to “kernel” mode
– set up the operation, start the hardware
– pass the CPU to a program to execute
• until the I/0 operation is complete (in system mode).
– The hardware will send an interrupt when it is done.
– The interrupt will pass the CPU back to the system call
– The system call will
• complete the I/O operation: any clean up or additional work needed
• then switch context to “user” mode
• and pass control back to the user program.
Services provided by system calls
•
•
•
•
Creating and managing processes
File management (Input and Output)
Directory and File management
Using timers and other system hardware
• Excellent summary in your text, chapter 1
– No need to memorize the function signatures!
System Calls (2)
Figure 1-18. Some of the major POSIX system calls. The return code s
is −1 if an error has occurred. The return codes are as follows: pid is a
process id, fd is a file descriptor, n is a byte count, position is an offset
within the file, and seconds is the elapsed time.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
System Calls (3)
Figure 1-18. Some of the major POSIX system calls. The return code s
is −1 if an error has occurred. The return codes are as follows: pid is a
process id, fd is a file descriptor, n is a byte count, position is an offset
within the file, and seconds is the elapsed time.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
System Calls (4)
Figure 1-18. Some of the major POSIX system calls. The return code s
is −1 if an error has occurred. The return codes are as follows: pid is a
process id, fd is a file descriptor, n is a byte count, position is an offset
within the file, and seconds is the elapsed time.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
System Calls (5)
Figure 1-18. Some of the major POSIX system calls. The return code s
is −1 if an error has occurred. The return codes are as follows: pid is a
process id, fd is a file descriptor, n is a byte count, position is an offset
within the file, and seconds is the elapsed time.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
System Calls for Process
Management
Figure 1-19. A stripped-down shell. Throughout this book,
TRUE is assumed to be defined as 1.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
System Calls for Process
Management
Figure 1-20. Processes have three segments:
text, data, and stack
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
System Calls for Directory
Management
Figure 1-22. (a) File system before the mount.
(b) File system after the mount.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
The Windows Win32 API (1)
Figure 1-23. The Win32 API calls that roughly correspond to
the UNIX calls of Fig. 1-18.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
The Windows Win32 API (2)
Figure 1-23. The Win32 API calls that roughly correspond to
the UNIX calls of Fig. 1-18.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
OPERATING SYSTEM
STRUCTURES
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
OS architectures
• An OS can be built based on one or more of
the following architectures
– Monolithic
– Layered
– Microkernel
– Client Server
– Virtual machine
– Exokernel
Monolithic
• Entire OS runs as a single program in kernel mode
– Many procedures for different purposes
– Complex: any procedure can call any other
– Some structure: system calls all execute the same way
• Basic structure of OS
– A main program that invokes the requested service procedure.
– A set of service procedures that carry out the system calls.
– A set of utility procedures that help the service procedures.
Monolithic Systems
Figure 1-24. A simple structuring model
for a monolithic system.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Layered
• Generalize the layers observed in the
monolithic structure
• Further separate the functions of the OS
• Make a layer for each of the groups of functions
• Note that only adjacent layers should
communicate directly
Layered Systems
Figure 1-25. Structure of the THE operating system.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Microkernel
• In a monolithic system errors can make the system fail
– e.g., an error in a driver added to the system
• Split the OS into smaller modules.
– One module runs in “kernel” mode and manages the whole OS
– Other modules run in user mode each as a separate process
• An error in a driver only breaks the module for that driver
not the whole OS
Example Microkernel (MINIX)
User tools and processes (shell …)
SERVICES (file server, process manager, …)
DEVICE DRIVERS (1 module each type of driver)
MICROKERNEL
Interrupts, communication between processes,
scheduling, timers,
Microkernels
Figure 1-26. Simplified structure of the
MINIX 3 system.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Virtual machines
• Allows to have
many “hosts” that appear to be
individual machines all running on
the same server.
• The virtual machines can even run
different operating systems
Virtual Machines
[adapted] Figure 1-29.
(a) A type 1 hypervisor. (b) A type 2 hypervisor.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Client-Server Model
Figure 1-27. The client-server model over a network.
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
End
Chapter 1
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
COMP 4736
Introduction to Operating Systems
Chapter 2 (a)
Mehrdad Oveisi
Fall 2022
PROCESSES AND THREADS
Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Processes
Multiprogramming
• When multiple programs share the same
processor
– An example of pseudo-parallelism
• Processes share processor (each get a short turn) so
that in a perceptible length of time it appears processes
run in parallel
• True parallelism happens when multiple
processors are running processes at the same
time
What is a process?
• A process (active entity) is a program (static entity) in
execution, and includes
– An address space, an area in memory (memory location x to
y) containing
• List of instructions
• Any required data
• The stack for the program
– Registers (program counter … )
– Other information (includes resources used, I/O status)
• Multiple processes may execute different instances of the same
program (even at the same time)
Process context
• The context of a process must be preserved, It
includes
– The values of the registers (e.g. Program counter)
– Any other information necessary to restore the
process after it has been saved.
• Saving and restoring the context of a process,
is called context switching
– When context switching the scheduler may be
used to determine which process to restore
Process States
Figure 2-3. The lowest layer of a process-structured
operating system handles interrupts and scheduling. Above
that layer are sequential processes.
Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Why should an OS use processes
• A computer application accepts input, processes it and
returns output
– Every application must use the computer hardware to execute (CPU,
I/0 devices …)
– It is inefficient to rewrite the low level code to interact with the
hardware within each application:
Let the OS deal with this and use the OS
• We wish to run multiple applications “at the same time” to
efficiently use our computer, multiprogramming
– Each application will run as one or more processes
– The data, I/0 use, and other resource use of one process must be
protected from the other applications
– The OS provides the support for sharing between processes
The Process Model (1)
Figure 2-1. (a) Multiprogramming of four programs.
Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
The Process Model (2)
Figure 2-1. (b) Conceptual model of
four independent, sequential processes.
Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
The Process Model (3)
Figure 2-1. (c) Only one program is active at once.
Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
The process model
• Multiprogramming (running multiple processes in “parallel”)
is based on a process model
• When a program is used a process is created that places that
program in memory, and identifies and labels the details
about that process needed for that program to run
• Once the process is started it will share the resources with
other active processes
• Because the process is sharing resources
– there is no way to accurately predict when a particular part of the
program will be completed. Our processes cannot make assumptions
about absolute timing.
– There must be protections so that a process cannot access data
belonging to or resources being used by another process.
Process Control Block
• A system runs an O/S to manage how
resources are shared between processes
– O/S must provide a consistent and secure interface
for the processes to interact with
– At any point in time when a process exists the
process can be uniquely described by a number of
elements (see next slide for a partial list)
– The Process control block is a data structure built
to hold the values of these elements for a given
process
– An image of the process’s address space (core
image)
Process Control Block
• An O/S manages processes using a process
table that may be
– a list of pointers to these Process Control blocks for
each process,
– or even a linked list of process control blocks
Process Control Block: Contents
• At any point in time when a process is executing the process
can be uniquely described by a number of elements including
– Program counter, context data (register values), memory pointers
(where in data or instructions for the program?)
– State (running, ready, blocked, suspended)
– Process Identifier (pid)
– Other status information (I/0 and other resource usage)
– Priority, Scheduling parameters
– Parent and child processes related to the process
• These elements are part of the Process control block, a data
structure built for each process
Using the process control block
• Consider a running process that has just been
interrupted
– The process control block is updated to include the present
values of all members
– The state of the process is changed from running to ready
(or another state)
– The ISR (Interrupt Service Routines) executes and on
return the operating system chooses the next process to
run
– The OS loads the information in the process control block
of the process about to run into the CPU
Process Creation
Four principal events that cause processes to be
created:
1. System initialization.
2. Execution of a process creation system call by
a running process.
3. A user request to create a new process.
4. Initiation of a batch job.
Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Process Creation
• Processes can be created by the OS or by a user
– OS will create a process to execute the program waiting in the queue
or just submitted (if nothing in queue) in a batch system
– OS will create processes (at boot time and after boot time) that run in
the background and administer resources such as printers (print
queue …)
– Logon of a new interactive user (OS creates new shell)
– Opening of a new window by an interactive user (OS)
– User or OS creates a new process from an existing process
– A parent process spawns a child process
» A file transfer server spawns a copy of itself to service the
transfer a a particular file
» A process spawns another process to watch for real time input
from hardware
Spawning
• The system call used to spawn a process is OS
dependent. So are the details of its operation
– In Windows use a CreateProcess
– In Unix/Linux use fork()
• fork() will create a copy (exact replica) of the parent
process (a child process) and begin the execution of
that child process
– The child will have an independent copy of the address
space (code and data)
– any subsequent changes in values in the address space are
unique to the process (parent or child) that made the
changes.
Spawning a process (windows)
• Use the CreateProcess system call
– Has many (~10 arguments)
– Must specify what program new process will run
– Creates new process
Spawning a process (linux)
• When fork() makes a new copy of a process the entire process
control block is copied, this means
– The child will have the same connections to open files or network
connections (this includes pointers within the files) as the parent. Both
parent and child must close those connections that are not needed
before proceeding.
– The program counter is copied, the next instruction to be executed in
the parent will be the first instruction executed in the child.
– fork() returns a integer, the pid (process id). It returns 0 to the child
(does not need to know its own id), and the actual process id of the
new process to the parent.
Creating a child (Linux/Unix)
• To create a process in UNIX use the fork command
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void)
• fork() creates a copy of the process
• fork() returns the process id of the new child process to the
parent, and a process id of 0 to the new child process. (-1 to
the parent on failure)
Example, creating a child
listen( listenfd, LISTENQ);
for( ; ; ) {
connfd = accept(listenfd, …);
if ( (pid = fork()) == 0 ) {
close(listenfd);
processit(connfd); /* uses connection to do something */
close( connfd );
exit (0);
}
close (connfd);
}
Interpretation of Example (1)
• The example is a part of a simple file transfer server.
– At the start of the sample code the server is waiting for a client to
contact it and request a file transfer. (listen command is listening for a
request)
– When a request is received the for loop is executed. The first line
inside the loop makes a connection to the client requesting the
transfer. Any data written or read on the connection will use the
connection id (connfd) like a file descriptor
– Next fork() is called to make copy of the process
• both connections (listening for further requests, and connection to
transfer data) are open in both the parent and the child processes.
• When fork() returns the memory image of the parent and child are
identical. The process control block will be almost identical, differing
mainly in process number and information about parents/children
Interpretation of Example (2)
– After fork() returns the value of the variable pid in the
memory space of the parent process is set to the process ID
of the new child process, and to 0 in the memory space of
the child process
– Next the if will take the execution of the program along
different traces for the parent and child processes.
– First consider the parent.
• The parent wishes to pass responsibility for transferring the file to the
child, it will not need to use the connection set up for file transfer.
• The parent’s will closing the file transfer connection (remember this
connection is still open in the child)
• The parent will then go back to listening for further requests (will
eventually end up back at the first line of our example code)
Interpretation of Example (3)
– Next consider the child.
• The child does not need to continue to listen for additional requests
for connections to the file transfer server, its sole responsibility is
servicing the request it was created to service
• The child will close the connection listening for new connection
requests (close(listenfd);)
• The child will then service the file transfer request (actually transfer
the file)
• The child will then close the connection established for file transfer
(now complete) (close( connfd );
• The child will terminate itself since it has now completed purpose
(exit())
Executing a different program
• The example we looked at continues to execute the same
program in both the parent and the child processes.
• What if we want to create a process to execute a completely
different program?
– Still use fork()
– But also use another system call to execve() (or one of five
other specific purpose exec system functions)
– The system function execve()
• Replaces the memory image (program, data, and stack)
by a completely different program memory image for a
new program.
• It then starts execution at the beginning of the new
program.
Simple example:
A command line shell
• The shell is one process
• When you type a command into the shell you are
asking the OS to create a process to run that
command
• In this case the new process would be created using
fork as we discussed
• The first line executed by the child process after the
fork would be an execve to start the process we just
requested on the command line.
Wait for the child?
• There are two possibilities
– Allow the parent to continue execution while the
child runs in background
• E. g. file transfer server:
parent continues listening for the next request to transfer a file
– Make the parent wait for the child to complete
before continuing it’s own execution
• E. g. interactive shell:
shell waits for command typed on the command line to complete,
prints any results, then the parent is ready for the next command
How to make the parent wait
• Use waitpid()
– Can tell the parent to wait for a particular child to
complete
– Can tell the parent to wait for any child to complete
– Can tell if the child process terminated normally or
with a particular error
Process Termination
• There are several kinds of reasons for a process to
terminate,
– Normal completion
– Terminated by another process
• By OS if time limit exceeded
• By administrator’s shell
• By parent
– Fatal Error
• Protection error (accessing prohibited hardware/data)
• I/0 error (file not found, …)
• Programming error (divide by zero, unitialized data …)
– Voluntary exit due to detected error
Termination
• From within the process
– ExitProcess(status) Windows
– exit(status) Unix/Linux
• On process terminating another
– kill() killpg() Unix/Linux
– TerminateProcess() Windows
Process Hierarchy
• In Unix / Linux (not windows)
– When one process creates another the creating process is
called the parent process, the created process is called the
child process.
– A process created by a child process is the child’s child or the
parent’s descendent.
– A parent’s parent is the child’s ancestor
– The parent and all its children form a process group.
– Processes can be put into new process groups or other
existing process groups using setpgid()
– All processes in a process groups can be sent a signal (like kill)
by sending to the process group rather than each member
Unix / Linux Zombie
• When a child process completes (or is terminated) before the
parent process it “dies” but is not removed from the system
• It remains so the parent process can access information about
the process.
– If the parent process calls wait() or waitpid() then the child process is
‘cleaned up’ and removed from the system
– If the parent process does not call wait() or waitpid() the child process
becomes a zombie and continues using a slot in the process table
– If the parent process terminates without calling wait then children are
passed to the init process and ‘waited’ by init
User and OS processes (1)
• Kernel (OS)
–
–
–
–
–
contains all OS functions
executes separately from processes
User processes run in protected mode.
OS is not considered a process
Older operating systems:
UP1
Kernel
UP2
UP3
User and OS processes (2)
• System call may be executed by changing to privileged
mode and executing
– so that to remove need of a context switch
• Approach used in UNIX
OS
user
Process Switching
Process States (1)
Three states a process may be in:
1. Running (actually using the CPU at that instant).
2. Ready (runnable; temporarily stopped to let
another process run).
3. Blocked (unable to run until some external
event happens).
Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Process States (2)
Figure 2-2. A process can be in running, blocked, or ready
state. Transitions between these states are as shown.
Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Process States
Initiating
Exiting
Process
terminated
Process
initiated
Ready
Scheduling
Next process
dispatched
Interrupt or
event
Blocked
Wait event
Timeout
Running
Process
complete
Transitions/States
• Initiating: New Process Control block built
• Initiating to Ready: OS accepts new process, loads process
control block into the process control table, allocates memory
and resources, loads process into memory
• Ready to Running: OS scheduler chooses process as next to
run and dispatches it to the CPU (loads registers and state into
CPU)
• Running to Ready: OS scheduler has interrupted because
– Allocated time for the process has been used
– Higher priority process is about to run (preempting present process)
Initiating
Exiting
Process
terminated
Process
initiated
Ready
Scheduling
Next process
dispatched
Timeout
Interrupt or
event
Running
Blocked
Wait event
Process
complete
Transitions/States
• Running to Exit: process has terminated (for one of the
reasons discussed )
• Running to Blocked: process has requested action that
requires waiting (like I/0)
• Blocked to Ready: Interrupt at end of wait has been
received, process that is waiting can now continue
• Blocked to exit: process terminated by another process
• Ready to exit: process terminated by another process
Initiating
Exiting
Process
terminated
Process
initiated
Ready
Scheduling
Next process
dispatched
Timeout
Interrupt or
event
Running
Blocked
Wait event
Process
complete
Multiprocessing: queues
done
Ready queue
Dispatch next job
in queue
Read queue
Read
complete
Read wait
Write queue
Write
complete
Write wait
Processor
What if all processes are blocked? (1)
• Each process uses some fraction of available
memory.
• Only so many processes will fit into the
physical memory
• Our present model will cause
– a process to move to blocked state when it must
wait.
– In blocked state it still occupies its slot in memory
What if all processes are blocked? (2)
• If there is not room to load additional processes into
memory,
and all processes are blocked
then the CPU will be idle
• THEN WHAT?
– Can increase the size of physical memory,
but this is a limited and expensive solution
– Add virtual memory and paging to the operating system.
Virtual Memory
• We will discuss the topic in detail later,
but for now lets just look deep enough to understand
how it effects our process model
• Virtual memory allows us to treat our system as if the
memory is larger than the actual physical memory (main
memory).
• A portion of our disk memory will be used to image
active processes that are not presently in main memory.
• This introduces another possible state, suspended.
Virtual Memory: suspended state
• A suspended process is not running
and its image is not stored in main memory.
Its image is stored on disk.
• A suspended process may still be ready to run or waiting for
an interrupt before it can become ready to run.
• But before it can run it must also be loaded back into actual
physical memory.
• Loading a suspended process into memory may require that
some other process in memory be suspended to make room
Swapping
• A simplified version of virtual memory
• Swapping copies the entire process image to
disk, and restores the entire image
• Virtual memory may copy pieces of the process
– can be all the pieces but usually isn’t
• Swapping is conceptually easier to understand
but not as efficient
Suspended process
• Not immediately available for execution
because not completely stored in main memory
• May or may not be blocked.
• Blocked (and suspended) processes
cannot be executed immediately
(must be loaded into main memory first)
even when the blocking condition is removed
Suspended process
• A process can be suspended (to prevent its
execution) by an agent such as
– an OS process,
– its parent process,
– or some other process.
• The suspended process cannot be reloaded
until the agent that placed it into a suspended state
indicates that it should be reloaded.
Process State
Process
initiated
Initiating
Insufficient
memory
Exiting
Enough
memory
Process
complete
activate
Ready
Ready/Suspend
suspend
Interrupt or
event
Timeout
Scheduling
Interrupt or
Next process
event
dispatched
activate
Blocked/Suspend
suspend
Running
Wait event
Blocked
Activate = load process into main memory
Additional Transitions/States
• Ready/Suspend State:
– process is in virtual memory
– but is ready to be loaded and run
• Blocked/Suspend State:
– process is in virtual memory
– and is awaiting an event before it can be reloaded
and run
Additional Transitions/States
• Blocked → Blocked/suspend:
– process is blocked,
– shortage of space in main memory,
– move process to virtual memory
• Blocked/suspend → Ready/suspend:
– Interrupt at end of wait has been received,
– process that is waiting can now continue,
– process is stored outside main memory
• cannot run before it is restored to main memory
Additional Transitions/States
• Ready/Suspend → Ready:
– load into main memory,
– may replace a process with lower priority,
– or replace any process if all processes are blocked.
• Ready → Ready/Suspend:
– ready process being preempted by higher priority
task
• e.g. which is being moved to Ready from Ready/Suspend
Additional Transitions/States
• Running → Ready/Suspend:
– If a presently suspended process is preempting
(having higher priority than) the present process,
it may be necessary to move the process directly
to Ready/Suspend rather than to ready
• Blocked/suspend → Blocked:
– space becomes available in main memory,
– the suspended blocked process is higher priority
than the next suspended ready process,
– and the block is about to be removed.
– (not a common transition)
COMP 4736
Introduction to Operating Systems
Chapter 2 (b)
Mehrdad Oveisi
Fall 2022
Threads
What is a process?
• A process (active entity) is a program (static entity) in
execution, and includes
– An address space, an area in memory (memory location x
to y) containing
• List of instructions
• Any required data
• The stack for the program
– Registers
– Other information (includes resources used, status of I/O )
• A process groups all resources needed to execute a
program
Multiprocessing shares memory
When multiple processes are
sharing the CPU, the memory
is also shared between them
– Each process can only access its
own area of memory (program
image, stack, …)
– Pointers (stored in registers) give
• the base address
• and the length or end address.
(shown here in # of words)
250
Dispatcher
1000
Process A
4000
Process B
5000
7000
Process C
10000
Processes/Threads
• Our OS switches between processes and otherwise
manages processes,
this incurs a significant overhead for each process
• Processes are useful for encapsulation and security.
– They cannot access each others memory space
– The resources they use are protected from other
processes
– This can be useful when processes are competing
for resources
• different owners who want optimal turnaround
Processes/Threads
• However, sometimes we want programs to be
able to
– interact and cooperate
– and even share memory space when they run
• For example, when processes are
– controlled by the same user,
– and/or cooperating with each other
• The process model does not allow this.
Path of Execution
• A path of execution can be thought of as
– a list of instructions in order they are executed
when a particular process executes.
• Two different processes
running the same program
can have different threads of execution
• A thread follows a particular path of execution
through a program
Thread Usage (1)
Figure 2-7. A word processor with three threads.
Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
What is a thread?
• A “lightweight process”
• Each thread has its own program image
– Program counter, registers,
– Stack (path of execution), automatic data (e.g. local
variables),
– State
• Each thread in a particular process shares
– Program text, Global variables
– Files and other communication connections
– State (different from process state)
Thread Usage (2)
Figure 2-8. A multithreaded Web server.
Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
What is shared
Process
Shared
Process
control block
Thread 1
Thread control
block
Thread 2
Thread control
block
Thread 3
Thread ocntrol
block
User address
space
Code
Global
variables
Heap
Static
variables
REGISTERS
REGISTERS
REGSITERS
STACK
STACK
STACK
Includes
Automatic
variables
Includes
Automatic
variables
Includes
Automatic
variables
Thread Usage (3)
Figure 2-9. A rough outline of the code for Fig. 2-8.
(a) Dispatcher thread. (b) Worker thread.
Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
The Classical Thread Model (1)
Figure 2-11. (a) Three processes each with one thread.
(b) One process with three threads.
Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
The Classical Thread Model (2)
Figure 2-12. The first column lists some items shared by all
threads in a process. The second one lists some items
private to each thread.
Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
The Classical Thread Model (3)
Figure 2-13. Each thread has its own stack.
Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Two characteristics of a process
Two main characteristics of a process:
• Resource ownership/management
• Scheduling/execution
Resource ownership
• Resource ownership/management
– Owned by a process
– Shared by a thread
• Address space holding a process image
– (program, data, state stored in process control block),
• main memory,
• connections to
– I/O devices,
– other hardware devices,
– and other processes (also stored in process control block)
Scheduling, execution
• Scheduling/dispatching/execution
– Owned/managed by each thread within a process
• Program counter, registers, stack, locally declared
(automatic) variables
• Follows an execution path through the program image
(trace through the code)
– Execution path is unique to each process,
and to each thread within a process
Recap: Multithreading
Multithreading:
The ability of the operating system to support
more than one concurrent thread of execution
within any given process.
Multithreading: advantages
• Increases efficiency over using multiple
processes because
– Fewer process context switches
• using shared image
– Less thread context to save
• program counter, local thread specific variables, thread
state
– Takes less time to create/terminate a thread than a
process (less to copy)
Multithreading: disadvantages
• Sharing increases possibility of synchronization problems
– Each thread can modify all variables in the memory image
• but cannot control the order modifications are done
– All files and other communication connections are shared.
• Reading in one thread, moves read pointer
– Suspending a process means suspending several threads to
allow the image to be swapped
– Exercise: Think about what happens if you fork a
multithreaded process
When threads are useful
• Multiprocessor systems
– simultaneous execution of different threads on multiple processors
• Efficiency advantages of multiprocessing within one process
– one thread blocked, run another
• Foreground (display and input) and background work
– using a word processor, spreadsheet …
• Asynchronous processing
– timed save in a word processor, I/0 processing in background …
• Modularization
– different threads in the process taking care of
different modularized tasks
• I/0 from/to various sources/destinations …
• Good examples in your text
Implementing Threads
(a)
(b)
Figure 2-16. (a) A user-level threads package.
(b) A threads package managed by the kernel.
Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Implementing threads
• Two main approaches to implementation
1. User level threads (POSIX Pthreads)
• Thread management is the responsibility of the user and
occurs completely in user mode.
• Thread scheduling is user controlled, and you can choose
the best way to schedule your own threads
• Blocking system calls or page faults block all threads (not
just one); must wrap the call so thread is not blocked if
device is busy
• Not taking advantages of multiprocessors
Implementing threads
• Two main approaches to implementation
2. Kernel level threads
• All thread management is done by the kernel
• Requires mode switch to kernel to move between threads
• API to the kernel thread facility provides
user access through system calls
• O/S dependent
User Level Threads
User Space
T1
T1
T2
T3
Thread
Library
P
Kernel Space
P
User level threads
• The application does the thread management in user space
with the help of a thread library
• The kernel is not aware that threads are being used
• An application begins as a process with a single thread
• The application keeps its own thread table to keep track of all
threads it has created using the library routine thread_create()
• The application must use thread_yield() to ask the scheduler
(library) to switch to the another thread
– No context switch or interrupt are needed (fast)
– Cannot let the OS scheduler manage sharing between threads
(because the OS (kernel level) does not know threads exist)
Recall: Process States
Figure 2-2. A process can be in running, blocked, or ready
state. Transitions between these states are as shown.
Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Process and Thread States
• We have discussed the possible states of a process
– running, blocked, ready, …
• Each thread in a process can have its own state.
– The states have the same names
– but we must remember that they refer to only one thread
• E.g., consider a process that is in the running state, and
it has multiple threads
– only one of its threads will be in the running state,
– the others will be in blocked state or in ready state
Some possible situations
• The process is in running state,
one thread yields to another thread in the
same process
another thread might get into its running state
• The process is in ready state,
one of the process’s threads is in running state
• The process is blocked in blocked state,
one of the process’s threads is in running state
User level thread multiprocessing
• The threaded application starts as a process containing a
single thread. The process is in the running state.
• The application uses the user-level thread library to create
additional threads
• When thread A begins to run it will enter the running state.
Thread A will continue until it calls thread_yield()
• When thread_yield() executes it will
– place Thread A into the ready state,
– place thread B into the running state (formerly in waiting state)
– and start running thread B
• The process is still in the running state
User level threads: timesharing
• The threaded application starts as a process containing a
single thread. The process is in the running state.
• The application uses the user-level thread library to create
additional threads
• Thread A (in process 1) has been executing for one process
timesharing quantum and the timer interrupt has transferred
control to the kernel.
• The kernel does not know about the threads, it saves the context
of process 1 and moves process 1 to ready state, and starts
executing process 2
• Thread A is still in running state even though process 1 is in ready
state
User level threads: blocking
• The threaded application starts as a process containing a
single thread. The process is in the running state.
• The application uses the user-level thread library to create
additional threads
• When thread A begins to run, it will enter the running state.
Thread A makes an I/O system call.
– Control is transferred to the kernel which blocks the process
– Remember the kernel does not know about the threads
• Thread A is still in state running,
even though the process is in state blocked
– It is not possible to run any other thread of the blocked process
User level threads: advantages
• The process does not have to switch to kernel
mode to do thread management
• Scheduling is implemented by the application
(using the thread library),
so scheduling can be application specific
• User level thread libraries can be portable (can
run on more than one OS)
User level: disadvantages
• When one thread makes a system call which causes it
to be blocked, the entire process is blocked
– Can use jacketing to mitigate
• A multithreaded application does not benefit from
multiprocessing or time sharing.
– these occur on a per process basis
– Not necessarily compatible with one another
• When a multithreaded process calls fork() should
some or all threads be duplicated?
– Not a simple question
Jacketing
• Convert blocking system calls to non-blocking system
calls
• Write a jacket routine that checks to see if the
blocking devise is presently busy
– If the devise is busy and would block,
• control is passed to a different thread.
• Otherwise execute the request
– When control returns from the other thread,
repeat the previous step
• Must have a system call to check if a blocking
device is busy, select()
Kernel-Level Threads
User Space
T1
P
Kernel Space
T2
T1
P
T3
Kernel-level threads
• Kernel maintains
– process control blocks describing each process and
– thread control blocks describing each thread
• Threads from the same process can be scheduled independently.
Scheduling can be done for threads rather than just for processes.
• If one thread is blocked,
the other threads in the process are not blocked
• Switching threads requires a system call
and is therefore less efficient
Hybrid Implementations
Figure 2-17. Multiplexing user-level threads
onto kernel-level threads.
Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
POSIX Threads (1)
Figure 2-14. Some of the Pthreads function calls.
Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Figure 2-15. An example program using threads.
Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Another Hello world for threads
• Process to be executed by each thread that is
created. When function returns thread
terminates
void *hola(void * arg) {
int myid=*(int *) arg;
printf("Hello, world, I'm %d\n",myid);
return arg;
}
Author: Mark Hays <hays@math.arizona.edu>
http://math.arizona.edu/~swig/documentation/pthreads/
Creating Threads
pthread_t threads[NTHREADS];
int ids[NTHREADS];
/* holds thread info */
/* holds thread args */
for (worker=0; worker<NTHREADS; worker++) {
ids[worker]=worker;
errcode = pthread_create (
&threads[worker],
NULL,
/* default thread attributes */
hola,
/* start routine
*/
&ids[worker]));
if (errcode != 0) {
errexit(errcode,"pthread_create");
}
}
Author: Mark Hays <hays@math.arizona.edu>
http://math.arizona.edu/~swig/documentation/pthreads/
Waiting for threads to terminate
for (worker=0; worker<NTHREADS; worker++) {
/* wait for thread to terminate and release its resources */
errcode=pthread_join(threads[worker],(void *) &status);
if (errcode != 0) { /* something went wrong while joining threads*/
errexit(errcode,"pthread_join");
}
/* check thread's exit status */
if (*status != worker) {
fprintf(stderr,"thread %d terminated abnormally\n",worker);
exit(1);
}
}
Author: Mark Hays <hays@math.arizona.edu>
http://math.arizona.edu/~swig/documentation/pthreads/
Download