You are on page 1of 78

CS 334: PRINCIPLES OF OPERATING SYSTEMS

Prerequisites: CS 151: Computer Organization and Architecture I and


CS 252: Computer Organization and Architecture II

Course Description
This course is about the concepts, structure and mechanisms of
operating systems, which is an essential part of any computer
system. The course intend to provides the design principles and
implementation issues of modern operating systems.
Course Contents
1. Introduction (OS Overview)
2. Process management
1. Threads
2. Concurrency (Mutuatl Exclusion & Synchronization)
3. Concurrency (Deadlocks & Starvation)
3. Scheduling
4. Memory management
5. File Systems & I/O management
6. Information security/Access control
7. OS in real time and embedded systems

Mode of Delivery
2 hrs lectures & 1 hrs practical per week.
Assessment

1. Continuous Assessment (CA): 40%

Two (2) timed tests @ 15%


 Test 1 = 6th week
 Test 2 = 12th week
 Practical work =10%

2. University Examination: 60%


Textbooks/References

1. Operating System Internals and Design Principles, by


William Stallings, Prentice-Hall, 6th edition published in
2009.

2. Modern Operating System, 2nd edition, by Tannenbaum, A.S.,


Prentice-Hall, 2001. (3rd edition published in 2008)

3. Operating System Concepts, 6th edition , by Silberschatz, A.,


Galvan,B. P., & Gagne, G. Published by John Wiley 2002.
7th edition published Dec. 2004.
Introduction
(Operating System Overview)

Introduction 5
Outline

Operating System
Objectives/Functions
 The Evolution of Operating Systems
 Major Achievements
 Developments Leading to Modern Operating
Systems

6
Introduction

A computer system consists of


1. Hardware
2. Software = programs
a. system programs
b. application programs
Introduction 7
Computer System Components
1. Hardware
– provides basic computing resources

2. Systems Programs
- Manages the operation of a computer itself and
application programs which can create end user
programs.
- Operating system
Controls all computer resources and provides the base
upon which the application programs can be written.

3. Applications programs – used to solve end user


problems
4. Users (people, machines, other PCs).

Introduction 8
Introduction

 A computer is a set of
resources for the movement,
storage, and processing of
data.

 The OS is responsible for


managing these resources.

Introduction 9
What is an Operating System?
 A program that acts as an intermediary between
a user of a computer and the computer
hardware.

 It is an extended machine
 Hides the messy details which must be performed
 Presents user with a virtual machine, easier to use

 It is a resource manager
 Each program gets time with the resource
 Each program gets space on the resource
 Use the computer hardware in an efficient manner.
Introduction 10
Services/Functions Provided by
the Operating System
 Program development
 Editors and debuggers.

 Program execution
 OS handles scheduling of numerous tasks
required to execute a program

 Access I/O devices


 Each device will have unique interface
 OS presents standard interface to users

Introduction 11
Services/Functions Provided by
the Operating System
 Control access to files
 Accessing different media but presenting a
common interface to users
 Provides protection in multi-access systems

 System access
 Controls access to the system and its
resources

Introduction 12
Services/Functions Provided by the
Operating System
 Error detection and response
 Internal and external hardware errors
 Software errors
 Operating system cannot grant request of
application

 Accounting
 Collect usage statistics
 Monitor performance

Introduction 13
OUTLINE

 Operating System Objectives/Functions

The Evolution of Operating


Systems
 Major Achievements
 Developments Leading to Modern Operating
Systems

14
Evolution of Operating Systems

 First generation 1945 - 1955


 vacuum tubes, plug boards
 Second generation 1955 - 1965
 transistors, batch systems
 Third generation 1965 – 1980
 ICs and multiprogramming
 Fourth generation 1980 – present
 personal computers, Multiprocessor

Introduction 15
Evolution of Operating Systems
 The OS has evolved from serial to
multiprogramming systems.

 Stages include
 Serial Processing
 Simple Batch Systems
 Multiprogrammed batch systems
 Time Sharing Systems

16
Evolution: Serial Processing
 No operating system
 Programmer interacted directly with the computer
hardware
 Machines run from a console with display lights,
toggle switches, input device, and printer
 Problems include:
 Scheduling (used a hardcopy sign-up sheet to reserve
computer time)
 Setup time (Each steps involved mounting or
dismounting tapes or setting up card decks)

17
Simple batch system
 Early computers were extremely
expensive
 Important to maximize processor utilization

 Monitor
 Software that controls the sequence of
events
 Batch jobs together
 Program returns control to monitor when
finished

18
Evolution of Operating Systems

Early batch system


 bring cards to 1401
 read cards to tape
 put tape on 7094 which does computing
 put tape on 1401 which prints output
Introduction 19
Monitor’s perspective
 Monitor controls the sequence of
events
 Resident Monitor is software
always in memory
 Monitor reads in job and gives
control
 Job returns control to monitor

20
Job Control Language
 The monitor performs a scheduling function:
• A batch of jobs is queued up, and jobs are executed as
rapidly as possible, with no intervening idle time.
• The monitor improves job setup time as well.

With each job, instructions are included in a primitive form of job


control language (JCL).
• A special type of programming language used to provide
instructions to the monitor.

 Provides instruction to the monitor


 What compiler to use
 What data to use
21
Modes of Operation
Considerations of memory protection and privileged
instructions lead to the concept of modes of
operation.

 User Mode
 User program executes in user mode
 Certain areas of memory protected from user access
 Certain instructions may not be executed
 Kernel Mode
 Monitor executes in kernel mode
 Privileged instructions may be executed, all memory
accessible.
22
Multiprogrammed Batch Systems
 In simple batch systems, CPU is often
idle
 Even with the automatic job sequencing
provided by a simple batch operating
system,
 I/O devices are slow compared to processor

23
Uniprogramming

 Processor must wait for I/O instruction to


complete before preceding

24
Multiprogramming

 When one job needs to wait for I/O, the


processor can switch to the other job

25
Multiprogramming

•The approach is known as multiprogramming, or multitasking.

• It is the central theme of modern operating systems.

26
Example

27
Example

28
Time Sharing Systems
 However, for many jobs, it is desirable to provide a
mode in which the user interacts directly with the
computer.

 Using multiprogramming to handle multiple interactive


jobs

 Processor’s time is shared among multiple users

 Multiple users simultaneously access the system through


terminals, with the OS interleaving the execution of each user
program in a short burst or quantum of computation.
29
Batch Multiprogramming vs. Time Sharing

Both batch processing and time sharing use


multiprogramming.

The key differences are listed here

30
Major Areas of advancements

 Operating Systems are among the most


complex pieces of software ever
developed
 Major advances include:
 Processes management
 Memory management
 Information protection and security
 Scheduling and resource management
 System structure

31
Different Architectural Approaches
(Developments Leading to Modern Operating Systems)

 Various approaches have been tried,


categories include:
 Microkernel architecture
 Multithreading
 Symmetric multiprocessing
 Distributed operating systems
 Object-oriented design

32
Microkernel Architecture
 Most early OS are a monolithic kernel
 Typically, a monolithic kernel is implemented as a single
process, with all elements sharing the same address
space.

 A microkernel assigns only a few essential functions to the


kernel
 Address spaces
 Interprocess communication (IPC)
 Basic scheduling

Other OS services are provided by processes, sometimes called


servers, that run in user mode and are treated like any other
application by the microkernel.
33
Microkernel Architecture
 The microkernel approach decouples kernel and server
development.
• Servers may be customized to specific application or
environment requirements.

 The microkernel approach simplifies implementation, provides


flexibility, and is well suited to a distributed environment.
• In essence, a microkernel interacts with local and remote
server processes in the same way, facilitating construction
of distributed systems.

34
Multithreading

 Process is divided into threads that can


run concurrently
 Thread
 Dispatchable unit of work
 executes sequentially and is interruptible
 Process is a collection of one or more
threads

35
Symmetric multiprocessing (SMP)

 An SMP system has


 multiple processors
 These processors share same main memory
and I/O facilities
 All processors can perform the same
functions
 The OS of an SMP schedules processes
or threads across all of the processors.

36
SMP Advantages
 Performance
 Allowing parallel processing
 Availability
 Failure of a single process does not halt the system
 Incremental Growth
 Additional processors can be added.
 Scaling
 Vendors can offer a range of products with different price
and performance characteristics based on the number of
processors configured in the system.

37
Multiprogramming and Multiprocessing

38
Distributed Systems (Networking)

 A distributed system is a collection of processors that do not


share memory or a clock. Each processor has its own local
memory.
 The processors in the system are connected through a
communication network.
 Communication takes place using a protocol.
 A distributed system provides user access to various system
resources.
 Access to a shared resource allows:
 Computation speed-up
 Increased data availability
 Enhanced reliability

Introduction 39
Object-oriented design

 Used for adding modular extensions to a


small kernel

 Enables programmers to customize an


operating system without disrupting
system integrity

40
Operating System Design Goals

 User goals – operating system should be


convenient to use, easy to learn, reliable,
safe, and fast.
 System goals – operating system should
be easy to design, implement, and
maintain, as well as flexible, reliable,
error-free, and efficient.

Introduction 41
Operating System Implementation

 Traditionally written in assembly language,


operating systems can now be written in higher-
level languages.
 Code written in a high-level language:
 can be written faster.
 is more compact.
 is easier to understand and debug.
 An operating system is far easier to port (move
to some other hardware) if it is written in a high-
level language.

Introduction 42
Process Management
Roadmap

How are processes represented


and controlled by the OS.
 Process states which characterize the
behaviour of processes.
 Data structures used to manage processes.

44
What is a “process”?

 A program in execution
 An instance of a program running on a
computer
 The entity that can be assigned to and
executed on a processor
 A unit of activity characterized by the
execution of a sequence of instructions,
a current state, and an associated set of
system instructions

45
Requirements of an Operating
System
 Fundamental Task: Process
Management
 The Operating System must
 Interleave the execution of multiple
processes
 Allocate resources to processes, and protect
the resources of each process from other
processes,
 Enable processes to share and exchange
information,
 Enable synchronization among processes.
46
Process Elements

 A process is comprised of:


 Program code (possibly shared)
 A set of data
 A number of attributes describing the state of
the process

47
Process Elements

 While the process is running it has a


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

48
Process Control Block
 Contains the process
elements
 Created and manage by the
operating system
 Allows support for multiple
processes

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

Dispatcher is a small program which


switches the processor from one
process to another
50
Roadmap

How are processes represented


and controlled by the OS.
 Process states which characterize the
behaviour of processes.
 Data structures used to manage processes.

51
Two-State Process Model

 Process may be in one of two states


 Running
 Not-running

52
Queuing Diagram

Etc … processes moved by the dispatcher of the OS to the CPU then back
to the queue until the task is competed

53
Five-State Process Model

In a two state model, the dispatcher would have to scan the list looking for
the process that is not blocked and that has been in the queue the longest.

To simplify that, the Not Running state can be split into two states:
• Ready (processes in the Not Running state which are ready to
execute)
• Blocked (processes in the Not Running state waiting for an I/O
operation to complete).
54
Using Two Queues

•Five state model would require an additional queue for the blocked
processes.

•But when an event occurs the dispatcher would have to cycle through the
entire queue to see which process is waiting for that event. This can cause
a huge overhead when there many
55
( 100’s or 1000’s) processes
Multiple Blocked Queues

It is more efficient to have a separate ‘blocked’ queue for each


type 56of event.
Suspended Processes
 Processor is faster than I/O so all processes could
be waiting for I/O
 Swap these processes to disk to free up more memory
and use processor on more processes
 Blocked state becomes suspend state when
swapped to disk
 Two new states
 Blocked/Suspend
 Ready/Suspend

 The main aim of suspending processes is to fully


utilize the processor.

57
One Suspend State

The simple solution is to add a single state – but this only allows processes
which are blocked to be swapped out.

58
Two Suspend States

• Ready: The process is in main memory and available for execution.


• Blocked: The process is in main memory and awaiting an event.
• Blocked/Suspend: The process is in secondary memory and awaiting an event.
• Ready/Suspend: The process is in secondary memory but is available for
execution as soon as it is loaded into main memory.

59
Reason for Process
Suspension
Reason Comment
Swapping The OS needs to release sufficient main memory to
bring in a process that is ready to execute.
Other OS Reason OS suspects process of causing a problem.
Interactive User e.g. debugging or in connection with the use of a
Request resource.
Timing A process may be executed periodically (e.g., an
accounting or system monitoring process) and may
be suspended while waiting for the next time.
Parent Process A parent process may wish to suspend execution of
Request a descendent to examine or modify the suspended
process, or to coordinate the activity of various
descendants.
Table 3.3 Reasons for Process Suspension

60
Roadmap

How are processes represented


and controlled by the OS.
 Process states which characterize the
behaviour of processes.
 Data structures used to manage processes.

61
Processes and Resources

62
Operating System Control
Structures (Control Tables)

• For the OS is to manage


processes and resources, it
must have information about the
current status of each process
and resource.

• Tables are constructed for


each entity the operating
system manages
63
Process Tables
 To manage processes the OS needs to know
details of the processes
 Current state
 Process ID
 Location in memory
 etc
 Process control block
 Process image is the collection of program data,
stack, and attributes

64
Process Attributes

 We can group the process control block


information into three general categories:
 Process identification
 Processor state information
 Process control information

65
Process Identification

 Each process is assigned a unique


numeric identifier.
 Many of the other tables controlled by
the OS may use process identifiers to
cross-reference process tables

66
Processor State Information

 This consists of the contents of


processor registers.
 User-visible registers
 Control and status registers
 Stack pointers
 Program status word (PSW)
 contains status information
 Example: the EFLAGS register on Pentium
processors

67
Process Control Information
 This is the information needed by the OS to control and
coordinate the various active processes.

68
Threads
Processes and Threads

 The unit of dispatching is referred to as a


thread or lightweight process
 The unit of resource ownership is
referred to as a process or task

70
Single Thread Approaches

 MS-DOS supports a
single user process and
a single thread.
 Some UNIX, support
multiple user processes
but only support one
thread per process

71
Multithreading
 Java run-time
environment is a single
process with multiple
threads
 Multiple processes and
threads are found in
Windows, Solaris, and
many modern versions of
UNIX

72
Relationship Between
Thread and Processes

73
Threads Analogy: The Hamburger
Restaurant

74
74
Single-Threaded Restaurant

75
75
Multithreaded Restaurant

76
76
Benefits of Threads
If there is an application or function that should be
implemented as a set of related units of execution,
 it is far more efficient to do so as a collection of threads -
rather than a collection of separate processes

 Takes less time to create a new thread than a


process
 Less time to terminate a thread than a process
 Switching between two threads takes less time that
switching processes
 Threads can communicate with each other
 without invoking the kernel

77
Threads

 Several actions that affect all of the


threads in a process
 The OS must manage these at the process
level.
 Examples:
 Suspending a process involves suspending
all threads of the process
 Termination of a process, terminates all
threads within the process

78

You might also like