CS345 02 - Computer Systems

Download Report

Transcript CS345 02 - Computer Systems

Unit 6
Operating Systems
Dr Damitha Kar unar atna
Univer sity of Colombo school of computing
Topics to discuss
• What is an operating system(OS)?
• Main objectives of an OS
• Main functions of an OS
• Evolution of OS.
• Process management.
• Context switching.
• Process state diagrams
Operating Systems
2
Components of a Computer
System
 hardware—electronic, mechanical, optical
devices.
 software—programs.
 Software saved on the storage media.
 Software burned into hardware – Firmware
 Liveware – Computer Users
Operating Systems
3
What is an operating system(OS)?
 An operating system (OS) is a resource manager.
 What is the necessity for a resource manager?
 Many processes are active at any given time and
compete for resources.
 An operating system provides orderly and
controlled allocation of the resources among
processes (jobs) that are competing for them.
Operating Systems
4
Different types of Software
 Application Software
 Systems Software
 Operating system
 System utilities
The quality of system software also directly affects
the application software
Operating Systems
5
Types of Software
Utility software: system
software designed to help
analyze, configure, optimize
or maintain a computer
(Anti-virus, Backup software,
Editors, Data compression,
Disk cleaners ….).
They are not essential to the
running of the computer
Transient Component
Operating Systems
6
What is an operating system(OS)?
Operating Systems
7
What is an operating system(OS)?
Kernel : Part of the
O/S that resides in
the main memory
all the time.
Operating Systems
8
Execution of source programs
Operating Systems
9
Address mapping for re-locatable
programs
Operating Systems
10
Different types of Oss
(Based on the processor)
• Windows/Linux – For personal computers
• Unix,z/OS, OS/390, VM – For mainframes
• MacOs – For Macs
• X Server, Windows Server – Server Operating
Systems
• Symbian, Android – For mobile phones.
Operating Systems
11
Different types of OSs
(Based on the users)
• Single User – Allows only a single user to use the
OS at any given time. The use may run several
processes at the same time.
• Example - DOS
• Multi User - Allows multiple users to access a
computer system at the same time
• Example UNIX, Time-sharing systems and Internet
servers.
Operating Systems
12
Different types of OSs(Based on
the number of tasks)
• Single Task– Allows only one running program at
any given time.
• Multi Task - A multi-tasking operating system
allows more than one program to be running at
the same time.
Operating Systems
13
Different types of OSs
• Real Time – OS is designed to run applications with
very precise timing and with a high degree of
reliability.
• The main objective of real-time operating systems is
their quick and predictable response to events.
• These types of OS are needed in situations where
downtime is costly or a program delay could cause a
safety hazard.
Operating Systems
14
Main objectives of an OS
Convenience: Make the computer more convenient to
use
◦ Provide easy to use interface for a normal users.
◦ Hide the complexity of the hardware devices from the
application developer.
Efficiency: Monitor and manage resources of the
computer system efficiently
◦
◦
◦
◦
CPU
Main memory
Secondary Storages
Various devices connected to the computer
Operating Systems
15
Services provided by an OS
Process Management
Storage (external) management
Memory management
I/O device management
Management of the File System
Networking
User Interface
Protection
Operating Systems
16
Services provided by an OS….
Error detection and response
◦ Hardware errors: memory error or device failure
◦ Software errors: arithmetic errors, access forbidden
memory locations
Accounting
◦ collect statistics (billing)
◦ monitor performance
Operating Systems
17
Types of User Interfaces
Command Line Interface(CLI)
Operating Systems
18
Types of User Interfaces
Graphical User Interface(GUI)
Operating Systems
19
OS and Processors
Can any OS run on any
processor?
Operating systems are software
Operating systems are designed
and developed for a specific CPU
family.
◦ Macintosh OS: Motorola 680xx,
PowerPC Gx, Intel
◦ DOS: Intel CPUs
◦ Windows 9x and XP: Intel 80386,
80486, and Pentium CPUs
◦ Linux: Intel CPUs
◦ MS NT & 2000: Intel CPUs
Operating Systems
20
Compatibility of Software
Question
Will software developed for one operating
system work on another?
For example will MS Word for Macintosh run
on a PC with Windows XP?
Operating Systems
21
Compatibility of Software
Question
Will software developed for one operating system
work on another?
Answer
No (unless there is special emulation software or
hardware). The software is typically developed
separately for each operating system.
Operating Systems
22
Evolution of Operating systems
Serial Processing (1940 – mid
1950s)
◦ Single user system.
◦ Programmer/User acts as the
operator and interacted with
the hardware.
◦ No operating system.
◦ Machines run from a console
with display lights, toggle
switches.
◦ Paper Tapes or Punched cards
for the program and I/O.
◦ Setup included loading the
compiler, source program,
saving compiled program, and
loading and linking
Operating Systems
Disadvantages :
 Scheduling: Hardcopy sigh-up sheet for
reserving time
 User could reserve for 45 mins and finish
in 30 mins => wastage of time
 User may not be able to finish in
scheduled time
23
Simple batch system
Simple Batch Processing Systems
◦ Use of high-level languages
◦ Jobs are batched together by the
language.
◦ Input/output is through punch
cards and magnetic tapes.
◦ Software called the Monitor was
introduced to sequence the jobs.
◦ Hardware support for the
monitor model
◦ Memory protection: some memory
areas are accessible only to the
monitor
◦ Privileged mode instructions: only
accessible to the monitor
◦ Interrupts (early machines did not
have this)
Operating Systems
24
Simple batch processing system
• The user submits a job (written on cards or tape) to a
computer operator.
• The computer operator place a batch of several jobs on an
input device.
• A special program called the monitor, manages the
execution of each program in the batch.
◦ “Monitor” is always in main memory.
◦ Monitor reads and loaded programs sequentially and then (the utility
programs when needed) passed the control to the loaded program.
◦ When a job terminates the control returns back to the monitor program.
◦ Alternate execution between user program and the monitor program.
• instructions for the monitor were given by using a special
purpose language called Job Control Language (JCL)
Operating Systems
25
Simple batch system …..
• A user program executes in user mode, in
which certain areas of memory were
protected from the user’s use, and user
program is not allowed to execute certain
instructions.
•The monitor executes in a system mode, or a
kernel mode and it can execute privileged
instructions and can access protected
memory segments.
• Machine time alternates between monitor
and the user programs.
Operating Systems
26
Simple batch system …..
Disadvantages
•A portion of the memory has to be
allocated for the monitor
•A small portion of the machine time is
consumed by the monitor.
Advantage of batch systems.
•Reduce setup time by batching similar
jobs.
Operating Systems
27
Card Deck of a job
Operating Systems
28
Simple batch system : Problems
◦ During I/O operations CPU is not used.
Operating Systems
29
Simple batch system : I/O
◦ I/O devices (Card Readers, Printers) slow when compared
to CPU.
Solution: Offline Operation (Satellite Computers)
◦ Speed up computation by loading jobs into memory from
tapes while card reading and line printing is
done off-line using smaller machines.
Operating Systems
30
Multiprogramming
Running multiple programs “at the same time”
◦ Requires sharing the CPU among multiple processes
Firefox
Word
javac
Firefox
Word
Transfer of control is called a context switch
Operating Systems
31
Multiprogramming
Why multiprogramming?
◦ Single user cannot keep CPU and I/O devices
busy at all times.
Multiprogramming organizes jobs (code and data)
so CPU always has one to execute.
• A subset of total jobs in system is kept in
memory.
• One job selected and CPU is give for that job.
• When it has to wait, OS switches to another job.
Operating Systems
32
Multiprogramming
Operating Systems
33
Time Sharing Systems
• Processor’s time is shared among multiple users
• Multiple users simultaneously access the system through
terminals.
Operating Systems
34
Multiprogramming Vs Time
Sharing Systems
• Multiprogramming maximizes CPU utilization
• Time-sharing minimizes user response time
Operating Systems
35
Loading the Operating System
• OS is also a software like any other, but has to be
loaded and run by the OS itself.
• The process of initializing the computer and loading
the OS is known as bootstrapping or booting the
system.
• The bootstrapping program normally exist in nonvolatile memory and is executed automatically when
the machine is turned on.
• The operating system software (kernel) copied into
RAM, usually from the hard disk, during the boot-up.
• Once loaded the OS wait for an event to occur (eg: user
typing a command) and process the event.
• OS is an event driven software.
Operating Systems
36
Loading the Operating System
• The kernel remains in RAM while the computer is
on and is in charge of the overall operation of the
computer system.
• The kernel contains the “internal programs” for
the most often used operations like file
management, memory management, security.
Operating Systems
37
Gaining Control
 The OS gets the control of the CPU when either an
external event or an internal event occurs.
 External Events
 Character typed at the console
 Completion of an I/O operation
 Timer quantum allowed for a process expires.
 Internal Events
 Division by zero,
 System call issued by a program
 Page Fault
 Unauthorized memory access
Operating Systems
38
Interrupts
 External events get the attention of the CPU
through Interrupts.
 For example when a disk driver has finished
transferring the requested data, it generates an
interrupt to the OS to inform the OS that the
task is over.
 Interrupts occur asynchronously to the ongoing
activity of the processor. Thus the times at which
interrupts occur are unpredictable.
Operating Systems
39
Interrupt Handlers
 Interrupt Handlers : Code that get executed
when an interrupt occurs.
 Associated with each type of interrupt
there is a specific program to handle that
type of interrupts – Interrupt handler
(Interrupt service routine)
Operating Systems
40
Getting the services of OS
 How do the user programs get the service of OS?
 User programs access the functionality of the OS through system calls –
privileged operations.
 Example : open(), close(), fork(),…….
 The execution of system call change the execution mode of the CPU to
supervisor mode.
Operating Systems
41
Processes
 Process is a fundamental concepts in modern operating systems.
 It was first introduced by the designers of Multics operating systems
in the 1960s.
 The programs that reside in main memory are absolutely different
from their counter-parts the program files on hard disks or any other
secondary storage devices.
Operating Systems
42
Process and a program
• A process is 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 program is a static set of instructions
Operating Systems
43
Process and a program
• A process exists in a limited span of time. Two or
more processes could be executing the same
program, each using their own data and
resources.
• A program is a static entity made up of
instructions. A program exists in the secondary
storage till it is deleted. A program does not
perform the action by itself.
Operating Systems
44
Process creation and termination
• When a new process is created, the operating system
builds the date structures that are used to manage the
process and allocates space in main memory to the
process.
• A process may terminates in a number of ways.
• After completion of the instructions.
• User terminates (kills) the process explicitly. For
example clicking on the cross button in the windows
applications.
• A process may terminate due to abnormal condition.
• When a process finishes, the operating system will free
the memory space it occupies and remove the data
structures it allocated to manage the process.
Operating Systems
45
Processes in a typical Linux system
Operating Systems
46
Uniprocessor Scheduling
Type of processes
• I/O bound processes
• Processor bound processes
Operating Systems
47
Uniprocessor Scheduling
Types of Scheduling
• Long-term scheduling(Job scheduling) : It determines
which programs are admitted to the system for
processing. Job scheduler selects processes from the
queue and loads them into memory for execution. Process
loads into the memory for CPU scheduling.
• Medium-term scheduling : Medium term scheduling is in
charge of swapping processes between the main memory
and the secondary storage.
• Short-term scheduling (low-level scheduling) : Determines
which ready process will be assigned the CPU when it next
becomes available.
Operating Systems
48
Scheduling Policies
◦ Non-preemptive
◦ Once a process is in the running state, it will
continue until it terminates or blocks itself for
I/O.
◦ Preemptive
◦ Currently running process may be interrupted
and moved to the Ready state by the OS.
◦ Allows for better service since any one process
cannot monopolize the processor for very long
Operating Systems
49
Processor Scheduling
Assigning the processor to the processes.
Turnaround time : Time required for a particular process to complete,
from submission time to completion.
Response time : The time taken in an interactive program from the
issuance of a command to the commence of a response to that
command.
Throughput : Number of processes completed per unit time. May range
from 10 / second to 1 / hour depending on the specific processes.
Waiting time : How much time a process spends in the ready queue
waiting its turn to get on the CPU.
Operating Systems
50
Long-term scheduling(Job
scheduling)
• Determines which processes are admitted to the
system for processing
• Controls the degree of multiprogramming
• If more processes are admitted
• better CPU usage
• less likely that all processes will be blocked
• The long term scheduler may attempt to keep a
mix of processor-bound and I/O-bound processes
Operating Systems
51
Medium-Term Scheduling
• Swapping decisions based on the need to manage
multiprogramming
• Done by memory management software
Operating Systems
52
Logical view of swapping
Operating Systems
53
Short-Term Scheduling
• Determines which process is going to execute next
(also called CPU scheduling)
• The short term scheduler is known as the
dispatcher
• Dispatching the CPU to the process
Operating Systems
54
Schedulers - Comparison
Long Term Scheduler
Short Term Scheduler
Medium Term Scheduler
Job Scheduler
CPU scheduler
Processes swapping
scheduler
Selects processes from a Selects those processes
pool and loads them into which are ready to
the memory for
execute for dispatching
execution
Swapped out/Reintroduces the processes
into memory and
execution can be
continued.
Controls the degree of
multiprogramming
Provides lesser control
over the degree of
multiprogramming
Controls the degree of
multiprogramming
Speed is lesser than
short term scheduler
Speed is fastest among
other two
Speed is in between
(short and long term
schedulers)
Operating Systems
55
Queuing Diagram for Scheduling
Operating Systems
56
Seven state process transition
Diagram
Typically, new
processes are not in the
main memory.
Operating Systems
57
Seven state process transition
Diagram
Medium Term
Scheduler
Operating Systems
Long Term
Scheduler
Short Term
Scheduler
58
Process Control Block (PCB)
All of the information needed to keep track of a process
when switching states is kept in a data package called a
process control block. The process control block typically
contains:
• An ID number that identifies the process
• Pointers to the locations in the program and its data where processing
last occurred
• Register contents
• States of various flags and switches
• A list of files opened by the process
• The priority of the process
• The status of all I/O devices needed by the process
Operating Systems
59
Context Switching
• A context switch is the mechanism to store and restore the state or
context of a CPU in Process Control block so that a process execution
can be resumed from the same point at a later time.
• Using this technique a context switcher enables multiple processes to
share a single CPU. Context switching is an essential part of a
multitasking operating system features.
• When the scheduler switches the CPU from executing one process to
execute another, the context switcher saves the content of all
processor registers for the process being removed from the CPU, in its
process control block.
• Context switch time is pure overhead.
• Context switching can significantly affect performance as modern
computers have a lot of general and status registers to be saved.
Operating Systems
60
Context Switching
Operating Systems
61
Short-term scheduling algorithms
• First-come-first served
• Round Robin
• Shortest Process next
Operating Systems
62
Objectives of Short-Term
Scheduling
User-oriented
◦ Response Time: Elapsed time from the
submission of a request to the beginning of
response
◦ Turnaround Time: Elapsed time from the
submission of a process to its completion
System-oriented
◦ processor utilization
◦ fairness
◦ throughput: number of process completed per
unit time
Operating Systems
63
First-come-first served scheduling
• Each process joins the end of the Ready queue.
• When the current process ceases to execute, the
process waited the longest time in the Ready
queue is assigned the CPU.
Operating Systems
64
Example - FCFS
Process
Arrival
Time
Service
Time
Finish
time
Turnaround
time
A
0
3
3
3
B
2
6
9
7
C
4
4
13
9
D
6
5
18
12
E
8
2
20
12
Operating Systems
Average Turnaround time
= (3 + 7+ 9+ 12 + 12)/5
= 8.60
65
Shortest Job First
(Shortest Process Next)
• Nonpreemptive policy
• Process with shortest expected processing time is
selected next
• Short process jumps ahead of longer processes
Operating Systems
66
Shortest Job First
(Shortest Process Next)
Process
Arrival
Time
Service
Time
Finish
time
Turnaround
time
A
0
3
3
3
B
2
6
9
7
C
4
4
15
11
D
6
5
20
14
E
8
2
11
3
Operating Systems
Average Turnaround time
= (3 + 7+ 11+ 14 + 3)/5
= 7.60
67
FCFS - Issues
A short process may have to wait a very
long time before it can execute
Favors CPU-bound processes
◦I/O processes have to wait until CPUbound process completes
Operating Systems
68
Round Robin Scheduling
• Clock interrupt is generated at periodic intervals.
• When an interrupt occurs, the currently running
process is placed in the ready queue
(preempted)
• Next process in the ready queue is assigned the
CPU.
• Known as time slicing
Operating Systems
69
Queuing Diagram
Operating Systems
70
Round Robin Scheduling
Scheduling Policy – Preemption, Time quantum for each process = 1
Process
Arrival
Time
Service
Time
Finish
time
Turnaround
time
A
0
3
4
4
B
2
6
18
16
C
4
4
17
13
D
6
5
20
14
E
8
2
15
7
0
Operating Systems
5
Average Turnaround time
= (4 + 16+ 13+ 14 + 7)/5
= 10.80
10
15
20
71
Round Robin Scheduling
Scheduling Policy – Preemption, Time quantum for each process = 4
Process
Arrival
Time
Service
Time
Finish
time
Turnaround
time
A
0
3
3
3
B
2
6
17
15
C
4
4
11
7
D
6
5
20
14
E
8
2
19
11
Average Turnaround time
= ( 3+ 15+ 7+ 14 + 11)/5
= 10.00
Gantt Chart ?
Operating Systems
72
Priority Scheduling
• Scheduler will always choose a process of higher
priority over one of lower priority
• Use multiple ready queues to represent multiple
levels of priority
• Lower-priority may suffer starvation
• Allow a process to change its priority based on its age or
execution history
Operating Systems
73
Priority Queuing
Can be either preemptive or non-preemptive
Operating Systems
74
Threads
A thread is the smallest schedulable unit in a system that can be
managed independently by an operating system. A thread can also be
viewed as an execution streams within a single process. Generally a
thread contained inside a process. It is possible to have processes with
one threads or processes with multiple threads.
All threads of a process share common code, data, and other resources,
including CPU registers.
• Allows multiple tasks to be performed simultaneously in a single address
space.
• Context switching generates less overhead.
Operating Systems
75
Threads …..
Threads are very useful whenever a process has multiple tasks to
perform independently of the others.
For example in a word processor, a background thread may check
spelling of a document while a foreground thread processes user
keystrokes, while another thread may automatically backs up the
edited section periodically.
Operating Systems
76
Von Neumann Architecture –
Hardware components
The hardware for a Von Neumann machine consists of three principle
components; processor, memory, and I/O facilities.
Both programs and data are stored in the memory
Operating Systems
77