• Thread library provides programmer with API for creating and managing threads
• Two primary ways of implementing
- Library entirely in user space
- Kernel-level library supported by the OS
Thread Library
Labels: OS 5
Multithreading Methods
Multithreading Methods
• Many-to-One
• One-to-One
• Many-to-Many
MANY-TO-ONE
• Many user-level threads mapped to single kernel thread
• Examples:
- Solaris Green Threads
- GNU Portable Threads
----------
ONE-TO-ONE
• Each user-level thread maps to kernel thread
• Examples
- Windows NT/XP/2000
- Linux
- Solaris 9 and later
----------
MANY-TO-MANY
• Allows many user level threads to be mapped to many kernel threads
• Allows the operating system to create a sufficient number of kernel threads
• Solaris prior to version 9
• Windows NT/2000 with the ThreadFiber package
Labels: OS 5
Kernel Thread
• Supported by the Kernel
• OS manages threads
- Slower to create and manage because of system calls
- A blocking system call will not cause the entire process to block.
- The kernel can schedule threads on different CPUs.
Labels: OS 5
User Thread
• Thread management done by user-level threads library
• A blocking system call will cause the entire process to block
- OS is unaware of threads
• The kernel cannot schedule threads on different CPUs.
• Example: Pthread, a POSIX standard (IEEE 1003.1c) API for thread creation and synchronization.
Labels: OS 5
Benefits of Multi-Threaded Programming
• Responsiveness
- User interaction in parallel with data retrieval
• Utilization of MP Architectures
• Resource Sharing between Threads (vs. Process)
E.g. Synchronization by accessing shared data
• Economy (vs. Process)
- If the task is the same, why not share the code?
- In Solaris 2, creating a process is about 30 times slower than threads. Context switch threads is about 5 times slower.
Labels: OS 5
Thread
• A piece of code that run in concurrent with other threads.
• Each thread is a statically ordered sequence of instructions.
• Threads are being extensively used express concurrency on both single and multiprocessors machines.
• Programming a task having multiple threads of control – Multithreading or Multithreaded Programming.
•
Labels: OS 5
Interprocess Communication
DIRECT COMMUNICATION
• Processes must name each other explicitly:
– send (P, message) – send a message to process P
– receive (Q, message) – receive a message from
process Q
• Properties of communication link
– Links are established automatically.
– A link is associated with exactly one pair of
communicating processes.
– The link may be unidirectional (e.g. signaling), but is
usually bi-directional (e.g. sockets).
----------
INDIRECT COMMUNICATION
• Messages are directed and received from
mailboxes (also referred to as ports).
– Each mailbox has a unique id. (e.g. shared memory,
shared file, message Q)
– Processes can communicate only if they share a
mailbox.
• Properties of communication link
– A link may be associated with many processes.
– Each pair of processes may share several
communication links.
– Link may be unidirectional or bi-directional.
• Operations
– create a new mailbox
– send and receive messages through mailbox
– destroy a mailbox
• Primitives are defined as:
send(A, message) – send a message to
mailbox A
receive(A, message) – receive a message
from mailbox A
----------
SYNCHRONIZATION
• Blocking send
- suspend sending process until message received
• Nonblocking send
- resume process immediately after sending message
• Blocking receive
- suspend receiving process until data is received
• Nonblocking receive
- return either a message or
- null if no message is immediately available
Synchronization Trade-Offs
• Blocking
- guarantees message has been delivered
- drastically reduces performance
• Non-Blocking
- much better performance (hides latency of message sending)
- could cause errors if messages are lost
----------
BUFFERING
• Buffering allows messages to be saved and read or transmitted later
• Requires sufficient memory to store messages
• Can drastically improve performance of applications
Types of Buffering
• Zero Capacity
- no buffering at all
- must be "listening" when a message comes in
• Bounded Capacity
- some max, n, of messages will be buffered
- be careful when queue gets full
• Unbounded Capacity
- no limit to the number of message
- not usually very realistic assumption
- this is not very realistic, buffers usually have finite capacity
----------
Producer-Consumer Example
• One process generates data – the producer
• The other process uses it – the consumer
• If directly connected – time coordination
- How would they coordinate the time ??
• One process generates data – the producer
• The other process uses it – the consumer
• If not directly connected – have a buffer
- Buffer must be accessible to both
- Finite Capacity N – Number in use - K
Labels: OS 4
5) Inter-Process Communication (IPC)
~ Mechanism for processes to communicate and to synchronize their actions
~ Message system – processes communicate with each other without resorting to shared variables
~ IPC facility provides two operations:
>> send(message) – message size fixed or variable
>> receive(message)
~ If P and Q wish to communicate, they need to:
>> establish a communication link between them
>> exchange messages via send/receive
~ Implementation of communication link
>> physical (e.g., shared memory, hardware bus)
>> logical (e.g., logical properties)
Labels: OS 4
4) Cooperating Process
~ Independent process cannot affect or be affected by the execution of another process
~ Cooperating process can affect or be affected by the execution of another process
~ Advantages of process cooperation
>>Information sharing
>>Computation speed-up
>>Modularity
>>Convenience
Labels: OS 4
3) Operations on Process (Process Operations)
~ Parent process create children processes, which, in turn create other processes, forming a tree of processes
~ Resource sharing
>>Parent and children share all resources
>>Children share subset of parent’s resources
>>Parent and child share no resources
~ Execution
>>Parent and children execute concurrently
>>Parent waits until children terminate
~ Address space
>>Child duplicate of parent
>>Child has a program loaded into it
~ UNIX examples
>>fork system call creates new process
>>exec system call used after a fork to replace the process’ memory space with a new program
~ Process executes last statement and asks the OS to delete it (exit)
>>Output data from child to parent (via wait)
>>Process’ resources are deallocated by OS
~ Parent may terminate execution of children processes (abort)
>>Child has exceeded allocated resources
>>Task assigned to child is no longer required
>>If parent is exiting
-->Some OS do not allow child to continue if its parent terminates
--All children terminated - cascading termination
Labels: OS 4
2) Process Scheduling
Representation of Process Scheduling
-----------------
~ Job queue – set of all processes in the system
~ Ready queue – set of all processes residing in main memory, ready and waiting to execute
~ Device queues – set of processes waiting for an I/O device
~ Processes migrate among the various queues
Ready Queue and Various I/O Device Queues
~ Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue
~ Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU
Addition of Medium Term Scheduling
~ Short-term scheduler is invoked very frequently (milliseconds) must be fast
~ Long-term scheduler is invoked very infrequently (seconds, minutes) may be slow
~ The long-term scheduler controls the degree of multiprogramming
~ Processes can be described as either:
>> I/O-bound process – spends more time doing I/O than computations, many short CPU bursts
>> CPU-bound process – spends more time doing computations; few very long CPU bursts
~ When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process
~ Context-switch time is overhead; the system does no useful work while switching
~ Time dependent on hardware support
Labels: OS 4
1) The Concept of Process (OS)
An operating system executes a variety of programs:
~ Batch system – jobs
~ Time-shared systems – user programs or tasks
Textbook uses the terms job and process almost
interchangeably.
Process – a program in execution; process execution
must progress in sequential fashion.
A process includes:
~ program counter
~ stack
~ data section
-----------------
As a process executes, it changes state
~ new: The process is being created.
~ running: Instructions are being executed.
~ waiting: The process is waiting for some event to occur.
~ ready: The process is waiting to be assigned to a processor
~ terminated: The process has finished execution.
Diagram of Process State
Information associated with each process
~ Process state
~ Program counter
~ CPU registers
~ CPU scheduling information
~ Memory-management information
~ Accounting information
~ I/O status information
Process Control Block (PCB)
In computer science, a thread of execution results from a fork of a computer program into two or more concurrently running tasks. The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process. Multiple threads can exist within the same process and share resources such as memory, while different processes do not share these resources.
On a single processor, multithreading generally occurs by time-division multiplexing (as in multitasking): the processor switches between different threads. This context switching generally happens frequently enough that the user perceives the threads or tasks as running at the same time. On a multiprocessor or multi-core system, the threads or tasks will generally run at the same time, with each processor or core running a particular thread or task. Support for threads in programming languages varies: a number of languages simply do not support having more than one execution context inside the same program executing at the same time. Examples of such languages include Python, and OCaml, because the parallel support of their runtime support is limited by the use of a central lock, called "Global Interpreter Lock" in Python, "master lock" in Ocaml. Other languages may be limited because they use threads that are user threads, which are not visible to the kernel, and thus cannot be scheduled to run concurrently. On the other hand, kernel threads, which are visible to the kernel, can run concurrently.
Many modern operating systems directly support both time-sliced and multiprocessor threading with a process scheduler. The kernel of an operating system allows programmers to manipulate threads via the system call interface. Some implementations are called a kernel thread, whereas a lightweight process (LWP) is a specific type of kernel thread that shares the same state and information.
Programs can have user-space threads when threading with timers, signals, or other methods to interrupt their own execution, performing a sort of ad-hoc time-slicing.
Threads compared with processes
Threads differ from traditional multitasking operating system processes in that:
~ processes are typically independent, while threads exist as subsets of a process
~ processes carry considerable state information, where multiple threads within a process share state as well as memory and other resources
~ processes have separate address spaces, where threads share their address space
~ processes interact only through system-provided inter-process communication mechanisms.
~ Context switching between threads in the same process is typically faster than context switching between processes.
Systems like Windows NT and OS/2 are said to have "cheap" threads and "expensive" processes; in other operating systems there is not so great a difference except the cost of address space switch which implies a TLB flush.
Labels: OS 4
Question and Answer
1. What are the major activities of an Operating System with regards to Process Management?
The operating system is responsible for the following activities in connection with process management.
Process creation and deletion.
Process suspension (process is in I/O wait queue, or “swapped” out to disk, …) and resumption (move to ready queue or execution) – manage the state of the process.
Provision of mechanisms for:
Process synchronization - concurrent processing is supported thus the need for synchronization of processes or threads.
Process communication
Deadlock handling
2. What are the major activities of an Operating System with regards to Memory Management?
Keep track of which parts of memory are currently being used and by whom.
Decide which processes to load when memory space becomes available - long term or medium term scheduler.
Mapping addresses in a process to absolute memory addresses - at load time or run time.
Allocate and deallocate memory space as needed.
Memory partitioning, allocation, paging (VM), address translation, defrag, …
Memory protection
3. What are the major activities of an Operating System with regards to Secondary-Storage Management?
- Free space management
- Storage allocation
- Disk scheduling – minimize seeks (arm movement … very slow operation)
- Disk as the media for mapping virtual memory space
- Disk caching for performance
- Disk utilities: defrag, recovery of lost clusters, etc.
4. What are the major activities of an Operating System with regards to File Management?
- File creation and deletion - system calls or commands.
- Directory creation and deletion - system calls or commands.
- Support of primitives for manipulating files and directories in an efficient manner - system calls or commands.
- Mapping files onto secondary storage.
- File backup on stable (nonvolatile) storage media.
EX: File Allocation Table (FAT) for Windows/PC systems
5. What is the purpose of the command-interpreter?
- The program that reads and interprets control statements is called variously:
·command-line interpreter (Control card interpreter in the “old batch days”)
·shell (in UNIX)
- Command.com for external commands in DOS
- Its function is to get and execute the next command statement.
Labels: OS 3
System Boot
~ Operating system must be made available to hardware so hardware can start it
~ Small piece of code – bootstrap loader, locates the kernel, loads it into memory, and starts it
~Sometimes two-step process where boot block at fixed location loads bootstrap loader
When power initialized on system, execution starts at a fixed memory location
~Firmware used to hold initial boot code
Labels: OS 3
Virtual Machine
=> A virtual machine is a tightly isolated software container that can run its own operating systems and applications as if it were a physical computer. A virtual machine behaves exactly like a physical computer and contains it own virtual (ie, software-based) CPU, RAM hard disk and network interface card (NIC).
An operating system can’t tell the difference between a virtual machine and a physical machine, nor can applications or other computers on a network. Even the virtual machine thinks it is a “real” computer. Nevertheless, a virtual machine is composed entirely of software and contains no hardware components whatsoever. As a result, virtual machines offer a number of distinct advantages over physical hardware.
IMPLEMENTATION
Modes:
- virtual user mode and virtual monitor mode,
- Actual user mode and actual monitor mode
Time
- Whereas the real I/O might have taken 100 milliseconds, the virtual I/O might take less time (because it is spooled) or more time (because it is interpreted.)
- The CPU is being multi-programmed among many virtual machines, further slowing down the virtual machines in unpredictable ways.
BENEFITS
In general, VMware virtual machines possess four key characteristics that benefit the user:
Compatibility: Virtual machines are compatible with all standard x86 computers
Isolation: Virtual machines are isolated from each other as if physically separated
Encapsulation: Virtual machines encapsulate a complete computing environment
Hardware independence: Virtual machines run independently of underlying hardware
Two advantages
To provide a robust level of security
- no direct sharing of resources.
- Two solutions
To allow system development to be done easily
- A perfect vehicle for OS research and development.
- difficult to implement due to the effort required to provide an exact duplicate to the underlying machine.
- Wine for Linux.
EXAMPLES
~ Compiled Java programs are platform-neutral bytecodes executed by a Java Virtual Machine (JVM).
~ JVM consists of
- class loader
- class verifier
- runtime interpreter
~ Just-In-Time (JIT) compilers increase performance
Labels: OS 3
System Generation (SYSGEN)
~ Operating systems are designed to run on any of a class
of machines; the system must be configured for each
specific computer site.
~ SYSGEN program obtains information concerning the
specific configuration of the hardware system.
~ Booting – starting a computer by loading the kernel.
~ Bootstrap program – code stored in ROM that is able to
locate the kernel, load it into memory, and start its
execution.
~ How to generate a system?
- Recompile
- Cause the creation of tables and selection of modules from a precompiled library
Link modules together to form the generated OS
SYSGEN is faster, but the generated OS may be overly general
- Completed table driven
All the code is always part of the system, and selection occurs at execution time
Most modern OS are constructed in this manner
~ Size and generality of the generated system and the ease of modification as the hardware configuration changes
Labels: OS 3
System Structure
• Simple Structure
• Layered Approach
The operating system is divided into a number of layers (levels), each built on top of lower layers. The bottom layer (layer 0), is the hardware; the highest (layer N) is the user interface.
With modularity, layers are selected such that each uses functions (operations) and services of only lower-level layers
Properties
Simplicity of construction
Simplicity of Debugging
Problems
Precise definition of layers
Example: Memory manager requires device driver of backing store (due to virtual memory)
The device driver requires CPU scheduler (since if the driver waits for IO, another task should be scheduled)
CPU scheduler may require virtual memory for large amount of information of some processes
Less efficiency: due to the number of layers a request should pass
Labels: OS 3
System Calls
System calls provide the interface between a running program and the operating system
Generally available as assembly-language instructions
Languages defined to replace assembly language for systems programming allow system calls to be made directly (e.g., C, C++)
Types of System Calls
• Process control
create/terminate a process (including self)
• File management
Also referred to as simply a file system or filesystem. The system that an operating system or program uses to organize and keep track of files. For example, a hierarchical file system is one that uses directories to organize files into a tree structure.Although the operating system provides its own file management system, you can buy separate file management systems. These systems interact smoothly with the operating system but provide more features, such as improved backup procedures and stricter file protection.
• Device management
Device Management is a set of technologies, protocols and standards used to allow the remote management of mobile devices, often involving updates of firmware over the air (FOTA). The network operator, handset OEM or in some cases even the end-user (usually via a web portal) can use Device Management, also known as Mobile Device Management, or MDM, to update the handset firmware/OS, install applications and fix bugs, all over the air. Thus, large numbers of devices can be managed with single commands and the end-user is freed from the requirement to take the phone to a shop or service center to refresh or update.
• Information maintenance
– get time
– set system data (OS parameters)
– get process information (id, time used)
• Communications
– establish a connection
– send, receive messages
– terminate a connectionlProcess control
– create/terminate a process (including self)
Labels: OS 3
Sytem Components
OPERATING SYSTEMS PROCESS MANAGEMENT
~ A process is a program in execution. A process needs
certain resources, including CPU time, memory, files, and
I/O devices, to accomplish its task.
~ The operating system is responsible for the following
activities in connection with process management.
> Process creation and deletion.
> process suspension and resumption.
> Provision of mechanisms for:
-> process synchronization
-> process communication
MAIN MEMORY MANAGEMENT
~ Memory is a large array of words or bytes, each with its
own address. It is a repository of quickly accessible data
shared by the CPU and I/O devices.
~ Main memory is a volatile storage device. It loses its
contents in the case of system failure.
~ The operating system is responsible for the following
activities in connections with memory management:
> Keep track of which parts of memory are currently being
used and by whom.
> Decide which processes to load when memory space
becomes available.
> Allocate and deallocate memory space as needed.
FILE MANAGEMENT
~ A file is a collection of related information defined by its
creator. Commonly, files represent programs (both
source and object forms) and data.
~ The operating system is responsible for the following
activities in connections with file management:
> File creation and deletion.
> Directory creation and deletion.
> Support of primitives for manipulating files and directories.
> Mapping files onto secondary storage.
> File backup on stable (nonvolatile) storage media.
I/O SYSTEM MANAGEMENT
~ The I/O system consists of:
> A buffer-caching system
> A general device-driver interface
> Drivers for specific hardware devices
SECONDARY STORAGE MANAGEMENT
~ Since main memory (primary storage) is volatile and too
small to accommodate all data and programs
permanently, the computer system must provide
secondary storage to back up main memory.
~ Most modern computer systems use disks as the
principle on-line storage medium, for both programs and
data.
~ The operating system is responsible for the following
activities in connection with disk management:
> Free space management
> Storage allocation
> Disk scheduling
PROTECTION SYSTEM
~ Protection refers to a mechanism for controlling access
by programs, processes, or users to both system and
user resources.
~ The protection mechanism must:
> distinguish between authorized and unauthorized usage.
> specify the controls to be imposed.
> provide a means of enforcement.
COMMAND INTERPRETER SYSTEM
~ Many commands are given to the operating system by
control statements which deal with:
> process creation and management
> I/O handling
> secondary-storage management
> main-memory management
> file-system access
> protection
> networking
Labels: OS 3
Operating System Services
1. Program execution – system capability to load a program into memory and to run it
2. I/O operations – since user programs cannot execute I/O operations directly, the operating system must provide some means to perform I/On
3. File-system manipulation – program capability to read, write, create, and delete files
4. Communications – exchange of information between processes executing either on the same computer or on different systems tied together by a network. Implemented via shared memory or message passing
5. Error detection – ensure correct computing by detecting errors in the CPU and memory hardware, in I/O devices, or in user program
Labels: OS 3