0% found this document useful (0 votes)
22 views85 pages

Operating System new

The document provides an overview of operating systems, detailing their purpose, services, and various types such as single-user, batch, multiprogrammed, time-sharing, and real-time systems. It explains key concepts like process management, memory protection, and system calls, emphasizing the importance of efficient resource management and user interaction. Additionally, it discusses operating system structures including simple, layered, microkernel, and virtual machine approaches.

Uploaded by

samali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views85 pages

Operating System new

The document provides an overview of operating systems, detailing their purpose, services, and various types such as single-user, batch, multiprogrammed, time-sharing, and real-time systems. It explains key concepts like process management, memory protection, and system calls, emphasizing the importance of efficient resource management and user interaction. Additionally, it discusses operating system structures including simple, layered, microkernel, and virtual machine approaches.

Uploaded by

samali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 85

Operating Systems

Lecture 1
Purpose of a Computer
System
 Computer systems consist of
software and hardware that
are combined to provide a tool
to solve specific problems in
an efficient manner
 Execute programs
Layered View of a
Computer System
Computer System
Hardware
Integer Control Keyboard Mouse
Unit Unit
CD
Floating Point
Unit
Cache

Processor
System Bus
HD
Mem
Bus

RAM/ROM Printer Monitor


Main Issues in Operating
Computer System
 The following are some of the main issues involved
in performing these tasks.
 1. Storing an executable on a secondary storage
device such as hard disk
 2. Loading executable from disk into the main
memory
 3. Setting the CPU state appropriately so that
program execution could begin
 4. Creating multiple cooperating processes,
synchronizing their access to shared data, and
allowing them to communicate with each other
Operating System Services
to resolve the issues 1/4
 Manage secondary storage devices:
1. Allocate appropriate amount of disk
space when files are created
2. Deallocate space when files are
removing.
3. Insure that a new file does not
overwrite an existing file
4. Schedule disk requests
2/4
 Manage primary storage
1. Allocate appropriate amount of memory space
when programs are to be loaded into the memory
for executing
2. Deallocate space when processes terminate
3. Insure that a new process is not loaded on top of
an existing process
4. Insure that a process does not access memory
space that does not belong to it
5. Minimize the amount of unused memory space
6. Allow execution of programs larger in size than the
available main memory
3/4
 Manage Process
1. Allow simultaneous execution of
processes by scheduling the CPU(s)
2. Prevent deadlocks between
processes
3. Insure integrity of shared data

4. Synchronize executions of
cooperating processes
4/4
 Allow a user to manage his/her files
and directories properly
1. User view of directory structure

2. Provide a mechanism that allows


users to protect their files and
directories
What is an
Operating System?
 A program that acts as an intermediary
between a user of a computer and the
computer hardware—provides the user a
simpler (virtual) machine to work with
 A program that allocates and deallocates
computer system resources in an efficient,
fair, and secure manner—a resource
manager
Operating System
Goals
 Execute user programs and make solving
user problems easier.
 Make the computer system convenient to
use.
 Use the computer hardware in an efficient
manner.
Single User Systems
 Personal computers – computer
system dedicated to a single user.
 Interactive
 User convenience and
responsiveness.
Single User Systems
 Can adopt technology developed for
larger operating systems—multi-process,
multi-user
 Individuals usually have sole use of
computer and do not need advanced
protection features.
 May run several different
types of operating systems
(Windows, MacOS,
UNIX, Linux)
Batch Systems
 First Basic system.
 Reduce setup time by batching similar jobs
 Automatic job sequencing – automatically
transfers control from one job to another.
 Resident monitor :
 initial control in monitor

 control transfers to job

 when job completes

control transfers back to monitor


Memory Layout
Multiprogrammed Systems
Several jobs are kept in
main memory at the
same time, and the CPU is
multiplexed among them.
CPU Brust: It refers to the
amount of time that a
process uses the CPU to
execute its instructions.
I/O Brust: It refers to the
amount of time it takes
for a process to complete
I/O operations.
Multiprogrammed Systems
Example: Two processes P1 and P2 with
CPU and I/O bursts of one time unit each
CPU I/O
Burst Burst

P1
P2

P1

P2
OS Features Needed for
Multiprogramming
 SPOOLing (Simultaneous Peripheral Operation On-Line):
Spooling is the process of temporary storage of data for
use and execution by a device, program, or system.
 Memory management : multiple programs can run
simultaneously without interfering with each other's
data. The OS allocates separate memory spaces for
each program, so they don't overlap or access each
other's information.
 CPU scheduling: This feature decides which program
gets to use the CPU (brain of the computer) and for how
long. It's like a traffic cop, ensuring that each program
gets a fair share of CPU time, so they can run efficiently
and complete tasks.
Time-sharing Systems
• Interactive system with multiprogramming:
Allows multiple users to interact with the computer
simultaneously, executing multiple programs at the
same time.-
• Reasonable response time:

Provides fast response times to users by allocating short


time slots to each user or program.
• Job swapping:

Temporarily transfers programs or jobs from memory to


disk storage (and vice versa) to free up memory
space.
• Online file system:
Provides constant access to a file system for users to
store, retrieve, and share files, programs, and data.
Real-time Systems
 Well-defined fixed-time constraints.
 Often used as a control device in a
dedicated application such as
controlling scientific experiments,
medical imaging systems, industrial
control systems, and some display
systems.
 Real-Time systems may be either hard
or soft real-time.
Real-time Systems …
 Hard real-time systems:
 guarantees that critical tasks be
completed on time. This goal requires that
all delays in the system be completed on
time. This goal requires that all delays in
the system be bounded, from the retrieval
of stored data to the time it takes the
operating system to finish any request
made of it.
 Plane landing systems, process
control in nuclear power plants,
respirators, etc.
Real-time Systems ...
 Soft real-time systems
 Output should be produced within the
given time constraints but if it is not, the
result is not life threatening
 Useful in applications (multimedia,
virtual reality) requiring advanced
operating-system features.
Interrupts, Traps, and Signals
 An interrupt is a signal generated by
a hardware device (usually an I/O
device) to get CPU’s attention
 There are two types of interrupts: -
Hardware interrupt: When a
hardware device (like a keyboard or
mouse) needs attention, it sends a
signal to the CPU through the system
bus (like a messenger).
 - Software interrupt (System Call): Answer
When a program needs something the Phone
from the operating system (like .
accessing a file), it executes a special .

operation called a system call, which .

sends a notification to the CPU Resume


Interrupts, Traps, and Signals
 A trap (or an exception) is a
software-generated
interrupt caused either by
an error (division by zero or
invalid memory access) or
by a user request for an
operating system service Answer
the Phone
.
.
.

Resume
Interrupt Handling
 Interrupt transfers control to the
interrupt service routine, generally,
through the interrupt vector, which
contains addresses of all the
interrupt service routines.
 Interrupt architecture must save the
address of the instruction after the
interrupted instruction and the CPU
state so that execution of the
interrupted process may continue
after the interrupt has been serviced.
Interrupt Handling …
 Incoming interrupts are disabled while another
interrupt is being processed to prevent lost
interrupts.
 An operating system is interrupt driven.
 Whenever an interrupt occurs, the controller
completes the execution of the current instruction
and starts the execution of an Interrupt Service
Routine (ISR) or Interrupt Handler. ISR tells the
processor or controller what to do when the
interrupt occurs. The interrupts can be either
hardware interrupts or software interrupts.
Hardware Protection
 Dual-Mode Operation
 I/O Protection
 Memory Protection
 CPU Protection
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.
 User mode – execution done on behalf
of a user.
 Monitor mode (also kernel mode or
system mode) – execution done on
behalf of operating system.
Dual-Mode Operation …
 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
set user mode

Privileged instructions can be issued only in monitor mode.


I/O Protection
 All I/O instructions are privileged instructions.
 When CPU is executing in user mode, it is not
allowed to execute these privileged instructions.
If it try to do so, a trap is generated and program
will be terminated.
 This is because we don’t want a user process to
hold I/O devices directly
 So, I/O will be done by using a system call.
Memory Protection
 The region in the memory that a process is allowed to access
is known as process address space
 To ensure correct operation of a computer system, we need
to ensure that a process cannot access memory outside its
address space. If we don’t do this then a process may,
accidentally or deliberately, overwrite the address space of
another process or memory space belonging to the operating
system
 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 that the process is allowed to access.


 Limit register – contains the size of the process.

 Memory outside the defined range is protected.


Use of Base and Limit
Register
Hardware Support
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(because
load timer value isn’t decided by user)
OS Components
 Process management
 Main memory management
 Secondary storage management
 I/O system management
 File management
 Protection system
 Networking
 Command-line interpreter (shells)
Operating System
Services
Services for user and users of
programs:
 Program execution
 I/O Operations
 File System Manipulation
 Communications between
processes/users
 Error detection and handling
Operating System
Services …
Services for efficient system
operation:
 Resource management
 Accounting
 Protection
OS Kernel
Users
Applications
Operating System API(Application Program Interface,
AUI(Application User Interface

Operating System Kernel


Computer Hardware
Real work is done in the kernel
Entry Points into Kernel

System Call Signal

Interrupt Trap
System Calls
 User processes must not be given
open access to the kernel code
 The system call interface layer
contains entry point in the kernel
code
 Any user or application request that
involves access to any system
resource must be handled by the
kernel code
Types Of System Calls
 Process Control
 File Management
 Device Management
 Information
maintenance
 Communications
System Call Execution
 The user program makes a call to a
library function.
 Library routine puts appropriate
parameters at a well-known place
(registers, stack, or a table in memory).
 The trap instruction is executed to
change mode from user to kernel.
 Control goes to operating system.
 Operating system determines which
system call is to be carried out.
Semantics of System
Call Execution …
 Kernel indexes the dispatch table,
which contains pointers to service
routines for system calls.
 Service routine is executed and
return parameter or error code
placed at well-known places (usually
a CPU register).
 Control given back to user program.
 Library function executes the
instruction following trap.
System Call …
Process

Library Call

System Call

trap

Dispatch Table
Service
Code
Kernel
Code
Operating System
Structures
 Simple Structure
 Layered Approach
 Microkernels
 Virtual Machines
Simple Structures …
 UNIX consists of two separable
parts, the kernel and the system
programs.
 Every thing below the system call
interface and above the physical
hardware is the kernel.
 An enormous amount of functionality
combined in one level, UNIX is
difficult to enhance as changes in
one section could adversely affect
other areas.
Simple Structures
MS-DOS : written to provide the most functionality in the
least space, so it was not divided into modules carefully
UNIX System Structure
Layered Approach
 The OS is broken up into a number of
layers
 Bottom layer is hardware and the topmost
layer ( layer N) is the user interface
 A typical layer consists of data structures
and a set of routines to service the layer
above it
 THE operating system by Dijkstra
 IBM’s OS/2
Layered Approach …
Layered Approach …
 Modularity
 Each layer uses functions and services of
only lower layers
 Simplifies debugging and system
verification.
 The major difficulty with layered approach
is careful definition of layers, because a
layer can only use the layers below it
 Less efficient than other approaches
Microkernel
 Structures the operating system by
removing all non-essential components
from the kernel and implementing them as
system and user level programs
 Smaller kernel
 Main function is to provide a
communication facility between client
programs and the various services that are
also running in the user space.
Microkernel …
 Easier to extend the OS—new services are
added to user space and consequently do not
require modification of the kernel and/or its
recompilation
 Easier to maintain operating system code
(enhancement, debugging, etc.)
 OS is easier to port from one hardware to
another
 More security and reliability
 Mach, MacOS X Server, QNX, OS/2, and
Windows NT
Windows NT Client-Server
Structure
Virtual Machines
 CPU scheduling and virtual memory
techniques used to emulate hardware of
the underlying machine, on which user can
install an operating system that the virtual
machine supports
 On a time-sharing system with virtual
machine support, users may be working on
different operating systems
 Pioneered by IBM VM operating system
that ran CMS, a single-user interactive
operating system
Virtual Machines …
 Difficult to implement.
 System development done without
disrupting normal system operation.
Virtual Machines …

Non Virtual Machine Virtual Machine


What is a process?
 Process – a program in execution;
process execution must progress in
sequential fashion.
 A process consists of:
 Code (text) section
 Data section
 Stack
 Heap
 Environment
 CPU state (program counter, etc.)
 Process control block (PCB)
CPU and I/O Bound
Processes
Processes can be:
 I/O-bound process – spends more time
doing I/O than computations, many
short CPU bursts.
I/O Burst CPU Burst I/O Burst CPU Burst

CPU-bound process – spends more time


doing computations; few very long
CPU bursts.
CPU Burst I/O CPU Burst I/O
Process States
As a process executes, it changes state
 new: The process is being created.

 ready: The process is waiting to be

assigned to a processor.
 running: Instructions are being

executed.
 waiting: The process is waiting for

some event to occur.


 terminated: The process has finished

execution.
Process States
Process Control Block
(PCB)
Process information and attributes
 Process state
 Program counter
 CPU registers
 CPU scheduling information
 Memory-management information
 Accounting information
 I/O status information
 Per process file table
 Process ID (PID)
 Parent PID, etc.
Process Control Block
(PCB)
CPU Switch From Process
to Process
Process Scheduling
Queues
 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 I/O devices.
 Process migration between the
various queues.
Queues in the OS
Queues in a Computer
System
Schedulers
 Long term scheduler
 Short term scheduler
 Medium term scheduler
Long Term Scheduler
 Long-term scheduler (or job scheduler) –
selects processes from the job pool to be
brought into the ready queue.
 Long-term scheduler is invoked very
infrequently (seconds, minutes)  (may be
slow).
 The long-term scheduler controls the degree
of multiprogramming.
 More processes, smaller percentage of time
each process is executed
Short Term Scheduler
 Short-term scheduler (or CPU scheduler) –
selects which process should be executed
next and allocates it the CPU through the
dispatcher.
 Short-term scheduler is invoked very frequently
(milliseconds)  (must be fast).
 Invoked when following events occur
 CPU slice of the current process finishes
 Current process needs to wait for an event
 Clock interrupt
 I/O interrupt
 System call
 Signal
Medium Term Scheduler
 Also known as swapper
 Selects an in-memory process and swaps
it out to the disk temporarily
 Swapping decision is based on several
factors
 Arrival of a higher priority process but no
memory available
 Poor mix of jobs
 Memory request of a process cannot be met
Addition of Medium Term
Scheduling
Context Switch
 When CPU switches to another process,
the system must save the state (context)
of the ‘current’ (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;
typically in microseconds
Process Creation
 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 a subset of parent’s resources.
 Parent and child share no resources.
 Execution
 Parent and children execute concurrently.
 Parent waits until children terminate.
Process Creation …
 Address space
 Child duplicate of parent.
 Child has a program loaded onto it.
 UNIX examples
 fork system call creates a new process
 exec system call used after a fork to
replace the process’ memory image with
a new executable.
Processes Tree on a UNIX
System
Process Termination
 Process executes the last statement and
requests the operating system to terminate
it (exit).
 Output data from child to parent (via wait).
 Process resources are deallocated by the
operating system, to be recycled later.
Process Termination …
 Parent may terminate execution of
children processes (abort).
 Child has exceeded allocated resources
(main memory, execution time, etc.).
 Parent needs to create another child but has
reached its maximum children limit
 Task performed by the child is no longer
required.
 Parent exits.
 Operating system does not allow child to continue
if its parent terminates.
 Cascaded termination
Process Management
in UNIX/Linux
Important process-related
UNIX/Linux system calls
 fork

 wait

 exec

 exit
fork()
 When the fork system call is executed, a
new process is created which consists of a
copy of the address space of the parent.
 This mechanism allows the
parent process to communicate
easily with the child process.

SYNOPSIS
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);
fork() ...
 The return code for fork is zero for the
child process and the process identifier
of child is returned to the parent
process.
 On success, both processes continue
execution at the instruction after the
fork call.
 On failure, -1 is returned to the parent
process and errno is set appropriately
to indicate the reason of failure; no
child is created
fork()—Sample Code
main()
{
int pid;
Parent Process pid = 1234

...
pid = fork();
if (pid == 0) {
/* Code for child */ Child Process pid = 0
...
}
else {
/* Code for parent */
...
}
... Kernel Space
}
fork()—Inherits from
the Parent
The child process inherits the
following attributes from the parent:
 Environment
 Open file descriptor table
 Signal handling settings
 Nice value
 Current working directory
 Root directory
 File mode creation mask (umask)
 Etc.
fork()—Child Differs
from the Parent
The child process differs from the
parent process:
 Different process ID (PID)
 Different parent process ID (PPID)
 Child has its own copy of parent’s
file descriptors
 Etc.
fork()—Reasons for
Failure
 Maximum number of processes
allowed to execute under one user
has exceeded
 Maximum number of processes
allowed on the system has exceeded
 Not enough swap space

You might also like