CSE210 MODULE 1 + Process Part of Module 2
CSE210 MODULE 1 + Process Part of Module 2
Operating Systems
Contents
Module 1: Introduction [10 Hrs] [Knowledge Level (1)]
Introduction to OS – Computer System Architecture , Operating
System Structure, Operations,– Different
management activities handled by the OS, Computing
environments, Operating System Services, User and OS interface,
System Calls and its types, System Programs[ loaders, linkers...],
Overview of OS design and implementation.
Module 2 : Process Concept, Operation on processes, IPC,
Course Outcome: Describe the fundamental concepts of
Operating Systems.
Operating Systems
2
What is Operating System ?
• An operating system is a program that manages a
computer’s hardware. It also provides a basis for
application programs and acts as an intermediary
between the computer user and the computer hardware.
• Operating system goals:
• A program that controls the execution of application
programs
• Make the computer system convenient to use.
• Use the computer hardware in an efficient manner.
Operating Systems
3
Where does the OS fit in?
• Basic Taxonomy
HW SW
Applications System
OS Non-OS
• Software is differentiated according to its purpose
• System software provides a general environment where
programmers/developers can create applications and
users can run applications
Operating Systems
4
Introduction
• A computer system
consists of
• hardware
• system programs
• application
programs
Operating Systems
5
System Goals
• Objectives of an OS
• Convenience
• An operating system makes a computer more
convenient to use.
• Efficiency
• An operating system allows the computer system
resources to be used in an efficient manner.
• Ability to Evolve
• Should permit effective development, testing, and
introduction of new system features and functions without
interfering with service.
Operating Systems
6
Two View points
• User view
• User sits in front of PC
• User sit at a terminal connected to a mainframe
• System view
• Resource allocator
• Control program
Operating Systems
7
Abstract view of a computer
system
1. Hardware – provides basic computing resources (CPU, memory, I/O
devices).
2. Operating system – controls and coordinates the use of the hardware
among the various application programs for the various users.
3. Applications programs – define the ways in which the system resources
are used to solve the computing problems of the users (compilers,
database systems, video games, business programs).
4. Users (people, machines, other computers).
Operating Systems
8
Abstract View of System Components
Operating Systems
9
Operating System Definitions
• Resource allocator – manages and allocates
resources.
• Control program – controls the execution of user
programs and operations of I/O devices .
• Kernel – the one program running at all times
[Everything else is either a system program (ships
with the operating system) , or an application
program].
Operating Systems
10
Operating System as a resource
manager
• It is actually a program that is executed by the
processor to control the processor!
• Directs the processor in the use of system resources
• Directs the processor when executing other
programs
• Processor stops executing the operating system in order to
execute other programs and, then, gives the control back to
the operating system
Operating Systems
11
Operating System as a resource
manager
• Memory management is decided by the
operating system and memory management
hardware in the processor.
• The operating system decides about the access of
programs and files to I/O devices.
• In case of multiple processors, it decides for
them all
Operating Systems
12
Operating system timeline
• First generation: 1945 – 1955
• Vacuum tubes
• Plug boards • Next generation: ???
• Second generation: 1955 – 1965 • Systems connected by high-
• Transistors speed networks?
• Batch systems • Wide area resource
management?
• Third generation: 1965 – 1980
• Integrated circuits
• Multiprogramming
• Fourth generation: 1980 – present
• Large scale integration
• Personal computers
Operating Systems
13
First generation: direct input
• Run one job at a time
• Enter it into the computer (might require
rewiring!)
• Run it
• Record the results
• Problem: lots of wasted computer time!
• Computer was idle during first and last steps
• Computers were very expensive!
• Goal: make better use of an expensive commodity:
computer time
Operating Systems
14
Second generation: batch systems
Operating Systems 15
• Advantages of batch systems
• move much of the work of the operator to the
computer
• increased performance since it was possible for job to
start as soon as the previous job finished
• Disadvantages
• turn-around time can be large from user standpoint
• more difficult to debug program
• due to lack of protection scheme, one batch job can
affect pending jobs (read too many cards, etc)
• a job could corrupt the monitor, thus affecting
pending jobs
• a job could enter an infinite loop
Operating Systems
16
Spooling
• Original batch systems used tape drives
• Later batch systems used disks for buffering
• Operator read cards onto disk attached to the computer
• Computer read jobs from disk
• Computer wrote job results to disk
• Operator directed that job results be printed from disk
• Disks enabled simultaneous peripheral operation on-line
(spooling)
• Computer overlapped I/O of one job with execution of
another
• Better utilization of the expensive CPU
• Still only one job active at any given time
Operating Systems
17
Third generation: multiprogramming
• Multiple jobs in memory
• Protected from one
another
Job 3 • Operating system protected
from each job as well
• Resources (time, hardware)
Job 2 split between jobs
Memory • Still not interactive
partitions Job 1 • User submits job
• Computer runs it
• User gets results minutes
(hours, days) later
Operating
system
Operating Systems 18
Timesharing
• It’s a logical extension of multiprogramming.
• The CPU executes multiple jobs by switching among them
• It allows many users to share the computer
simultaneously
• It uses time scheduling and multiprogramming to provide
each user with a small portion of a time shared computer.
Operating Systems
19
Timesharing
Operating Systems
20
Desktop Systems
• Personal computers – computer system dedicated to a single user.
• I/O devices – keyboards, mice, display screens, small printers.
• User convenience and responsiveness.
• Can adopt technology developed for larger operating system’ often
individuals have sole use of computer and do not need advanced
CPU utilization of protection features.
• May run several different types of operating systems
(Windows, MacOS, UNIX, Linux)
Operating Systems
21
Parallel Systems
• Multiprocessor systems with more than on CPU in close
communication.
• Tightly coupled system – processors share memory and a clock;
communication usually takes place through the shared memory.
• Advantages of parallel system:
• Increased throughput
• Economical
• Increased reliability
• graceful degradation
• fail-soft systems
Operating Systems
22
A Dual-Core Design
• Multi-chip and multicore
• Systems containing all chips
• Chassis containing multiple separate systems
Parallel Systems (Cont.)
• Symmetric multiprocessing (SMP)
• Each processor runs and identical copy of the
operating system.
• Many processes can run at once without
performance deterioration.
• Most modern operating systems support SMP
• Asymmetric multiprocessing
• Each processor is assigned a specific task; master processor
schedules and allocated work to slave processors.
• More common in extremely large systems
Operating Systems
24
Symmetric Multiprocessing
Architecture
Operating Systems 25
Distributed Systems
• Distribute the computation among several physical
processors.
• Loosely coupled system – each processor has its own local
memory; processors communicate with one another through
various communications lines, such as high-speed buses or
telephone lines.
• Advantages of distributed systems.
• Resources Sharing
• Computation speed up – load sharing
• Reliability
• Communications
Operating Systems
26
Distributed Systems (cont)
• Requires networking infrastructure.
• Local area networks (LAN) or Wide area
networks (WAN)
• May be either client-server or peer-to-peer
systems.
Operating Systems
27
General Structure of Client-
Server
Operating Systems 28
Handheld Systems
• Personal Digital Assistants (PDAs)
• Cellular telephones
• Issues:
• Limited memory
• Slow processors
• Small display screens.
Operating Systems
29
Clustered Systems
• Like multiprocessor systems, but multiple systems working together
• Usually sharing storage via a storage-area network (SAN)
• Provides a high-availability service which survives failures
• Asymmetric clustering has one machine in hot-standby mode
• Symmetric clustering has multiple nodes running applications,
monitoring each other
• Some clusters are for high-performance computing (HPC)
• Applications must be written to use parallelization
Clustered Systems
Hardware Protection
• Dual-Mode Operation
• I/O Protection
• Memory Protection
• CPU Protection
Operating Systems
32
Dual-Mode Operation
• Sharing system resources requires operating system to
ensure that an incorrect program cannot cause other
programs to execute incorrectly.
• Provide hardware support to differentiate between at least
two modes of operations.
1. User mode – execution done on behalf of a user.
2.Monitor mode (also kernel mode or system mode) –
execution done on behalf of operating system.
Operating Systems
33
Dual-Mode Operation (Cont.)
• Mode bit added to computer hardware to indicate the current
mode: monitor (0) or user (1).
• When an interrupt or fault occurs hardware switches to monitor
mode.
Interrupt/fault
monitor user
Operating Systems
34
I/O Protection
• All I/O instructions are privileged instructions.
• Must ensure that a user program could never gain control
of the computer in monitor mode (I.e., a user program
that, as part of its execution, stores a new address in the
interrupt vector).
Operating Systems
35
Use of A System Call to Perform I/O
Operating Systems 36
Memory Protection
• Must provide memory protection at least for the interrupt
vector and the interrupt service routines.
• In order to have memory protection, add two registers that determine the
range of legal addresses a program may access:
• Base register – holds the smallest legal physical
memory address.
• Limit register – contains the size of the range
• Memory outside the defined range is protected.
Operating Systems
37
Use of A Base and Limit Register
Operating Systems 38
Hardware Address Protection
Operating Systems
39
Hardware Protection
Operating Systems
40
CPU Protection
• Timer – interrupts computer after specified period to
ensure operating system maintains control.
• Timer is decremented every clock tick.
• When timer reaches the value 0, an interrupt
occurs.
• Timer commonly used to implement time sharing.
• Time also used to compute the current time.
• Load-timer is a privileged instruction.
Operating Systems
41
Operating System Services
• Program execution – system capability to load a program into memory
and to run it.
• I/O operations – since user programs cannot execute I/O operations
directly, the operating system must provide some means to perform I/O.
• File-system manipulation – program capability to read, write, create,
and delete files.
• Communications – exchange of information between processes executing
either on the same computer or on different systems tied together by a
network.
• Error detection – ensure correct computing by detecting errors in the
CPU and memory hardware, in I/O devices, or in user programs.
Operating Systems
42
Additional Operating System Functions
Additional functions exist not for helping the user, but rather
for ensuring efficient system operations.
• Resource allocation – allocating resources to multiple users or
multiple jobs running at the same time.
• Accounting – keep track of and record which users use how much
and what kinds of computer resources for account billing or for
accumulating usage statistics.
• Protection – ensuring that all access to system
resources is controlled.
Operating Systems
43
Process Management
• A process is a program in execution. It is a unit of work within the system.
Program is a passive entity, process is an active entity.
• Process needs resources to accomplish its task
• CPU, memory, I/O, files
• Initialization data
• Process termination requires reclaim of any reusable resources
• Single-threaded process has one program counter specifying location of next
instruction to execute
• Process executes instructions sequentially, one at a time, until completion
• Multi-threaded process has one program counter per thread
• Typically system has many processes, some user, some operating system
running concurrently on one or more CPUs
• Concurrency by multiplexing the CPUs among the processes / threads
Process Management Activities
The operating system is responsible for the following
activities in connection with process management:
n Client-Server Computing
l Dumb terminals supplanted by smart PCs
l Many systems now servers, responding to requests generated
by clients
Compute-server system provides an interface to client to
request services (i.e., database)
File-server system provides interface for clients to store
and retrieve files
Computing Environments - Peer-to-Peer
• Soft real-time
• Limited utility in industrial control of robotics
• Useful in applications (multimedia, virtual reality)
requiring advanced operating-system features.
Operating Systems
64
System Calls
• Programming interface to the services provided by the OS
• Typically written in a high-level language (C or C++)
• Mostly accessed by programs via a high-level Application Programming
Interface (API) rather than direct system call use
• Three most common APIs are Win32 API for Windows, POSIX API for
POSIX-based systems (including virtually all versions of UNIX, Linux, and
Mac OS X), and Java API for the Java virtual machine (JVM)
Note that the system-call names used throughout this ppt are generic
Example of System Calls
• System call sequence to copy the contents of one
file to another file
Example of Standard API
System Call Implementation
• Typically, a number associated with each system call
• System-call interface maintains a table indexed according to these
numbers
• The system call interface invokes the intended system call in OS
kernel and returns status of the system call and any return values
• The caller need know nothing about how the system call is
implemented
• Just needs to obey API and understand what OS will do as a
result call
• Most details of OS interface hidden from programmer by API
• Managed by run-time support library (set of functions built into
libraries included with compiler)
API – System Call – OS
Relationship
System Call Parameter Passing
• Often, more information is required than simply identity of
desired system call
• Exact type and amount of information vary according to OS
and call
• Three general methods used to pass parameters to the OS
• Simplest: pass the parameters in registers
• In some cases, may be more parameters than registers
• Parameters stored in a block, or table, in memory, and
address of block passed as a parameter in a register
• This approach taken by Linux and Solaris
• Parameters placed, or pushed, onto the stack by the program and
popped off the stack by the operating system
• Block and stack methods do not limit the number or length of
parameters being passed
Parameter Passing via Table
Types of System Calls
• Process control
• create process, terminate process
• end, abort
• load, execute
• get process attributes, set process attributes
• wait for time
• wait event, signal event
• allocate and free memory
• Dump memory if error
• Debugger for determining bugs, single step execution
• Locks for managing access to shared data between
processes
Types of System Calls
• File management
• create file, delete file
• open, close file
• read, write, reposition
• get and set file attributes
• Device management
• request device, release device
• read, write, reposition
• get device attributes, set device attributes
• logically attach or detach devices
Types of System Calls (Cont.)
• Information maintenance
• get time or date, set time or date
• get system data, set system data
• get and set process, file, or device attributes
• Communications
• create, delete communication connection
• send, receive messages if message passing model to host
name or process name
• From client to server
• Shared-memory model create and gain access to memory
regions
• transfer status information
• attach and detach remote devices
Types of System Calls (Cont.)
• Protection
• Control access to resources
• Get and set permissions
• Allow and deny user access
Examples of Windows and Unix System Calls
Standard C Library Example
• C program invoking printf() library call, which
calls write() system call
Operating System Structure
• General-purpose OS is very large program
• Various ways to structure ones
• Simple structure – MS-DOS
• More complex -- UNIX
• Layered – an abstrcation
• Microkernel -Mach
Simple Structure -- MS-DOS
• MS-DOS – written to
provide the most
functionality in the
least space
• Not divided into
modules
• Although MS-DOS has
some structure, its
interfaces and levels of
functionality are not well
separated
Non Simple Structure -- UNIX
• UNIX – limited by hardware functionality, the
original UNIX operating system had limited
structuring.
• The UNIX OS consists of two separable parts
• Systems programs
• The kernel
• Consists of everything below the system-call interface and
above the physical hardware
• Provides the file system, CPU scheduling, memory management,
and other operating-system functions; a large number of functions
for one level
Traditional UNIX System Structure
Beyond simple but not fully layered
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
Microkernel System Structure
• Moves as much from the kernel into user space
• Mach example of microkernel
• Mac OS X kernel (Darwin) partly based on Mach
• Communication takes place between user modules using
message passing
• Benefits:
• Easier to extend a microkernel
• Easier to port the operating system to new architectures
• More reliable (less code is running in kernel mode)
• More secure
• Detriments:
• Performance overhead of user space to kernel space
communication
Microkernel System Structure
Application File Device user
Program System mode
Driver
messages messages
microkernel
hardware
Modules
• Many modern operating systems implement
loadable kernel modules
• Uses object-oriented approach
• Each core component is separate
• Each talks to the others over known interfaces
• Each is loadable as needed within the kernel
• Overall, similar to layers but with more flexible
• Linux, Solaris, etc
Solaris Modular Approach
iOS
• Apple mobile OS for iPhone, iPad
• Structured on Mac OS X, added
functionality
• Does not run OS X applications
natively
• Also runs on different CPU architecture (ARM vs. Intel)
• Cocoa Touch Objective-C API for
developing apps
• Media services layer for graphics,
audio, video
• Core services provides cloud
computing, databases
• Core operating system, based on Mac OS X kernel
Android
• Developed by Open Handset Alliance (mostly Google)
• Open Source
• Similar stack to IOS
• Based on Linux kernel but modified
• Provides process, memory, device-driver management
• Adds power management
• Runtime environment includes core set of libraries and Dalvik virtual
machine
• Apps developed in Java plus Android API
• Java class files compiled to Java bytecode then translated to
executable than runs in Dalvik VM
• Libraries include frameworks for web browser (webkit), database
(SQLite), multimedia, smaller libc
89
Process Concept
• An operating system executes a variety of programs:
• Batch system – jobs
• Time-shared systems – user programs or tasks
• Process – a program in execution; process execution must progress
in sequential fashion
• Multiple parts
• The program code, also called text section
• Current activity including program counter, processor registers
• Stack containing temporary data
• Function parameters, return addresses, local variables
• Data section containing global variables
• Heap containing memory dynamically allocated during run time
Process Concept (Cont.)
• Program is passive entity stored on disk (executable file), process is
active
• Program becomes process when executable file loaded into
memory
• Execution of program started via GUI mouse clicks, command line
entry of its name, etc
• One program can be several processes
• Consider multiple users executing the same program
Process in Memory
Process State
• 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
Process Control Block (PCB)
Information associated with each process (also called task
control block)
• Process state – running, waiting, etc
• Program counter – location of instruction to
next execute
• CPU registers – contents of all process-
centric registers
• CPU scheduling information- priorities,
scheduling queue pointers
• Memory-management information – memory allocated
to the process
• Accounting information – CPU used, clock time elapsed since
start, time limits
• I/O status information – I/O devices allocated to process,
list of open files
CPU Switch From Process to Process
Context Switch
• 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 via a context switch
• Context of a process represented in the PCB
• Context-switch time is overhead; the system does no useful work
while switching
• The more complex the OS and the PCB the longer the
context switch
• Time dependent on hardware support
• Some hardware provides multiple sets of registers per CPU
multiple contexts loaded at once
Operations on Processes
• System must provide mechanisms for:
• process creation,
• process termination,
• and so on as detailed next
Process Creation
• Parent process create children processes, which, in turn create other
processes, forming a tree of processes
• Generally, process identified and managed via a process identifier
(pid)
• Resource sharing options
• Parent and children share all resources
• Children share subset of parent’s resources
• Parent and child share no resources
• Execution options
• Parent and children execute concurrently
• Parent waits until children terminate
A Tree of Processes in Linux
init
pid =
1
emacs tcsch
ps
pid = pid =
pid =
9204 4005
9298
Process Creation (Cont.)
• 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 Termination
• Process executes last statement and then asks the operating
system to delete it using the exit() system call.
• Returns status data from child to parent (via wait())
• Process’ resources are deallocated by operating system
• Parent may terminate the execution of children processes using
the abort() system call. Some reasons for doing so:
• Child has exceeded allocated resources
• Task assigned to child is no longer required
• The parent is exiting and the operating systems does not
allow a child to continue if its parent terminates
Process Termination
• Some operating systems do not allow child to exists if its parent has
terminated. If a process terminates, then all its children must also be
terminated.
• cascading termination. All children, grandchildren, etc. are
terminated.
• The termination is initiated by the operating system.
• The parent process may wait for termination of a child process by using
the wait()system call. The call returns status information and the pid of
the terminated process
pid = wait(&status);
• If no parent waiting (did not invoke wait()) process is a zombie
• If parent terminated without invoking wait , process is an orphan
Interprocess Communication
• Processes within a system may be independent or cooperating
• Cooperating process can affect or be affected by other processes,
including sharing data
• Reasons for cooperating processes:
• Information sharing
• Computation speedup
• Modularity
• Convenience
• Cooperating processes need interprocess communication (IPC)
• Two models of IPC
• Shared memory
• Message passing
Communications Models
(a) Message passing. (b) shared memory.
Interprocess Communication-Shared Memory
• Shared data
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item
buffer[BUFFER_SIZE
];
int in = 0; int out =
0;
/* consume the
item in next
consumed */
}
Interprocess Communication – Message Passing
• Mechanism for processes to communicate and to
synchronize their actions