Unit 6 Operating Systems

77
Unit 6 Operating Systems Dr Damitha Karunaratna University of Colombo school of computing

description

Unit 6 Operating Systems. Dr Damitha Karunaratna University 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. - PowerPoint PPT Presentation

Transcript of Unit 6 Operating Systems

Page 1: Unit 6 Operating  Systems

Unit 6Operating Systems

Dr Damitha Karunaratna

University of Colombo school of computing

Page 2: Unit 6 Operating  Systems

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

Page 3: Unit 6 Operating  Systems

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

Page 4: Unit 6 Operating  Systems

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

Page 5: Unit 6 Operating  Systems

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

Page 6: Unit 6 Operating  Systems

Types of Software

Operating Systems 6

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

Page 7: Unit 6 Operating  Systems

What is an operating system(OS)?

Operating Systems 7

Page 8: Unit 6 Operating  Systems

What is an operating system(OS)?

Operating Systems 8

Kernel : Part of the O/S that resides in the main memory all the time.

Page 9: Unit 6 Operating  Systems

Execution of source programs

Operating Systems 9

Page 10: Unit 6 Operating  Systems

Address mapping for re-locatable programs

Operating Systems 10

Page 11: Unit 6 Operating  Systems

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

Page 12: Unit 6 Operating  Systems

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

Page 13: Unit 6 Operating  Systems

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

Page 14: Unit 6 Operating  Systems

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

Page 15: Unit 6 Operating  Systems

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

Page 16: Unit 6 Operating  Systems

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

Page 17: Unit 6 Operating  Systems

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

Page 18: Unit 6 Operating  Systems

Types of User Interfaces Command Line Interface(CLI)

Operating Systems 18

Page 19: Unit 6 Operating  Systems

Types of User Interfaces Graphical User Interface(GUI)

Operating Systems 19

Page 20: Unit 6 Operating  Systems

OS and Processors

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

Can any OS run on any processor?

Page 21: Unit 6 Operating  Systems

Compatibility of Software

QuestionWill 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

Page 22: Unit 6 Operating  Systems

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

Page 23: Unit 6 Operating  Systems

Evolution of Operating systemsSerial 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 23

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

Page 24: Unit 6 Operating  Systems

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

Page 25: Unit 6 Operating  Systems

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

Page 26: Unit 6 Operating  Systems

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

Page 27: Unit 6 Operating  Systems

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

Page 28: Unit 6 Operating  Systems

Card Deck of a job

Operating Systems 28

Page 29: Unit 6 Operating  Systems

Simple batch system : Problems◦ During I/O operations CPU is not used.

Operating Systems 29

Page 30: Unit 6 Operating  Systems

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

Page 31: Unit 6 Operating  Systems

Multiprogramming Running multiple programs “at the same time”

◦ Requires sharing the CPU among multiple processes

Transfer of control is called a context switch

Operating Systems 31

Firefox Word javac Firefox Word

Page 32: Unit 6 Operating  Systems

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

Page 33: Unit 6 Operating  Systems

Multiprogramming

Operating Systems 33

Page 34: Unit 6 Operating  Systems

Time Sharing Systems• Processor’s time is shared among multiple users

• Multiple users simultaneously access the system through terminals.

Operating Systems 34

Page 35: Unit 6 Operating  Systems

Multiprogramming Vs Time Sharing Systems• Multiprogramming maximizes CPU utilization• Time-sharing minimizes user response time

Operating Systems 35

Page 36: Unit 6 Operating  Systems

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 non-

volatile 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

Page 37: Unit 6 Operating  Systems

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

Page 38: Unit 6 Operating  Systems

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

Page 39: Unit 6 Operating  Systems

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

Page 40: Unit 6 Operating  Systems

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

Page 41: Unit 6 Operating  Systems

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

Page 42: Unit 6 Operating  Systems

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

Page 43: Unit 6 Operating  Systems

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

Page 44: Unit 6 Operating  Systems

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

Page 45: Unit 6 Operating  Systems

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

Page 46: Unit 6 Operating  Systems

Processes in a typical Linux system

Operating Systems 46

Page 47: Unit 6 Operating  Systems

Uniprocessor Scheduling

Type of processes• I/O bound processes• Processor bound processes

Operating Systems 47

Page 48: Unit 6 Operating  Systems

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

Page 49: Unit 6 Operating  Systems

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

Page 50: Unit 6 Operating  Systems

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

Page 51: Unit 6 Operating  Systems

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

Page 52: Unit 6 Operating  Systems

Medium-Term Scheduling• Swapping decisions based on the need to manage

multiprogramming

• Done by memory management software

Operating Systems 52

Page 53: Unit 6 Operating  Systems

Logical view of swapping

Operating Systems 53

Page 54: Unit 6 Operating  Systems

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

Page 55: Unit 6 Operating  Systems

Schedulers - ComparisonLong Term Scheduler Short Term Scheduler Medium Term Scheduler

Job Scheduler CPU scheduler Processes swapping scheduler

Selects processes from a pool and loads them into the memory for execution

Selects those processes which are ready to execute for dispatching

Swapped out/Re-introduces 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

Page 56: Unit 6 Operating  Systems

Queuing Diagram for Scheduling

Operating Systems 56

Page 57: Unit 6 Operating  Systems

Seven state process transition Diagram

Operating Systems 57

Typically, new processes are not in the main memory.

Page 58: Unit 6 Operating  Systems

Seven state process transition Diagram

Operating Systems 58

Long Term Scheduler

Short Term Scheduler

Medium Term Scheduler

Page 59: Unit 6 Operating  Systems

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

Page 60: Unit 6 Operating  Systems

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

Page 61: Unit 6 Operating  Systems

Context Switching

Operating Systems 61

Page 62: Unit 6 Operating  Systems

Short-term scheduling algorithms• First-come-first served

• Round Robin

• Shortest Process next

Operating Systems 62

Page 63: Unit 6 Operating  Systems

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

Page 64: Unit 6 Operating  Systems

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

Page 65: Unit 6 Operating  Systems

Example - FCFS

Operating Systems 65

Process Arrival Time

Service Time

Finish time

Turnaround time

A 0 3 3 3B 2 6 9 7C 4 4 13 9D 6 5 18 12E 8 2 20 12

Average Turnaround time = (3 + 7+ 9+ 12 + 12)/5= 8.60

Page 66: Unit 6 Operating  Systems

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

Page 67: Unit 6 Operating  Systems

Shortest Job First(Shortest Process Next)

Operating Systems 67

Process Arrival Time

Service Time

Finish time

Turnaround time

A 0 3 3 3B 2 6 9 7C 4 4 15 11D 6 5 20 14E 8 2 11 3

Average Turnaround time = (3 + 7+ 11+ 14 + 3)/5= 7.60

Page 68: Unit 6 Operating  Systems

FCFS - Issues

Operating Systems 68

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 CPU-bound process completes

Page 69: Unit 6 Operating  Systems

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

Page 70: Unit 6 Operating  Systems

Queuing Diagram

Operating Systems 70

Page 71: Unit 6 Operating  Systems

Round Robin Scheduling Scheduling Policy – Preemption, Time quantum for each process = 1

Operating Systems 71

Process Arrival Time

Service Time

Finish time

Turnaround time

A 0 3 4 4B 2 6 18 16C 4 4 17 13D 6 5 20 14E 8 2 15 7

0 5 10 15 20

Average Turnaround time = (4 + 16+ 13+ 14 + 7)/5= 10.80

Page 72: Unit 6 Operating  Systems

Round Robin Scheduling Scheduling Policy – Preemption, Time quantum for each process = 4

Operating Systems 72

Process Arrival Time

Service Time

Finish time

Turnaround time

A 0 3 3 3B 2 6 17 15C 4 4 11 7D 6 5 20 14E 8 2 19 11

Average Turnaround time = ( 3+ 15+ 7+ 14 + 11)/5= 10.00

Gantt Chart ?

Page 73: Unit 6 Operating  Systems

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

Page 74: Unit 6 Operating  Systems

Priority Queuing

Operating Systems 74

Can be either preemptive or non-preemptive

Page 75: Unit 6 Operating  Systems

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

Page 76: Unit 6 Operating  Systems

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

Page 77: Unit 6 Operating  Systems

Von Neumann Architecture – Hardware components

The hardware for a Von Neumann machine consists of three principle components; processor, memory, and I/O facilities.

Operating Systems 77

Both programs and data are stored in the memory